Execute Python code in QM

Functions, classes, components, etc. Only high quality content.

Execute Python code in QM

Postby Gintaras on Sat Jul 02, 2016 2:02 pm

Macro Python scripting help     ?
;These functions can be used to execute Python code in QM.

;SETUP

;Download and install Python 32-bit.
;There are multiple Python implementations. I tested the "traditional" implementation (CPython) from www.python.org .

;To install CPython:
;;;1. Download and install Python 32-bit Windows version from
;;;;;;;;;https://www.python.org/downloads/
;;;;;;I tested version 3.5.2 on Windows 10 and 7, and version 3.4.3 on Windows 8.1. I recommend version 3.4.3, read below why. I didn't test 64-bit version, it probably would not work with QM.
;;;;;;IMPORTANT: When installing, check "Add to PATH". After installing, restart QM to update its copy of PATH variable.
;;;2. Download and install PyWin32 from
;;;;;;;;;https://sourceforge.net/projects/pywin32/files/pywin32/
;;;;;;Choose the version that matches your Python version, for example pywin32-220.win32-py3.5.exe for Python 3.5.x or pywin32-220.win32-py3.4.exe for Python 3.4.x.
;;;;;;IMPORTANT: Run the setup program as administrator.
;;;Possible problems with Python 3.5.x:
;;;;;;On Windows 7: error "api-ms-win-crt-runtime-l1-1-0 dll missing".
;;;;;;;;;I solved this not easily. Tried several ways, and now don't know what was actually needed, but the problem finally disappeared after installing KB2999226: https://support.microsoft.com/en-us/kb/2999226.
;;;;;;;;;I tried to install all Windows updates, Visual C++ 2015 Redistributable package, KB2999226. Then uninstall/install the Python programs and restart QM.
;;;;;;;;;Or you can try an older Python version, eg 3.4.3.
;;;;;;On Windows 8.1: Python 3.5.2 and 3.6.0a2 setup failed. Version 3.4.3 + pywin32-220.win32-py3.4.exe worked without problems.

;TEST

;Run macros in "test" folder.



Also I tested ActiveState Python 2.7.10, it was easier to install. But its newer version 2.7.13 does not work.
Attachments
Python.qml
(10 KiB) Downloaded 297 times
Gintaras
Site Admin
 
Posts: 10411
Joined: Thu Dec 19, 2002 7:36 pm
Location: Lithuania

Re: Execute Python code in QM

Postby Start_Learning on Sun Jul 03, 2016 3:55 am

Is there anyway to capture output of a "print" statement in python to a QM string or to QM output window?
Macro test PythonExec1     ?
PythonExec ""

#ret
print("Hello World!")
Start_Learning
 
Posts: 80
Joined: Thu Nov 12, 2015 3:24 pm

Re: Execute Python code in QM

Postby Gintaras on Sun Jul 03, 2016 11:16 am

If the QM macro is a console exe, Python print() will write to its console.

Else:

Function PythonPrintToQM     ?
;Redirects Python print() function output to QM output.

;REMARKS
;print() will be redirected in all Python code added by PythonExec or PythonAddCode that are called after calling this function in current thread.

;EXAMPLE
;PythonPrintToQM
;PythonExec ""
;
;#ret
;print("test")
;print("") # empty line
;print("one\ntwo") # multiline
;print(2) # number
;print("list", 1, 2, 3)
;print("list2", 1, 2, 3, sep=", ")


PythonAddCode ""

#ret
import sys
import ctypes
import win32gui

def printQM(*a, sep = ' '):
,s = ""
,for v in a:
,,if(s != ""): s += sep
,,s += str(v)
,
,if(printQM.hwndQM == 0): printQM.hwndQM=win32gui.FindWindow("QM_Editor", None)
,
,ctypes.windll.user32.SendMessageW(printQM.hwndQM, 12, -1, s)
printQM.hwndQM = 0

print = printQM



Macro test PythonPrintToQM     ?
out
PythonPrintToQM
PythonExec ""


#ret
print("test")
print("") # empty line
print("one\ntwo") # multiline
print(2) # number
print("list", 1, 2, 3)
print("list2", 1, 2, 3, sep=", ")




Or alternative:

Macro Python print output to QM - alternative     ?
out
PythonExec ""


#ret

def Script():
,print("test")
,print("") # empty line
,print("one\ntwo") # multiline
,print(2) # number


# ---------------------------------------------------------------------------------
# Put your Python script in the Script() function. Don't need to change other code.

import sys
import ctypes
import os
import win32gui

class PrintRedirector(object):
,def __init__(self):
,,self.hwndQM=win32gui.FindWindow("QM_Editor", None)
,
,def write(self, message):
,,if(message=='\n'): return
,,ctypes.windll.user32.SendMessageW(self.hwndQM, 12, -1, message)

sys.stdout = PrintRedirector()
try: Script()
finally: sys.stdout = sys.__stdout__

Gintaras
Site Admin
 
Posts: 10411
Joined: Thu Dec 19, 2002 7:36 pm
Location: Lithuania

Re: Execute Python code in QM

Postby Start_Learning on Sun Jul 03, 2016 3:17 pm

Thanks a lot for your "PrintToQM" workarounds. They worked as expected. :D
Start_Learning
 
Posts: 80
Joined: Thu Nov 12, 2015 3:24 pm

Re: Execute Python code in QM

Postby kyjdp on Tue Jan 31, 2017 6:39 am

I downloaded from the website http://www.activestate.com/activepython/downloads the latest 2.7.13 operating tests, there has been an error

My system version is: win7 64-bit
Attachments
1.png
1.png (41.1 KiB) Viewed 1944 times
kyjdp
 
Posts: 275
Joined: Wed Dec 21, 2016 7:48 am

Re: Execute Python code in QM

Postby Gintaras on Tue Jan 31, 2017 8:31 am

I tested ActiveState Python 2.7.13 now, and it does not work.

Try the second option - https://www.python.org/ . I did not test its newest version 3.6.2. The older version 3.5.2 works on my Windows 7 64-bit.
Gintaras
Site Admin
 
Posts: 10411
Joined: Thu Dec 19, 2002 7:36 pm
Location: Lithuania


Return to QM Extensions

Who is online

Users browsing this forum: No registered users and 1 guest