-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
1821 lines (1668 loc) · 91.5 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>reveal.js</title>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/cuttlesoft.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<!--
Abstract/Elevator Pitch (max 300 characters):
Get under the hood and learn about Python's beloved Abstract Syntax Tree. We'll discuss the AST's role in Python's compilation process, how it affects Bytecode, and how you can use it's optimizations to improve your code's speed at runtime. Write better code!
Description:
Python is a wonderfully accessible language for novice and veteran programmers alike. Understanding how Python works can be a key step in writing better code.
Ever wonder how Python code is run? Overheard people arguing about whether Python is interpreted or compiled? In this talk, we will delve into the lifecycle of a piece of Python code in order to understand the role that Python's Abstract Syntax Tree plays in shaping the runtime of your code. Utilizing your newfound knowledge of AST optimizations, you will better understand how Python code is compiled in order to write better code.
-->
<!-- 1 - INTRODUCTION AND OUTLINE -->
<!--
* Intro (1 minute)
- Quick introduction of me and my motivation for giving this talk
-->
<section>
<h1>The AST and Me</h1>
<aside class="notes">
This is my 4th PyCon and first time speaking, so thank you all for having me. I'm very happy to be here.
</aside>
</section>
<section class="intro">
<img class="intro__image" data-src="img/intro/me.jpg">
<h3 class="left dark-shadow" style="margin-left:300px;">Emily Morehouse</h3>
<h5 class="left dark-shadow" style="margin-left:300px;">Co-Founder, Director of Engineering</h5>
<h5 class="left dark-shadow" style="margin-left:300px;">@ Cuttlesoft</h5>
<ul class="intro__ul dark-shadow">
<li class="intro__li intro__li--marker"">
Denver, CO
</li>
<li class="intro__li intro__li--twitter"">
<a href="https://twitter.com/emilyemorehouse" target="_blank">@emilyemorehouse</a>
</li>
<li class="intro__li intro__li--github"">
<a href="https://github.com/emilyemorehouse" target="_blank">emilyemorehouse</a>
</li>
</ul>
<aside class="notes">
I'm Emily Morehouse, the director of engineering at Cuttlesoft.
<br><br>
Cuttlesoft is a digital product development firm where I get to work on anything and everything from
CI/CD pipelines and system architectures to web and mobile development, UX and IOT.
</aside>
</section>
<section data-transition="none">
<h4>We're going to cover:</h4>
<ul>
<li>Life cycle of a piece of Python code</li>
<li class="hidden">Interacting with your code at various stages</li>
<li class="hidden">Current Python optimizations</li>
<li class="hidden">Practical applications</li>
</ul>
<aside class="notes">
That said, what we're going to cover in this talk:
<ul>
<li>10,000 foot view of how we get from source code to execution of a piece of Python code</li>
</ul>
</aside>
</section>
<section data-transition="none">
<h4>We're going to cover:</h4>
<ul>
<li>Life cycle of a piece of Python code</li>
<li>Interacting with your code at various stages</li>
<li class="hidden">Current Python optimizations</li>
<li class="hidden">Practical applications</li>
</ul>
<aside class="notes">
What we're going to cover in this talk:
<ul>
<li>How to interact and inspect each step of that process</li>
</ul>
</aside>
</section>
<section data-transition="none">
<h4>We're going to cover:</h4>
<ul>
<li>Life cycle of a piece of Python code</li>
<li>Interacting with your code at various stages</li>
<li>Current Python optimizations</li>
<li class="hidden">Practical applications</li>
</ul>
<aside class="notes">
What we're going to cover in this talk:
<ul>
<li>Current Python optimizations in the compilation process</li>
</ul>
</aside>
</section>
<section data-transition="none">
<h4>We're going to cover:</h4>
<ul>
<li>Life cycle of a piece of Python code</li>
<li>Interacting with your code at various stages</li>
<li>Current Python optimizations</li>
<li>Practical applications</li>
</ul>
<aside class="notes">
What we're going to cover in this talk:
<ul>
<li>AND Practical applications of using ASTs both in and outside of the Python world.</li>
</ul>
</aside>
</section>
<!-- <section>
<h4>What you should get out of this talk:</h4>
<ul>
<li>An understanding of how Python works.*</li>
<li>Knowledge of how to interact with Python's AST and bytecode</li>
<li>Taste of what you can do</li>
</ul>
</section> -->
<!-- I don't know how I feel about this slide, so I'm hiding it for now. -->
<!-- <section>
<h4>What spurred this?</h4>
<p>A Tangent of CPython Contributions!</p>
</section> -->
<!-- 2 - WHY? -->
<!--
* Why should you care about Python internals? (2 minutes)
- Why a bit of knowledge about Python internals goes a long way - from code speedups to understanding tracebacks
-->
<section class="why" data-transition="none">
Should you care about language internals?
<img class="why__gif" data-src="img/gifs/sunny.gif" />
<img class="why__gif why__gif--hidden" data-src="img/gifs/sunny2.gif" loop="infinite" />
<aside class="notes">
Should you care about programming language internals?
</aside>
</section>
<section class="why" data-transition="none">
Should you care about language internals?
<img class="why__gif" data-src="img/gifs/sunny.gif" />
<img class="why__gif" data-src="img/gifs/sunny2.gif" loop="infinite" />
<aside class="notes">
Yes, yes you should.
<br><br>
** DRINK SOME WATER! 🚰 **
</aside>
</section>
<!-- 3 - OVERVIEW -->
<!--
* A brief overview of compiling a piece of Python code - from source code through bytecode and execution, focusing on the role of the AST. 5,000-foot overview. (3 minutes)
- Build an understanding of the broad steps it takes to compile a piece of Python code
-->
<section class="overview" data-background="img/overview/car.jpg">
<h2>A Peek Under The Hood</h2>
<aside class="notes">
Let's take a peek under Python's hood.
</aside>
</section>
<section class="overview">
<h2>From Source Code To Execution:</h2>
<aside class="notes">
<ul>
<li>We'll start with our journey from source code to execution to build an understanding of the broad steps it takes to compile a piece of Python code</li>
</ul>
</aside>
</section>
<section class="overview" data-transition="none">
<header class="overview__header">From Source Code To Execution:</header>
<h3>Q: Interpreted or compiled?</h3>
</section>
<section class="overview" data-transition="none">
<header class="overview__header">From Source Code To Execution:</header>
<h3>A: Both!<h3>
</section>
<section class="overview" data-transition="none">
<h4>Compiler → Generates Bytecode</h4>
<h4 class="hidden">Interpreter → Executes Bytecode</h4>
<aside class="notes">
<ul>
<li>Compiler generates bytecode</li>
</ul>
</aside>
</section>
<section class="overview" data-transition="none">
<h4>Compiler → Generates Bytecode</h4>
<h4>Interpreter → Executes Bytecode</h4>
<aside class="notes">
<ul>
<li>Interpreter makes sense of the bytecode in order to execute your code</li>
<li>One of the ways in which Python is “dynamic” is that the same bytecode doesn’t always have the same effect.</li>
</ul>
</aside>
</section>
<!-- 3B - LIFECYCLE -->
<section class="lifecycle">
<h2>Life Cycle of a Piece of Python Code</h2>
<aside class="notes">
Let's zoom in a bit to see more of this process.
</aside>
</section>
<section class="lifecycle" data-transition="none">
<header class="lifecycle__header">Life cycle of a Piece of Python Code</header>
<img class="lifecycle__image" data-src="img/lifecycle/lifecycle-1.png">
<aside class="notes">
So we'll start with our Python source code.
</aside>
</section>
<section class="lifecycle" data-transition="none">
<header class="lifecycle__header">Life cycle of a Piece of Python Code</header>
<img class="lifecycle__image" data-src="img/lifecycle/lifecycle-2.png">
<aside class="notes">
<ul>
We tokenize and parse that source code...
</ul>
</aside>
</section>
<section class="lifecycle" data-transition="none">
<header class="lifecycle__header">Life cycle of a Piece of Python Code</header>
<img class="lifecycle__image" data-src="img/lifecycle/lifecycle-3.png">
<aside class="notes">
<ul>
...into a parse tree.
<br><br>
Parse trees are a bit more detailed than we want, you'll see a digram in a moment.
</aside>
</section>
<section class="lifecycle" data-transition="none">
<header class="lifecycle__header">Life cycle of a Piece of Python Code</header>
<img class="lifecycle__image" data-src="img/lifecycle/lifecycle-4.png">
<aside class="notes">
So we transform our parse tree...
</aside>
</section>
<section class="lifecycle" data-transition="none">
<header class="lifecycle__header">Life cycle of a Piece of Python Code</header>
<img class="lifecycle__image" data-src="img/lifecycle/lifecycle-5.png">
<aside class="notes">
...into an Abstract syntax tree.
</aside>
</section>
<section class="lifecycle" data-transition="none">
<header class="lifecycle__header">Life cycle of a Piece of Python Code</header>
<img class="lifecycle__image" data-src="img/lifecycle/lifecycle-6.png">
<aside class="notes">
Another transformation into...
</aside>
</section>
<section class="lifecycle" data-transition="none">
<header class="lifecycle__header">Life cycle of a Piece of Python Code</header>
<img class="lifecycle__image" data-src="img/lifecycle/lifecycle-7.png">
<aside class="notes">
... a control flow graph, or CFG, which is a directed graph that models the flow of a program
</aside>
</section>
<section class="lifecycle" data-transition="none">
<header class="lifecycle__header">Life cycle of a Piece of Python Code</header>
<img class="lifecycle__image" data-src="img/lifecycle/lifecycle-8.png">
<aside class="notes">
From here, we can now emit...
</aside>
</section>
<section class="lifecycle" data-transition="none">
<header class="lifecycle__header">Life cycle of a Piece of Python Code</header>
<img class="lifecycle__image" data-src="img/lifecycle/lifecycle-9.png">
<aside class="notes">
... our bytecode.
</aside>
</section>
<section class="lifecycle" data-transition="none">
<header class="lifecycle__header">Life cycle of a Piece of Python Code</header>
<img class="lifecycle__image" data-src="img/lifecycle/lifecycle-10.png">
<aside class="notes">
The CPython virtual machine then executes the bytecode..
</aside>
</section>
<section class="lifecycle" data-transition="none">
<header class="lifecycle__header">Life cycle of a Piece of Python Code</header>
<img class="lifecycle__image" data-src="img/lifecycle/lifecycle-11.png">
<aside class="notes">
... to get our final output.
<br><br>
Now, this is quite the process with some very intricate steps,
so we're going to focus on a few key parts.
</aside>
</section>
<section class="lifecycle" data-transition="none">
<header class="lifecycle__header">Life cycle of a Piece of Python Code</header>
<img class="lifecycle__image" data-src="img/lifecycle/lifecycle-12.png">
<aside class="notes">
Our source, the AST, and generated bytecode.
</aside>
</section>
<!-- 3C - WHAT IS AN AST? -->
<section class="ast">
<h2>What is an Abstract Syntax Tree?</h2>
</section>
<section class="ast">
<header class="ast__header">What is an Abstract Syntax Tree?</header>
<p>Tree representing the structure of your source code.</p>
<aside class="notes">
An AST is a structural representation of your code in a tree format where every node represents a language construct (e.g. expressions, statements, variables, literals etc)
</aside>
</section>
<section class="tree">
<h2>What is... a tree?</h2>
</section>
<section class="tree" data-transition="none">
<header class="tree__header">What is... a tree?</header>
<img class="tree__tree tree__tree--left" data-src="img/ast-primer/pinetree.jpg">
<img class="hidden tree__tree tree__tree--right" data-src="img/ast-primer/complextree.png">
<aside class="notes">
This is a tree.
</aside>
</section>
<section class="tree" data-transition="none">
<header class="tree__header">What is... a tree?</header>
<img class="tree__tree tree__tree--left" data-src="img/ast-primer/pinetree.jpg">
<img class="tree__tree tree__tree--right" data-src="img/ast-primer/complextree.png">
<aside class="notes">
This is ALSO a tree.
</aside>
</section>
<section class="tree" data-transition="none">
<header class="tree__header--walkthru">What is... a tree?</header>
<img class="tree__walkthru" data-src="img/ast-primer/walkthru-1.png">
<aside class="notes">
<ul>
<li>Trees have one <em>root</em> -- the top node</li>
</ul>
</aside>
</section>
<section class="tree" data-transition="none">
<header class="tree__header--walkthru">What is... a tree?</header>
<img class="tree__walkthru" data-src="img/ast-primer/walkthru-2.png">
<aside class="notes">
<ul>
<li>Nodes can branch off...</li>
</ul>
</aside>
</section>
<section class="tree" data-transition="none">
<header class="tree__header--walkthru">What is... a tree?</header>
<img class="tree__walkthru" data-src="img/ast-primer/walkthru-3.png">
<aside class="notes">
<ul>
<li>...to other nodes.</li>
</ul>
</aside>
</section>
<section class="tree" data-transition="none">
<header class="tree__header--walkthru">What is... a tree?</header>
<img class="tree__walkthru" data-src="img/ast-primer/walkthru-4.png">
<aside class="notes">
<ul>
<li>But each node except the root has a single unique parent</li>
<li>So when we read this..</li>
</ul>
</aside>
</section>
<section class="tree" data-transition="none">
<header class="tree__header--walkthru">What is... a tree?</header>
<img class="tree__walkthru" data-src="img/ast-primer/walkthru-5.png">
<aside class="notes">
<ul>
<li>... we start at the top node...</li>
</ul>
</aside>
</section>
<section class="tree" data-transition="none">
<header class="tree__header--walkthru">What is... a tree?</header>
<img class="tree__walkthru" data-src="img/ast-primer/walkthru-6.png">
<aside class="notes">
<ul>
<li>... and work our way as far down our first branch...</li>
</ul>
</aside>
</section>
<section class="tree" data-transition="none">
<header class="tree__header--walkthru">What is... a tree?</header>
<img class="tree__walkthru" data-src="img/ast-primer/walkthru-7.png">
<aside class="notes">
<ul>
<li>...as we can, before we start on the next side...</li>
</ul>
</aside>
</section>
<section class="tree" data-transition="none">
<header class="tree__header--walkthru">What is... a tree?</header>
<img class="tree__walkthru" data-src="img/ast-primer/walkthru-8.png">
<aside class="notes">
<ul>
<li>...performing a ...</li>
</ul>
</aside>
</section>
<section class="tree" data-transition="none">
<header class="tree__header--walkthru">What is... a tree?</header>
<img class="tree__walkthru" data-src="img/ast-primer/walkthru-9.png">
<aside class="notes">
<ul>
<li>...depth first.. </li>
</ul>
</aside>
</section>
<section class="tree" data-transition="none">
<header class="tree__header--walkthru">What is... a tree?</header>
<img class="tree__walkthru" data-src="img/ast-primer/walkthru-10.png">
<aside class="notes">
<ul>
<li>...traversal. (pause to show full tree)</li>
</ul>
</aside>
</section>
<section class="ast">
<header class="ast__header">Parse Tree vs AST</header>
<code class="snippet__code">(1 + 2) * 3</code>
<aside class="notes">
Let's look at the difference between our two types of trees.
</aside>
</section>
<section class="astprimer" data-transition="none">
<header class="astprimer__header">Parse Tree vs AST</header>
<img class="astprimer__tree astprimer__tree--left" data-src="img/ast-primer/parsetree.png">
<img class="hidden astprimer__tree astprimer__tree--right" data-src="img/ast-primer/ast.png">
<code class="snippet__code">(1 + 2) * 3</code>
<aside class="notes">
First, we have a parse tree for a very simple piece of code.
<br><br>
This is essentially a one-to-one mapping our source code based on our language grammar in tree form
</aside>
</section>
<section class="astprimer" data-transition="none">
<header class="astprimer__header">Parse Tree vs AST</header>
<img class="astprimer__tree astprimer__tree--left" data-src="img/ast-primer/parsetree.png">
<img class="astprimer__tree astprimer__tree--right" data-src="img/ast-primer/ast.png">
<code class="snippet__code">(1 + 2) * 3</code>
<aside class="notes">
<p>Here we have an AST for the same piece of code.
We leave behind syntactical specifics so we can instead focus on how underlying objects are structured and the true meaning of the code</p>
<p>It's important to note that there is information that you lose when moving from a parse tree to an abstract syntax tree, and that's very much on purpose.</p>
<p>A CST has all of the information from your source code, including things like comments, whereas an AST is sort of stripped down and simplified. Thus, ASTs are simpler and easier to work with, but may not have all the information you want for certain applications.</p>
</aside>
</section>
<!-- 4 - AST AND DIS MODULES - PART 1 -->
<!--
* Using the AST module to inspect a piece of code (4 minutes) (*This section will go through some trial and error when preparing and it may be best to simplify for brevity. Less code with better understanding is better, using `dis` may muddy the waters)
- Code examples of:
- Using the built-in AST module to inspect a few small piece of code
- Using dis to inspect the bytecode from the example
- Running the dissected code
-->
<section class="demo dig">
<h2>Let's dig in.</h2>
<img class="dig__image" data-src="img/gifs/digging puppy tree.gif" />
<aside class="notes">
Now that we have an understanding of the life cycle of a piece of Python code
let's see how we can interact with the process ourselves.
<br><br>
** DRINK SOME WATER! 🚰🚰🚰 **
</aside>
</section>
<section class="demo" data-transition="none">
<h2>Primary Tools:</h2>
<aside class="notes">
These are built-in to the Python standard library.
</aside>
</section>
<section class="demo" data-transition="none">
<header class="demo__header">Primary Tools:</header>
<ul>
<li>ast module</li>
<li class="hidden">dis module</li>
</ul>
</section>
<section class="demo" data-transition="none">
<header class="demo__header">Primary Tools:</header>
<ul>
<li>ast module</li>
<li>dis module</li>
</ul>
</section>
<section class="demo" data-transition="none">
<h2>Secondary Libraries:</h2>
</section>
<section class="demo" data-transition="none">
<header class="demo__header">Secondary Libraries:</header>
<ul>
<li>astor</li>
<li class="hidden">meta</li>
<li class="hidden">codegen</li>
</ul>
</section>
<section class="demo" data-transition="none">
<header class="demo__header">Secondary Libraries:</header>
<ul>
<li>astor</li>
<li>meta</li>
<li class="hidden">codegen</li>
</ul>
</section>
<section class="demo" data-transition="none">
<header class="demo__header">Secondary Libraries:</header>
<ul>
<li>astor</li>
<li>meta</li>
<li>codegen</li>
</ul>
<aside class="notes">
Some of these don't fully work with Python 3, but they work for the most part.
</aside>
</section>
<section class="demo ast-demo" data-transition="none">
<img class="ast-demo__code" data-src="img/demo/0 - import.png" />
<aside class="notes">
We'll first import the built-in modules that we need.
</aside>
</section>
<section class="demo ast-demo" data-transition="none">
<img class="ast-demo__code" data-src="img/demo/1 - source.png" />
<aside class="notes">
Then we'll start with a very simple piece of Python code - a print statement
that takes a string as its argument.
<br><br>
We can then generate its AST...
</aside>
</section>
<section class="demo ast-demo" data-transition="none">
<img class="ast-demo__code" data-src="img/demo/2 - node.png" />
<aside class="notes">
<p>...using `ast.parse`. We specify the source, as well as the mode that we want to process our code in.</p>
<p>It's important for us to use exec here, as it allows us to run any sort of valid Python code,</p>
<p>whereas eval only allows us to execute expressions</p>
</aside>
</section>
<section class="demo ast-demo" data-transition="none">
<img class="ast-demo__code" data-src="img/demo/2b - node.png" />
<aside class="notes">
So now we have a tree generated from our code, stored as an _ast.Module.
</aside>
</section>
<section class="demo ast-demo" data-transition="none">
<img class="ast-demo__code" data-src="img/demo/2c - node dump.png" />
<aside class="notes">
So what does this look like? Well, we can dump the AST, but it doesn't give us anything that's extremely legible right off the bat,
<br><br>
but if we look closely, we see that we have a function called "print" that is being passed a string.
</aside>
</section>
<section class="demo ast-demo" data-transition="none">
<img class="ast-demo__code" data-src="img/demo/3 - compile.png" />
<aside class="notes">
We'll push forward. We can now take this AST and compile it into bytecode by calling `compile`.
</aside>
</section>
<section class="demo ast-demo" data-transition="none">
<img class="ast-demo__code" data-src="img/demo/3b - type compiled.png" />
<aside class="notes">
Awesome! We now have a something called a code object.
</aside>
</section>
<section class="demo ast-demo" data-transition="none">
<img class="ast-demo__code" data-src="img/demo/3c - dir compiled.png" />
<aside class="notes">
We can poke at that and get a little more information out of it.
</aside>
</section>
<section class="code-objects" data-transition="none">
<h2>Code Objects:</h2>
<aside class="notes">
What's a code object?
</aside>
</section>
<section class="code-objects" data-transition="none">
<header class="code-objects__header">Code objects:</header>
<ul>
<li>Contains instructions and information needed to run the code.</li>
<li class="hidden">Internal representation of a piece of Python code.</li>
</ul>
<aside class="notes">
Code objects are immutable structures used as internal representations of Python code that are generated by the Python compiler...
</aside>
</section>
<section class="code-objects" data-transition="none">
<header class="code-objects__header">Code objects:</header>
<ul>
<li>Contains instructions and information needed to run the code.</li>
<li>Internal representation of a piece of Python code.</li>
</ul>
<aside class="notes">
... that store necessary information to run the code.
There are a lot of parts to a code object, and most of them are only meaningful
to the bytecode interpreter. BUT, there are a few interesting parts for us to touch on.
</aside>
</section>
<section class="code-objects" data-transition="none">
<header class="code-objects__header">Code objects:</header>
<ul>
<li>co_name</li>
<li class="hidden">co_varnames</li>
<li class="hidden">co_stacksize</li>
<li class="hidden">co_consts</li>
<li class="hidden">co_argcount</li>
<li class="hidden">co_code</li>
</ul>
<aside class="notes">
<p>A name stored as a string for this code object.</p>
<p>For a function this would be the function’s name.</p>
<p>For a class this would be the class’ name.</p>
<p>Since our code object was generated using the compile method, we can't specify the name, it automatically gets named module.</p>
</aside>
</section>
<section class="code-objects" data-transition="none">
<header class="code-objects__header">Code objects:</header>
<ul>
<li>co_name</li>
<li>co_varnames</li>
<li class="hidden">co_stacksize</li>
<li class="hidden">co_consts</li>
<li class="hidden">co_argcount</li>
<li class="hidden">co_code</li>
</ul>
<aside class="notes">
A tuple containing the names of the local variables (including arguments).
</aside>
</section>
<section class="code-objects" data-transition="none">
<header class="code-objects__header">Code objects:</header>
<ul>
<li>co_name</li>
<li>co_varnames</li>
<li>co_stacksize</li>
<li class="hidden">co_consts</li>
<li class="hidden">co_argcount</li>
<li class="hidden">co_code</li>
</ul>
<aside class="notes">
The maximum size required of the value stack when running this object. This size is statically computed by the compiler.
</aside>
</section>
<section class="code-objects" data-transition="none">
<header class="code-objects__header">Code objects:</header>
<ul>
<li>co_name</li>
<li>co_varnames</li>
<li>co_stacksize</li>
<li>co_consts</li>
<li class="hidden">co_argcount</li>
<li class="hidden">co_code</li>
</ul>
<aside class="notes">
A tuple containing the code literals used by the bytecode.
</aside>
</section>
<section class="code-objects" data-transition="none">
<header class="code-objects__header">Code objects:</header>
<ul>
<li>co_name</li>
<li>co_varnames</li>
<li>co_stacksize</li>
<li>co_consts</li>
<li>co_argcount</li>
<li class="hidden">co_code</li>
</ul>
<aside class="notes">
The number of positional arguments the code object expects to receive, including those with default values.
</aside>
</section>
<section class="code-objects" data-transition="none">
<header class="code-objects__header">Code objects:</header>
<ul>
<li>co_name</li>
<li>co_varnames</li>
<li>co_stacksize</li>
<li>co_consts</li>
<li>co_argcount</li>
<li>co_code</li>
<!-- <li>co_filename</li> -->
<!-- <li>co_freevars</li> -->
<!-- <li>co_names</li> -->
<!-- <li>co_argcount</li> -->
<!-- <li>co_kwonlyargcount</li> -->
<!-- <li>co_nlocals</li> -->
<!-- <li>co_firstlineno</li> -->
<!-- <li>co_lnotab</li> -->
<!-- <li>co_flags</li> -->
<!-- <li>co_zombieframe</li> -->
</ul>
<aside class="notes">
And our most important part for us -- the string representing the sequence of bytecode instructions.
</aside>
</section>
<section class="demo ast-demo" data-transition="none">
<img class="ast-demo__code" data-src="img/demo/4 - bytecode.png" />
<aside class="notes">
We know we can peek inside the code object and see a bunch of different things, so let's take a look at our raw bytecode. Awesome, this definitely looks like something a computer could understand.
</aside>
</section>
<section class="demo ast-demo" data-transition="none">
<img class="ast-demo__code" data-src="img/demo/5 - exec.png" />
<aside class="notes">
We can also run our compiled code directly using exec
</aside>
</section>
<section class="demo ast-demo" data-transition="none">
<img class="ast-demo__code" data-src="img/demo/6 - pretty bytecode.png" />
<aside class="notes">
Let's go back and take a closer look at out bytecode. We can force the interpreter to show us bytecode that's more legible to humans.
<br><br>Somewhere along the lines, our compiler makes sense of these bytes, but it would take a lot of time for us to go through the giant switch statement that handles opcodes to figure it our ourselves.
</aside>
</section>
<section class="demo ast-demo" data-transition="none">
<img class="ast-demo__code" data-src="img/demo/7 - dis.png" />
<aside class="notes">
We can use Python's disassembler to help us out. We can now see a clear depiction of what is going on -
<p>(elaborate)</p>
</aside>
</section>
<section class="demo ast-demo" data-transition="none">
<img class="ast-demo__code" data-src="img/demo/8 - show code.png" />
<aside class="notes">
And there's a built-in helper function to print out a bunch of this useful information for us.
<p>(elaborate)</p>
<br><br>
But... what does all of this look like in tree form?
</aside>
</section>
<section class="demo snippet" data-transition="none">
<img class="snippet__tree snippet__tree--print" data-src="img/trees/print.png">
<img class="snippet__dis snippet__dis--print" data-src="img/dis/print.png">
<code class="snippet__code">print("may the force be with you")</code>
<aside class="notes">
Here's the AST and disassembled bytecode for a print statement, side by side.
<br><br>
This is pretty readable at this point! We can see that there's a single statement
so we don't need any nesting in our AST.
We'll call LOAD_NAME for our print function, LOAD_CONST for our string that we
passed in, and then simply execute and return.
<br><br>
Simple enough?
</aside>
</section>
<section class="demo snippet" data-transition="none">
<img class="snippet__tree" data-src="img/trees/if print.png">
<img class="snippet__dis" data-src="img/dis/if print.png">
<code class="snippet__code snippet__code--multi">
if a == 23: <br />
print("may the force be with you")
</code>
<aside class="notes">
But as we continue to add code, even just a simple if statement...
</aside>
</section>
<section class="demo snippet" data-transition="none">
<img class="snippet__tree" data-src="img/trees/set if print.png">
<img class="snippet__dis" data-src="img/dis/set if print.png">
<code class="snippet__code snippet__code--multi">
a = 32 <br />
if a == 23: <br />
print("may the force be with you")
</code>
<aside class="notes">
... or variable declaration, we see that our examples get progressively more complex,
but our AST still helps us visualize the path that our code takes.
<br><br>
However, a caveat to all of this is that what we wind up with in our AST or bytecode is not necessarily going to match our
source code 1 to 1. There are certain shortcuts that our compiler takes when creating our
bytecode, so let's take a look....
</aside>
</section>
<!-- 5 - CURRENT COMPILER OPTIMIZATIONS -->
<!--
* Current optimizations built into the AST (and ones that could be) (3 minutes)
- What does one gain from optimizing the AST?
- How is the AST optimized?
- Constant folding, dead code elimination, copy propagation. (Easy, quick descriptions of each)
-->
<section class="optimizations" data-transition="none">
<h2>Current Optimizations</h2>
<aside class="notes">
... at what sorts of compiler optimizations are affecting our bytecode.
</aside>
</section>
<section class="optimizations" data-transition="none">
<header class="optimizations__header">Current Optimizations</header>
<ul>
<li>Python's compiler is purposefully simple</li>
<li class="hidden">Peephole Optimizer</li>
<li class="hidden">Few AST optimizations besides constant folding</li>
</ul>
<aside class="notes">
Python's compiler is purposefully simple, or as simple as can be.
<br><br>
The best way to optimize Python is to replace the implementation for another interpreter,
like PyPy, Jython, Cython, IronPython, Stackless Python, even MicroPython.
<br><br>
Python, the language, can be completely independent of the implementation used to bring the language to life.
</aside>
</section>
<section class="optimizations" data-transition="none">
<header class="optimizations__header">Current Optimizations</header>
<ul>
<li>Python's compiler is purposefully simple</li>
<li>Peephole Optimizer</li>
<li class="hidden">Few AST optimizations besides constant folding</li>
</ul>
</section>
<section class="optimizations" data-transition="none">
<header class="optimizations__header">Current Optimizations</header>
<ul>
<li>Python's compiler is purposefully simple</li>
<li>Peephole Optimizer</li>
<li>Few AST optimizations besides constant folding</li>
</ul>
</section>
<section class="optimizations">
<h2>Peephole Optimizations</h2>
</section>
<section class="optimizations">
<header class="optimizations__header">Peephole Optimizations</header>
<p>Looking around without moving your head.</p>
<aside class="notes">
</aside>
</section>
<section class="optimizations">
<header class="optimizations__header">Peephole Optimizations</header>
<code class="snippet__code snippet__code--tinymulti">
x = 1<br />
y = x + 2
</code>
<aside class="notes">
</aside>
</section>
<section class="optimizations">
<header class="optimizations__header">Peephole Optimizations</header>
<code>y = 1 + 2</code>
<aside class="notes">
</aside>
</section>
<section class="optimizations">
<header class="optimizations__header">Peephole Optimizations</header>
<img class="snippet__tree" data-src="img/dis/pre-peephole.png">
<img class="snippet__dis" data-src="img/dis/post-peephole.png">
<aside class="notes">
One of the other interesting examples of a peephole optimization is the example seen here. Essentially,
the optimzation takes excessive or redundant logic and simplifies it for you.
</aside>
</section>
<section class="optimizations">
<h2>Constant Folding</h2>
</section>
<section class="optimizations">
<header class="optimizations__header">Constant Folding</header>
<p>Evaluating constant expressions at compile time.</p>
<aside class="notes">
Constant folding is the process of recognizing and evaluating constant expressions at compile time rather than computing them at runtime. Terms in constant expressions are typically simple literals, such as the integer literal 2, but they may also be variables whose values are known at compile time
</aside>
</section>
<section class="demo snippet" data-transition="none">
<img class="snippet__tree snippet__tree--demo" data-src="img/trees/pre-fold.png">
<img class="snippet__dis snippet__dis--demo" data-src="img/dis/pre-fold.png">
<code class="snippet__code">a = 2 * 3</code>
<aside class="notes">
Now, not all languages do this, and sometimes don't evaulate all expressions into constants, but a simple example of this is taking 2 times 3...
</aside>
</section>
<section class="demo snippet" data-transition="none">
<img class="snippet__tree snippet__tree--demo" data-src="img/trees/post-fold.png">
<img class="snippet__dis snippet__dis--demo" data-src="img/dis/post-fold.png">
<code class="snippet__code">a = 6</code>
<aside class="notes">
...and replacing it with 6.
</aside>
</section>
<!-- 6 - AST AND DIS MODULES - PART 2 TODO -->
<!-- 7 - AST -> BYTECODE -> RUNTIME -->
<!--
* How the AST affects your Code, and how your bytecode affects your runtime (5 minutes)
- Spoiler alert: it creates those *.pyc and *.pyo files (neatly stored away in the __pycache__ directory in Python 3.2+! And those contain bytecode.
- Mention the difference between *.pyc files and *pyo files.
-->
<section class="bytecode">
<h2>How The AST Affects Your Code</h2>
</section>
<section class="bytecode" data-transition="none">
<header class="bytecode__header">How The AST Affects Your Code</header>
<ul>
<li>Bytecode is created using an AST</li>