Jump to content

pyscripter

Members
  • Content Count

    1005
  • Joined

  • Last visited

  • Days Won

    66

Posts posted by pyscripter


  1. 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'];


  2. 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 :classic_smile:.

     

    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.


  3. 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.


  4. @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.

     


  5. 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 length ndim indicating the shape of the memory as an n-dimensional array. Note that shape[0] * ... * shape[ndim-1] * itemsize MUST be equal to len.

     

    If you know the dimensions of the array you can directly cast to an equivalent pascal multidimensional array.


  6. 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.


  7. 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.


  8. 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.

     


  9. 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.

×