Jump to content

KoRiF

Members
  • Content Count

    32
  • Joined

  • Last visited

Posts posted by KoRiF


  1. in order to untangle the tangle, first take a look at reg_env.bat and how the EXEC variable is initialized and used

    It’s difficult to answer in absentia, but it looks like an incorrect initialization of some parameter on your side, an environment variable, or a bug in the component itself

    try updating to the latest version, and then check the settings, explicitly specifying directories wherever possible


  2. Hi,

     

    your issue is from here 

    snim0e = re.search(r"SNlM0e\":\"(.*?)\"", resp.text)

    check your "resp" value and does it fits to regular expression as expected

     

    generally speaking, your problem may have nothing to do with Delphi

    anyway, debugging is required on the python side to determine the cause

     

    have you read the Authentification section?

     

    (tools like Postman can also help)


  3. i think the first step you have to do is to convert to numpy array that supports memory buffer protocol

     

    This means that you will be able to operate on data in memory as if it were a C array.

    The next tricky step is to pass this array to Object Pascal which depends on your architecture and needs. For example, you could use the SharedMemory approach as the most universal

    The last step is parse your memory buffer on Delphi side 


  4. On 4/24/2023 at 6:19 PM, JGMS said:

    I want OpenCV + Numpy to deal with image handling smoothly. Any tips for examples?
     

     

     

    1. You could consider direct use of OpenCV for Delphi depending on your task and supposed "ecosystem". This is live project with working examples. There is also experimental version of OOP-oriented OpenCV
    2. Usage of P4D-Data-Scinces does not preclude you from using Python in a script style. If you can deal with some namespace confusion ("global" python variables and functions are actually accessible on Delphi side via the name of the corresponding modules available via BuiltinModule() and MainModule(), you can experiment with print(dir(...)) ) then you may well use some mix (and in some cases even be forced to use it) - although this may not be the best idea if done haphazardly 
    3. If you are strong in C++ you can try use OpenCV in Delphi through C++Builder intermediate adapter 

     

     

     

     


  5. No, not quite, but it was your link that helped me unravel the tangle and find the required solution. So thank you very much!!!

     

    My current solution is:

    1. Create field that contains reference to function that will be called a-la lambda later
    2. Create cdecl Delphi method that wraps the call of the "lambda" field with cdecl convention
    3. In the module initialization event handler initialize "lambda" field by a stub and add cdecl Delphi method-wrapper to the module under the name that cannot be just a 'lambda' for some odd reason 
    4. Create method that evaluates added delphi method as python callable object  
    5. Assign "lambda" field with an anonymous Delphi method 
    6. Evaluate added Delphi method that wraps "lambda" as python callable object
    7. Pass evaluated python callable object as python callable parameter
    8. Repeat steps (5) - (7) in other place if necessary

     

    In order to emulate several "lambdas" simultaneously, we would need to maintain something like a dictionary on the python side

     

    And yes, strictly speaking, this is neither a lambda nor on the Delphi side (in Delphi there is no pure concept of a lambda at all, and even a very close "anonymous method" term poorely suited due to, ironically, our callable object is renamed several times here), nor on the python side (it's just a permanent object of a named python function there)

     

    However, I use the term "lambda" because it fits the problem well conceptually: we wanted to define our callable at the call site, and in terms of the top-level algorithm (not the implementation), we achieved this

     

    This mostly works as expected, although for some other use cases I faced with unexpected issues in argument parsing. However, these issues are likely outside the scope of the question.

     

    Memory leaks are still relevant for such "lambda emulation" and this is an important aspect since calls can be repeated thousands of times so need to be careful

     

    Many thanks again!  

     

     


  6. In my opinion your problem may also be thread synchronization

    I don't know how the Telegram library is implemented in Python
     

    it may well be that the output will still work, but after the bot stops and your application has time to close so you don't see the output

    take a look at the DelayWrites and RawOutput properties of TPythonGUIInputOutput and try playing with this

     

    also, take a look at the examples of triggering event handling in Delphi and try to use it for debugging (set breakpoint there)

    See more examples in P4D Tutorials

     

    Hope this helps


  7. 10 hours ago, kalmen said:

    not even sure whether the bot is running

    you need to make sure your script is running at all, try running something a-la helloworld first

     

    if does not, then the problem may be in the "wrong" python, so you would have to explicitly set the paths to python dll file in the properties of the PythonEngine

     

    Alternatively you can check some example working with fast-Telega-based TelegramBot (voice-oriented messaging). Hope, this helps.


  8. from what I see here, your problem lies in the plane of python and not Delphi

    Your error says that the ElementNamespaceClassLookup field of the etree object has not been properly initialized

    why it happened is hard to say


    based on the fact that the problem occurs at the time of import, we can assume a problem with the version of the library

     

    I'm sorry, I didn't carefully read the condition under which your problem occurs.

    there could also probably be a problem with initializing some Python object that is logically meant to be a singleton, a resource access violation (which implies exclusive ownership) or something like that

    try also rewriting the code so that imports are called from Delphi only once

    to change your logic You can learn how to pass variables and events between Python and Delphi in the webinar examples.

     

    P.S. IMHO debugging problems like this is the Achilles heel of nested Python (Python4Delphi is great but it may be tricky to debug python issues)

    P.P.S. it seems to me that your task could be solved by the "classic" means of Delphi, although my knowledge may be outdated
     https://blogs.embarcadero.com/create-word-docs-using-the-direct-office-component-in-delphi-on-windows/
    http://delphiprogrammingdiary.blogspot.com/2018/09/ms-word-automation-in-delphi.html

     

    P.P.P.S. this is definitely a matter of taste but I would store the multiline python script in an external py file (and load it with a-la TStringList.LoadFromFile()) or at least using TMemo.Lines.Text make it available in dfm file and separate editor window. It would make life much easier

    • Like 1

  9. 1 hour ago, Fr0sT.Brutal said:

    Jokes aside - what you mean by lambdas? Inline functions?

    I mean python ananymous functions  https://python-reference.readthedocs.io/en/latest/docs/operators/lambda.html with "lambda" keyword syntax

    Idea was create callable Python object in Pascal code using some wrapping and then pass it instead of lambda into, say, pandas .apply() method

     

    The question can be somewhat generalized as follows: create an object in Pascal that can be passed to Python as a callable.

    While wrapping anonymous method Delphi would be an elegant solution, but it is not necessary

     


  10. I'm not sure if this is your case, but a typical headache is multiple Python installations on the target machine (environment variables may differ and the application may try to use "another" python).

    If no errors are displayed, it may be related to the execution rights.

    Try to play around to determine if python script is trying execute at all and at least get an error message. Maybe create and run simplest demo app - say, from Python4Delphi samples?

    Hope this can helps


  11. Sorry, I misunderstood your statement.

     

    did not notice that this code is different from the original specified in the question

     

    In this case, you just do not have an assignment to the PythonDelphiVar1 variable in Python script there and naturally you get none.

     

    But of course it's a matter of taste which approach to use.


  12. On 1/16/2023 at 4:14 AM, pyscripter said:

     

    Demo03 shows you how to use TPythonDelphiVar.

     

    On 1/15/2023 at 4:31 PM, uefi said:

    PythonEngine1.ExecString('fun=("Hello World")'); Result:=( 'Value = ' + PythonEngine1.EvalStringAsStr('fun') );

    Seems, you get 'none' because your 'fun' variable definition is out of scope. 

     

    You try to call 'Py_RunString' under the hood

    https://github.com/tangentstorm/py4d/blob/master/PythonForDelphi/Components/Sources/Core/PythonEngine.pas#L5204

    https://github.com/tangentstorm/py4d/blob/master/PythonForDelphi/Components/Sources/Core/PythonEngine.pas#L3766

     

    You could get more information from here: https://docs.python.org/3/c-api/veryhigh.html

    Quote
    PyObject *PyRun_StringFlags(const char *str, int start, PyObject *globals, PyObject *locals, PyCompilerFlags *flags)
    Return value: New reference.

    Execute Python source code from str in the context specified by the objects globals and locals with the compiler flags specified by flags. <...>

    Returns the result of executing the code as a Python object, or NULL if an exception was raised.

     

    So far, your 'fun' variable seems empty and returns as 'none'

     

    If you don't want to delve into Python / Python C API you could just to use TPythonDelphiVar according to pyscripter's  advice:

    declare a name for the python scope variable https://github.com/pyscripter/python4delphi/blob/master/Demos/Demo03/Unit1.dfm#L137

    and use it in your script https://github.com/pyscripter/python4delphi/blob/master/Demos/Demo03/Unit1.dfm#L42 

     

    Hope, this helps


  13. On 1/6/2023 at 1:56 PM, Juan C.Cilleruelo said:

    I've seen the EvalScript method and the ExecuteScript method of the TPythonEngine component. 


    I need to get from Delphi, some feedback about any problem executing the Python Script. 

            The type of error,

             the line,

             the column where start the problem, etc.     

           

    

    As far as I understand your question, you just need get access to the Python error output, this is usual situation:
    https://github.com/pyscripter/python4delphi/blob/master/Demos/Demo01/Unit1.dfm#L119


  14. On 9/9/2022 at 12:51 PM, BennieC said:

    Thank you for this - it does seem to be the right way to go as my current string parsing is far too slow.  However I have no idea how to use either of the two options.

     

    I'm not sure which solution will be faster and more stable for you, but given the large amount of data, you are quite able to notice the difference.


    Generally speaking, these are not two different options, but two sides of the same coin.

    Buffer Protocol obliges to place data in memory deterministically and sequentially
    (that is, in the spirit of the buffer of a C language). Python builds some of its objects around such a buffer

    Shared Memory is an operating system level concept that allows transferring access to such a buffer from one process to another

     

     

    https://docs.python.org/3/library/multiprocessing.shared_memory.html

    On 9/9/2022 at 12:51 PM, BennieC said:

    how do I return my result which seems to be a list of arrays.  at present the return value is just the tuple

     

    Support for the buffer protocol means exactly that your data displayed as a tuple is actually placed in memory in a serial buffer

    https://numpy.org/doc/stable/reference/generated/numpy.ndarray.data.html

    On 9/9/2022 at 12:51 PM, BennieC said:

     

    In the MemShare solution - how do I determine the memory position in Python and how do I determine the memory structure to use the binary representation.

     

    https://docs.python.org/3/library/array.html#array.array.buffer_info

    Determining the address of the buffer in memory would be too tricky, but it is not required at all.
    You have to create a shared buffer using an arbitrary string name and all you need to do is pass this identifier between Delphi and Python 

    (and maybe convert to numpy https://jakevdp.github.io/blog/2014/05/05/introduction-to-the-python-buffer-protocol/#:~:text=¶,manipulate large arrays of data.)

     

    here is shown example to pass data from TStringGrid to share on the Python side but common idea could be the same: 

    https://github.com/KoRiF/MultyPy4Delphi/blob/master/UnitGridDataPy.pas#L608

    https://github.com/KoRiF/MultyPy4Delphi/blob/master/UnitGridDataPy.pas#L561

    https://github.com/KoRiF/MultyPy4Delphi/blob/master/UnitGridDataPy.pas#L333

    https://github.com/KoRiF/MultyPy4Delphi/blob/master/UnitGridDataPy.pas#L666

     

     

     

     

    On 9/9/2022 at 12:51 PM, BennieC said:

     

    Are there any resources you can point me to as I am at a complete loss/

    https://helpful.knobs-dials.com/index.php/Python_usage_notes_-_struct,_buffer,_array,_bytes,_memoryview

     

    https://docs.python.org/3/library/multiprocessing.shared_memory.html

    https://docs.python.org/3/c-api/memoryview.html

    https://mingze-gao.com/posts/python-shared-memory-in-multiprocessing/#test-code

     

    Hope, this helps

×