Earlier we learned how to create a Python type using Delphi classes. How about creating a Python type with some containers or collections capabilities in Delphi and accessing its elements? Python4Delphi PyObject contains Type Services routines e.g Basic, Number, Sequence, Mapping which can be overridden with our custom Python Types Delphi classes. This post helps to do that.
Python4Delphi Demo27 Sample App shows how to create a Module, Python type, Import the module, and Python Type in a python script, create a sequence type object and access some of the service routines like length, indexing, etc. You can find the Demo27 source on GitHub.
Prerequisites: Download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out this video Getting started with Python4Delphi.
Components used in Python4Delphi Demo27 Sample App:
- TPythonEngine: A collection of relatively low-level routines for communicating with Python, creating Python types in Delphi, etc. It’s a singleton class.
- TPythonGUIInputOutput: Inherited from TPythonInputOutput (which works as a console for python outputs) Using this component Output property you can associate the Memo component to show the Output.
- TPythonModule: It’s inherited from TMethodsContainer class allows creating modules by providing a name. You can use routines AddMethod, AddMethodWithKW, AddDelphiMethod, AddDelphiMethodWithKeywords to add a method which should be type compatible with this routine parameter. You can create events using the Events property.
- TPythonType: This component helps to create a new python type in Delphi which is inherited from the hierarchy of classes (set of APIs to create, manage methods, and members).
- TMemo: A multiline text editing control, providing text scrolling. The text in the memo control can be edited as a whole or line by line.
You can find the Python4Delphi Demo27 sample project from the extracted repository ..Python4DelphiDemosDemo27.dproj. Open this project in RAD Studio 10.4.1 and run the application.
Implementation Details:
- PythonEngine1 provides the connection to Python or rather the Python API. This project uses Python3.9 which can be seen in TPythonEngine DllName property.
- PythonGUIInputOutput1 provides a conduit for routing input and output between the Graphical User Interface (GUI) and the currently
executing Python script. - PythonModule1 with a Module name test is created.
- In this sample, Created a Delphi class(TMySeq) implementing a new Python type. It is derived from TPyObject. It overrides some Mapping services virtual methods such as MpLength, MpSubscript.
1 2 3 4 5 6 7 8 9 10 |
function TMySeq.MpLength: NativeInt; begin Result := 10; end; function TMySeq.MpSubscript(obj: PPyObject): PPyObject; begin Result := obj; GetPythonEngine.Py_XINCREF(obj); end; |
- During PythonType1 initialization assign the property PyObjectClass with the class TMySeq.
1 2 3 4 5 |
procedure TForm1.PythonType1Initialization(Sender: TObject); begin with Sender as TPythonType do PyObjectClass := TMySeq; end; |
- On Clicking Execute Button the below code is executed.
1 2 3 4 |
procedure TForm1.Button1Click(Sender: TObject); begin PythonEngine1.ExecStrings(Memo2.Lines); end; |
Which will execute the python script mentioned below.
1 2 3 4 5 6 7 8 9 10 |
import test S=test.CreateMySeq() print (S) print (len(S)) print (S[1]) print (S[1,2]) print (S[1:2]) print (S[1:20:2]) print (S[...]) print (S[1,4,5:8, 10:20:2, ...]) |
Note. CreateMySeq in the above script is created automatically by Python4Delphi when the property GenerateCreateFunction of TPythonType1(MySeq) is true.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition