Jump to content

pyscripter

Members
  • Content Count

    920
  • Joined

  • Last visited

  • Days Won

    56

Everything posted by pyscripter

  1. pyscripter

    Threadvar "per object"

    Indeed: python4delphi/Source/PythonEngine.pas at master · pyscripter/python4delphi (github.com)
  2. In the attachment you can find a high-level interface-based encapsulation of the Direct2D SVG functionality. It allows you to parse SVG files and draw them to a GDI/GDI+ DC. Requires Windows 10 with Creators Update or later. Main form code to display SVG files: { TForm1 } procedure TForm1.FormCreate(Sender: TObject); begin fSVG := GetD2DSVGHandler.NewSvg; fSVG.LoadFromFile('..\..\browser.svg'); //fSVG.FixedColor := TAlphaColorRec.Red; //fSVG.Opacity := 0.5; fSVG.GrayScale := True; end; procedure TForm1.Paint; begin inherited; fSvg.PaintTo(Canvas.Handle, TRectF.Create(ClientRect), True); end; procedure TForm1.Resize; begin inherited; Invalidate; end; Features: Scale to any size Keep aspect ratio (optionally) Control opacity Recolor to any color Draw in gray scale Samples: The above in grayscale: Svg.zip
  3. What is the svg you are painting? Can you show the svg text? By the way you should be using the SvgIconImageList which includes the above work.
  4. pyscripter

    Python4Delphi in a thread

    Dealing with python threads and the Global Interpreter Lock (GIL) is quite tricky. You need to either: Understand and use correctly the python API Initialization, Finalization, and Threads — Python 3.12.3 documentation or Use the high-level encapsulation provided by P4D, following the instructions in https://github.com/pyscripter/python4delphi/wiki/PythonThreads religiously. Otherwise, things can very easily go wrong.
  5. pyscripter

    Python4Delphi in a thread

    Have you followed the instructions?
  6. pyscripter

    Python4Delphi in a thread

    Please do not insert such long pieces of code in your script. You could have added the project as an attachment. Please read the relevant info PythonThreads · pyscripter/python4delphi Wiki (github.com)
  7. pyscripter

    New ChatLLM application.

    I have created a new Delphi application called ChatLLM for chatting with Large Language Models (LLMs). Its primary purpose is to act as a coding assistant. Features: Supports both cloud based LLM models (ChatGPT) and local models using Ollama. Supports both the legacy completions and the chat/completions endpoints. The chat is organized around multiple topics. Can save and restore the chat history and settings. Streamlined user interface. Syntax highlighting of code (python and pascal). High-DPI awareness. The application uses standard HTTP client and JSON components from the Delphi RTL and can be easily integrated in other Delphi applications. You do not need an API key to use Ollama models and usage is free. It provides access to a large number of LLM models such as codegemma from Google and codelllama from Meta. The downside is that it may take a long time to get answers, depending on the question, the size of the model and the power of your CPU and GPU. Chat topics The chat is organized around topics. You can create new topics and move back and forth between the topics using the next/previous buttons on the toolbar. When you save the chat all topics are soved and then restored when you next start the application. Questions within a topic are asked in the context of the previous questions and answers of that topic. Screenshots: Settings using gpt-3.5-turbo, which is cheaper and faster than gpt-4: UI: Further prompting: The code is not actually correct (Serialize returns a string) but it is close. If you want to test ChatLLM you can download the executable.
  8. pyscripter

    Gutter width in Delphi 12/12.1

    It is almost a show stopper for laptop screens. I wonder why it has not attracted more votes and the attention of Embarcadero.
  9. pyscripter

    Gutter width in Delphi 12/12.1

    Not sure. It looks worse in a high DPI monitor.
  10. pyscripter

    New ChatLLM application.

    The latter.
  11. You don't need to modify WrapDelphi. Just use the latest version. function TValueToPyObject(const Value: TValue; DelphiWrapper: TPyDelphiWrapper; out ErrMsg: string): PPyObject; begin if Value.IsEmpty then Result := GetPythonEngine.ReturnNone else case Value.Kind of tkClass: Result := DelphiWrapper.Wrap(Value.AsObject); tkClassRef: Result := DelphiWrapper.WrapClass(Value.AsClass); tkInterface: Result := DelphiWrapper.WrapInterface(Value); tkRecord{$IFDEF MANAGED_RECORD},tkMRecord{$ENDIF}: Result := DelphiWrapper.WrapRecord(Value); tkArray, tkDynArray: Result := DynArrayToPython(Value, DelphiWrapper, ErrMsg); tkPointer: if Value.IsType<PPyObject> then Result := Value.AsType<PPyObject> else begin Result := nil; ErrMsg := rs_ErrValueToPython; end; else Result := SimpleValueToPython(Value, ErrMsg); end; end;
  12. pyscripter

    LockBox 3 via GetIt broken since April 2024 Update

    Indeed, but Github repo owners can safeguard against that by adding a .gitattributes file.
  13. pyscripter

    LockBox 3 via GetIt broken since April 2024 Update

    This is a common problem with Delphi github repos: https://github.com/JAM-Software/Virtual-TreeView/issues/1151#issuecomment-1332032004 Virtual-TreeView/.gitattributes at master · JAM-Software/Virtual-TreeView (github.com)
  14. In an older post I showed how to patch a virtual/non virtual, public/private method. A recently came across an Vcl bug that requires patching a non-virtual constructor. Does anyone know how to do that?
  15. pyscripter

    How to patch a constructor?

    @Stefan Glienke Thanks. Would you happen know whether you can patch message methods? The only thing I could find is an old article Hallvard's Blog: Hack#15: Overriding message and dynamic methods at run-time (hallvards.blogspot.com).
  16. There have been a lot of questions in this forum about running python code in threads using Python4Delphi. I have created a comprehensive guide in this Wiki topic.
  17. Your taps Variant has not assigned a value. You are missing: taps := MainModule.taps;
  18. pyscripter

    convert native object instance to wrapped instance

    See TPyDelphiWrapper.Wrap(AObj: TObject; AOwnership: TObjectOwnership): PPyObject; Also you do need need your TPyEventFromScript. This is the old and hard way. You can use instead (e.g. in your FormCreate): PyDelphiWrapper1.RegisterDelphiWrapper(TPyClassWrapper<TEventFromScript>).Initialize; Since you are using interfaces there is also: function TPyDelphiWrapper.WrapInterface(const IValue: TValue): PPyObject;
  19. pyscripter

    Running python code in Delphi threads

    @maomao2028 Works fine here without memory leaks. See attached project. Note the use of DelayWrites := True in the PythonGUIInputOutput and the calls to TPythonThread.Py_End_Allow_Threads and TPythonThread.Py_Begin_Allow_Threads. Demo01.zip
  20. pyscripter

    Adding docs strings to wrapped functions

    Forgot to say that unfortunately you cannot just set the __doc__ property of Wrapped classes and methods. It has to be done at the time of wrapping. A trivial implementation of IDocServer would be: uses TypInfo; type TMyDocServer = class(TInterfacedObject, IDocServer) private function ReadTypeDocStr(ATypeInfo: PTypeInfo; out ADocStr: string): Boolean; function ReadMemberDocStr(AMember: TRttiMember; out ADocStr: string): Boolean; procedure Initialize; procedure Finalize; function Initialized: Boolean; end;{ TMyDocServer } procedure TMyDocServer.Finalize; begin end; procedure TMyDocServer.Initialize; begin end; function TMyDocServer.Initialized: Boolean; begin Result := True; end; function TMyDocServer.ReadMemberDocStr(AMember: TRttiMember; out ADocStr: string): Boolean; begin Result := False; if AMember.Name = 'getVersion' then begin Result := True; ADocStr := 'getVersion'; end; end; function TMyDocServer.ReadTypeDocStr(ATypeInfo: PTypeInfo; out ADocStr: string): Boolean; begin Result := False; if ATypeInfo = TypeInfo(ThostAPI ) then begin Result := True; ADocStr := 'ThostAPI doc'; end; end; and then PyDocServer := TMyDocServer.Create; DelphiWrapper.RegisterDelphiWrapper(TPyClassWrapper<THostAPI>).Initialize;
  21. pyscripter

    Adding docs strings to wrapped functions

    WrapDelphi defines the following: IDocServer = interface ['{4AF0D319-47E9-4F0A-9C71-97B8CBB559FF}'] function ReadTypeDocStr(ATypeInfo: PTypeInfo; out ADocStr: string): Boolean; function ReadMemberDocStr(AMember: TRttiMember; out ADocStr: string): Boolean; procedure Initialize; procedure Finalize; function Initialized: Boolean; end; var PyDocServer: IDocServer = nil; You need to implement the interface and assign the implemented interface to PyDocServer. PythonDocs.pas provides an implementation based on xml files, which is used by delphivcl and delphifmx. But if, what you are after is to provide docstrings to a few methods, it would be easier to create your own implementation.
  22. System.AnsiStrings.AnsiCompareStr under POSIX converts the ansistrings to UnicodeStrings and then compares. FPC has a function UTF8CompareStr, which sounds promising, but it also converts the ansi strings to UTF-16 and then compares them. Unfortunately it appears that there is no good way to directly compare utf8 strings without converting them. I hope I am wrong.
  23. Unfortunately (see CompareStringA function (winnls.h) - Win32 apps | Microsoft Learn😞 It also appears that System.AnsiStrings.AnsiCompareStr ignores the code page of the ansi strings.
  24. pyscripter

    [help] how to convert delphi TBytes to python Bytes

    Look at PyBytes_AsString, or PyBytes_AsStringAndSize functions. For example if obj is a bytes PPyObject. the following converts it to an AnsiString. AnsiString(PyBytes_AsString(obj)) Also PyObjectAsVariant(obj) converts it to a variant. In the opposite direction to create a bytes object in Delphi use PyBytes_FromStringAndSize
  25. Have you updated to the latest sources?
×