-
Content Count
1005 -
Joined
-
Last visited
-
Days Won
66
Posts posted by pyscripter
-
-
TPythonGUIInputOutput works with a linked memo. Use TPythonInputOutput instead.
-
As you can see from your output AddressNumber is a string (as well as the other fields). You need to convert it to an integer.
-
I am not exactly sure what you are trying to do, Also the 4th line in your script is doing nothing since you are assigning a new value in the next statement.
However, if you want to access these values in Delphi and store them say in a Delphi data structure, the easiest way is using VarPyth. For instance to access the AddressNumber, after running the above script, you use something like:
var AddressNumber := MainModule.od[0]['AddressNumber'];
-
29 minutes ago, JonRobertson said:Python's subprocess does more than DOS / command line stuff.
You can always use the this python module from Delphi using P4D
.
One of the most advanced such Delphi functions in the Jcl unit JclSysUtils. Look at ExecuteCmdProcess and the various Execute overloads. It uses overlapped IO for super efficient redirection of the produced output, instead of using a special thread or a timer. These functions are also suitable for execution from a task/thread, if you do not want to block the main thread.
The unit pyscripter/Source/uSysUtils.pas at master · pyscripter/pyscripter (github.com) extends the jcl functions to allow for an stdin pipe, customized environment variables and a buffered raw output.
-
You don't need to create and destroy zPythonDelphiVar in every thread.
Add zPythonDelphiVar to the form:
object zPythonDelphiVar: TPythonDelphiVar Engine = PythonEngine1 Module = '__main__' VarName = 'result' Left = 64 Top = 120 end
Then modify ExecuteWithPython to
procedure TPythonThread_Test1.ExecuteWithPython; var i: Integer; begin fOutputs := TStringList.Create; for i := 1 to 10000 do begin GetPythonEngine.ExecString(UTF8Encode(Script)); GetPythonEngine.CheckError; fOutputs.Add(Form1.zPythonDelphiVar.Value); end; end;
and it should work as you would expect.
And since you mentioned it, a fellow Brazilian @lmbelo Lucas Belo, is a major contributor to P4D and would certainly be in a position to help.
-
2 hours ago, djxandytche said:My intention was to show something that seems to me to be a problem in p4d.
You never explained what is the problem.
-
@djxandytche "Here is my code, please check and find the bugs" is not a good way to get support. Why should anyone spend one hour of their time, understanding and debugging your code?
You say you are beginner in Python. Then why do you start by running python code in threads, which requires some expert knowledge? Do you realize that there is no performance benefits in running python code in threads like this? They will be executed sequentially, thanks to the GIL.
-
2 hours ago, RegiStax said:OK. Thanks, can you point to the documentation that states this ?
For Delphi is here: Fundamental Syntactic Elements (Delphi) - RAD Studio (embarcadero.com) and for fpc Identifier - Free Pascal wiki.
-
arr := np.&ones(VarPythonCreate([N, N]), np.float64)
You do not need the &. It is only needed when using a keyword such as "array" (and Delphi does not need it even then).
-
Also, since you are interested in image manipulations, have a look at this thread: Place image in a TImage - Python4Delphi - Delphi-PRAXiS [en] (delphipraxis.net)
-
np.float64 corresponds to pascal double. Use double instead of single.
1 hour ago, RegiStax said:ArrItem := BuiltinModule.int(arr.GetItem(I));
Use BuiltinModule.float instead of BuiltinModule.int.
For multidimensional arrays you need to use the ndim and shapes fields of the Buffer structure.
An array of
Py_ssize_t
of lengthndim
indicating the shape of the memory as an n-dimensional array. Note thatshape[0] * ... * shape[ndim-1] * itemsize
MUST be equal tolen
.If you know the dimensions of the array you can directly cast to an equivalent pascal multidimensional array.
-
1 hour ago, djxandytche said:Could you check if there is still a problem in my code that I am not seeing/noticing?
Have you read the docs and the limitations of using subinterpreters?
Initialization, Finalization, and Threads — Python 3.12.4 documentation Also the docs for the PyInterpreterConfig structure?
pandas and numpy probably do not support running in subinterpreters.
I suggest that you forget about sub-interpreters unless you know very well what you are doing. But then you are on your own.
-
With SafePyEngine you do not get that choice. It is equivalent to emNewState, which is by far the most common use case.
-
@djxandytche Your code for running python code in threads is reinventing the wheel and the result is evidently incorrect.
Just follow the guidelines at PythonThreads · pyscripter/python4delphi Wiki (github.com) paying attention also to the preparation section.
Also, have a close look also to Demos 33 and 36.
-
A fix has been applied to P4D. Please check.
-
1
-
-
1 hour ago, shineworld said:File download failed.
I am aware of that. This is due Sourceforge changing the way you download files directly. Will work with the next update for this one just download from PyScripter - Manage /PyScripter-v5.0 at SourceForge.net and install.
-
@Stéphane Wierzbicki PyScripter with LLM support was released. See LLM_Support · pyscripter/pyscripter Wiki (github.com) for details.
-
2
-
-
I don't this is going to work. You are going to have duplicates of what is supposed to be Singletons (WrapDelphi, PythonEngine). It may work with run-time packages, but you would have to put all run-time packages in the same folder as the pyd files.
-
Have you followed the instructions at
-
I have used Python for many many years and really like the language. However it is slow. Really slow. And it does not use those damn CPU cores in my computer.
If you look at Python (created in 1989) and Ruby (created in 1993), they have a lot in common and the same limitations. Ruby is also quite nice and there was a lot of buzz around it about 10-15 years ago, but the slowness almost killed the interest in the language. On the other hand, and in a hard to explain way, Python flourished. In an irreversible way. It is now the main programming language in schools and universities, including computer science degrees. It is here to stay for many years to come.
Of course you can make python faster (Cython, using optimized python extension modules, multiprocessing etc.), but it takes an effort.
The focus of python development has been two areas:
- type safety (kind of odd for a dynamic language)
- execution speed
The two at some point may converge. There are a few developments regarding speed that offer some hope;
- Per interpreter Gil introduced in Python 12 (see https://en.delphipraxis.net/topic/10372-true-thread-parallelism-with-p4d-and-python-12). This is only accessible in embedded Python, using the python API. There were proposals to make it accessible through the interpreter, but were rejected in favour of the following developments.
-
Experimental Just-in-Time (JIT) Compilation introduced in the coming Python 3.13 (so far, the speed improvements are negligible)
-
Free threaded python (GIL-free) also introduced in python 3.13. Sounds good but it is incompatible with the GIL-enabled version of python. Extension modules are also incompatible and the two versions will be available in parallel, which sounds messy.
-
-
20 minutes ago, shineworld said:that exposes OnMouseDown, OnMouseMove, OnMouseUp, OnMouseWheel, OnResize, and OnPaint
WrapDelphi now automatically exposes all events. However, there has been no release of delphivcl recently incorporating these changes. If you create a custom delphivcl using the latest sources you do not need your custom panel. Panel should expose the above events.
-
3 hours ago, limelect said:self.button1.SetProps(Parent=self, Caption="Add", OnClick=self.Button1Click)
You don't need Parent = self. Otherwise that's the idea.
-
1 minute ago, Stéphane Wierzbicki said:Any ETA ?
Real soon!
-
1
-
I/O doesn't work
in Python4Delphi
Posted · Edited by pyscripter
Use the OnSendUniData event to process data sent by the python engine.