请选择 进入手机版 | 继续访问电脑版
  • 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python for delphi教程

原作者: [db:作者] 来自: [db:来源] 收藏 邀请


     

 

 +   

Using Delphi and Python together

Talk for the Australian Delphi User Group (ADUG), July and August 2001
-Andy Bulka
[email protected]

Delphi = a great object oriented language, and a fantastic RAD environment and framework.  You can build excellent GUI's in Delphi that run very fast and have a small .EXE footprint.  Runs on linux  too.  There is a free version of Delphi, too.

Python  =  is rapidly rising in popularity due to its pseudo-code-like, syntactically minimalist but straightforward syntax.  Like the Delphi community, the Python community of programmers has a nice feel about it, and it is committed to developing rich, high quality, open-source class libraries for Python - without the hype of Java. 

This paper will demonstrate how to incorporate Python in any of your Delphi apps!  Or conversely, how to use Delphi as a GUI for your python apps.

Here is an example of a finished app.

Related 'Python for Delphi' Links on this site:

tutorial - Andy's Python Delphi Tutorial - Getting started with the basics.  Also use these techniques if you are using Delphi 5 or below, and Python 2.0 or below.   Includes info on getting these free components.
code - Here are Andy's Extensions  - a delphi unit that adds a few utility functions to Python for Delphi. 
tutorial - Here is a later tutorial using the Latest Techniques in Python for Delphi - use these if you have Delphi 6 and Python 2.1 or higher.
discussion & tips - Here is a discussion and tips on python for delphi deployment issues.
animated slideshow tutorial  - Here is an animated visual viewlet demo of using the Python for Delphi components to build a Delphi app that talks to python.
example and screenshot of a Delphi GUI application which uses python code for all its business logic.

return to main Andy Patterns home page

Introduction and Tutorial

Introduction

Create classes in python and have Delphi use these classes as if they were native Delphi classes!  Benefit:  python can do all the business logic and hard work, whilst Delphi can be used for the GUI and application framework etc.

The interactive shell and fast development time in python makes building business logic here.  Define your domain classes and build all the units that do work.  Only use Delphi for GUI.

On the other hand, if you want python to talk to and control delphi, then use COM.

Installation

Install Python for win32 Official distribution installer           

Then install the Python for Delphi components from
     http://www.multimania.com/marat/delphi/python.htm    

Python for Delphi is a set of components that wrap up the Python 1.5 Dll into Delphi. They let you easily execute Python scripts, create new Python modules and new Python types. You can create Python extensions as Dlls and much more, including being able to create classes in python and have Delphi use these classes as if they were native Delphi classes!
 

so you finish up with the following palette:

which are the engine, the atomEngine, pythonDelphiVar, etc.  Note: You only need to use about three of these components to do most of your python-delphi work, the rest are mostly historical or very advanced.

Basic Use - A simple test

A simple Python evaluator:

  1. Create a new Form
  2. Drop a TMemo (or a TRichEdit)
  3. Drop a TPythonGUIInputOutput 
    for displaying Python's messages
  4. Drop a TMemo for the source code
  5. Drop a TPythonEngine
  6. Connect the attribute IO of the 
    TPythonEngine to the 
    TPythonGUIInputOutput.
  7. Connect the attribute Output of 
    TPythonGUIInputOutput to 
    the TRichEdit.
  8. Drop a TButton and 
    call it "Execute script"
  9. Double-click on the button and add:
     PythonEngine1.ExecStrings( Memo1.Lines );    
  1. Write in the Memo1: some python code e.g. print 2+2 or 
    that pictured below
  2. Click on the Execute button


click the button and

python is working with Delphi at a rudimentary level. 

The above demonstration is an example of sending strings of code to the python interpreter and receiving Python standard output.  You can drive python in this way as much as you like, by pumping text commands to it

  PythonEngine.ExecStrings( commandLines )    

The above Delphi component wrapper method is not always convenient, since ExecStrings expects commandLines to be a TStrings type, whereas sometimes you want to send a single string to the python interpreter / engine (and not have to create a stringlist)  So I wrote a wrapper procedure  PyExe (see AndysPythonDelphi extensions) which just takes a string.  You can also send multiple lines to PyExe by separating lines with #13 (carriage return) or linefeed.

  procedure PyExe(cmds: string; 
               engine: TAtomPythonEngine);    

