Python For Delphi Developers (I) - Embarcadero RAD Studio, Delphi, & C .

9m ago
10 Views
1 Downloads
683.27 KB
23 Pages
Last View : 19d ago
Last Download : 3m ago
Upload by : Cannon Runnels
Transcription

Python for Delphi Developers (Part I) Webinar by Kiriakos Vlahos (aka PyScripter) and Jim McKeeth (Embarcadero)

Motivation and Synergies Introduction to Python Introduction to Python for Delphi CONTENTS Simple Demo TPythonModule TPyDelphiWrapper

Massive increase in popularity Language of choice for Data Analytics and Machine Learning/Artificial Intelligence Python: Why Should Delphi Developers Care? Rapidly replacing Java as the core programming language in Computer Science degrees Huge number of packages available (250K at PyPI) All the latest and greatest open-source libraries are available to Python immediately Perceived as productive and easy to learn Complementary strengths with Delphi

Gain access to Python libraries from your Delphi applications Use Python as a scripting language for Delphi applications Python-Delphi: Potential Synergies Make code developed in Delphi accessible from python scripts Bring together RAD and GUI Delphi development with python programming Combine the strengths of each language

Popularity of Python

Python vs. Java Interest over time (Google Trends) Java Python

Delphi vs. Python Maturity Delphi/Pascal Python (1995/1970!) (1989) High (begin end) Low (indentation based) No Yes Strong static typing Dynamic (duck) typing Manual Reference counting Object orientation Multi-platform Verbosity REPL Typing Memory management Compiled Performance Multi-threading RAD bytecode

Python for Delphi (I) Low-level access to the python API High-level bi-directional interaction with Python Wrapping of Delphi objects for use in python scripts using RTTI Access to Python objects using Delphi custom variants Creating python extension modules with Delphi classes and functions

Python for Delphi (II) Delphi version support 2009 or later Platform support Windows 32 & 64 bits Linux MacOS Mostly non-visual components Can be used in console applications Lazarus/FPC support

Getting Started – Installing Python Select a Python distribution www.python.org (official distribution) Anaconda (recommended for heavy data-analytics work) Python 2 vs. Python 3 32-bits vs. 64-bits Download and run the installer Installation options (location, for all users) Install python packages you are planning to use (can be done later) Use the python package installer (pip) from the command prompt eg. pip install numpy

Getting Started – Installing Python for Delphi Clone or download and unzip the Github repository into a directory (e.g., D:\Components\P4D). Start RAD Studio. Add the source subdirectory (e.g., D:\Components\P4D\Source) to the IDE's library path for the targets you are planning to use. Open and install the Python4Delphi package specific to the IDE being used. For Delphi Sydney and later it can be found in the Packages\Delphi\Delphi 10.4 directory. For earlier versions use the package in the Packages\Delphi\Delphi 10.3- directory. Note: The package is Design & Runtime together

P4D Components Component Functionality PythonEngine Load and connect to Python. Access to Python API (low-level) PythonModule Create a Python module in Delphi and make it accessible to Python PythonType Create a python type (class) in Delphi PythonInputOutput Receive python output PythonGUIInputOutput Send python output to a Memo PyDelphiWrapper Make Delphi classes and objects accessible from Python (hi-level) VarPython Hi-level access to Python objects from Delphi using custom variants (unit not a component)

SynEdit Simple Demo (I) Memo

All components are using default properties PythonGUIInputOutput linked to PythonEngine and Memo Source Code: Simple Demo (II) procedure TForm1.btnRunClick(Sender: TObject); begin GetPythonEngine.ExecString(UTF8Encode(sePythonC ode.Text)); end;

Using TPythonModule (I) Python Delphi def is prime(n): """ totally naive implementation """ if n 1: return False function IsPrime(x: Integer): Boolean; begin if (x 1) then Exit(False); q math.floor(math.sqrt(n)) for i in range(2, q 1): if (n % i 0): return False return True var q : Floor(Sqrt(x)); for var i : 2 to q do if (x mod i 0) then Exit(False); Exit(True); end;

Using TPythonModule (II) Python def count primes(max n): res 0 for i in range(2, max n 1): if is prime(i): res 1 return res Output Number of primes between 0 and 1000000 78498 Elapsed time: 3.4528134000000037 secs def test(): max n 1000000 print(f'Number of primes between 0 and {max n} {count primes(max n)}') def main(): print(f'Elapsed time: {Timer(stmt test).timeit(1)} secs') if name ' main ': main()

Using TPythonModule (III) Add a TPythonModule to the form and link it to the PythonEngine ModuleName: delphi module Implement python function delphi is prime by writing a Delphi event procedure TForm1.PythonModuleEvents0Execute(Sender: TObject; PSelf, Args: PPyObject; var Result: PPyObject); Var N: Integer; begin with GetPythonEngine do if PyArg ParseTuple(Args, 'i:delphi is prime',@N) 0 then begin if IsPrime(N) then Result : PPyObject(Py True) else Result : PPyObject(Py False); Py INCREF(Result); end else Result : nil; end;

Using TPythonModule (IV) Python from delphi module import delphi is prime def count primes(max n): res 0 for i in range(2, max n 1): if delphi is prime(i): res 1 return res Output Number of primes between 0 and 1000000 78498 Elapsed time: 0.3073742000000017 secs 10x improvement! But hold on. Delphi can do something python can’t do easily: Use threads and multiple CPU cores

