Skip to content

Commit c979b56

Browse files
committed
Added Webinar demos
1 parent 593c66d commit c979b56

13 files changed

Lines changed: 3713 additions & 1 deletion

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ P4D makes it very easy to use python as a scripting language for Delphi applicat
1414

1515
The best way to learn about how to use Python for Delphi is to try the extensive range of demos available. Also studying the unit tests for VarPyth and WrapDelphi can help understand what is possible with these two units.
1616

17-
### Wiki topics ###
17+
### Wiki topics
1818
- [Installation](https://github.com/pyscripter/python4delphi/wiki/Installation)
1919
- [Supported Platforms](https://github.com/pyscripter/python4delphi/wiki/SupportedPlatforms)
2020
- [How Python for Delphi finds your Python distribution](https://github.com/pyscripter/python4delphi/wiki/FindingPython) (read before trying the demos)
21+
22+
### Learn about Python for Delphi
23+
- [Tutorials](https://github.com/pyscripter/python4delphi/tree/master/Tutorials)
24+
- [Demos](https://github.com/pyscripter/python4delphi/tree/master/Demos)
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
object Form1: TForm1
2+
Left = 0
3+
Top = 0
4+
Caption = 'TPythonModule Demo'
5+
ClientHeight = 655
6+
ClientWidth = 983
7+
Color = clBtnFace
8+
Font.Charset = DEFAULT_CHARSET
9+
Font.Color = clWindowText
10+
Font.Height = -11
11+
Font.Name = 'Tahoma'
12+
Font.Style = []
13+
OldCreateOrder = False
14+
PixelsPerInch = 96
15+
TextHeight = 13
16+
object Splitter1: TSplitter
17+
Left = 0
18+
Top = 401
19+
Width = 983
20+
Height = 3
21+
Cursor = crVSplit
22+
Align = alTop
23+
ResizeStyle = rsUpdate
24+
ExplicitTop = 201
25+
ExplicitWidth = 383
26+
end
27+
object sePythonCode: TSynEdit
28+
Left = 0
29+
Top = 17
30+
Width = 983
31+
Height = 384
32+
Align = alTop
33+
Font.Charset = DEFAULT_CHARSET
34+
Font.Color = clWindowText
35+
Font.Height = -13
36+
Font.Name = 'Consolas'
37+
Font.Style = []
38+
Font.Quality = fqClearTypeNatural
39+
TabOrder = 0
40+
UseCodeFolding = False
41+
Gutter.Font.Charset = DEFAULT_CHARSET
42+
Gutter.Font.Color = clWindowText
43+
Gutter.Font.Height = -11
44+
Gutter.Font.Name = 'Consolas'
45+
Gutter.Font.Style = []
46+
Highlighter = SynPythonSyn
47+
Lines.Strings = (
48+
'from timeit import Timer'
49+
'import math'
50+
''
51+
'def is_prime(n):'
52+
' """ totally naive implementation """'
53+
' if n <= 1:'
54+
' return False'
55+
''
56+
' q = math.floor(math.sqrt(n))'
57+
' for i in range(2, q + 1):'
58+
' if (n % i == 0):'
59+
' return False'
60+
' return True'
61+
''
62+
''
63+
'def count_primes(max_n):'
64+
' res = 0'
65+
' for i in range(2, max_n + 1):'
66+
' if is_prime(i):'
67+
' res += 1'
68+
' return res'
69+
''
70+
'def test():'
71+
' max_n = 1000000'
72+
73+
' print(f'#39'Number of primes between 0 and {max_n} = {count_prim' +
74+
'es(max_n)}'#39')'
75+
''
76+
'def main():'
77+
' print(f'#39'Elapsed time: {Timer(stmt=test).timeit(1)} secs'#39')'
78+
''
79+
'if __name__ == '#39'__main__'#39':'
80+
' main()')
81+
end
82+
object HeaderControl1: THeaderControl
83+
Left = 0
84+
Top = 0
85+
Width = 983
86+
Height = 17
87+
Font.Charset = DEFAULT_CHARSET
88+
Font.Color = clWindowText
89+
Font.Height = -11
90+
Font.Name = 'Tahoma'
91+
Font.Style = [fsBold]
92+
Sections = <
93+
item
94+
Alignment = taCenter
95+
AutoSize = True
96+
ImageIndex = -1
97+
Text = 'Python Source code'
98+
Width = 983
99+
end>
100+
ParentFont = False
101+
end
102+
object Panel1: TPanel
103+
Left = 0
104+
Top = 614
105+
Width = 983
106+
Height = 41
107+
Align = alBottom
108+
TabOrder = 2
109+
object btnRun: TButton
110+
Left = 24
111+
Top = 5
112+
Width = 75
113+
Height = 25
114+
Caption = 'Run'
115+
TabOrder = 0
116+
OnClick = btnRunClick
117+
end
118+
object Button1: TButton
119+
Left = 160
120+
Top = 5
121+
Width = 137
122+
Height = 25
123+
Caption = 'Use delphi_is_prime'
124+
TabOrder = 1
125+
OnClick = Button1Click
126+
end
127+
object Button2: TButton
128+
Left = 303
129+
Top = 5
130+
Width = 163
131+
Height = 25
132+
Caption = 'Use delphi_count_primes'
133+
TabOrder = 2
134+
OnClick = Button2Click
135+
end
136+
end
137+
object Panel2: TPanel
138+
Left = 0
139+
Top = 404
140+
Width = 983
141+
Height = 210
142+
Align = alClient
143+
TabOrder = 3
144+
object HeaderControl2: THeaderControl
145+
Left = 1
146+
Top = 1
147+
Width = 981
148+
Height = 17
149+
Font.Charset = DEFAULT_CHARSET
150+
Font.Color = clWindowText
151+
Font.Height = -11
152+
Font.Name = 'Tahoma'
153+
Font.Style = [fsBold]
154+
Sections = <
155+
item
156+
Alignment = taCenter
157+
AutoSize = True
158+
ImageIndex = -1
159+
Text = 'Python Output'
160+
Width = 981
161+
end>
162+
ParentFont = False
163+
end
164+
object mePythonOutput: TMemo
165+
Left = 1
166+
Top = 18
167+
Width = 981
168+
Height = 191
169+
Align = alClient
170+
Font.Charset = DEFAULT_CHARSET
171+
Font.Color = clWindowText
172+
Font.Height = -13
173+
Font.Name = 'Consolas'
174+
Font.Style = []
175+
ParentFont = False
176+
TabOrder = 1
177+
end
178+
end
179+
object SynPythonSyn: TSynPythonSyn
180+
Options.AutoDetectEnabled = False
181+
Options.AutoDetectLineLimit = 0
182+
Options.Visible = False
183+
Left = 760
184+
Top = 32
185+
end
186+
object SynEditPythonBehaviour: TSynEditPythonBehaviour
187+
Editor = sePythonCode
188+
Left = 760
189+
Top = 80
190+
end
191+
object PythonEngine: TPythonEngine
192+
IO = PythonGUIInputOutput
193+
Left = 880
194+
Top = 32
195+
end
196+
object PythonGUIInputOutput: TPythonGUIInputOutput
197+
UnicodeIO = True
198+
RawOutput = False
199+
Output = mePythonOutput
200+
Left = 880
201+
Top = 80
202+
end
203+
object PythonModule: TPythonModule
204+
Engine = PythonEngine
205+
Events = <
206+
item
207+
Name = 'delphi_is_prime'
208+
OnExecute = PythonModuleEvents0Execute
209+
end
210+
item
211+
Name = 'delphi_count_primes'
212+
OnExecute = PythonModuleEvents1Execute
213+
end>
214+
DocString.Strings = (
215+
'Delphi module')
216+
ModuleName = 'delphi_module'
217+
Errors = <>
218+
Left = 880
219+
Top = 136
220+
end
221+
end
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
unit MainForm;
2+
3+
interface
4+
5+
uses
6+
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
7+
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, SynEdit, Vcl.StdCtrls,
8+
PythonEngine, PythonGUIInputOutput, SynEditPythonBehaviour,
9+
SynEditHighlighter, SynEditCodeFolding, SynHighlighterPython, Vcl.ExtCtrls;
10+
11+
type
12+
TForm1 = class(TForm)
13+
sePythonCode: TSynEdit;
14+
HeaderControl1: THeaderControl;
15+
Panel1: TPanel;
16+
Splitter1: TSplitter;
17+
Panel2: TPanel;
18+
HeaderControl2: THeaderControl;
19+
mePythonOutput: TMemo;
20+
SynPythonSyn: TSynPythonSyn;
21+
SynEditPythonBehaviour: TSynEditPythonBehaviour;
22+
PythonEngine: TPythonEngine;
23+
PythonGUIInputOutput: TPythonGUIInputOutput;
24+
btnRun: TButton;
25+
PythonModule: TPythonModule;
26+
Button1: TButton;
27+
Button2: TButton;
28+
procedure btnRunClick(Sender: TObject);
29+
procedure Button1Click(Sender: TObject);
30+
procedure Button2Click(Sender: TObject);
31+
procedure PythonModuleEvents0Execute(Sender: TObject; PSelf, Args: PPyObject;
32+
var Result: PPyObject);
33+
procedure PythonModuleEvents1Execute(Sender: TObject; PSelf, Args: PPyObject;
34+
var Result: PPyObject);
35+
private
36+
{ Private declarations }
37+
public
38+
{ Public declarations }
39+
end;
40+
41+
var
42+
Form1: TForm1;
43+
44+
implementation
45+
46+
{$R *.dfm}
47+
48+
uses
49+
System.Threading,
50+
System.Math;
51+
52+
procedure TForm1.btnRunClick(Sender: TObject);
53+
begin
54+
GetPythonEngine.ExecString(UTF8Encode(sePythonCode.Text));
55+
end;
56+
57+
function IsPrime(x: Integer): Boolean;
58+
begin
59+
if (x <= 1) then Exit(False);
60+
61+
var q := Floor(Sqrt(x));
62+
for var i := 2 to q do
63+
if (x mod i = 0) then
64+
Exit(False);
65+
Exit(True);
66+
end;
67+
68+
function CountPrimes(MaxN: integer): integer;
69+
begin
70+
var Count := 0;
71+
TParallel.&For(2, MaxN, procedure(i: integer)
72+
begin
73+
if IsPrime(i) then
74+
AtomicIncrement(Count);
75+
end);
76+
Result := Count;
77+
end;
78+
79+
procedure TForm1.Button1Click(Sender: TObject);
80+
begin
81+
sePythonCode.Text :=
82+
'from delphi_module import delphi_is_prime'#13#10 + //0
83+
'from timeit import Timer'#13#10 + //1
84+
''#13#10 + //3
85+
'def count_primes(max_n):'#13#10 + //4
86+
' res = 0'#13#10 + //5
87+
' for i in range(2, max_n + 1):'#13#10 + //6
88+
' if delphi_is_prime(i):'#13#10 + //7
89+
' res += 1'#13#10 + //8
90+
' return res'#13#10 + //9
91+
''#13#10 + //10
92+
'def test():'#13#10 + //11
93+
' max_n = 1000000'#13#10 + //12
94+
' print(f''Number of primes between 0 and {max_n} = {count_primes(max_n)}'')'#13#10 + //13
95+
''#13#10 + //14
96+
'def main():'#13#10 + //15
97+
' print(f''Elapsed time: {Timer(stmt=test).timeit(1)} secs'')'#13#10 + //16
98+
''#13#10 + //17
99+
'if __name__ == ''__main__'':'#13#10 + //18
100+
' main()'#13#10; //19
101+
end;
102+
103+
procedure TForm1.Button2Click(Sender: TObject);
104+
begin
105+
sePythonCode.Text :=
106+
'from delphi_module import delphi_count_primes'#13#10 + //0
107+
'from timeit import Timer'#13#10 + //1
108+
''#13#10 + //10
109+
'def test():'#13#10 + //11
110+
' max_n = 1000000'#13#10 + //12
111+
' print(f''Number of primes between 0 and {max_n} = {delphi_count_primes(max_n)}'')'#13#10 + //13
112+
''#13#10 + //14
113+
'def main():'#13#10 + //15
114+
' print(f''Elapsed time: {Timer(stmt=test).timeit(1)} secs'')'#13#10 + //16
115+
''#13#10 + //17
116+
'if __name__ == ''__main__'':'#13#10 + //18
117+
' main()'#13#10; //19
118+
end;
119+
120+
procedure TForm1.PythonModuleEvents0Execute(Sender: TObject; PSelf, Args:
121+
PPyObject; var Result: PPyObject);
122+
Var
123+
N: Integer;
124+
begin
125+
with GetPythonEngine do
126+
if PyArg_ParseTuple(Args, 'i:delphi_is_prime',@N) <> 0 then
127+
begin
128+
if IsPrime(N) then
129+
Result := PPyObject(Py_True)
130+
else
131+
Result := PPyObject(Py_False);
132+
Py_INCREF(Result);
133+
end else
134+
Result := nil;
135+
end;
136+
137+
procedure TForm1.PythonModuleEvents1Execute(Sender: TObject; PSelf, Args:
138+
PPyObject; var Result: PPyObject);
139+
Var
140+
N: Integer;
141+
begin
142+
with GetPythonEngine do
143+
if PyArg_ParseTuple(Args, 'i:delphi_is_prime',@N) <> 0 then
144+
begin
145+
Result := PyLong_FromLong(CountPrimes(N));
146+
Py_INCREF(Result);
147+
end else
148+
Result := nil;
149+
end;
150+
151+
end.
152+

0 commit comments

Comments
 (0)