Blaise 97 UK 6P Max Kleiner Python4Delphi
Blaise 97 UK 6P Max Kleiner Python4Delphi
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
You do also have
the evaluation of an expression. But the evaluation of an
expression works only for arithmetic expressions and not for instructions !
The use of variables and functions is of course possible but constructs like for, def, catch,
class, print, import... are not implemented, you use for this ExecStrings() and not
EvalStrings().
Handshaking with Python arrays or tuples layout does have some complications.
Normal Python arrays (as for standard CPython) are normally called “Lists”.
A numpy.array type (or a mutable list) in Python is a special type that is more memory and
layout efficient than a normal Python list of normal Py floating point objects.
If you want to use Delphi and access Numpy.array or list, I really suppose that the
straightest way to do it would be to implement a way to export some simple straight C
functions that access the Numpy.array type.
Numpy.array wraps a standard block of memory that is accessed as a native C array type.
This in turn, does NOT map cleanly to Delphi array types as created by a Delphi method to
Python.
Let me go deeper in that point, converting a Delphi-array or list to for example a list goes in
the end with a dll-function from the Python library ('PyList_SetItem'):
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:
This
exporting of Delphi-
methods to use in Python-scripts
works also with the creation of a dll as Demo09
Making a Python module as a dll explains (I'll show that in the Tutor III).
More or less some external files as normal Python-scripts is also on your way. For example we call
the script test.py and we import 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())
1 *.py :
The norm input source code that we've written.
2 *.pyc :
The compiled bytecode. If you import a module,
py will build a *.pyc file that contains bytecode to
make importing it again later easier and faster.
3 *.pyd:
The mentioned Windows dll file from Python.
First we check our Python installation. Python provides for all user and current user installations.
All user installations place the Py dll in the Windows System directory and write registry info to
HKEY_LOCAL_MACHINE.
Current user installations place the dll in the install path and
the registry info in HKEY_CURRENT_USER version < py 3.5 .
So, for current user installations we need to try and find the install path since it may not be on the
system path.
$IFDEF MSWINDOWS}
function IsPythonVersionRegistered(PythonVersion : string;
out InstallPath: string; out AllUserInstall: Boolean) : Boolean;
// The above convention was changed in Python 3.5. Now even for all user
// installations the dll is located at the InstallPath.
// Also from vers.3.5 onwards 32 bit version have a suffix -32 e.g. "3.6-32"
// See also PEP 514
var
key: string;
VersionSuffix: string;
MajorVersion : integer;
MinorVersion : integer;
begin
Result := False;
InstallPath := '';
AllUserInstall := False;
MajorVersion := StrToInt(PythonVersion[1]);
MinorVersion := StrToInt(PythonVersion[3]);
VersionSuffix := '';
{$IFDEF CPUX86}
if (MajorVersion > 3) or ((MajorVersion = 3) and (MinorVersion >= 5)) then
VersionSuffix := '-32';
{$ENDIF}
key:= Format('\Software\Python\PythonCore\%s%s\InstallPath',
[PythonVersion, VersionSuffix]);
procedure pyinit;
external 'Py_Initialize@C:\maXbox\EKON25\python37.dll cdecl';
procedure pyexit(retval: integer);
external 'Py_Exit@C:\maXbox\EKON24\python37.dll cdecl';
Now
we use to invoke a Python script
as an embedding const and use the dll functionality of Import('PyRun_SimpleString');
To run python code direct in a maXbox, Free Pascal or whatever script you need to
import just the 3 dll functions, above all PyRun_SimpleStringFlags or without flags:
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 samefrom a
shell, command or terminal.
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
https://entwickler-konferenz.de/location-en/