Maxbox Starter86 2 Python4Delphi
Maxbox Starter86 2 Python4Delphi
Python4Delphi II
______________________________________________________________________________
maXbox Starter86_2 – Code with Python4Delphi
Be yourself; Everyone else is already taken. — Oscar Wilde.
1/7
_PIC: p4d_d10_4.png
UnicodeIO = True
RawOutput = False
Output = Memo2
Left = 64
end
Lines.Strings = (
'import sys'
'print ("Version:", sys.version)'
'import spam'
'print (spam.foo('#39'hello world'#39', 1))'
'p = spam.CreatePoint( 10, 25 )'
'print ("Point:", p)'
'p.x = 58'
'print (p.x, p)'
'p.OffsetBy( 5, 5 )'
'print (p)'
'print ("Current value of var test is: ", test)'
'test.Value = "New value set by Python"'
'print (spam.getdouble())'
'print (spam.getdouble2())')
ParentFont = False
2/7
What would be if we use in a internal Python-script some Delphi-
methods like in the above script methods of the import module
spam? First we had to initialize the module spam, we just need to
add our new methods:
Ans here's the example of functions defined for the module spam in
this context the function spam_foo with forms caption return:
3/7
function TPythonEngine.ArrayToPyList(const items: array of const) : PPyObject;
var
i : Integer;
begin
Result := PyList_New( High(items)+1 );
if not Assigned(Result) then
raise EPythonError.Create('Could not create a new list object');
for i := Low(items) to High(items) do
PyList_SetItem( Result, i, VarRecAsPyObject( items[i] ) );
end;
PyList_SetItem:function (dp:PPyObject;idx:NativeInt;item:PPyObject):integer;
cdecl;
PyList_SetItem:= Import('PyList_SetItem');
The other way round, as I said we can't map cleanly Python lists
to Delphi array types, we get the data sort of as the base type
strings from PyObjectAsString:
I think the common base type in Delphi (to export) is the array
and the common base type in Python (to import) is the list. So
this we can see as a proof of concept code:
https://github.com/maxkleiner/python4delphi/blob/master/Demos/Demo
07/test.py
http://py4d.pbworks.com/w/page/9174535/Wrapping%20Delphi%20Objects
4/7
explicit the module spam, previously generated in Delphi:
import sys
print "Win version:", sys.winver
import spam
print (spam.foo('hello world', 1))
p = spam.CreatePoint( 10, 25 )
print ("Point:", p)
p.x = 58
print (p.x, p)
p.OffsetBy( 5, 5 )
print (p)
print ("Current value of var test is: ", test)
test.Value = "New value set by Python"
print (spam.getdouble())
5/7
This is a simplified interface to PyRun_SimpleString leaving the
PyCompilerFlags* argument set to NULL. Normally the Python inter-
preter is initialized by Py_Initialize() so we use the same inter-
preter as from a shell, command or terminal.
_PIC: p4d_d10_4_pyengine.png
...
Py_BuildValue := Import('Py_BuildValue');
Py_Initialize := Import('Py_Initialize');
PyRun_String := Import('PyRun_String');
PyRun_SimpleString := Import('PyRun_SimpleString');
PyDict_GetItemString := Import('PyDict_GetItemString');
PySys_SetArgv := Import('PySys_SetArgv');
Py_Exit := Import('Py_Exit');
...
6/7
Wiki & EKON P4D topics
• https://entwickler-konferenz.de/delphi-innovations-
fundamentals/python4delphi/
• http://www.softwareschule.ch/examples/weatherbox.txt
Note: You will need to adjust the demos from github accordingly,
to successfully load the Python distribution that you have
installed on your computer.
Docs: https://maxbox4.wordpress.com/blog/
http://www.softwareschule.ch/download/maxbox_starter86.pdf
http://www.softwareschule.ch/download/maxbox_starter86_1.pdf
http://www.softwareschule.ch/download/maxbox_starter86_2.pdf
7/7