Jump to content

pyscripter

Members
  • Content Count

    761
  • Joined

  • Last visited

  • Days Won

    40

Everything posted by pyscripter

  1. pyscripter

    Using Delphi enum in Python

    It's not how it works.
  2. pyscripter

    Using Delphi enum in Python

    As mentioned above you just use python strings in place of enumerated values, eg. exported delphi method using WrapDelphi procedure Test(Color: TColor) in python Test('RED')
  3. pyscripter

    Using Delphi enum in Python

    I have no idea what you are trying to do, but it does not make sense to define the enumeration both in Delphi and in Python. I recommend that you familiarize yourself with: WrapDelphi - high level access to Delphi from python VarPyth - high level access to python from Delphi. The video tutorials offer a good introduction to both. If you use WrapDelphi to export delphi functions to python then you just use python strings in place of enumeration values.
  4. pyscripter

    Using Delphi enum in Python

    result := ExtractPythonObjectFrom(Red);
  5. pyscripter

    Using Delphi enum in Python

    There is no need to export anything. Delphi enum values are converted to python strings and sets to lists of strings. On the opposite direction you can access python enum values from Delphi in the same way you access other objects. The easiest way is to use VarPyth. var colorEnum := MainModule.Color var Red := colorEnum.RED
  6. pyscripter

    Versioning in my program

    @Lainkes Under Project, Options, Version Info, you can choose to include version info with your project. The version info includes the version number but also a number of other strings including Comments. Your application can read those in a number of ways. See for instance How to determine Delphi Application Version - Stack Overflow, The most comprehensive way I found is using the Jcl library's unit JclFileInfo. This unit includes a class TJclFileVersionInfo which provides easy access to all information stored in version info. Here is for example a function that retrieves the version number using jcl: function ApplicationVersion : string; var ExeFile : string; begin ExeFile := Application.ExeName; if VersionResourceAvailable(ExeFile) then begin with TJclFileVersionInfo.Create(ExeFile) do begin Result := BinFileVersion; Free; end; end else Result := '1.0.0'; end; Note that this approach is Windows only. For Andoid apps you can access the version info differently (e,g,How can I get at the file version info of a file when running Delphi on Android? - Stack Overflow)
  7. pyscripter

    How I can clear engine after exec script ?

    You need to decrease the refcount of these pyobjects after using them.
  8. pyscripter

    How I can clear engine after exec script ?

    Hint: The answer is in PythonThreads · pyscripter/python4delphi Wiki (github.com) Other solutions include: Modify your scripts so that they do not create global variables (wrap everything inside a function which you execute) At the end of your python script delete (del) any global variables you create Use Run_CommandAsObjectWithDict or ExecStrings, passing new empty dictionaries to the optional locals, and globals parameters, which you then destroy.
  9. pyscripter

    Async script with PythonThread

    Assuming you are on Windows use: import asyncio asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) loop = asyncio.new_event_loop() This should work both in the main thread and in other threads. Google for "set_wakeup_fd" to see this is a well known issue.
  10. pyscripter

    Async script with PythonThread

    See PythonThreads · pyscripter/python4delphi Wiki (github.com) which explains how to run python code in delphi threads. This keeps the delphi main thread from blocking.
  11. pyscripter

    TPyDelphiWrapper - memory leaks

    @iqrfYou are absolutely right, PyErr_Fetch returns new references and clears the error. The code in ProcessSystemExit has now been fixed. Thanks!
  12. TColumn.SetVisible is called, so inside that method Self is the TColumn object on which the method is called.
  13. pyscripter

    TPyDelphiWrapper - memory leaks

    The exit() function raises a SystemExit exception and if it is not handled it shuts down python. Don't use it in P4D scripts.
  14. If you create an Engine client (e.g. TPythonDelphiVar TPythonModule) after the engine is initialized, you should call the client Initialize method. If the client is created before the engine is initialized, you should not call Initialize.
  15. pyscripter

    Freeing TPythonModule after using it takes long

    Could you post your minimal Delphi project in a zip file?
  16. pyscripter

    Freeing TPythonModule after using it takes long

    Destroying the engine and associated modules is normally instantaneous. Most of the demos do that. So that delay should be related to something "special" you are doing. Is python code running while you are doing that? As an aside, why do you need to manually free the engine?
  17. pyscripter

    Freeing TPythonModule after using it takes long

    You are not supposed to destroy TPythonModules before engine destruction. They are automatically destroyed when the engine is destroyed. And it does not achieve the desired effect of removing it from python. Python does not have a good mechanism for removing imported modules and this is not recommended in general. See for instance How do I really delete an imported module? (archive.org). Also from Reloading modules in Python - GeeksforGeeks Not sure what causes the delay.
  18. Use for key in spam.myPoint.ColumnA.Keys.ToArray(): print(key) It is possible to allow for a more natural iteration, but it would require to hand craft the TDictionary wrapper. See for instance how it is done in TPyDelphiStrings in WrapDelphiClasses.
  19. You could allow for both Point() and Point(x, y) by modifying CreateWith if PyArg_ParseTupleAndKeywords(args, kwds, '|ICreatePoint') then // no arguements else if PyArg_ParseTupleAndKeywords(args, kwds, 'ii|:CreatePoint', @fx, @fy) then // two integer arguements begin ... end;
  20. To be able to create columns you need to also to customize the TColumn wrapper: TColumnWrapper = class(TPyClassWrapper<TColumn>) constructor CreateWith(APythonType: TPythonType; args, kwds: PPyObject); override; end; constructor TColumnWrapper.CreateWith(APythonType: TPythonType; args, kwds: PPyObject); var W: integer; P: PPyObject; begin Create(APythonType); GetPythonEngine.PyArg_ParseTuple(args, 'iO:CreateColumn', @W, @P); DelphiObject := TColumn.Create; DelphiObject.Width := W; DelphiObject.Visible := GetPythonEngine.PyObject_IsTrue(P) = 1; end; then register it in Form.Create PyDelphiWrapper.RegisterDelphiWrapper(TColumnWrapper).Initialize; PyDelphiWrapper.RegisterDelphiWrapper(TPyClassWrapper<TDictionary<string, TColumn>>).Initialize; Then the following code works: from spam import myPoint, Column print(myPoint.ColumnA['Note'].Width) print(myPoint.ColumnA['Note'].Visible) new_column = Column(100, False) myPoint.ColumnA.Add('Note1', new_column) print(myPoint.ColumnA['Note1'].Width) print(myPoint.ColumnA['Note1'].Visible)
  21. Assuming your register the dictionary as shown above the following works. from spam import myPoint print(myPoint.ColumnA['Note'].Width) print(myPoint.ColumnA['Note'].Visible)
  22. Please use the latest version from https://github.com/pyscripter/python4delphi/. Not the Getit version. Here PyDelphiWrapper.RegisterDelphiWrapper(TPyClassWrapper<TDictionary<string, TColumn>>).Initialize; works. You could also subclass TDictionary<string, TColumn>. type TColumnDict = class( TDictionary<string, TColumn>) end; and in Form.Create PyDelphiWrapper.RegisterDelphiWrapper(TPyClassWrapper<TColumnDict>).Initialize;
  23. You need to register the TDictionary<String,TColumn> in Form.Create, PyDelphiWrapper.RegisterDelphiWrapper(TPyClassWrapper<TDictionary<String,TColumn>>).Initialize; Have not tested, Does it work?
  24. pyscripter

    RegExpressions and preUnGreedy

    See the record helpers in https://github.com/pyscripter/Pcre-Jit-Delphi/blob/master/Demos/benchmark.dpr and the methods SetAdditionalPCREOptions
  25. XML used to be considered the universal data format. Now is a bit passé with JSON, YAML etc being "in". I got involved in XML parsing, since SVG files are in XML format. XML Delphi support At the surface the XML support in Delphi is very good: You have TXMLDocument/IXMLDocument offering high-level support (Xml.xmldoc) Support for the standard DOM interfaces (Xml.XmlDom) Multiple implementations including (MSXML, OmniXML, OpenXML and more) Ability to plug in your own implementation Multiple platform support. The most common way of accessing XML is through TXMLDocument/IXMLDocument. However there is a big catch: PERFORMANCE. Say you want to use MSXML and you specify 'MSXML' as your DefaultDomVendor. (or you simply include the implementation unit Xml.Win.msxmldom in your uses clause). Your create an XML document and you access the top node: var Doc: IXMLDocument = TXMLDocument.Create(nil); var Node: IXMLNode := Doc.DocumentElement; Node is an IXMLInterface implemented by TXMLNode (TInterfacedObject defined in Xml.XmlDoc). TXMLNode wraps an IDOMNode stored in a private field FDOMNode. IDOMNode is defined in Xml.Xmldom. The IDOMNode is implemented by the used vendor in this case Xml.Win.msxmldom by a class TMSDOMNode TMSDOMNode (also a TInterfacedObject) wraps IXMLDOMNode stored in a private field FMSNode. IXMLDOMNode is defined in Winapi.msxml. As a result when you create any IXMLNode, a TXMLNode is created and this creates a TMSDOMNode which points to an IXMLDOMNode. Any call/property access to IXMLNode translates in a call of IDOMNode which then calls IXMLDOMNode. The created TInterfaced objects also need to be destroyed when you release your XML Node. The same two-level indirection applies to all XML objects (attributes, Children) and cause a huge degradation of performance. Conclusion If you care about speed forget about TXMLDocument. You can access the Vendor implementation or even better in the case of MSXML the Microsoft ActiveX objects directly: uses WinAPI.msxml var XML: IXMLDOMDocument3 := CoDOMDocument60.Create; XML.loadXML(XMLString); var DocNode: IXMLDOMNode := XML.documentElement; In SVG parsing and processing accessing directly the ActiveX objects reduced processing time by more than 50%. Additional tip A common performance pitfall with MSXML is explained in http://www.gerixsoft.com/blog/delphi/msxml. The fastest way to iterate through ChildNodes is via getFirstChild/nextSibling and Attributes via nextNode.
×