KoRiF
Members-
Content Count
32 -
Joined
-
Last visited
Everything posted by KoRiF
-
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
-
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)
-
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
-
How to pass a callback function from Pascal as a parameter to a Python function?
KoRiF replied to Heimo's topic in Python4Delphi
Does TPythonModule.Events can help to resolve your problem? -
I'm afraid to add a submodule you have to go beyond Python4Delphi components and use Python C-API try to study how the TPythonModule instance is created and act similarly You will need to modify this approach someway to add the submodule entry to the dictionary for the supermodule: try use PythonEngine.PyModule_GetDict(PythonModule.Module) https://github.com/pyscripter/python4delphi/blob/1cd66211f586468d9fe3a1352344073759e64d8a/Source/PythonEngine.pas#L3655 Check also: https://github.com/pyscripter/python4delphi/blob/master/Source/PythonEngine.pas#L3649 https://python.readthedocs.io/en/stable/c-api/import.html?highlight=pyimport_getmoduledict#c.PyImport_GetModuleDict https://python.readthedocs.io/en/stable/c-api/import.html#c.PyImport_AddModuleObject this will require some experimentation, but I hope you succeed
-
I think the best you can is to refer to the PythonEngine's DoRedirectIO sources the line sys.stdin=sys.stderr=sys.stdout=DebugOutput() redirects error output onto PythonEngine.IO so far you could try to use other redirection somewhere in your code Hope this helps
-
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 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 If you are strong in C++ you can try use OpenCV in Delphi through C++Builder intermediate adapter
-
Is there easy way to create lambda in Object Pascal and pass it to Python? Thank you in advance.
-
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: Create field that contains reference to function that will be called a-la lambda later Create cdecl Delphi method that wraps the call of the "lambda" field with cdecl convention 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 Create method that evaluates added delphi method as python callable object Assign "lambda" field with an anonymous Delphi method Evaluate added Delphi method that wraps "lambda" as python callable object Pass evaluated python callable object as python callable parameter 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!
-
You have to install every of dcl***.dpk But first you must install foundation libraries as said there: PythonEnviroments allows effectively manage [embedded] Python environments Lightweight-Python-Wrappers allows easily wrap any python library as Delphi component and easily use it in any Object Pascal project Read this for more concept understanding
-
get well & be healthy!
-
Try this. With proper installation of the whole stack of components, this should solve your problem automatically
-
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
-
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.
-
Passing a python object to a delphi function parameter
KoRiF replied to iqrf's topic in Python4Delphi
This stuff may be a bit less straightforward: https://github.com/pyscripter/python4delphi/blob/master/Tutorials/Webinar I/ModuleDemo/MainForm.pas#L126 Please check this: Webinar Hope this helps. P.S. this can clear rather odd arg parsing syntax: https://docs.python.org/3/c-api/arg.html -
Beginner to Delphi. Have no idea what I'm doing wrong here
KoRiF replied to Willicious's topic in Delphi IDE and APIs
please write to me directly if you are still interested -
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
-
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
-
Programs with embedded P4D only run on my own PC, not on any other
KoRiF replied to JGMS's topic in Python4Delphi
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 -
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.
-
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 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
-
Getting feedback from the execution. As exceptions or in another kind of management.
KoRiF replied to Juan C.Cilleruelo's topic in Python4Delphi
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 -
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 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 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 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
-
Seems, some answers to this question can be helpful for you too Also call stack could give you more info...
-
I gonna run python script in separate thread. My PythonEngine has IO field assigned with TPythonGUIInputOutput component What is the correct way to synchronize output (e.g. print("Hello World!") to Delphi's TMemo component? As far as I know VCL is not thread safe... Thank you.