Andy's Extensions
Home Up

 

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:

bullettutorial - 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.
bulletcode - Here are Andy's Extensions  - a delphi unit that adds a few utility functions to Python for Delphi. 
bullettutorial - 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.
bulletdiscussion & tips - Here is a discussion and tips on python for delphi deployment issues.
bulletanimated 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.
bulletexample and screenshot of a Delphi GUI application which uses python code for all its business logic.

return to main Andy Patterns home page