-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
1109 lines (1023 loc) · 48 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>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-131298185-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-131298185-1');
</script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Python++</title>
<link rel="stylesheet" href="reveal/css/reveal.css">
<link rel="stylesheet" href="reveal/css/theme/black.css" id="theme">
<link rel="stylesheet" href="reveal/lib/css/solarized-dark.css">
<link rel="stylesheet" href="reveal/css/custom.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 ) ? 'reveal/css/print/pdf.css' : 'reveal/css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<!-- Title -->
<section>
<h1>Python++</h1>
<p><i>linux.conf.au 2019</i></p>
<p>
<small>Jan Groth</small>
</p>
</section>
<!-- Intro -->
<section>
<section>
<h2>About me</h2>
<ul>
<li>DevOps Engineer at Versent* in Sydney**</li>
<li>Developer Background</li>
</ul>
</p>
<p>
<small class="fragment">
(*) We are hiring<br/>
(**) In Sydney and Melbourne
</small>
</p>
</section>
<section>
<h2>This talk</h2>
<p>
<ul class="fragment">
<li class="fragment strike">Here are 10 Python features - now start using them!</li>
<li class="fragment">Things that I find useful when writing code in Python</li>
<li class="fragment">Don't take notes</li>
</ul>
</p>
</section>
</section>
<!-- Python3 & venv -->
<section>
<section>
<h2>What year is this?</h2>
</section>
<section>
<ul>
<li><strong>The Dark Knight</strong> is the most popular movie</li>
<li><strong>Lehman Brothers</strong> file for bankruptcy</li>
<li><strong>Spotify</strong> launches in Sweden</li>
<li><strong>Lady Gaga</strong> has number-one singles with <strong>Just Dance</strong> and <strong>Poker Face</strong></li>
</ul>
</section>
<section>
<h1>2008</h1>
</section>
<section>
<blockquote><strong>Python 3.0</strong> was released on December 3, <strong>2008</strong></blockquote>
<blockquote>It was designed to <strong>rectify fundamental design flaws</strong> in the language—the changes required could not be implemented while retaining full backwards compatibility with the 2.x series, which necessitated a <strong>new major version</strong> number</blockquote>
<p>
<small>
Source: Wikipedia
</small>
</p>
</section>
<section>
<p>Python 2</p>
<p>or</p>
<p>Python 3?</p>
</section>
<section>
<p>Best reason to stop using Python 2.7 <strong>now</strong>:</p>
<iframe src="https://free.timeanddate.com/countdown/i6k6by43/n951/cf100/cm0/cu5/ct4/cs0/cacffffce/co0/cr0/ss0/cacfff/cpc000/pct/tcfff/fn3/fs250/szw576/szh243/iso2020-01-01T00:00:00/bas5/bacf00/pa10" allowTransparency="true" frameborder="0" width="605" height="272"></iframe>
</section>
<section>
<p>Python <strong>versions</strong> and project <strong>dependencies</strong></p>
</section>
<section>
<p>One Python environment <strong>per project</strong></p>
<p>! <strong>Don't</strong> use the system environment</p>
</section>
<!--<section>-->
<!--<p>Installing <em>boto3</em> and <em>pyyaml</em>...</p>-->
<!--<div class="fragment">-->
<!--<p><em>requirements.txt</em>:</p>-->
<!--<div class="left">-->
<!--<pre data-badge="✘" data-badge-fg="#dc322f;">-->
<!--<code class="bash" data-trim data-noescape>-->
<!--boto3==1.9.71-->
<!--botocore==1.12.71-->
<!--docutils==0.14-->
<!--jmespath==0.9.3-->
<!--python-dateutil==2.7.5-->
<!--PyYAML==3.13-->
<!--s3transfer==0.1.13-->
<!--six==1.12.0-->
<!--urllib3==1.24.1-->
<!--</code>-->
<!--</pre>-->
<!--</div>-->
<!--<div class="right">-->
<!--<pre data-badge="✔" data-badge-fg="#859900;">-->
<!--<code class="bash" data-trim data-noescape>-->
<!--boto3-->
<!--PyYAML-->
<!--</code>-->
<!--</pre>-->
<!--</div>-->
<!--</div>-->
<!--</section>-->
<!--<section>-->
<!--<p>Project dependencies as <strong>unspecific as possible</strong></p>-->
<!--<p>Declare <strong>only direct</strong> dependencies<br/>-->
<!--Ignore transitive dependencies</p>-->
<!--</section>-->
<section data-background="#f7f2d3">
<h2>Tip #1</h2>
<p>Stay ahead of Python's versions</p>
<ul>
<li>Use Python 3</li>
<li>Use a tool to manage environments</li>
<ul>
<li>venv</li>
<li>pipenv</li>
</ul>
</ul>
</section>
</section>
<!-- Conventions & finding good names -->
<section data-state="changeToBlack">
<section>
<h2>Readability</h2>
<p>Write <strong>once</strong>. Read <strong>many</strong> times.</p>
</section>
<section>
<p>More conventions - less thinking ...</p>
<pre data-badge="✔" data-badge-fg="#859900;">
<code class="python" data-trim data-noescape>
CONSTANTS_IN_UPPERCASE = 123
variables_are_lowercase = 'OH YES!'
def methods_are_lowercase_too():
pass
def all_use_snake_case():
pass
</code>
</pre>
</section>
<section>
<p>File names...</p>
<pre data-badge="✘" data-badge-fg="#dc322f;">
<code class="bash" data-trim data-noescape>
lambda.py
nexus.py
</code>
</pre>
<pre data-badge="✔" data-badge-fg="#859900;">
<code class="bash" data-trim data-noescape>
rewards-calculation-lambda.py
query-nexus-for-latest-snapshot-version.py
</code>
</pre>
</section>
<!--<section data-transition="slide-in fade-out">-->
<!--<p>Method names...</p>-->
<!--<pre data-badge="✘" data-badge-fg="#dc322f;">-->
<!--<code class="python" data-trim data-noescape>-->
<!--# creates new sftp client-->
<!--def sftp_client(username, password):-->
<!--pass-->
<!--</code>-->
<!--</pre>-->
<!--<pre data-badge="✔" data-badge-fg="#859900;">-->
<!--<code class="python" data-trim data-noescape>-->
<!--def create_sftp_client(username, password):-->
<!--pass-->
<!--</code>-->
<!--</pre>-->
<!--</section>-->
<!--<section data-transition="fade-in slide-out">-->
<!--<p>Method names...</p>-->
<!--<pre data-badge="✘" data-badge-fg="#dc322f;">-->
<!--<code class="python" data-trim data-noescape>-->
<!--# creates new sftp client-->
<!--def sftp_client(username, password):-->
<!--pass-->
<!--</code>-->
<!--</pre>-->
<!--<pre data-badge="✔" data-badge-fg="#859900;">-->
<!--<code class="python" data-trim data-noescape>-->
<!--def <mark>create_sftp_client</mark>(username, password):-->
<!--pass-->
<!--</code>-->
<!--</pre>-->
<!--</section>-->
<section data-transition="slide-in fade-out">
<p>Complex code ...</p>
<pre data-badge="✘" data-badge-fg="#dc322f;">
<code class="python" data-trim data-noescape>
# scale out if less than 3 instances
if len(current_instances) < 3:
add_instance()
</code>
</pre>
<pre data-badge="✔" data-badge-fg="#859900;">
<code class="python" data-trim data-noescape>
if should_scale_out(current_instances):
add_instance()
</code>
</pre>
</section>
<section data-transition="fade-in slide-out">
<p>Complex code ...</p>
<pre data-badge="✘" data-badge-fg="#dc322f;">
<code class="python" data-trim data-noescape>
# scale out if less than 3 instances
if len(current_instances) < 3:
add_instance()
</code>
</pre>
<pre data-badge="✔" data-badge-fg="#859900;">
<code class="python" data-trim data-noescape>
if <mark>should_scale_out</mark>(current_instances):
add_instance()
</code>
</pre>
</section>
<section data-background="#f7f2d3">
<h2>Tip #2</h2>
<p>Use naming conventions</p>
<p>Make an effort to find good names</p>
<p>Introduce methods to increase readability</p>
</section>
</section>
<!-- Beauty lies in simplicity -->
<section>
<section>
<h2>Writing beautiful code</h2>
<p>There is <strong>beauty</strong> in <strong>simplicity</strong>.</p>
</section>
<section>
<p>Using the language construct that fits best</p>
<div class="left">
<pre data-badge="✘" data-badge-fg="#dc322f;">
<code class="python" data-trim data-noescape>
counter = 0
while counter < 5:
print(counter)
counter += 1
</code>
</pre>
</div>
<div class="right">
<pre data-badge="✔" data-badge-fg="#859900;">
<code class="python" data-trim data-noescape>
for i in range(5):
print(i)
</code>
</pre>
</div>
</section>
<section>
There is often a <strong>pythonic</strong> way
</section>
<section data-transition="slide-in fade-out">
<p><strong>Loop</strong> vs <strong>List Comprehension</strong></p>
<pre data-badge="✘" data-badge-fg="#dc322f;">
<code class="python" data-trim data-noescape>
symbols = '$¢£¥€¤'
codes = []
for symbol in symbols:
codes.append(ord(symbol))
>>> [36, 162, 163, 165, 8364, 164]
</code>
</pre>
<pre data-badge="✔" data-badge-fg="#859900;">
<code class="python" data-trim data-noescape>
symbols = '$¢£¥€¤'
codes = [ord(symbol) for symbol in symbols]
>>> [36, 162, 163, 165, 8364, 164]
</code>
</pre>
</section>
<section data-transition="fade-in slide-out">
<p><strong>Loop</strong> vs <strong>List Comprehension</strong></p>
<pre data-badge="✘" data-badge-fg="#dc322f;">
<code class="python" data-trim data-noescape>
symbols = '$¢£¥€¤'
codes = []
for symbol in symbols:
codes.append(ord(symbol))
>>> [36, 162, 163, 165, 8364, 164]
</code>
</pre>
<pre data-badge="✔" data-badge-fg="#859900;">
<code class="python" data-trim data-noescape>
symbols = '$¢£¥€¤'
codes = <mark>[ord(symbol) for symbol in symbols]</mark>
>>> [36, 162, 163, 165, 8364, 164]
</code>
</pre>
</section>
<section data-transition="fade-in">
<p>Commenting every single line is essential ...</p>
</section>
<section data-transition="fade-in">
<p>..if you are writing in Assembler.</p>
<pre>
<code class="assembler" data-trim data-noescape>
flash2ram:
lpm ;get constant
st Y+,r0 ;store in SRAM and increment Y-pointer
adiw ZL,1 ;increment Z-pointer
dec flashsize
brne flash2ram ;if not end of table, loop more
ret
</code>
</pre>
</section>
<section data-transition="fade-in">
<p>In Python not so much.</p>
<pre>
<code class="python" data-trim data-noescape>
def create_password_hash(arg):
# Randomise Salt
salt = os.urandom(6)
# Convert String to Byte Array
res = arg.encode()
# Hash through 10000 times.
for lp in range(100000):
# Always initialize hashlib, because [...]
m = hashlib.sha256()
# Swap between the two inputs into the digest based on loop number
m.update(res) if lp % 2 else m.update(salt)
m.update(salt) if lp % 2 else m.update(res)
# Finish the digest
res = m.digest()
</code>
</pre>
</section>
<section data-transition="fade-in">
<p>In Python not so much.</p>
<pre>
<code class="python" data-trim data-noescape>
def create_password_hash(arg):
<strike># Randomise Salt</strike>
salt = os.urandom(6)
<strike class="strike"># Convert String to Byte Array</strike>
res = arg.encode()
<strike class="strike"># Hash through 10000 times.</strike>
for lp in range(100000):
<mark># Always initialize hashlib, because [...]</mark>
m = hashlib.sha256()
<strike class="strike"># Swap between the two inputs into the digest based on loop number</strike>
m.update(res) if lp % 2 else m.update(salt)
m.update(salt) if lp % 2 else m.update(res)
<strike class="strike"># Finish the digest</strike>
res = m.digest()
</code>
</pre>
</section>
<section data-background="#f7f2d3">
<h2>Tip #3</h2>
<h4>Less is more</h4>
<p>Write code that is both functional and simple</p>
<p>Use Python idioms where you can</p>
<p>Comment only what you cannot say in code</p>
</section>
</section>
<!-- Scope & classes -->
<section>
<section>
<h2>Classes</h2>
</section>
<section>
<p>A <strong>basic</strong> calculator</p>
</section>
<section data-transition="slide-in fade-out">
<p>Calculations on top of a <strong>base</strong> number</p>
<pre>
<code class="python" data-trim data-noescape>
base = 10
def multiply(number):
return base * number
def add(number):
return base + number
print(multiply(5)) # ...50
print(add(7)) # ...12
</code>
</pre>
</section>
<section>
<p>Time passes ...</p>
<p class="fragment">... new requirements are coming in:</p>
</section>
<section data-transition="slide-in fade-out">
<p>Calculations on top of a <strong>second</strong> number</p>
<pre>
<code class="python" data-trim data-noescape>
base = 10
def multiply(number):
return base * number
def add(number):
return base + number
print(multiply(5)) # ...50
print(add(7)) # ...12
.
</code>
</pre>
</section>
<section data-transition="fade-in fade-out">
<p>Calculations on top of a <strong>second</strong> number</p>
<pre>
<code class="python" data-trim data-noescape>
base = 10
def multiply(number):
return base * number
def add(number):
return base + number
print(multiply(5)) # ...50
print(add(7)) # ...12
base = 6
print(multiply(5)) # ...30
print(add(7)) # ...13
</code>
</pre>
</section>
<section data-transition="fade-in fade-out">
<p>Problem: Separation of <strong>state</strong>...</p>
<pre>
<code class="python" data-trim data-noescape>
<mark>base = 10</mark>
def multiply(number):
return base * number
def add(number):
return base + number
print(multiply(5)) # ...50
print(add(7)) # ...12
<mark>base = 6</mark>
print(multiply(5)) # ...30
print(add(7)) # ...13
</code>
</pre>
</section>
<section data-transition="fade-in fade-out">
<p>...and <strong>behaviour</strong></p>
<pre>
<code class="python" data-trim data-noescape>
base = 10
<mark>def multiply(number):</mark>
return base * number
<mark>def add(number):</mark>
return base + number
print(multiply(5)) # ...50
print(add(7)) # ...12
base = 6
print(multiply(5)) # ...30
print(add(7)) # ...13
</code>
</pre>
</section>
<section data-transition="fade-in slide-out">
<p>...make the code <strong>much harder</strong> to understand</p>
<pre>
<code class="python" data-trim data-noescape>
base = 10
def multiply(number):
return base * number
def add(number):
return base + number
<mark>print(multiply(5)) # ...50</mark>
print(add(7)) # ...12
base = 6
<mark>print(multiply(5)) # ...30</mark>
print(add(7)) # ...13
</code>
</pre>
</section>
<section>
<h4>
Using <strong>classes</strong> to redesign the calculator
</h4>
<br/>
<ul>
<li>A class is a <strong>blueprint</strong> for objects</li>
<li>An object <strong>encapsulates</strong> data as well as methods</li>
</ul>
</section>
<section data-transition="slide-in fade-out">
<p>API Design first ...</p>
<pre>
<code class="python" data-trim data-noescape>
calculator = <mark>Calculator(base=10)</mark>
print(calculator.multiply_by(5))
print(calculator.add(7))
</code>
</pre>
</section>
<section data-transition="fade-in slide-out">
<p>API Design first ...</p>
<pre>
<code class="python" data-trim data-noescape>
calculator = Calculator(base=10)
print(calculator.<mark>multiply_by(5)</mark>)
print(calculator.<mark>add(7)</mark>)
</code>
</pre>
</section>
<section data-transition="slide-in fade-out">
<p>Implementation</p>
<pre>
<code class="python" data-trim data-noescape>
class Calculator:
def __init__(self, base):
self.calc_base = base
def multiply_by(self, number):
return self.calc_base * number
def add(self, number):
return self.calc_base + number
</code>
</pre>
</section>
<section data-transition="fade-in fade-out">
<p>Implementation</p>
<pre>
<code class="python" data-trim data-noescape>
<mark>class Calculator:</mark>
def __init__(self, base):
self.calc_base = base
def multiply_by(self, number):
return self.calc_base * number
def add(self, number):
return self.calc_base + number
</code>
</pre>
</section>
<section data-transition="fade-in">
<p>Implementation</p>
<pre>
<code class="python" data-trim data-noescape>
class Calculator:
<mark>def __init__(self, base):</mark>
<mark>self.calc_base = base</mark>
def multiply_by(self, number):
return self.calc_base * number
def add(self, number):
return self.calc_base + number
</code>
</pre>
</section>
<section data-transition="fade-in slide-out">
<p>Implementation</p>
<pre>
<code class="python" data-trim data-noescape>
class Calculator:
def __init__(self, base):
self.calc_base = base
<mark>def multiply_by(self, number):</mark>
<mark>return self.calc_base * number</mark>
def add(self, number):
return self.calc_base + number
</code>
</pre>
</section>
<section data-transition="slide-in fade-out">
<p>Different calculators are now separate objects ...</p>
<pre>
<code class="python" data-trim data-noescape>
<mark>base_ten_calc = Calculator(base=10)</mark>
print(base_ten_calc.multiply_by(5)) # ...50
print(base_ten_calc.add(7)) # ...17
<mark>base_six_calc = Calculator(base=6)</mark>
print(base_six_calc.multiply_by(5)) # ...30
print(base_six_calc.add(7)) # ...13
</code>
</pre>
</section>
<section data-transition="fade-in slide-out">
<p>Different calculators are now separate objects ...</p>
<pre>
<code class="python" data-trim data-noescape>
base_ten_calc = Calculator(base=10)
<mark>print(base_ten_calc.multiply_by(5)) # ...50</mark>
print(base_ten_calc.add(7)) # ...17
base_six_calc = Calculator(base=6)
<mark>print(base_six_calc.multiply_by(5)) # ...30</mark>
print(base_six_calc.add(7)) # ...13
</code>
</pre>
</section>
<section data-transition="slide-in fade-out">
<p>
A real-world example - <br/>
Syncing a file between SFTP and S3
</p>
<pre>
<code class="python" data-trim data-noescape>
s2s_sync = Sftp2S3(server_name='a.b.c', bucket_name='my-bucket')
s2s_sync.sync_file('foo.txt')
s2s_sync.sync_file('baz/bar.txt')
</code>
</pre>
</section>
<section data-transition="fade-in">
<p>
A real-world example - <br/>
Syncing a file between SFTP and S3
</p>
<pre>
<code class="python" data-trim data-noescape>
s2s_sync = <mark>Sftp2S3(server_name='a.b.c', bucket_name='my-bucket')</mark>
s2s_sync.sync_file('foo.txt')
s2s_sync.sync_file('baz/bar.txt')
</code>
</pre>
</section>
<section data-transition="fade-in slide-out">
<p>
A real-world example - <br/>
Syncing a file between SFTP and S3
</p>
<pre>
<code class="python" data-trim data-noescape>
s2s_sync = Sftp2S3(server_name='a.b.c', bucket_name='my-bucket')
s2s_sync.<mark>sync_file('foo.txt')</mark>
s2s_sync.<mark>sync_file('baz/bar.txt')</mark>
</code>
</pre>
</section>
<section data-transition="slide-in fade-out">
<p>Implementation Design</p>
<pre>
<code class="python" data-trim data-noescape>
class Sftp2S3:
def __init__(self, server_name, bucket_name):
self.sftp_server = self._init_sftp(server_name)
self.bucket = boto3.resource('s3').Bucket(bucket_name)
def _init_sftp(self, server_name): pass
def _needs_sync(self, file_name): pass
def _copy_from_sftp(self, file_name): pass
def sync_file(self, file_name):
if (self._needs_sync(file_name)):
self._copy_from_sftp(file_name)
</code>
</pre>
</section>
<section data-transition="fade-in">
<p>Implementation Design</p>
<pre>
<code class="python" data-trim data-noescape>
class Sftp2S3:
<mark>def __init__(self, server_name, bucket_name):</mark>
<mark>self.sftp_server = self._init_sftp(server_name)</mark>
<mark>self.bucket = boto3.resource('s3').Bucket(bucket_name)</mark>
def _init_sftp(self, server_name): pass
def _needs_sync(self, file_name): pass
def _copy_from_sftp(self, file_name): pass
def sync_file(self, file_name):
if (self._needs_sync(file_name)):
self._copy_from_sftp(file_name)
</code>
</pre>
</section>
<section data-transition="fade-in slide-out">
<p>Implementation Design</p>
<pre>
<code class="python" data-trim data-noescape>
class Sftp2S3:
def __init__(self, server_name, bucket_name):
self.sftp_server = self._init_sftp(server_name)
self.bucket = boto3.resource('s3').Bucket(bucket_name)
def _init_sftp(self, server_name): pass
def _needs_sync(self, file_name): pass
def _copy_from_sftp(self, file_name): pass
<mark>def sync_file(self, file_name):</mark>
<mark>if (self._needs_sync(file_name)):</mark>
<mark>self._copy_from_sftp(file_name)</mark>
</code>
</pre>
</section>
<section>
<h4>This is just a sneak peek into classes</h4>
<br/>
<ul>
<li><strong>Re-use</strong> code elsewhere</li>
<li>Utilize <strong>inheritance</strong> to implement your own <strong>generators</strong>, <strong>collections</strong> ...</li>
<li>And ...</li>
</ul>
</section>
<section>
<p>Unit Testing</p>
<pre>
<code class="python" data-trim data-noescape>
class TestCalculator(TestCase):
def test_multiply_with_zero_is_zero(self):
# setup
calc = Calculator(29)
# exercise
result = calc.multiply_by(0)
# verify
self.assertEqual(0, result)
</code>
</pre>
</section>
<section data-background="#f7f2d3">
<h2>Tip #4</h2>
<p>Encapsulate behaviour as well as state</p>
<p>Use classes - The door to a whole new world</p>
</section>
</section>
<!-- Use an IDE -->
<section>
<section>
<h2>The right tool for the job</h2>
</section>
<section>
<p>What does this code print?</p>
<img src="resources/faculty.png">
</section>
<section>
<p>... not much</p>
<img src="resources/pycharm.png">
</section>
<section>
Can <i><strike>vim</strike> <strike>emacs</strike> <strike>sublime</strike></i> a text editor do this?
</section>
<section>
<p>Remove unused imports ...</p>
<img src="resources/import.png">
</section>
<section>
<p>Reformat code according to Python standards ...</p>
<img src="resources/reformat.png">
</section>
<section>
<p>Throw in a breakpoint ...</p>
<img src="resources/debugger.png">
</section>
<section>
<p>Change a method name across the whole project ...</p>
<img src="resources/refactor.png">
</section>
<section data-background="#f7f2d3">
<h2>Tip #5</h2>
<p>Python is a high-level programming language.</p>
<p>Use an IDE.
<p>
<p>Just because you can.</p>
</section>
</section>
<!-- Language Features-->
<!--<section>-->
<!--<section>-->
<!--<h2>Language Features</h2>-->
<!--</section>-->
<!--<section data-transition="slide-in fade-out">-->
<!--<p>Using Jinja templates to generate Jenkins pipelines</p>-->
<!--<pre data-badge="pipeline.groovy">-->
<!--<code class="groovy" data-trim data-noescape>-->
<!--GIT_BRANCHES = ['master', 'development']-->
<!--...-->
<!--for (branch in GIT_BRANCHES) {-->
<!--...-->
<!--}-->
<!--</code>-->
<!--</pre>-->
<!--<pre data-badge="config.yml" class="fragment">-->
<!--<code class="yaml" data-trim data-noescape>-->
<!--git:-->
<!--branches:-->
<!-- - master-->
<!-- - development-->
<!--</code>-->
<!--</pre>-->
<!--<pre data-badge="pipeline.groovy.j2" class="fragment">-->
<!--<code class="python" data-trim data-noescape>-->
<!--GIT_BRANCHES = {{ git.branches }} # Does not come out as a groovy literal...-->
<!--</code>-->
<!--</pre>-->
<!--</section>-->
<!--<section data-transition="fade-in slide-out">-->
<!--<p>Using Jinja templates to generate Jenkins pipelines</p>-->
<!--<pre data-badge="pipeline.groovy">-->
<!--<code class="groovy" data-trim data-noescape>-->
<!--GIT_BRANCHES = ['master', 'development']-->
<!--...-->
<!--for (branch in GIT_BRANCHES) {-->
<!--...-->
<!--}-->
<!--</code>-->
<!--</pre>-->
<!--<pre data-badge="config.yml">-->
<!--<code class="yaml" data-trim data-noescape>-->
<!--git:-->
<!--branches:-->
<!-- - master-->
<!-- - development-->
<!--</code>-->
<!--</pre>-->
<!--<pre data-badge="pipeline.groovy.j2">-->
<!--<code class="python" data-trim data-noescape>-->
<!--GIT_BRANCHES = {{ git.branches <mark>| to_groovy_list</mark> }} # Custom Jinja filter...-->
<!--</code>-->
<!--</pre>-->
<!--</section>-->
<!--<section data-transition="slide-in fade-out">-->
<!--<p>Registering a custom Jinja filter</p>-->
<!--<pre data-badge="render_pipeline1.py">-->
<!--<code class="python" data-trim data-noescape>-->
<!--from jinja2 import Environment-->
<!--jinja_env = Environment() # [...]-->
<!--jinja_env.filters['quoted'] = quoted-->
<!--jinja_env.filters['to_groovy_list'] = to_groovy_list-->
<!--def quoted(text):-->
<!--return "'{}'".format(text)-->
<!--def to_groovy_list(list):-->
<!--return '[{}]'.format(', '.join(map(quoted, list)))-->
<!--</code>-->
<!--</pre>-->
<!--</section>-->
<!--<section data-transition="fade-in fade-out">-->
<!--<p>Registering a custom Jinja filter</p>-->
<!--<pre data-badge="render_pipeline1.py">-->
<!--<code class="python" data-trim data-noescape>-->
<!--from jinja2 import Environment-->
<!--jinja_env = Environment() # [...]-->
<!--jinja_env.filters['quoted'] = quoted-->
<!--jinja_env.filters['to_groovy_list'] = to_groovy_list-->
<!--def quoted(text):-->
<!--return "'{}'".format(text)-->
<!--<mark>def to_groovy_list(list):</mark>-->
<!--<mark>return '[{}]'.format(', '.join(map(quoted, list)))</mark>-->
<!--</code>-->
<!--</pre>-->
<!--</section>-->
<!--<section data-transition="fade-in slide-out">-->
<!--<p>Registering a custom Jinja filter</p>-->
<!--<pre data-badge="render_pipeline1.py">-->
<!--<code class="python" data-trim data-noescape>-->
<!--from jinja2 import Environment-->
<!--jinja_env = Environment() # [...]-->
<!--jinja_env.filters['quoted'] = quoted-->
<!--<mark>jinja_env.filters['to_groovy_list'] = to_groovy_list</mark>-->
<!--def quoted(text):-->
<!--return "'{}'".format(text)-->
<!--def to_groovy_list(list):-->
<!--return '[{}]'.format(', '.join(map(quoted, list)))-->
<!--</code>-->
<!--</pre>-->
<!--</section>-->
<!--<section data-transition="slide-in fade-out">-->
<!--<p>The same, using a decorator</p>-->
<!--<pre data-badge="render_pipeline2.py">-->
<!--<code class="python" data-trim data-noescape>-->
<!--jinja_env = Environment() # [...]-->
<!--def custom_filter(func):-->
<!--jinja_env.filters[func.__name__] = func-->
<!--@custom_filter-->
<!--def quoted(text):-->
<!--return "'{}'".format(text)-->
<!--@custom_filter-->
<!--def to_groovy_list(list):-->
<!--return '[{}]'.format(', '.join(map(quoted, list)))-->
<!--</code>-->
<!--</pre>-->
<!--</section>-->
<!--<section data-transition="fade-in slide-out">-->
<!--<p>The same, using a decorator</p>-->
<!--<pre data-badge="render_pipeline2.py">-->
<!--<code class="python" data-trim data-noescape>-->
<!--jinja_env = Environment() # [...]-->
<!--<mark>def custom_filter(func):</mark>-->
<!--jinja_env.filters[func.__name__] = func-->
<!--<mark>@custom_filter</mark>-->
<!--def quoted(text):-->
<!--return "'{}'".format(text)-->
<!--<mark>@custom_filter</mark>-->
<!--def to_groovy_list(list):-->
<!--return '[{}]'.format(', '.join(map(quoted, list)))-->
<!--</code>-->
<!--</pre>-->
<!--</section>-->
<!--<section>-->
<!--<p>Implementing a CLI tool</p>-->
<!--<pre data-badge="deploy.sh">-->
<!--<code class="plaintext" data-trim data-noescape>-->
<!--query-nexus.py \-->
<!----nexus-url 'https://nexus.local' \-->
<!----repo 'maven-releases' \-->
<!----group 'com.company.ms' \-->
<!----artifact 'my-api' \-->
<!----version '1.0.8'-->
<!--</code>-->
<!--</pre>-->
<!--</section>-->