Using TPythonModule (V) Implement delphi count primes using TParallel.For function CountPrimes(MaxN: integer): integer; begin var Count : 0; TParallel.&For(2, MaxN, procedure(i: integer) begin if IsPrime(i) then AtomicIncrement(Count); end); Result : Count; end; 70x improvement! Output Number of primes between 0 and 1000000 78498 Elapsed time: 0.04709590000000219 secs Python from delphi module import delphi count primes from timeit import Timer import math def test(): max n 1000000 print(f'Number of primes between 0 and {max n} {delphi count primes(max n)}')

Using PyDelphiWrapper PyDelphiWrapper allows you to expose Delphi objects, records and types using RTTI and customised wrapping of common Delphi objects. Add a TPyDelphiWrapper on the form and link it to a PythonModule. In this demo we will wrap a Delphi record containing a class function. type TDelphiFunctions record class function count primes(MaxN: integer): integer; static; end; var DelphiFunctions: TDelphiFunctions; procedure TForm1.FormCreate(Sender: TObject); begin var Py : PyDelphiWrapper.WrapRecord(@DelphiFunctions, ons)) as TRttiStructuredType); PythonModule.SetVar('delphi functions', Py); PythonEngine.Py DecRef(Py); end;

WrapDelphi Demo31 Shows you how you can create Delphi GUIs with Python Create forms Subclass Forms (and other Delphi types) Add python Form event handlers Use customized wrapping of common RTL and VCL objects Common Dialogs StringLists Exception handling

Conclusions With Python for Delphi you can get the best of both worlds P4D makes it very easy to integrate Python into Delphi applications in RAD way Expose Delphi function, objects, records and types to Python using low or high-level interfaces In a future webinar we will cover Using python libraries and objects in Delphi code (VarPyth) Python based data analytics in Delphi applications Creating Python extension modules using Delphi Python GUI development using the VCL

Resources Python4Delphi library github.com/pyscripter/python4delphi PyScripter IDE github.com/pyscripter/pyscripter Thinking in CS – Python3 Tutorial openbookproject.net/thinkcs/python/english3e/ PyScripter blog pyscripter.blogspot.com/ Webinar blog post -webinar/

For Delphi Sydney and later it can be found in the Packages\Delphi\Delphi 10.4 directory. For earlier versions use the package in the Packages\Delphi\Delphi 10.3- directory. Note: The package is Design & Runtime together. P4D Components Component Functionality PythonEngine Load and connect to Python. Access to Python API (low-level .

Related Documents:

1 1.0 Borland Delphi 1995-02-14 2 2.0 Borland Delphi 2 1996-02-10 3 3.0 Borland Delphi 3 1997-08-05 4 4.0 Borland Delphi 4 1998-07-17 5 5.0 Borland Delphi 5 1999-08-10 6 6,0 Borland Delphi 6 2001-05-21 7 7.0 Borland Delphi 7 2002-08-09 8 8.0 Borland Delphi 8 pour .NET 2003-12-22 2005 9.0 Borland Delphi 2005 2004-10-12 2006 10.0 Borland Delphi .

Migrating Borland Delphi applications to the Microsoft .NET Framework with Delphi 8 features and code constructs in Delphi 7 must be replaced by safe counterparts in Delphi for .NET. Many Delphi 7 language features are no longer available in the Delphi for .NET environment

The FTP Client Engine for Delphi component library supports and has been tested with all 32-bit and 64-bit versions of Delphi including: Borland Delphi (2.0, 3.0, 4.0, 5.0. 6.0 and 7.0) Borland Delphi 8 for .NET Borland Delphi 2005 & 2006 Borland Turbo Delphi

For example buying Delphi XE 2 also gets you Delphi 7, Delphi 2007, Delphi 2009, Delphi 2010 and Delphi XE. Also the technology has changed so much during the last 10 years, and end users are no longer restricted to get ting the information through desktop applications, they use the

Python Programming for the Absolute Beginner Second Edition. CONTENTS CHAPTER 1 GETTING STARTED: THE GAME OVER PROGRAM 1 Examining the Game Over Program 2 Introducing Python 3 Python Is Easy to Use 3 Python Is Powerful 3 Python Is Object Oriented 4 Python Is a "Glue" Language 4 Python Runs Everywhere 4 Python Has a Strong Community 4 Python Is Free and Open Source 5 Setting Up Python on .

Python 2 versus Python 3 - the great debate Installing Python Setting up the Python interpreter About virtualenv Your first virtual environment Your friend, the console How you can run a Python program Running Python scripts Running the Python interactive shell Running Python as a service Running Python as a GUI application How is Python code .

Kelebihan Borland Delphi 7.0 Borland delphi 7.0 merupakan pilihan bagi sebagian kalangan programmer untuk membuat aplikasi. Hal ini disebabkan kelebihan yang ada pada borland delphi 7.0 berikut ini beberapa kelebihan borlan delphi 7.0 antara lain : Berbasis Objek Orientid programming, seperti bagian yang ada pada program

ALEX RIDER www.anthonyhorowitz.com. Never Say Die Exclusive Extract The start of another day. Alex went into the bathroom, showered and cleaned his teeth. Then he got dressed. He had started school a week ago, arriving at the start of the fall semester – the autumn term, he would have called it back in London. There was no uniform at the Elmer E. Robinson High School. Today, Alex threw on .