freelanceprogrammers.org Forum Index » Delphi
Write a module function in Delphi
Joined: 03 May 2005
Posts: 2
Write a module function in Delphi
Hi,
i`d like to write a simple module function in Delphi that can be
called from python passing to it a python list as unique parameter.
Such function should read the list and use its content to do some
basic operation in Delphi returning to python a boolean value.
After examining the VarPyth module i`ve found a lot of nice way to
manipulate python lists and object from Delphi but i don`t find what
i`m looking for: a simple way to call a module function implemented in
Delphi passing to it a list.
I`ve made some experiments with TPythonModule Events, a very easy way
to generate module function, but i don`t understand how to pass a list
parameter from python, having it correctly parsed in Delphi.
Could you help me ?
Many Thanks
Alessandro
Joined: 29 Apr 2005
Posts: 103
Write a module function in Delphi
Hi Alessandro,
> i`d like to write a simple module function in Delphi
that can be
> called from python passing to it a python list as unique
parameter.
> Such function should read the list and use its content to do some
> basic operation in Delphi
returning to python a boolean value.
> After examining the VarPyth module i`ve found a lot of nice way to
> manipulate python lists and object from Delphi
but i don`t find what
> i`m looking for: a simple way to call a module function
implemented in
> Delphi passing to it a list.
> I`ve made some experiments with TPythonModule Events, a very easy
way
> to generate module function, but i don`t understand how to pass a
list
> parameter from python, having it correctly parsed in Delphi.
>
> Could you help me ?
Sure, here`s an example of how to do it:
Create a new project, then replace the
content of the .pas and .dfm files with the following sections.
Note that it’s important to use (and
understand) the PyArg_ParseTuple API which helps you extract the parameters
that your function expects.
Note that you can return nil only if you
set an error.
Note that the method ReturnNone with
return a pre-incremented None object reference. This is the same as:
Result := Py_None;
Py_IncRef(Result);
Hope that helps,
Morgan
------------------------------<
Unit1.pas >-----------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants,
Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, PythonEngine,
PythonGUIInputOutput;
type
TForm1 = class(TForm)
Memo1: TMemo;
Memo2: TMemo;
Button1: TButton;
PythonEngine1: TPythonEngine;
PythonGUIInputOutput1:
TPythonGUIInputOutput;
PythonModule1: TPythonModule;
ListBox1: TListBox;
procedure
PythonModule1Events0Execute(Sender: TObject; PSelf,
Args: PPyObject; var Result:
PPyObject);
procedure Button1Click(Sender:
TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
VarPyth;
{$R *.dfm}
procedure
TForm1.PythonModule1Events0Execute(Sender: TObject; PSelf,
Args: PPyObject; var Result: PPyObject);
var
i : Integer;
pList : PPyObject;
vList : Variant;
begin
with PythonEngine1 do
begin
if PyArg_ParseTuple( Args, `O:foo`,
[@pList] ) = 0 then
begin
Result := nil;
Exit;
end;
vList := VarPythonCreate(pList);
if VarIsPythonSequence(vList) then
begin
ListBox1.Items.Clear;
for i := 0 to vList.Length-1 do
ListBox1.Items.Add(vList.GetItem(i));
Result := ReturnNone;
end
else
begin
Result := nil;
PyErr_SetString(PyExc_AttributeError^, `Foo: expected a sequence as first
parameter`);
end;
end;
end;
procedure TForm1.Button1Click(Sender:
TObject);
begin
PythonEngine1.ExecStrings(Memo2.Lines);
end;
end.
------------------------------< Unit1.dfm
>-----------------------------
object Form1: TForm1
Left = 218
Top = 114
Width = 870
Height = 640
Caption = `Form1`
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = `MS Shell Dlg 2`
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Memo1: TMemo
Left = 56
Top = 24
Width = 737
Height = 257
TabOrder = 0
end
object Memo2: TMemo
Left = 56
Top = 312
Width = 489
Height = 193
Lines.Strings = (
`import spam`
`spam.foo([1, 2, ``a``, ``b``,
``c``, 3.14, [1, 2, 3], {``a``:1, ``b``:2}, ` +
`"hello"])`)
TabOrder = 1
end
object Button1: TButton
Left = 160
Top = 536
Width = 75
Height = 25
Caption = `Click Me`
TabOrder = 2
OnClick = Button1Click
end
object ListBox1: TListBox
Left = 568
Top = 312
Width = 225
Height = 249
ItemHeight = 13
TabOrder = 3
end
object PythonEngine1: TPythonEngine
IO = PythonGUIInputOutput1
Left = 32
Top = 56
end
object PythonGUIInputOutput1:
TPythonGUIInputOutput
Output = Memo1
Left = 32
Top = 88
end
object PythonModule1: TPythonModule
Engine = PythonEngine1
Events = <
item
Name = `foo`
OnExecute =
PythonModule1Events0Execute
end>
ModuleName = `spam`
Errors = <>
Left = 32
Top = 120
end
end
Joined: 03 May 2005
Posts: 2
Write a module function in Delphi
Morgan,
thank you for the help, it`s exactly what i`m looking for.
Many Thanks
Alessandro
> Hi Alessandro,
>
>
>
> > i`d like to write a simple module function in Delphi that can be
>
> > called from python passing to it a python list as unique parameter.
>
> > Such function should read the list and use its content to do some
>
> > basic operation in Delphi returning to python a boolean value.
>
> > After examining the VarPyth module i`ve found a lot of nice way to
>
> > manipulate python lists and object from Delphi but i don`t find what
>
> > i`m looking for: a simple way to call a module function implemented in
>
> > Delphi passing to it a list.
>
> > I`ve made some experiments with TPythonModule Events, a very easy way
>
> > to generate module function, but i don`t understand how to pass a list
>
> > parameter from python, having it correctly parsed in Delphi.
>
> >
>
> > Could you help me ?
>
>
>
> Sure, here`s an example of how to do it:
>
> Create a new project, then replace the content of the .pas and .dfm
files
> with the following sections.
>
>
>
> Note that it`s important to use (and understand) the
PyArg_ParseTuple API
> which helps you extract the parameters that your function expects.
>
> Note that you can return nil only if you set an error.
>
> Note that the method ReturnNone with return a pre-incremented None
object
> reference. This is the same as:
>
> Result := Py_None;
>
> Py_IncRef(Result);
>
>
>
> Hope that helps,
>
>
>
> Morgan
>
>
>
> ------------------------------< Unit1.pas >-----------------------------
>
> unit Unit1;
>
>
>
> interface
>
>
>
> uses
>
> Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms,
>
> Dialogs, StdCtrls, PythonEngine, PythonGUIInputOutput;
>
>
>
> type
>
> TForm1 = class(TForm)
>
> Memo1: TMemo;
>
> Memo2: TMemo;
>
> Button1: TButton;
>
> PythonEngine1: TPythonEngine;
>
> PythonGUIInputOutput1: TPythonGUIInputOutput;
>
> PythonModule1: TPythonModule;
>
> ListBox1: TListBox;
>
> procedure PythonModule1Events0Execute(Sender: TObject; PSelf,
>
> Args: PPyObject; var Result: PPyObject);
>
> procedure Button1Click(Sender: TObject);
>
> private
>
> { Private declarations }
>
> public
>
> { Public declarations }
>
> end;
>
>
>
> var
>
> Form1: TForm1;
>
>
>
> implementation
>
>
>
> uses
>
> VarPyth;
>
>
>
> {$R *.dfm}
>
>
>
> procedure TForm1.PythonModule1Events0Execute(Sender: TObject; PSelf,
>
> Args: PPyObject; var Result: PPyObject);
>
> var
>
> i : Integer;
>
> pList : PPyObject;
>
> vList : Variant;
>
> begin
>
> with PythonEngine1 do
>
> begin
>
> if PyArg_ParseTuple( Args, `O:foo`, [@pList] ) = 0 then
>
> begin
>
> Result := nil;
>
> Exit;
>
> end;
>
> vList := VarPythonCreate(pList);
>
> if VarIsPythonSequence(vList) then
>
> begin
>
> ListBox1.Items.Clear;
>
> for i := 0 to vList.Length-1 do
>
> ListBox1.Items.Add(vList.GetItem(i));
>
> Result := ReturnNone;
>
> end
>
> else
>
> begin
>
> Result := nil;
>
> PyErr_SetString(PyExc_AttributeError^, `Foo: expected a
sequence as
> first parameter`);
>
> end;
>
> end;
>
> end;
>
>
>
> procedure TForm1.Button1Click(Sender: TObject);
>
> begin
>
> PythonEngine1.ExecStrings(Memo2.Lines);
>
> end;
>
>
>
> end.
>
>
>
> ------------------------------< Unit1.dfm >-----------------------------
>
> object Form1: TForm1
>
> Left = 218
>
> Top = 114
>
> Width = 870
>
> Height = 640
>
> Caption = `Form1`
>
> Color = clBtnFace
>
> Font.Charset = DEFAULT_CHARSET
>
> Font.Color = clWindowText
>
> Font.Height = -11
>
> Font.Name = `MS Shell Dlg 2`
>
> Font.Style = []
>
> OldCreateOrder = False
>
> PixelsPerInch = 96
>
> TextHeight = 13
>
> object Memo1: TMemo
>
> Left = 56
>
> Top = 24
>
> Width = 737
>
> Height = 257
>
> TabOrder = 0
>
> end
>
> object Memo2: TMemo
>
> Left = 56
>
> Top = 312
>
> Width = 489
>
> Height = 193
>
> Lines.Strings = (
>
> `import spam`
>
>
>
> `spam.foo([1, 2, ``a``, ``b``, ``c``, 3.14, [1, 2, 3], {``a``:1,
> ``b``:2}, ` +
>
> `"hello"])`)
>
> TabOrder = 1
>
> end
>
> object Button1: TButton
>
> Left = 160
>
> Top = 536
>
> Width = 75
>
> Height = 25
>
> Caption = `Click Me`
>
> TabOrder = 2
>
> OnClick = Button1Click
>
> end
>
> object ListBox1: TListBox
>
> Left = 568
>
> Top = 312
>
> Width = 225
>
> Height = 249
>
> ItemHeight = 13
>
> TabOrder = 3
>
> end
>
> object PythonEngine1: TPythonEngine
>
> IO = PythonGUIInputOutput1
>
> Left = 32
>
> Top = 56
>
> end
>
> object PythonGUIInputOutput1: TPythonGUIInputOutput
>
> Output = Memo1
>
> Left = 32
>
> Top = 88
>
> end
>
> object PythonModule1: TPythonModule
>
> Engine = PythonEngine1
>
> Events = <
>
> item
>
> Name = `foo`
>
> OnExecute = PythonModule1Events0Execute
>
> end>
>
> ModuleName = `spam`
>
> Errors = <>
>
> Left = 32
>
> Top = 120
>
> end
>
> end
All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help
China Wholesale - Electronics Products
Character Studio - Tutorials and Help