So that you can do things like:

  PyExe('print 5');
  PyExe('print 5+23'+#13+'print "welcome"');
          // note two commands here    

Controlling where Python standard output goes

Every Delphi form must have a PythonEngine component and a PythonInputOutput component.

There are two types of 'PythonIO' components - one that directs all output to a memo or richEdit, or you can use an IO component which lets you define a custom event handler to handle the python output.  You can redirect output to raize codesite or even to the standard Delphi Debug Event Log Window: e.g.

  1. Create a new form, add a PythonEngine component and a PythonInputOutput component.
  2. Wire them together (Python engine's IO property to point to the PythonInputOutput component)
  3. Redirect all python standard output to the standard Delphi debug console, by double clicking on the PythonInputOutput component's OnSendData event and putting in the code OutputDebugString( PChar(Data));

PythonDelphiVars

To communicate between Delphi and Python at a deeper level we use the Delphi PythonDelphiVar component.  It is a piece of Delphi that the python interpreter / engine can actually see!  It looks like this:

The most important PythonDelphiVar component property is VarName property which is the name by which python 'sees' this component. 

The Name property of the PythonDelphiVar component is, of course, how Delphi refers to the PythonDelphiVar component. 
The VarName property of the PythonDelphiVar component is how the Python side refers to the PythonDelphiVar component.

In the picture above, from Python's point of view, I have called the PythonDelphiVar component VAR1 which means we can write Python code like:

  PyExe('VAR1.Value = 101');    

or in a python script, just simply

  VAR1.Value = 101    

which results in the PythonDelphiVar component having a value of 101.  Delphi can access this component's value with

  PythonDelphiVar1.ValueAsString    

TIP:  In practice, I use the convention of making the name of each PythonDelphiVar component and its VarName property the same.  

Example #1 - Running an entire python program and storing the results in a set of PythonDelphiVar components. 

All Delphi does is trigger off the script (say when a button get hit) and then retieves the results from the PythonDelphiVar components.

The Delphi side:

  1. Create a new form, add a PythonEngine component (name it PE) and a PythonInputOutput component.  Wire them together as previously discussed in controlling where the Python output goes to, above.
  2. Drag down two PythonDelphiVar components and name them HEADERVAR and RESULTVAR.  Set the VarName properties to the same as their names.
  3. Create a button with the code:
procedure TForm2.Button2Click(Sender: TObject);
begin
    PyExeFile('unit2.py', PE);
end;
 

The Python side

  1. Create a python unit called unit2.py and put it in the same folder as your delphi units.  Add the following code to the python unit:
print "Welcome to Python unit2"
HEADERVAR.Value = '----- Welcome -------'
RESULTVAR.Value = 200 * 3
 

Now run the delphi form and press the button.  Our PythonDelphiVar components should be populated.  How do we know? 

  1. Just add another button with the code:
procedure TForm2.Button3Click(Sender: TObject);
begin
  showmessage( HEADERVAR.ValueAsString 
    +#13+ RESULTVAR.ValueAsString );
end;
 

Example #2 - Loading an external python program and calling a function in it, storing the result in a PythonDelphiVar component. 

            The Python side

Modify the unit2.py  python script as follows, to include a function which takes a comma separated list of numbers and returns the sum of those numbers.  The result is stored in a PythonDelphiVar component named RESULTVAR.

import string

def calcAvg(str):
  total = 0
  vallist = map(lambda n: int(n), string.split(str,','))
  for val in vallist:
    total += val
  RESULTVAR.Value = total / len(vallist)


The Delphi side:

procedure TForm2.FormCreate(Sender: TObject);
begin
  PyExeFile('unit2.py', PE);   
   // this loads in the python class
end;
procedure TForm2.Button4Click(Sender: TObject);
begin
	PyExe('calcAvg("1,5,10")', PE);
	showmessage( RESULTVAR.ValueAsString );
end;
 

Now add an enhancement where, on the Delphi side, you allow the user to type in the list he or she wants to calculate the average on.  Add an edit component and a button with the following code. 

procedure TForm2.Button5Click(Sender: TObject);
begin
  PyExe('calcAvg("' + edit1.text + '")', PE);
  showmessage( RESULTVAR.ValueAsString );
end;


    yields 13.  Success!

Direct access to Python Classes

First we used stdout to communicate between Delphi and Python.

Then we used special pigeon holes ( PythonDelphiVar components ) to communicate between Delphi and Python.

Now we are going to learn how to gain access to actual Python classes and instances, and call the methods of those instances using normal Delphi dot notation.  And of course being able to set and get properties of those instances would be cool too, wouldn't it?

Using TPythonAtom

Basically the technique is to define, in Delphi, some OleVariant variables which hold references to Python objects.  We can then access methods and properties on these python objects using the familiar dot syntax that we use in Delphi (and most other languages) e.g.

  pythonicCustomer.Address := '23 Smith st.' ;
  pythonicCustomer.RunReport() ;    

Official documentation on this technique is found in Demo12 of the examples that come with the Python for Delphi components. "Simply add the PythonAtom in the uses clause, declare a new var of type OleVariant and call the function getAtom( any Python object ). It will return a new OleVariant that will let you access properties or methods very simply, as you would do with Word !" See also latest featuresNote: if you don't understand the preceding paragraph, that's ok, since you won't have to know about PythonAtom in the next examples, because I have wrapped the difficult stuff up in a simple function or two.

The Delphi side:

  1. Create a new form, and drag down the pythonAtomEngine component and name it PE.  Drop a TPythonGUIInputOutput for displaying Python's messages. Connect this to your richtext or memo component.  More info on redirecting python IO earlier in this article.
  2. Add AndyDelphiPyComCtrlspythonAtom units to your uses clause, e.g.
implementation

uses AndyDelphiPy, ComCtrls, pythonAtom;

var
   aCustomer : OleVariant;
 
  1. Then in the formshow or formCreate event add the code that loads the main unit of your python code..
	procedure TfrmMain.FormShow(Sender: TObject);
	begin
		PyExeFile('YourPythonApp.py', PE);    

At this point the python interpreter has loaded and executed your python classes and any code in the unit you are loading.  Now we can create instances of python classes in Delphi and store the references to them in variants.

	aCustomer := PyClass('Customer()', pdv, PE);

The above code shows how to instantiate a python class Customer and store that reference in a Delphi variant aCustomer

The pdv is a necessary intermediate python delphi var used for the instantiation and is not used subsequently.  Drag and drop a PythonDelphiVar component and name it "pdv" (the pdvsimply stands for python delphi var) and set the Varname property to "pdv" [ ASIDE:  ** Matthew Vincent. suggested during the talk that I could perhaps create this pdv component within my PyClass wrapper function, thus eliminating the pdv parameter from the PyClass function.  I couldn't get this to work, as the pythonDelphiVar component seems to need to be created at design time - creating it at runtime with the form as the owner worked, but didn't actually satisfy the python engine.  Even if this would have worked, we would still have to specify the owning form as a paramter to PyClass, which means we would have gained nothing in terms of the number of parameters we would have had to pass.... ]

Note also that  the PyClass delphi function is another one of the AndysPythonDelphi extensions which simplifies the process of instantiating a python class from Delphi.  Normally you have to deal with a couple of lines of code and some reference counting, in order to instantiate a python class.  By using my wrapper function PyClass the process is reduced to a simple one line call.

  1. When this particular example delphi app runs, after the execution of the FormShow event, the customer instance will have been created and will be ready to be used. Let's add a button so that we can manipulate that instance.  Create a button and add the following code:
  aCustomer.Surname := 'Bloggs' ;
  aCustomer.Address := '23 Smith st.' ;
  inc( aCustomer.NumOrders ); 
  showmessage( 'Customer info: ' + 
      aCustomer.RunReport() );

That's it for the Delphi side of things. 

The Python side:

Now let's work on the python side of things, which only involved creating a single python unit named YourPythonApp.py in the same folder as your delphi app.  

class Customer:
  def __init__(self, surname=''):
    self.Surname = surname
    self.Address = 'unknown address'
    self.NumOrders = 0
  def RunReport(self):
    return 'Customer ' + self.Surname + 
     ' of ' + self.Address + 
     ' has made ' + `self.NumOrders` +
     ' orders so far.'
 

Note that the method __init__ in the above class is the  constructor method (like .Create in Delphi).

Running the Delphi app and clicking the button should give you:

Success!

Passing python objects as parameters

Your Delphi code can now interact seamlessly with python objects.  One last thing to watch out for is the situation where your delphi code passes a python instance around as a parameter to other python methods, you cannot simply pass the variant reference e.g.

  aCustomer.AddOrder(anOrder)  # won't work    

instead you must pass the pure python reference.  So define a python method in each python class called something like 'myself' e.g.

  class Order:
  ...
    def myself(self):
      return self    

then you will be able to successfully:

  aCustomer.AddOrder(anOrder.myself)   	# works    

If you are lucky, you may only need to define the 'myself' function just once, perhaps in some Python base class.  And of course you can call the function anything you like, just don't call it 'self' since that already reserved by both Delphi and Python.

Deployment

Easy - no registry settings or anything fancy.

As well as your compiled delphi executable, just add the python dll (only about 500 k) in the folder containing your app, plus a Lib folder containing any extra python units you are using.  For example random.py and whrandom.py could be placed into the Lib folder if you had used them.  The examples used in this article did not use any extra units (the string unit that we used for string.split is a "built in" unit and so does not need to be supplied with your app).

 
          plus

 

Advanced topic: Here is a discussion and tips on python for delphi deployment issues.

Andy's helpful minor extensions to Python-for-Delphi

These are Andy's extra high-level wrapper functions used and described in this article.  These functions use and augment the standard Python-for-Delphi components.

More info on python

If you need further informations on Python, visit the official Web site at http://www.python.org/

Andy's Easy Tips on Reading Python Code

Python is a simple, straightforward and elegant language. It uses standard conventions of accessing methods and properties and is fully OO. Types are associated with objects not variables, so you don’t need to declare variables. Functions are called like afunction(param1, param2) and objects are created from classes the same way e.g. o = MyClass(). Python is case sensitive.

There are no begin end reserved words or { } symbols in Python to indicate code blocks – this is done through indentation. The colon in if lzt: simply means ‘then’. The idiom of testing objects (rather than expressions) in an if statement makes sense as python treats empty lists, None and 0 as false.

Python understands named parameters e.g. In a method call, afunction(From=None) means you are passing None (null / nil) as the ‘From’ parameter, whilst in a method definition From=None means that if the caller does not supply this parameter, then it will default to None.

The first argument of all methods defined inside classes must be ‘self’. This argument fills the role of the reserved word this in C++ or Java or self in Delphi. Most languages supply this parameter (a reference to the current instance) implicitly whereas Python makes this concept explicit. At runtime this parameter is supplied by the system, not the caller of the method, thus the def AddOrder(self, order) method in reality takes one parameter when calling it: AddOrder( order ).

The statement pass means do nothing.

You can return multiple items at once e.g. return (2, 50) and also assign multiple items at once e.g. x, y, z = 0 or even expressions like result, status, errmsg = myfunction(1, 90).

Other class files/modules are imported using import somefile. __init__ methods are simply constructors. Finally, a lambda is just a one line function that reads functionname = lambda paramlist : returnedexpression.

Both Python and JPython (Java Python, now called Jython) are open source, free and available from http://www.python.org/

 :-)

Related 'Python for Delphi' Links on this site:

tutorial - Andy's Python Delphi Tutorial - Getting started with the basics.  Also use these techniques if you are using Delphi 5 or below, and Python 2.0 or below.   Includes info on getting these free components.
code - Here are Andy's Extensions  - a delphi unit that adds a few utility functions to Python for Delphi. 
tutorial - Here is a later tutorial using the Latest Techniques in Python for Delphi - use these if you have Delphi 6 and Python 2.1 or higher.
discussion & tips - Here is a discussion and tips on python for delphi deployment issues.
animated slideshow tutorial  - Here is an animated visual viewlet demo of using the Python for Delphi components to build a Delphi app that talks to python.
example and screenshot of a Delphi GUI application which uses python code for all its business logic.

return to main Andy Patterns home page

 


 

 

Andy's helpful minor extensions to Python-for-Delphi

These are Andy's Delphi wrapper functions used and described in this article.  

These functions make use of and augment the standard Python-for-Delphi components.

Save the following Delphi code as AndyDelphiPy.pas and use this unit as necessary
when programming with Python-for-Delphi components . 
Of course, ensure it is in your delphi compilation library path.

unit AndyDelphiPy; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, PythonEngine, AtomPythonEngine, Rzcsintf, ComCtrls, pythonAtom; procedure PyExe(cmds: string; engine: TAtomPythonEngine); procedure PyExeFile(fp: string; engine: TAtomPythonEngine); function PyClass(pyclass: string; pydelphivar : TPythonDelphiVar; engine: TAtomPythonEngine): OleVariant; function PyVarToAtom(pydelphivar : TPythonDelphiVar; engine: TAtomPythonEngine): OleVariant; procedure PyConsoleOut(const Data: String); implementation procedure PyExe(cmds: string; engine: TAtomPythonEngine)
var s: TStringList; begin s := TStringList.create; try s.text := cmds; engine.ExecStrings( s ); finally s.free; end; end; procedure PyExeFile(fp: string; engine: TAtomPythonEngine)
var s: TStringList; begin s := TStringList.create; try if pos(':\', fp) = 0 then fp := ExtractFilePath(Application.ExeName) + fp; s.LoadFromFile( fp ); engine.ExecStrings( s ); finally s.free; end; end; function PyVarToAtom(pydelphivar : TPythonDelphiVar; engine: TAtomPythonEngine): OleVariant
var v: PPyObject; begin v := pydelphivar.ValueObject; result := getAtom(v); GetPythonEngine.Py_XDECREF(v); end; function PyClass(pyclass: string; pydelphivar : TPythonDelphiVar; engine: TAtomPythonEngine): OleVariant;
begin PyExe(pydelphivar.VarName + '.Value = ' + pyclass, engine); result := PyVarToAtom(pydelphivar, engine); end; procedure PyConsoleOut(const Data: String)
begin OutputDebugString( PChar(Data)); end; end.

Related 'Python for Delphi' Links on this site:

tutorial - Andy's Python Delphi Tutorial - Getting started with the basics.  Also use these techniques if you are using Delphi 5 or below, and Python 2.0 or below.   Includes info on getting these free components.
code - Here are Andy's Extensions  - a delphi unit that adds a few utility functions to Python for Delphi. 
tutorial - Here is a later tutorial using the Latest Techniques in Python for Delphi - use these if you have Delphi 6 and Python 2.1 or higher.
discussion & tips - Here is a discussion and tips on python for delphi deployment issues.
animated slideshow tutorial  - Here is an animated visual viewlet demo of using the Python for Delphi components to build a Delphi app that talks to python.
example and screenshot of a Delphi GUI application which uses python code for all its business logic.

return to main Andy Patterns home page

 


 

 

Related 'Python for Delphi' Links on this site:

tutorial - Andy's Python Delphi Tutorial - Getting started with the basics.  Also use these techniques if you are using Delphi 5 or below, and Python 2.0 or below.   Includes info on getting these free components.
code - Here are Andy's Extensions  - a delphi unit that adds a few utility functions to Python for Delphi. 
tutorial - Here is a later tutorial using the Latest Techniques in Python for Delphi - use these if you have Delphi 6 and Python 2.1 or higher.
discussion & tips - Here is a discussion and tips on python for delphi deployment issues.
animated slideshow tutorial  - Here is an animated visual viewlet demo of using the Python for Delphi components to build a Delphi app that talks to python.
example and screenshot of a Delphi GUI application which uses python code for all its business logic.

return to main Andy Patterns home page

Latest Python Delphi techniques & developments

As of October 2001 a totally new way of talking to python we developed, within the Python for Delphi framework.  You use variants, and get smart references to python objects.

So whilst before you could create a reference to an instance to a class, in an olevariant.  Now we use variants.

Also we can instantiate classes directly (without needing to use my PyClass() function etc. etc.

Here is what Morgan, one of the authors of the components has to say:
  http://groups.yahoo.com/group/pythonfordelphi/message/210

The latest way of programming in python for Delphi if you have Delphi 6 and python 2.1 or higher.

-Andy Bulka.

These techniques are demonstrated in Demo25 in the examples folder of your Python for Delphi distribution.

The old vs. the new ways.

Because Delphi 6 has custom variants, they can point to specific smart proxies for python objects.  Before Delphi 6, you could have an oleVariant pointing to a python instance, but you couldn't do the smart things like know that it was a list type etc.

The following examples assume you have a module called LoginMgr.py in the python path or in the same folder as your application  .exe that you have built in Delphi.  Note that the python file LoginMgr.py contains within it a python class called LoginMgr which has a method called somemethod.

Old way   (see my basic tutorial for more info 
on the AndyDelphiPy functions.)
New way  (requires Delphi 6)
// Drop a TPythonAtomEngine onto your form 
// or datamodule and name it PE
// You also need to drop a pythonDelphiVar 
// component and call it pdv

uses AndyDelphiPy;

var
  obj : olevariant;
begin

AndyDelphiPy.PyExeFile('LoginMgr.py', PE);
obj  := AndyDelphiPy.PyClass('LoginMgr()', pdv, PE);
obj.somemethod()    // call the method

// Drop a TPythonAtomEngine or TPythonEngine 
// onto your form or datamodule.

uses VarPyth;

var
  mymodule, obj: variant;
begin

mymodule := Import('LoginMgr');
obj := mymodule.LoginMgr();
obj.somemethod()    // call the method

Note that it it possible to slightly mix the old and new way, so that if you use the AndyDelphiPy.PyExeFile('LoginMgr.py', PE); to import the module then you can then switch to the new way, declare an obj: variant; then instantiate an instance using obj := MainModule.LoginMgr();  However you still need Delphi 6 and so you might as well just use the new way properly.

Widestrings

Declare your delphi strings widestrings if you want to get more than 255 chars back from calls to python methods that return strings. e.g.

var
   s : widestring;
begin
  s := mypythonclassinstance.somemethod() ;
 showmessage(s) ;

Booleans

If your python method call returns 1 or 0 and this is supposed to be interpreted as a boolean, then cast it inside Delphi e.g.

if Boolean( mypythonclassinstance.somemethod()) then
    ....

Accessing syspath directly

Here is a function that accesses a global variable called SysModule and access the syspath directly.

This function also calls VarIsPythonSequence which tests to see if the parameter passed is a list or not.

procedure TForm1.Button2Click(Sender: TObject);
const
  LIB = 'E:\\ZopeWebSite\\bin\\lib';
  LIBDLL = 'E:\\ZopeWebSite\\bin\\DLLs';
var
  re : variant;
  m : variant;
begin
  memo1.lines.Add('SysModule.path is ' + SysModule.path);
  memo1.lines.Add('');
  Assert(VarIsPythonSequence(SysModule.path));
  displaySysPath(ListBox1);    
  if not Boolean(SysModule.path.Contains(LIB)) then
    SysModule.path.insert(0,LIB);
  SysModule.path.append(LIBDLL);
  memo1.lines.Add('SysModule.path now is ' + \
                   SysModule.path + #13#13);
  displaySysPath(ListBox1);    
  fixSysPath;    
  re := Import('re');
  showmessage(re);    
  m := Import('xml.dom.minidom');
  showmessage(m);    
end;    

Playing with sys paths

This is an example of how to set the python system path as seen by delphi's instance of the python interpreter (as represented by the pythonEngine component).  Note that it is imperative that you have \\ as the slashed in your path as otherwise things like \fred will actually be interpreted as \f (whatever that escaped character is) plus 'red'. 

Technique 1

procedure TForm1.fixSysPath;
const
  LIB = 'E:\\ZopeWebSite\bin\\lib';
  LIBDLL = 'E:\\ZopeWebSite\\bin\\DLLs';
begin    
  // this is illegal
  // SysModule.path.Clear;       
  // this will work with latest python for delphi components OK.
  //SysModule.path := NewPythonList;       
  // this is a boring but effective solution as well.
  while SysModule.path.Length > 1 do   
    SysModule.path.pop;    
  SysModule.path.append(LIBDLL);
  SysModule.path.append(LIB);
end;    

Technique 2

procedure TForm1.btnClearSyspathToJustLibClick(Sender: TObject);
var
  currdir, libdir : string;
begin
  currdir := ExtractFilePath( Application.ExeName );    

NOTE: Simply putting a window path as the currdir will ultimately fail since the paths returned by Delphi have single slashes and python needs wither unix slashes or \\ slashes. See here for an algorithm to handle this.

  libdir := EnsurePathHasDoubleSlashes(libdir);    
  libdir := currdir + 'Lib';    
  SysModule.path := NewPythonList;      
  // Relies on Jan 2002 install of python for Delphi components    
  SysModule.path.append(currdir);
  SysModule.path.append(libdir);
end;    

NOTE:  See the python for delphi deployment section for a more in-depth discussion of paths.

Supplimentary utility to display the python syspath in a delphi gui control.

procedure TForm1.btnDisplaySysPathClick(Sender: TObject);
begin
  ListBox1.clear;
  displaySysPath(ListBox1);
end;

Writing a Delphi function that uses a python function to do the hard work.

Here is an example of writing a delphi utility function that takes a string, and splits it up (delimited by comma) and puts the result into a delphi list box.  We are using python splitfunction to do the splitting - cool eh?

procedure TForm1.splitAstring(str:string; lstbox: TListBox);
var
  s, lzt : variant;
  i : integer;
begin
  s := VarPythonCreate(str);   
          // convert normal string into a python string.
  lzt := s.split(',');    
  for i := 0 to lzt.Length-1 do
    lstbox.Items.Add(lzt.GetItem(i))
end;    

Displaying the python syspath in a delphi listbox

Even though we have a pointer to a python list object (via a Delphi variant), we still have to call .GetItem(i) on a python list rather than the python syntax of lzt[i] - why?  Because we are in Delphi and thus we cannot use python syntax. 

procedure TForm1.displaySysPath(lstbox: TListBox);
var
  lzt : variant;
  i : integer;
begin
  Assert(VarIsPythonSequence(SysModule.path));
  lzt := SysModule.path;
  for i := 0 to lzt.Length-1 do
    lstbox.Items.Add(lzt.GetItem(i));
  lstbox.Items.Add('----------------------------------');
end;    

Loading python base64 and minidom module and processing XML in Delphi

procedure TForm1.minidomLoadClick(Sender: TObject);
var
  m, doc, top : variant;
  s : string;
begin
  fixSysPath;
  displaySysPath(ListBox1);    
  m := Import('base64');
  showmessage(m);
  s := m.encodestring(
'this is some text which I am going to encode then decode again.'
);
  showmessage(s + #13+#13 + m.decodestring(s));    
  m := Import('xml.dom.minidom');
  doc := m.Document();
  showmessage(doc);
    
  top := doc.createElement( 'Workspace' );
  top.setAttribute('Version', '1.1 beta4');
  doc.appendChild(top);    
  s := doc.toxml();
  showmessage('doc.toxml()' + #13+#13 + s);    
end;    

Importing your own class

Ensure you have a TPythonAtomEngine or TPythonEngine onto your form or datamodule.

var
  mymodule, obj: variant;
begin    
mymodule := Import('LoginMgr');
obj := mymodule.LoginMgr();
obj.somemethod()    // call the method    

Morgan's original tutorial on the new techniques

VarPyth unit:

It wraps a Python object and lets you act on it (call methods or use properties) like you would do in Python, and like you did with PythonAtom, but there are differences:  

the variant always maintains its link to a Python object, and is never casted to a basic variant type (integer, string, array) when returning a value, like it was with PythonAtom.

v1 := VarPythonCreate(1);
v2 := VarPythonCreate(2);    

v1 + v2 will return a new variant that will hold the a Python object that is the result of the addition, and the addition will be performed by Python itself, using the PyNumber_Add API.     

Previously, with PythonAtom, if you accessed a property or called a method, the value returned by Python was automatically converted to a basic variant type, or wrapped up into an PythonAtom variant otherwise.     

The main advantage of this solution was to convert Python sequences (lists or tuples) into array of variants, letting us access them using the index operator (list[x]).

Now, it is not possible anymore, as the returned sequence will be returned as a new custom variant, and our custom variant doesn't support indexing!!!  I don't know how to do it? Maybe adding the flag varArray to our VType and setting array bounds that let us access any item, but I'm not sure... Anyway, it could work with sequences, but not with dictionaries, as it
accepts any type as a key.

So, to work around this problem I added several special methods/properties:

foo.GetItem(index): same as foo[index]
foo.SetItem(index, value): same as foo[index] = value
foo.Length or foo.Length(): same as len(foo)
foo.GetSlice(index1, index2): same as foo[index1:index2]
foo.SetSlice(index1, index2, value): same as foo[index1:index2] = value
foo.Contains(value): same as value in foo
foo.DeleteItem(Index): same del foo[index]          

Note that my special methods are not case sensitive.     

Note also that you must use the parenthesis with functions or methods that have no argument, to distinguish your call with a property access:

list.sort()    

if you write list.sort only, you'll get the instance method object instead of performing the sort action.     

The advantage of the new solution is that you always work with the Python objects, and you can do anything you want with them, like you would in Python. The arithmetic operations, or comparisons, work the same, and you can't add an integer to a string, for instance, as you can with variants.

v1 := VarPythonCreate(1);
v2 := v1 + 'hello'; --> Python exception    

but

v2 := 'hello' + v1;     

will work as the string is a variant that forces the right operand to be casted to a string variant, letting the concatenation to apply.      But

v2 := VarPythonCreate('hello') + v1; --> Python exception  

And

v2 := v1 + 'hello';     

--> Python exception as hello is converted to a Python variant and then an addition is beformed between a number and a
string, and Python does not like it!     

You can write also:

v1 := VarPythonCreate([1, 2, 3]);
v2 := v1 + VarPythonCreate([4, 5, 6]); --> you'll get: [1, 2, 3, 4, 5, 6]          

There are several facility functions that let you test the different types of Python objects, or that let you import other Python modules, or access the None object.

Now, you can easily execute a Python script and then access the global vars of the main module:

GetPythonEngine.ExecString('x = 2');
ShowMessage( MainModule.x ); // will display 2          

you can access the sys module with the function SysModule, or you can import any module with the Import function, that returns the imported module object.

myModule := Import('myModule');
myModule.foo(bar);          

You can create a Python variant with:

v := VarPythonCreate(myPythonObjectPointer);
v := VarPythonCreate(1);
v := VarPythonCreate('abc');
v := VarPythonCreate(3.14);
v := VarPythonCreate(True);
v := VarPythonCreate(VarArrayOf([1, 2, 3]));
v := VarPythonCreate([1, 2, 3]);
v := VarPythonCreate([1, 2, 3], stTuple);
v := None;
v := NewPythonList;
v := NewPythonList(3);
v := NewPythonDict;          

you can access to the Python object pointer stored in the variant with

ptr := ExtractPythonObjectFrom(v);          

you can test the variant with:

VarIsPython(v)
VarIsSequence(v)
VarIsMapping(v)
VarIsNumber(v)
...          

you can check if 2 variants shares the same Python object with

VarIsSame(v1, v2)          

you can check if an instance has a class that is or inherits from a class with:

VarIsInstanceOf(AInstance, AClass)          

or you can check the inheritence between 2 classes with

VarIsSubclassOf(ADerived, AClass)          

Note that these 2 functions requires Python 2 or later.     

You can check None with:

  VarIsNone(v)
or
  v = None
or
  VarIsSame(v, None)         

You can instanciate a class like you would in Python:

v := MainModule.MyClass()          

Note, that your class object must the property of a container and can't be stored in a variant:

cl := MainModule.MyClass; // cl refers to MyClass
v := cl(); // won't work because Delphi refuses to compile it!         

You can cast a Python variant to another type any time simply by assigning it to a variable of the required type or by forcing the cast with:

if Boolean(list.Contains(1)) then ...          

So, I hope these explanations will help you understand better the new approach.       Feel free to give your comments...      

Morgan

Demo25

Here are some choice extracts from demo25 (from the Demo25 folder that you get when you download the Python for Delphi components), which show how to use this stuff:

Playing with lists and dictionaries

procedure TMain.btnTestSequencesClick(Sender: TObject); var a, b, c : Variant; begin // initialize the operands // you can either use the overloaded function with an array of const // or use the VarArrayOf function that returns an array of variants that will // be casted to a Python list. a := VarPythonCreate([1, 2, 3]); Assert(VarIsPython(a)); Assert(VarIsPythonSequence(a)); Assert(VarIsPythonList(a)); Assert(a.Length = 3); // this is a special property that does the same as: len(a) in Python Assert(a.Length() = 3); // this is a special method that does the same as the special property Assert(len(a) = 3); Assert(a.GetItem(0) = 1); // this is a special method that lets you do the same as: a[0] in Python Assert(a.GetItem(1) = 2); Assert(a.GetItem(2) = 3); Assert(String(a) = '[1, 2, 3]'); b := VarPythonCreate(VarArrayOf([4, 5, 6])); Assert(VarIsPython(b)); Assert(VarIsPythonSequence(b)); Assert(VarIsPythonList(b)); Assert(b.Length = 3); Assert(b.Length() = 3); Assert(len(b) = 3); Assert(b.GetItem(0) = 4); Assert(b.GetItem(1) = 5); Assert(b.GetItem(2) = 6); Assert(String(b) = '[4, 5, 6]'); // concatenation c := a + b; // check result of operation Assert(String(c) = '[1, 2, 3, 4, 5, 6]'); // check that operation did not change the content of operands. Assert(String(a) = '[1, 2, 3]'); Assert(String(b) = '[4, 5, 6]'); // now with a litteral: note that with D6 SP1, we can't // concatenate a custom variant with an var array of variants c := a + b + VarPythonCreate(['Hello', 'World!', 3.14]); Assert( String(c) = '[1, 2, 3, 4, 5, 6, ''Hello'', ''World!'', 3.1400000000000001]' ); c := a + VarPythonCreate(['Hello', 'World!', 3.14]) + b; Assert( String(c) = '[1, 2, 3, ''Hello'', ''World!'', 3.1400000000000001, 4, 5, 6]' ); c := VarPythonCreate(['Hello', 'World!', 3.14]) + a + b; Assert( String(c) = '[''Hello'', ''World!'', 3.1400000000000001, 1, 2, 3, 4, 5, 6]' ); // multiplication c := a * 3; // in Python the multiplication of sequence concatenates n times the sequence Assert( String(c) = '[1, 2, 3, 1, 2, 3, 1, 2, 3]' ); // comparisons //------------ // equal c := a = b; Assert(c = False); c := a = a; Assert(c = True); Assert( String(a) = '[1, 2, 3]'); // not equal c := a <> b; Assert(c = True); Assert( not (c = b) ); c := a <> a; Assert(c = False); Assert( String(a) = '[1, 2, 3]'); // greater than c := a > b; Assert(c = False); c := b > a; Assert(c = True); Assert( String(a) > '[1, 1, 1]'); // greater or equal than c := a >= b; Assert(c = False); c := b >= a; Assert(c = True); c := a >= a; Assert(c = True); Assert( String(a) >= '[1, 2, 3]' ); // less than c := a < b; Assert(c = True); c := b < a; Assert(c = False); Assert( String(a) < '[4, 4, 4]'); // less or equal than c := a <= b; Assert(c = True); c := b <= a; Assert(c = False); c := a <= a; Assert(c = True); Assert( String(a) <= '[1, 2, 3]'); // copy c := a; Assert( c = a); Assert( VarIsSame(c, a) ); // checks if 2 variants share the same Python object. // sequence methods: c := b + a; c.sort(); // note that you must you the parenthesis to distinguish the // call between a method or a property. Assert( c = (a+b) ); c := NewPythonList; // facility for building sequences Assert( not VarIsTrue(c) ); // c is false because it's an empty collection c.append(1); c.append(2); c.append(3); Assert( VarIsTrue(c) ); // c is true because it's not an empty collection Assert(c = a); Assert( c.pop() = 3 ); Assert( String(c) = '[1, 2]'); c := NewPythonList(3); // facility for building sequences c.SetItem(0, 1); c.SetItem(1, 2); c.SetItem(2, 3); Assert(c = a); c.DeleteItem(1); Assert(c = VarPythonCreate([1,3])); Assert(VarPythonCreate([1,2,3,4]).GetSlice(1, 3) = VarPythonCreate([2,3])); // same as x = [1,2,3,4]; x[1:3] Assert(VarPythonCreate([1,2,3,4]).GetSlice(1, Ellipsis) = VarPythonCreate([2,3,4])); // same as x = [1,2,3,4]; x[1:] Assert(VarPythonCreate([1,2,3,4]).GetSlice(1, -1) = VarPythonCreate([2,3])); // same as x = [1,2,3,4]; x[1:-1] c := VarPythonCreate([1,2,3,4]); c.SetSlice(1, 3, VarPythonCreate([7, 8, 9])); Assert( c = VarPythonCreate([1, 7, 8, 9, 4]) ); Assert( Boolean(c.Contains( 7 )) ); // same as 7 in c Assert( not Boolean(c.Contains( 77 )) ); c.DelSlice(1,3); Assert( c = VarPythonCreate([1,9,4]) ); c := VarPythonCreate([1, 2, 3, 4], stTuple); // test a tuple Assert( VarIsPythonTuple(c) ); Assert( VarIsPythonSequence(c) ); Assert( c.GetItem(1) = 2 ); Assert( c.Length = 4 ); c := NewPythonTuple(3); c.SetItem(0, 1); c.SetItem(1, 2); c.SetItem(2, 3); Assert( VarIsPythonTuple(c) ); Assert( VarIsPythonSequence(c) ); Assert( c.GetItem(1) = 2 ); Assert( c.Length = 3 ); // Done! Log('Sequence test was Ok.'); end; procedure TMain.btnTestMappingsClick(Sender: TObject); var a, b, c, keys, values : Variant; begin // initialize the operands a := NewPythonDict; Assert(VarIsPython(a)); Assert(VarIsPythonMapping(a)); Assert(VarIsPythonDict(a)); a.SetItem( 'a', 1 ); a.SetItem( 'b', 2 ); a.SetItem( 'c', 3 ); Assert(a.Length = 3); // this is a special property that does the same as: len(a) in Python Assert(a.Length() = 3); // this is a special method that does the same as the special property Assert(len(a) = 3); Assert(a.GetItem('a') = 1); // this is a special method that lets you do the same as: a[0] in Python Assert(a.GetItem('b') = 2); Assert(a.GetItem('c') = 3); b := NewPythonDict; Assert(VarIsPython(b)); Assert(VarIsPythonMapping(b)); Assert(VarIsPythonDict(b)); b.SetItem( 'd', 4 ); b.SetItem( 'e', 5 ); b.SetItem( 'f', 6 ); Assert(b.Length = 3); Assert(b.Length() = 3); Assert(len(b) = 3); Assert(b.GetItem('d') = 4); Assert(b.GetItem('e') = 5); Assert(b.GetItem('f') = 6); // copy c := a; Assert( c = a); Assert( VarIsSame(c, a) ); // checks if 2 variants share the same Python object. // dict meth

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
用Matlab求解微分方程发布时间:2022-07-18
下一篇:
matlab——微分方程发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap