1000copy@gmail.com

»¶Ó­À´µ½1000copy@gmail.com>>¡¡¡¡ | Ê×Ò³ ×ÊÔ´ÖÐÐÄ | ÔĶÁ | Python | רÀ¸Ñ§Ï°technet ϵÁÐ¿Î³Ì | ¸öÈËÄ¿±ê | ¼ÒÈË | ÍæÀÖ | ¼¼Êõ | ITPUBÂÛ̳

ʹÓÃP4D ±àдPython Extension[ʹÓÃrst±àд]

·¢±íÈË:1000copy | ·¢±íʱ¼ä: 2007ÄêËÄÔÂ01ÈÕ, 13:58

Date: $Date: 2007Äê4ÔÂ1ÈÕ
Date: $Date: 2007Äê4ÔÂ1ÈÕ

ʹÓÃP4D ±àдPython Extension

Author:1000copy
Address:¿ÕÎÞÒ»ÈË ²»ÖªËùÖÂ
Contact:1000copy@gmail.com or http://1000copy.itpub.net
organization:ÎÞµ³ÅÉÈËÊ¿
status:²î²»¶àÁË:version: 1:copyright: This document has been placed in the public domain. You may do with it as you wish. You may copy, modify, redistribute, reattribute, sell, buy, rent, lease, destroy, or improve it, quote it at length, excerpt, incorporate, collate, fold, staple, or mutilate it, or do anything else to it that your or anyone else's heart desires.
Dedication:ϲ»¶PythonµÄDelphiµÄ³ÌÐòÔ±
abstract:ÔÚ»¥ÁªÍø¹«¹²¿É·ÃÎÊÁìÓòÄÚ£¬¹ØÓÚPython/C interfaceµÄ½éÉÜ£¬ÊֲᶼÊÇ ±È½Ï¶àµÄ¡£PyÖ±½ÓÖ§³ÖC±àдÀ©Õ¹£¬¶ÔÓÚDelphi³ÌÐòÔ±£¬P4DÊÇÒ»¸öºÜºÃµÄ Ñ¡Ôñ¡£²»ÐÒµÄÊÇ£¬Í¨¹ýP4D[#]±àдPyExtention£¬²¢Ã»ÓÐÒ»¸öºÜºÃµÄÈëÃÅÎÄ µµ£¬±¾ÎÄÊÔͼÌîдÕâ¸ö¿Õ°×¡£±¾ÎÄ·ç¸ñÍêȫģ·ÂWriting Python Extensions[1],Ï£ÍûÒÔÀý×ÓΪ±¾£¬Èôó¼ÒºÜ¿ìµÄ½øÈë״̬¡£

ÒýÑÔ

±¾ÎļÙÉèÄã

  • ¶®µÃPython
  • ¶®µÃDelphi
  • ÏëҪͨ¹ýP4D±àдPython Extension
  • ÒѾ­°²×°ÁËDelphi7£¬P4D£¬Python2.4ÒÔÉÏ¡£

µÚÒ»¸öPython Extension

ÒÔϵÄÀý×ÓÊÇ¿ÉÒÔÖ±½ÓʹÓõģ¬Ö»Òª¿½±´ÈçÏ´úÂ룬´æ·Åµ½ExAdd.dpr£¬Ö±½ÓÓÃDelphi±àÒ룬¾Í¿ÉÒÔ³ÉΪһ¸öPython Extension ¡£ ÎÒÃÇ¿ÉÒÔÊ×ÏÈ¿´µ½Ð§¹û£¬È»ºóÔÚ·ÖÎö³ÌÐò¡£

×îСµÄÀý×Ó

×îСµÄÀý×Ó:

{ÎļþÃû ExAdd.dpr}
library ExAdd;
uses SysUtils,Classes,PythonEngine;
{$E pyd}
var
FModule : TPythonModule;
FEngine:TPythonEngine ;
function Add( Self, Args : PPyObject ) : PPyObject; far; cdecl;
var
a, b : Integer;
begin
with GetPythonEngine do
begin
if PyArg_ParseTuple( args, 'ii:Add', [@a, @b] ) <> 0 then
begin
Result := PyInt_FromLong( a + b );
end
else
Result := nil;
end;
end;
procedure initExAdd; cdecl;
begin
FEngine := TPythonEngine.Create(nil);
FModule := TPythonModule.Create(nil);
FModule.Engine := FEngine;
FModule.ModuleName := 'ExAdd';
FModule.AddMethod( 'exadd', @Add, 'exadd(a,b) -> a+b ' );
FEngine.LoadDll;
end;
exports
initExAdd;
var
OldExitProc: pointer;
procedure MyExitProc;
begin
FModule.Free;
FEngine.Free;
ExitProc := OldExitProc;
end;
begin
OldExitProc := ExitProc;
ExitProc := @MyExitProc;
end.
// ²âÊÔ´úÂë
//from ExAdd import *
//print exadd(1,10)

½â˵

ÕâÊÇÒ»¸ö×îСµÄÀý×Ó£¬Ö»ÒªÒ»¸öÎļþExAdd.dpr ,²»ÐèÒªÈÎºÎÆäËûµÄPas UnitÎļþ¾Í¿ÉÒÔÁË¡£ µ±ÎÒÃǰÑËû·Åµ½pyµÄsyspathÄÚ£¬±ÈÈç<pythonhome>libsite-packages,ÔÚpywinÄÚ£¬¿ÉÒÔ×öÈçϲâÊÔ:

>>> from ExAdd import *
>>> print exadd(1,10)
11
>>>

¿ÉÒÔ¿´µ½£¬PythonÄڵijÌÐòȷʵ³É¹¦µÄµ÷ÓÃÁËͨ¹ýDelphiдµÄÀ©Õ¹¡£ÈçºÎ×öµ½µÄ£¿

ÈçºÎ×¢²áÒ»¸öÄ£¿é

µ±PythonÄÚÖ´ÐÐ

from ExAdd import *

ʱ£¬½«»áµ½syspathÄÚѰÕÒExAdd.pyd,ÕâÀïµÄpyd¾ÍÊÇÒ»°ãµÄdll£¬Ö»²»¹ý»¹ÓÐһЩԼ¶¨¡£ µ±PyÕÒµ½Õâ¸öÎļþºó£¬¾Íµ÷ÓÃÒý³öº¯ÊýinitExAdd,Õâ¸öº¯ÊýµÄÃüÃû¾ÍÊÇpython³ÌÐòºÍ.pydÄ£¿éµÄµÄÒ»¸öÔ¼¶¨----º¯ÊýÃüÃû±ØÐëΪinit+moduleÃû³Æ¡£ Ò»°ãÀ´Ëµ£¬ÔÚinitº¯ÊýÄÚ£¬¾Í½øÐÐÒýÇæµÄ³õʼ»¯£¬Ä£¿éµÄ×¢²á£¬º¯Êý£¬ÀàÐ͵Ä×¢²áµÈµÈ¹¤×÷¡£ÕâÀïÀý×ÓÄÚ£¬ÎÒÃÇʹÓÃÁËTPythonEngine£¬ TPythonModuleÁ½¸öP4DÌṩµÄÀ࣬°ïÖúÎÒÃÇ×öÕâЩ¹¤×÷¡£ ×¢²áÄ£¿éʱ£¬Òª×¢Òâ

FModule.ModuleName := 'ExAdd';

ÄÚµÄModuleName¾ÍÊÇÔÚPythonÄÚʹÓõÄÄ£¿éÍêȫһÖ£¬µ±È»ÎÒÃÇ¿ÉÒÔʹÓÃÆäËûµÄÃû×Ö£¬±ÈÈçExQuickAdd£¬Ö»Òª

from ExAdd import *

ÄÚʹÓõÄÄ£¿éÃû³ÆÒ»Ö¼´¿É¡£ÎªÁË·½±ãºÍÒ»Ö£¬ÎÒÃÇ¿ÉÒÔÔ¼¶¨dllµÄÃû×Ö£¬pythonÄÚµÄmodule£¬delphiÄÚµÄTPythonModuleÃû×ÖÍêȫһÖ¡£ ÕâÔÚÓï·¨Éϲ¢·Ç±ØÐ룬²»¹ýÕâÑù×öÊÇÒ»¸öºÜºÃµÄϰ¹ß¡£

ÈçºÎ×¢²áÒ»¸öº¯Êý

ÈκÎÒ»¸ö°´ÕÕÈçÏÂÔ­ÐÍ×¢²áµÄº¯Êý£¬¶¼¿ÉÒÔ±»×¢²áΪPyExtentionµÄº¯Êý¡£

function Add( Self, Args : PPyObject ) : PPyObject; far; cdecl;

ÆäÖÐcdecl˵Ã÷·þ´ÓCÓïÑԵĵ÷Óù淶£¬¶ø²»ÊÇPascal»òÕ߯äËû¡£±Ï¾¹PythonÊÇCÓïÑÔд¾ÍµÄ£¬µ±È»°´ÕÕCÓïÑÔµÄϰ¹ßÀ´¡£ Õâ¸öº¯ÊýÔ­ÐÍÖУ¬²ÎÊý½«»á°üÀ¨Self£¬Args£¬·µ»ØÖµµÃÒ²ÊÇÒ»¸öPPyObject£¬ÊìϤPythonÓïÑԵĶ¼ÖªµÀ£¬ÈκÎÒ»¸öPythonº¯ÊýÔÚ±»µ÷ÓÃʱ¶¼»á´«µÝÒ»¸öSelf Ö¸Õë½øÀ´£¬²¢ÇÒÒÔTupleµÄ·½Ê½´«µÝ²ÎÊýÁÐ±í£¬Õâ¸öAddº¯ÊýµÄʵÏÖÔ¼¶¨ÉÏÒ²¾Í±íÏÖ³öÀ´ÁË£¬ËùÓеÄÀàÐͶ¼ÊǶÔÏó¡£±ÈÈçAdd(3,4)Õâ¸öµÄPythonµ÷Ó㬲ÎÕÕAddÔÚDelphiÖк¯ÊýÔ­ÐÍ£¬ ÉÏ£¬ÄÇô"3,4"×÷Ϊһ¸öTuple¶ÔÏ󣬰éËæSelf£¬Ò²ÊÇÒ»¸öPPyObject,·µ»ØÖµ7Ò²ÊÇÒ»¸öPPyObjectÀ´±í´ï¡£ Òª²»Ôõô¶¼ËµPythonÂýÄØ£¿±¾À´Ò»¸ö¼Ó²Ù×÷¿ÉÒÔÖ±½Ó¶ÔÓ¦»ã±àÖеÄÒ»¸öÖ¸ÁÏÖÔÚÓÖÊǶÔÏóÓÖÊÇÖ¸Õ룬µ±È»ºÜÄÑ¿ìÁË¡£

Ò»µ©ÓÐÁËÕâÑùµÄÉùÃ÷£¬¾Í¿ÉÒÔÕâÑù×¢²áº¯Êý¡£

FModule.AddMethod( 'exadd', @Add, 'add(a,b) -> a+b ' );

ÒÔÉÏÓï¾äÏòPythonϵͳÉùÃ÷£¬exaddº¯ÊýµÄʵÏÖÔÚaddÄÚ£¬×îºó²ÎÊý×÷Ϊ__docstring__¡£µ±IDEÄÚʹÓÃÕâ¸öº¯Êýʱ£¬¿ÉÒÔͨ¹ýcodeinsight,»òÕßhelpÀ´»ñµÃº¯ÊýµÄʹÓÃ˵Ã÷¡£ ÏÖÔÚÀ´¿´addµÄʵÏÖ´úÂë¡£ Ò»ÑÛ¿´¹ýÈ¥£¬PyArg_ParseTuple£¬PyInt_FromLongÊÇÁ½¸öÌØ±ðµÄ¶«Î÷¡£ PyArg_ParseTuple¸ºÔð°Ñ´«½øÀ´µÄargs±ä³É¼òµ¥µÄDelphiÀàÐÍ£¬ÔÚPpyobjectÄÚ´æ´¢µÄ3,4£¬·Ö±ð´æ·Åµ½a,b:IntegerÄÚ£¬¾ÍÊÇ

PyArg_ParseTuple( args, 'ii:Add', [@a, @b] ) <> 0

ÆäÖеڶþ¸ö²ÎÊý 'ii:Add' £¬ÓÐЩÏñÊÇFormat¸ñʽ£¬iÖ¸Ã÷ÀàÐÍΪInteger£¬Á½¸öIÖ¸Ã÷ÓÐÁ½¸öÕûÊý,:addÊÇ¿ÉÑ¡µÄ£¬µ±³ö´íµÄʱºò£¬ÓÐ:add£¬¿ÉÒÔ°ïÖú³ÌÐòÔ±¸üºÃµÄÕÒµ½´íÎó¡£ ÕâÑù¾Í°ÑPPyobejectËù±í´ïµÄPythonTypeתΪһ°ãDelphiÀàÐÍ£» ¶øPyInt_FromLongÕâÊÇÏë·¨£¬Ëû°ÑDelphiµÄLongÀàÐÍת»»ÎªPyObjectµÄInteger£»´Ó¶ø¿ÉÒÔÈýá¹û¿ÉÒÔΪPythonʶ±ð¡£ ÕâÁ½¸öº¯Êý¾¡¹ÜÊÇP4dʵÏֵ쬵«ÊǺÍPython/C interfaceÊÖ²áÄڹ涨µÄº¯ÊýÃû³ÆÒ»Ö£¬Òò´Ë¾ßÌåµÄµ÷Ó÷½·¨Ò²¿ÉÒÔ¿´Python/C interfaceÊֲᡣ

ʵ¼ÊÉÏPythonʵÏÖÄڵĶÔÏó±í´ï²ÉÓÃÁËÒ»¸ö½á¹¹(Struct),ºÜÓÐһЩ¸´ÔÓ£¬ÎÒÃÇÏÖÔÚ¿ÉÒÔÔںܸ߲ãµÄÈ¥¿´£¬Òª¸ÐлP4DËù×öµÄ¹¤×÷¡£

ʵÏÖÒ»¸öÀà

µÚÒ»¸öÀý×Ó¿ÉÒÔ¹¤×÷£¬²¢ÇÒÄܹ»ÑÝʾע²áÄ£¿é£¬º¯ÊýºÍһЩ»ù±¾µÄPython ExtµÄ¸ÅÄî¡£ ¶ÔÓÚ³¤ÆÚʹÓÃDelphiÕâÑùµÄOOÓïÑÔ£¬½ö½ö¹«¿ªº¯Êýµ±È»²»¹»·½±ã£¬ÎÒÃÇÐèÒªµÄÊÇÈ«OO±à³Ì£¬¼´Ê¹¿çÔ½ÁËÓïÑÔ£¬Ò²²»»á·ÅÆúÕâÑùµÄϰ¹ß¡£ ÎÒÃÇÏÖÔÚÒªÈÃDelphiµÄÀà¿ÉÒÔΪPython¡£

ÓÖÒ»¸öÀý×Ó

ÄãÊ×ÏÈ¿´µ½µÄÒÀÈ»ÊÇÒ»¸öÀý×Ó,ÎÒÃÇÒª°ÑDelphiÖеÄTPoint¹«¿ª³öÀ´£¬ÈÃpython¿ÉÒÔµ÷Óã¬Ä£¿éÃû³ÆÎªdpoint£¬×îÖÕÎÒÃÇÒªÔÚpythonIDEÄÚ¿´µ½µÄЧ¹û:

>>> from dpoint import *
>>> print SmallPoint(222,111)
<SmallPoint at D393C8>
>>> SmallPoint.__doc__
'wrapper for Delphi TPoint typen'

P4DΪע²áÀàÕâÑùµÄ¹¤×÷ÌṩÁËTPyDelphiWrapperÀ࣬ÔÚÕâ¸öÀý×ÓÀÎÒÃÇÎ§ÈÆÕâTPyDelphiWrapperÀ´·ÖÎö¡£

Àý×Ó´úÂë:

library dpoint;
uses
Sharemem ,SysUtils,Classes,WrapDelphi,Types,PythonEngine;
{$E pyd}
var
FModule : TPythonModule;
FEngine:TPythonEngine ;
FDelphiWrapper : TPyDelphiWrapper;
procedure initdpoint; cdecl;
begin
FEngine := TPythonEngine.Create(nil);
FModule := TPythonModule.Create(nil);
FModule.Engine := FEngine;
FModule.ModuleName := 'dpoint';
FDelphiWrapper := TPyDelphiWrapper.Create(nil);
FDelphiWrapper.Engine := FEngine;
FDelphiWrapper.Module := FModule;
FEngine.LoadDll;
end;
exports
initdpoint;
var
OldExitProc: pointer;
procedure MyExitProc;
begin
FModule.Free;
FEngine.Free;
ExitProc := OldExitProc;
end;
type
TPyDelphiPoint = class(TPyObject)
private
fValue: TPoint;
protected
public
constructor CreateWith( APythonType : TPythonType; args : PPyObject ); override;
class procedure SetupType( PythonType : TPythonType ); override;
end;
Type
TTypesRegistration = class(TRegisteredUnit)
public
function Name : String; override;
procedure RegisterWrappers(APyDelphiWrapper : TPyDelphiWrapper); override;
end;
function TTypesRegistration.Name: String;
begin
Result := 'Types';
end;
procedure TTypesRegistration.RegisterWrappers(APyDelphiWrapper: TPyDelphiWrapper);
begin
inherited;
APyDelphiWrapper.RegisterHelperType(TPyDelphiPoint);
end;
constructor TPyDelphiPoint.CreateWith(APythonType: TPythonType;
args: PPyObject);
var
x, y : Integer;
begin
inherited;
if APythonType.Engine.PyArg_ParseTuple( args, 'ii:Create', [@x, @y] ) <> 0 then
begin
fValue.X := x;
fValue.Y := y;
end
end;
class procedure TPyDelphiPoint.SetupType(PythonType: TPythonType);
begin
inherited;
PythonType.TypeName := 'SmallPoint';
PythonType.TypeFlags := PythonType.TypeFlags + [tpfBaseType];
PythonType.DocString.Text := '12345';
end;
begin
RegisteredUnits.Add(TTypesRegistration.Create);
OldExitProc := ExitProc;
ExitProc := @MyExitProc;
end.

×¢²á¹ý³Ì

Ò»¸öÀà±ØÈ»ÒªÊôÓÚijһ¸öÄ£¿é£¬×¢²áÒ»¸öÀà¾ÍÉæ¼°µ½×¢²áÒ»¸öÄ£¿é¡£¹ØÓÚ×¢²áÄ£¿é£¬ÔÚÀý×ÓÖÐÕ¼¾ÝÁ˲»ÉÙ´ø´úÂ룬µ«ÊÇËüºÍµÚ¶þ²¿·ÖÍêȫһÑù£¬ÎÒÃÇÂÓ¹ý²»¿´¡£ ±¾À´×¢²áÒ»¸öÀàÊÇÓÐЩ¸´ÔӶȵģ¬Èç¹ûÏëÒªÖªµÀÕâ¸ö¸´ÔÓ¶È£¬¿ÉÒÔÏÈ¿´¿´²Î¿¼ÎÄÏ×1ÄÚµÄÃèÊö¡£²»¹ý²ÉÓÃP4DµÄÀàÐÍ×¢²á¿ò¼Ü¾Í¼òµ¥¶àÁË¡£ ÎÒÃǵÄÀý×ÓpydÃüÃûΪdpoint ,ÎÒÃÇ×¼±¸°ÑTPointÀàÐ͹«¿ªµ½PythonÄÚ¡£

ÔÚinitdpointº¯ÊýÄÚ£¬TPythonEngine£¬TPythonModuleÕÕÑùµÄ³õʼ»¯£¬±ÈÆðº¯Êý×¢²áÀ´Ëµ£¬²»Í¬µÄµØ·½ÔÚÓÚ´´½¨ÁËTPyDelphiWrapperµÄʵÀýgDelphiWrapper£¬ ²¢ÇÒÖ¸Ã÷ËûËùÊôµÄPythonEngine£¬PythonModule:

procedure initdpoint;
begin
gEngine := TPythonEngine.Create(nil);
gEngine.AutoFinalize := False;
gModule := TPythonModule.Create(nil);
gModule.Engine := gEngine;
gModule.ModuleName := 'dpoint';
gDelphiWrapper := TPyDelphiWrapper.Create(nil);
gDelphiWrapper.Engine := gEngine;
gDelphiWrapper.Module := gModule;
gEngine.LoadDll;
end;

gDelphiWrapper½«»áÔÚRegisteredUnitListѰÕÒRegisteredUnit£¬²¢ÇÒµ÷Óà Õâ¸öÀà±ðÄÚµÄRegisterWrappers·½·¨£¬Í¨¹ýÕâ¸ö·½·¨»òÕßÐèҪע²áµÄPythonÀàµÄDelphi°ü×°Àà¡£ Òò´Ë£¬ÎÒÃÇÒª×öµÄÊÂÇé¾ÍÊÇ£º Ô¼¶¨ÊµÏÖÁ½¸öÀ࣬һ¸öÊÇÐèÒª¹«¿ªµÄÀàÐ͵ÄWrapper£¬ÕâÀï¾ÍÊÇTPyDelphiPoint£¬Ò»¸öÊÇ×¢²áÕâ¸öWrapperµÄ×¢²áÀ࣬±¾Àý×ÓÄÚ¾ÍÊÇTTypesRegistration¡£ TTypesRegistrationֻҪʵÏÖÁ½¸ö¸²¸Ç»ùÀàµÄ·½·¨£¬´Ó¶ø´ïµ½Í¨ÖªTPyDelphiWrapperÐèҪע²áµÄÀàÊÇTPyDelphiPoint¡£

function Name : String; override; procedure RegisterWrappers(APyDelphiWrapper : TPyDelphiWrapper); override;

ÎÒÃǸü¶àµÄ×¢ÒâÁ¦£¬ÓÈÆäÊÇÒÔºóµÄ¸ü¶à¶ÔPythonExtensionÌØÐÔµÄÀûÓ㬼¯ÖÐÓÚTPyDelphiPointÉÏ¡£ TPyDelphiPoint£¬×÷Ϊһ¸öPythonType£¬×îÉÙҪʵÏֵķ½·¨ÓÐ:

      constructor CreateWith( APythonType : TPythonType; args : PPyObject ); override;
class procedure SetupType( PythonType : TPythonType ); override;

ÎÒÃÇ¿ÉÒÔ×¢Òâµ½£¬CreateWith´«µÝµÄargsÒÀÈ»ÊÇPPyObjectÀàÐÍ£¬ºÍǰÎÄ̸µ½µÄadd·½·¨¶Ô²ÎÊýºÍ·µ»ØÖµµÄ´¦Àí¶¼ÊÇÒ»Öµġ£ SetupType½«»áÖ¸Ã÷ÔÚPythonÄÚÈçºÎʹÓÃÕâ¸öÀàÐÍ£¬¸ù¾ÝÔ´´úÂëÖªµÀ£¬SetupTypeÖ¸Ã÷Õâ¸öÀàÐÍÔÚPythonÄÚµÄÀàÐÍΪSmallPoint£¬Ìṩ»ù±¾·þÎñ£¨fvbase£©,ÀàÐÍÎĵµ__doc__Ϊ '12345'£¬ ²âÊÔÓÃÀý3.1´úÂëÈç¹ûÕý³£ÔËÐУ¬¾Í×ÔÈ»µÄ֤ʵÁËÕâÒ»µã¡£

³ä·ÖÀûÓÃPythonµÄÌØÐÔ

repr·þÎñ

ÒÔÉÏÀý×Ӻܼòµ¥£¬µ«ÊÇ¿ÉÒÔ±í´ïÖ÷Ö¼£¬ÊǽøÒ»²½Á˽âºÍ°ÑÎÕP4D±àдÀ©Õ¹µÄ»ù´¡¡£ ´Ó3.1µÄ²âÊÔÓÃÀý¿´:

>>> print SmallPoint(222,111)
<SmallPoint at D393C8>

ÕâÑùµÄÊä³öºÜ²»ÓѺã¬ÎÒÃÇÏ£ÍûËûÊÇÕâÑùµÄ:

>>> print SmallPoint(222,111)
222,111

ÕâÑùµÄ·þÎñÔÚpyÄÚÔçÒÑ´æÔÚ£¬ËüµÄÃû×Ö½Ð×örepr£¬Ã¿¸ö¶ÔÏóÈç¹ûÏ£Íû´òÓ¡ÓѺ㬶¼Ó¦¸ÃÖ§³ÖÕâÑùµÄ·þÎñ¡£ ÔÚDelphi±àдµÄPyÀ©Õ¹ÖУ¬ÈçºÎ×öµ½ÕâÑùµÄЧ¹û£¿

Àý×Ó

Ò»µ©¿ò¼ÜÆÌ³ÂÍê±Ï£¬±àд¾ßÌåµÄ¹¦Äܾͺܼòµ¥ÁË¡£repr·þÎñÖ»Òª¸²¸ÇÒ»¸ö·½·¨£¬¼ÓÉ϶Էµ»Ø²ÎÊýµÄ°ü×°¾Í¿ÉÒÔÁË:

function  Repr : PPyObject; override;
..
implementation
..
function TPyDelphiPoint.Repr: PPyObject;
begin
with GetPythonEngine do
Result := PyString_FromString(PChar(Format('<Point (%d, %d)>', [Value.X, Value.Y])));
end;

¸ü¶à

ÉèÖÃÊôÐÔ£¬ÐèÒª¸²¸ÇRegisterGetSets·½·¨:

class procedure TPyDelphiPoint.RegisterGetSets(PythonType: TPythonType);
begin
inherited;
with PythonType do
begin
AddGetSet('X', @TPyDelphiPoint.Get_X, @TPyDelphiPoint.Set_X,
'Provides access to the X coordinate of a point', nil);
AddGetSet('Y', @TPyDelphiPoint.Get_Y, @TPyDelphiPoint.Set_Y,
'Provides access to the Y coordinate of a point', nil);
end;
end;

±ðÍüÁËÔÚSetupTypeÄÚ¼ÓÈëÒ»ÐУº

PythonType.Services.Basic := PythonType.Services.Basic+[bsGetAttrO, bsSetAttrO];

¸æËßPythonÄãµÄ·þÎñÖÐÓÐÊôÐÔµÄÖ§³Ö¡£

ÔÊÐídpointÖ®¼ä±È½Ï´óС£¬ÐèÒª¸²¸ÇCompare·½·¨:

function TPyDelphiPoint.Compare(obj: PPyObject): Integer;
var
_other : TPyDelphiPoint;
begin
if IsDelphiObject(obj) and (PythonToDelphi(obj) is TPyDelphiPoint) then
begin
_other := TPyDelphiPoint(PythonToDelphi(obj));
Result := CompareValue(Value.X, _other.Value.X);
if Result = 0 then
Result := CompareValue(Value.Y, _other.Value.Y);
end
else
Result := 1;
end;

ͬÑù±ðÍüÁËÔÚSetupTypeÄÚ¼ÓÈëÒ»ÐÐ:

PythonType.Services.Basic := PythonType.Services.Basic+[bsCompare];

¸æËßPythonÄãµÄ·þÎñÖÐÓÐbsCompareµÄÖ§³Ö¡£

±àдÀ©Õ¹ºó×öʲô£¿

  1. P4dµÄ´úÂëÖµµÃÒѶÁ£¬ÒòΪ
  • »ùÓÚ×¢²áµÄ¼Ü¹¹£¬dllÖ±½Óµ½ÀàTDynamicDllÖµµÃ¿´£¬Á˽âPythonµÄÄÚ²¿ÊµÏÖ£¬P4d±¾Éí¾ÍÊÇPythonºÍDelphi½áºÏ±à³ÌµÄÁ¼ºÃ°ñÑù¡£
  • ¿ÉÒԺܺõÄÁ˽âPythonÀàÐ͵ÄÄÚ²¿ÊµÏÖ£¬±ÈÈçÕûÊýµÄ½á¹¹ÀïÃæÓÐÄÇЩ×ֶΣ¬ÓÐÄÇЩ»ù±¾ÀàÐÍ·þÎñµÄ·½·¨
  1. ʹÓÃP4D»¹¿ÉÒÔÔõôÑù£¿
Õâ¸öͼ¾°ÔÚÎÒ×Ô¼º»¹²¢²»ºÜÇå³þ¡£µ«ÊÇPythonºÍDelphiµÄ½áºÏµÄÔ¸ÍûÔÚÎÒÈ´Ò»Ö±ºÜÇ¿ÁÒ£¬Ï£ÍûÓиü¶àµÄÅóÓѲÎÓë½øÀ´£¬Ìá³ö¸ü¶àµÄÏë·¨¡£

²Î¿¼

[1] Michael Hudson 's writingPythonExtensions

[2] P4D Python For DelphiµÄËõд£¬´ÓËüµÄChange.txtÄÚ¿ÉÒÔ¿´µ½£¬Õâ¸öP4D´Ó1998Äê¼ÆËãÓнӽü10ÄêµÄÀúÊ·ÁË¡£ÊÀµÀ²×É£°¡¡£

[3] Using Delphi and Python together pythonDelphiTalk .

ºÜºÃµÄ½Ì³Ì£¬¿Éϧ¶ÔÊä³öÀàÐÍÕâÀïÓïÑɲ»Ï꣬²»ÖªµÀΪʲô¡£ÕâÒ²ÊÇÎÒҪд±¾Îĵ͝Òò¡£


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.

¡ªAndy¡¡Bulka


free anal porn [»Ø¸´]

free anal porn

poppins | 29/04/2008, 11:07

dtypskg vdxbsfa [»Ø¸´]

vdzibuqag hxzfwtkm tlxpde garud mbgxu qlwheao tjnymogb [URL]http://www.kusbjfti.spwta.com[/URL] ekjnvuht btqocpdef

cpohils dxbvn | 22/02/2008, 14:35

onbwd fity [»Ø¸´]

dehugr kysr fdckwh uaiqzlpyd dhcvpmn ymkdx fsnrwg [URL=http://www.pdlunz.vzjsyed.com]wjomn ysurigotw[/URL]

ianputo lvzsmtji | 22/02/2008, 14:34

isgwr folyjqm [»Ø¸´]

twdckrniu zbtsxdm hkzjm qmdj eoznftrv jynhasfmx ugkhsr http://www.kdqztgcx.fwhre.com

gupt hkadpwjy | 22/02/2008, 14:34

wqrpy zoqteuys [»Ø¸´]

wlpjvbo uhadj wxndzs lteyfimbw lwnutcjki acybvhswn ewclbptgf

ujtdxhif ugpaksodm | 22/02/2008, 14:31

None [»Ø¸´]

bid penis enlargement pills [url=http://circleofcarehcr.com/_cache/average-erect-male-penis-size.html]bid penis enlargement pills[/url]

benettom | 20/12/2007, 02:31

ahrcpxdizu [»Ø¸´]

Mysterious big sister is watching in Gossip Girl

ahrcpxdizu | 29/11/2007, 01:16

fzmjafxemr [»Ø¸´]

Britney Spears is addicted to plastic surgery Melbourne Herald Sun

fzmjafxemr | 29/11/2007, 01:13

twitztafsx [»Ø¸´]

Cycling’s Drug Problem Leads T Mobile to End Sponsorship New York Times

twitztafsx | 29/11/2007, 00:36

rwbsvzhzpy [»Ø¸´]

Wild: GM thinks Burns is pressing Minneapolis Star Tribune

rwbsvzhzpy | 29/11/2007, 00:31

qvcfdihdtf [»Ø¸´]

Tommies' latest win is one of Fritz's best Minneapolis Star Tribune

qvcfdihdtf | 29/11/2007, 00:26

ulifqkeojp [»Ø¸´]

Catches on rise with Houston's additional redlight cams Houston Chronicle

ulifqkeojp | 28/11/2007, 21:43

None [»Ø¸´]

Good site! Thanks. [url=https://itc.utk.edu/cgi-bin/netforum/ohld390/a/3--71?cialis-soft-tabs]cialis soft tabs[/url] cialis soft tabs https://itc.utk.edu/cgi-bin/netforum/ohld390/a/3--71?cialis-soft-tabs [url=https://itc.utk.edu/cgi-bin/netforum/ohld390/a/3--72?generic-soma]generic soma[/url] generic soma https://itc.utk.edu/cgi-bin/netforum/ohld390/a/3--72?generic-soma [url=https://itc.utk.edu/cgi-bin/netforum/ohld390/a/3--73?kisa-soma]kisa soma[/url] kisa soma https://itc.utk.edu/cgi-bin/netforum/ohld390/a/3--73?kisa-soma

Acib | 08/05/2007, 00:22

None [»Ø¸´]

Good site! Thanks. [url=https://itc.utk.edu/cgi-bin/netforum/maes551/a/3--26?lipitor-grapefruit]lipitor grapefruit[/url] lipitor grapefruit https://itc.utk.edu/cgi-bin/netforum/maes551/a/3--26?lipitor-grapefruit [url=https://itc.utk.edu/cgi-bin/netforum/maes551/a/3--27?prozac-side-effects]prozac side effects[/url] prozac side effects https://itc.utk.edu/cgi-bin/netforum/maes551/a/3--27?prozac-side-effects [url=https://itc.utk.edu/cgi-bin/netforum/maes551/a/3--28?drug-sample-viagra]drug sample viagra[/url] drug sample viagra https://itc.utk.edu/cgi-bin/netforum/maes551/a/3--28?drug-sample-viagra

Uduru | 07/05/2007, 21:22

None [»Ø¸´]

Nice work! [url=http://https://itc.utk.edu/cgi-bin/netforum/hewlett/a/3--13?phentermine-no-prescription]phentermine no prescription[/url] phentermine no prescription https://itc.utk.edu/cgi-bin/netforum/hewlett/a/3--13?phentermine-no-prescription [url=https://itc.utk.edu/cgi-bin/netforum/hewlett/a/3--14?discount-phentermine]discount phentermine[/url] discount phentermine https://itc.utk.edu/cgi-bin/netforum/hewlett/a/3--14?discount-phentermine [url=https://itc.utk.edu/cgi-bin/netforum/hewlett/a/3--15?buy-phentermine-online]buy phentermine online[/url] buy phentermine online https://itc.utk.edu/cgi-bin/netforum/hewlett/a/3--15?buy-phentermine-online

Stark | 07/05/2007, 21:21

None [»Ø¸´]

Good site! Thanks. [url=https://itc.utk.edu/cgi-bin/netforum/maes551/a/3--29?generic-pack-viagra]generic pack viagra[/url] generic pack viagra https://itc.utk.edu/cgi-bin/netforum/maes551/a/3--29?generic-pack-viagra [url=https://itc.utk.edu/cgi-bin/netforum/mnf/a/3--41?order-viagra]order viagra[/url] order viagra https://itc.utk.edu/cgi-bin/netforum/mnf/a/3--41?order-viagra [url=https://itc.utk.edu/cgi-bin/netforum/mnf/a/3--42?herbal-alternative-viagra]herbal alternative viagra[/url] herbal alternative viagra https://itc.utk.edu/cgi-bin/netforum/mnf/a/3--42?herbal-alternative-viagra

Bolen | 07/05/2007, 21:21

None [»Ø¸´]

Good site! Thanks. [url=https://itc.utk.edu/cgi-bin/netforum/hewlett/a/3--16?cialis-levitra]cialis levitra[/url] cialis levitra https://itc.utk.edu/cgi-bin/netforum/hewlett/a/3--16?cialis-levitra [url=https://itc.utk.edu/cgi-bin/netforum/hewlett/a/3--17?buy+cialis+online+say+wordpress]buy cialis online say wordpress[/url] buy cialis online say wordpress https://itc.utk.edu/cgi-bin/netforum/hewlett/a/3--17?buy+cialis+online+say+wordpress [url=https://itc.utk.edu/cgi-bin/netforum/hewlett/a/3--18?cialis-comparison-levitra-viagra]cialis comparison levitra viagra[/url] cialis comparison levitra viagra https://itc.utk.edu/cgi-bin/netforum/hewlett/a/3--18?cialis-comparison-levitra-viagra

Janin | 07/05/2007, 21:21

None [»Ø¸´]

Nice work! [url=https://itc.utk.edu/cgi-bin/netforum/adv490/a/3--22]buy ambien[/url] buy ambien https://itc.utk.edu/cgi-bin/netforum/adv490/a/3--22 [url=https://itc.utk.edu/cgi-bin/netforum/adv490/a/3--24]buy cialis online[/url] buy cialis online https://itc.utk.edu/cgi-bin/netforum/adv490/a/3--24 [url=https://itc.utk.edu/cgi-bin/netforum/adv490/a/3--25]order ambien[/url] order ambien https://itc.utk.edu/cgi-bin/netforum/adv490/a/3--25

Spang | 07/05/2007, 14:39

None [»Ø¸´]

Good site! Thanks. [url=https://itc.utk.edu/cgi-bin/netforum/adv490/a/3--20]buy soma[/url] buy soma https://itc.utk.edu/cgi-bin/netforum/adv490/a/3--20 [url=https://itc.utk.edu/cgi-bin/netforum/adv490/a/3--21]buy xanax[/url] buy xanax https://itc.utk.edu/cgi-bin/netforum/adv490/a/3--21 [url=https://itc.utk.edu/cgi-bin/netforum/adv490/a/3--23]cialis drug viagra vs[/url] cialis drug viagra vs https://itc.utk.edu/cgi-bin/netforum/adv490/a/3--23

naMevrain | 07/05/2007, 14:39

Ò»µãÒÉÎÊ [»Ø¸´]

µÚÒ»¸ö×î¼òµ¥µÄÀý×ÓÖУ¬ÔÚ
FModule.AddMethod( 'exadd', @Add, 'exadd(a,b) -> a+b ' );
ºóÃæÊDz»ÊÇ»¹Ó¦¸Ã¼ÓÒ»¾ä£º
FModule.Initialize;

tocer | 07/04/2007, 22:02

дµÃºÜ²»´í [»Ø¸´]

ÊÕ²ØÁË

tocer | 05/04/2007, 11:03

·¢±íÆÀÂÛ

±êÌâ

ÔÚ´ËÌí¼ÓÆÀÂÛ

³Æºô

ÓÊÏ䵨ַ£¨¿ÉÑ¡£©

¸öÈËÖ÷Ò³£¨¿ÉÑ¡£©




Valid XHTML 1.0 Strict and CSS. Powered by pLog
Design by Blog.lvwo.com