Jump to content
Sign in to follow this  
mbaghb

using python4delphi to call python function and get return result

Recommended Posts

hi

i working to a project that need run python function in delphi. i use python4delphi and writed some codes but that not worked properly. If anyone knows the answers to these questions, I would be very grateful if they would give me the answers

 1- How to call a python function with array of double as argument through Python4Delphi and receive the result of the function, which type is array of doubles, in Delphi.

 2- Is it possible to import numpy,scipy,...  just once and not need to import them every time i call the python function function

 

  sLineBreak := #13#10;
  Script := 'import numpy as np' + sLineBreak +
            'def CCalcCustomRegr(args):' + sLineBreak +
            '   some python code.......' + sLineBreak +
            '    return result '+ sLineBreak;

 

i need call CCalcCustomRegr with array of doubles as argument. and get result value which is array of doubles type.

 

Share this post


Link to post
19 hours ago, pyscripter said:

thank you. that is solved my problem for passing value from delphi to python script as argument. but i dont find solution to import numpy or scipy just once to the python script. actully i coding the dll in delphi to use it in mql5. i have some functions in python script that need numpy,scipy,... and no need import its every time i called functions. how to import its once when initalizing pythonengin and pythonmodule. that is very important for increase performance to get function result.  

Share this post


Link to post
1 hour ago, mbaghb said:

ow to import its once when initalizing pythonengin and pythonmodule. that is very important for increase performance to get function result

Importing a module for second, third.. time has no significant performance cost.

  • Like 1

Share this post


Link to post
On 10/27/2023 at 10:18 PM, pyscripter said:

Importing a module for second, third.. time has no significant performance cost.

thank you again.
 I checked a few things and finally, to build the my DLL project  and avoid importing  numpy , scipy, vmdpy,emd,PyEMD,... etc., for each function call, I implemented them in the "initialization" section. In this way, only calculations are performed in functions. Of course, as you said, there was no big difference in performance cost, it just got a little better.

The point that I noticed is the large size of the DLL file, which is about 8 MB. Is this normal based on the functions of the library that should be loaded or is there a solution for it?What do you suggest to increase performance using DLL to calculate python scripts?

 

And another point, is there a complete document of all functions, methods, etc. for P4D that I can download?

 

i appreciate you for your attention and guidance and excuse me if i don't create new topic I thought that if someone wants to create a DLL file with p4d, they might have the same problems and they will find answers to their questions here.

 

 function PyInit:integer;
var
  script:string;
begin
  try
    if (not Assigned(PythonEngine)) then
    begin
      PythonEngine := TPythonEngine.Create(nil);
      PythonEngine.AutoFinalize:=true;
      PythonEngine.UseLastKnownVersion:=true;
      PythonModule := TPythonModule.Create(nil);
      PythonModule.ModuleName:='MyDelphiModule';
      PythonModule.Engine:=PythonEngine;
      PythonEngine.LoadDll;
      script :=
       'import numpy as np'+ sLineBreak +
       'from PyEMD import CEEMDAN'+ sLineBreak +
       'from vmdpy import VMD'+ sLineBreak;
       GetPythonEngine.ExecString(UTF8Encode(script));
    end;
    Result:=0;
  except
    Result:=1;
  end;
end;


initialization
  MaskFPUExceptions(True);
  PyInit;

 

Share this post


Link to post
2 hours ago, mbaghb said:

The point that I noticed is the large size of the DLL file,

I have no idea why and how you are building a DLL, but from the size I can  tell that you are pulling in a large chank of the Delphi RTL.

 

2 hours ago, mbaghb said:

And another point, is there a complete document of all functions, methods, etc. for P4D that I can download?

Not except the source code.

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×