summaryrefslogtreecommitdiff
path: root/reveal.js/python.html
blob: d27cd4952e1b484a2a2908388858862028ae419a (plain)
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
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Introduction to Python</title>
    <meta name="author" content="Simeon Simeonov">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="stylesheet" href="dist/reset.css">
    <link rel="stylesheet" href="dist/reveal.css">

    <link rel="stylesheet" href="dist/theme/statnett.css" id="theme">

    <!-- Theme used for syntax highlighting of code -->
    <link rel="stylesheet" href="plugin/highlight/monokai.css" id="highlight-theme">
    <!-- <link rel="stylesheet" href="plugin/highlight/zenburn.css" id="highlight-theme"> -->
  </head>
  <body>
    <div class="reveal">

      <!-- Any section element inside of this container is displayed as a slide -->
      <div class="slides">

        <section>
          <h2>Introduction to Python</h2>
          <h4>Data Engineering @ Statnett</h4>
          </br>
          <p><small>Simeon Simeonov</small></p>
        </section>

        <section>
          <h2>Goals</h2>
          </br>
          <ul>
            <li><p>Present the Python programming language in a different way than <a href="https://docs.python.org">https://docs.python.org</a></p></li>
            <li><p>Avoid information overload</p></li>
            <li><p>Use examples and interaction rather than documents and slides</p></li>
          </ul>
        </section>

        <section>
          <h2>Preliminary plan</h2>
          </br>
          <ul>
            <li><p><b>Basics: About the language, the Python eco-system, types, modules, functions, scopes, decorators, string formatting</b></p></li>
            <li><p>Object-oriented programming in Python: How Python "really works"</p></li>
            <li><p>Control flow: if / for / while / try, iterators, "tactical programming" tips</p></li>
            <li><p>A brief tour through Python's standard library</p></li>
            <li><p>Code design and best practices: How to design your code</p></li>
          </ul>
        </section>

        <section>
          <h2>What is Python?</h2>
          </br>
          <ul>
            <li><p>Python is an interpreted high-level general-purpose programming language - advanced through the Python Enhancement Proposal (PEP) process</p></li>
            <li><p>CPython is the reference implementation of Python, written in C (alternatives: pypy, jython)</p></li>
            <li><p>python - interpreter and interpreter shell (alternatives: ipython, bpython)</p></li>
            <li><p>libpython</p></li>
            <li><p>Calling C from Python: Cython, CFFI, ctypes</p></li>
          </ul>
        </section>

        <section>
          <h2>Philosophy</h2>
          </br>
          <pre data-id="code-animation">
            <code class="bash" data-trim type="text/template">
              $ python
            </code>
          </pre>
          <pre data-id="code-animation">
            <code class="python" data-trim type="text/template">
              import this

              # The Zen of Python, by Tim Peters

              # Beautiful is better than ugly.
              # Explicit is better than implicit.
              # Simple is better than complex.
              # Complex is better than complicated.
              # Flat is better than nested.
              # Sparse is better than dense.
              # Readability counts.
              # Special cases aren't special enough to break the rules.
              # Although practicality beats purity.
              # Errors should never pass silently.
              # Unless explicitly silenced.
              # In the face of ambiguity, refuse the temptation to guess.
              # There should be one-- and preferably only one --obvious way to do it.
              # Although that way may not be obvious at first unless you're Dutch.
              # Now is better than never.
              # Although never is often better than *right* now.
              # If the implementation is hard to explain, it's a bad idea.
              # If the implementation is easy to explain, it may be a good idea.
              # Namespaces are one honking great idea -- let's do more of those!
            </code>
          </pre>
        </section>

        <section>
          <h2>Built-in functions</h2>
          </br>
          <p>Few built-in functions.</p>
          <p><a href="https://docs.python.org/3/library/functions.html">https://docs.python.org/3/library/functions.html</a></p>
          <ul>
            <li><em>dir([obj])</em> - returns a list of valid attributes for that object</li>
            <li><em>id(obj)</em> - returns the "identity" of an object - an integer which is guaranteed to be unique</li>
            <li><em>print(...)</em> - prints objects to a text stream</li>
            <li><em>str(...)</em> - returns a string version of object</li>
            <li><em>type(obj)</em> - returns the type of an object</li>
          </ul>
        </section>

        <section>
          <h2>Common built-in types</h2>
          </br>
          <p>Python uses <a href="https://en.wikipedia.org/wiki/Duck_typing"><em>duck typing</em></a> and has typed objects but untyped variable names.</p>
          <p>Type constraints are not checked at compile time; rather, operations on an object may fail, signifying that the given object is not of a suitable type. Despite being dynamically-typed, Python is strongly-typed, forbidding operations that are not well-defined (for example, adding a number to a string) rather than silently attempting to make sense of them.</p>
          <pre data-id="code-animation">
            <code class="python" data-trim type="text/template">
              s = 'foo'  # this is a string / str, same as str('foo'), may be encoded, immutable
              b = b'foo'  # bytes, same as bytes('foo', 'utf-8'), may be decoded, immutable
              i = 6  # int, same as int('6'), immutable
              f = 0.1  # float, same as float('0.1'), immutable
              b = False  # bool, same as bool(0), bool(''), bool(None)... immutable / constant
              n = None  # NoneType, similar to 'null' in other languages, immutable / constant
              l = [1, False, 'foo']  # list, same as list((1, False, 'foo'))
              t = (1, False, 'foo')  # tuple, same as tuple([1, False, 'foo']), immutable
              d = {'foo': 1, 'bar': 8}  # dict, same as dict(foo=1, bar=8), similar to hash in other languages
              s = {'foo', 'bar', 1, 1, 4}  # set, same as set(['foo', 'bar', 1, 1, 4]), removes duplicates
            </code>
          </pre>
          <p>Classes, functions, modules, .... and even types are simply other types :)</p>
        </section>

        <section>
          <h2>Modules</h2>
          </br>
          <p>A module is a file containing Python definitions and statements. The file name is the module name with the suffix <em>.py</em> appended. Within a module, the module's name (as a string) is available as the value of the global variable <em>__name__</em></p>
          <p>When a module named <em>foo</em> is imported, the interpreter first searches for a built-in module with that name (<em>sys.builtin_module_names</em>). If not found, it then searches for a file named <em>foo.py</em> in a list of directories given by the variable <em>sys.path</em>. <em>sys.path</em> is initialized from these locations:</p>
          <ul>
            <li>the directory containing the input script (or the current directory when no file is specified)</li>
            <li><em>PYTHONPATH</em> - env. variable - a list of directory names</li>
            <li>the installation-dependent default locations</li>
          </ul>

          <p>The module is then imported only once and "cached" in <em>sys.modules</em></p>
        </section>

        <section>
          <h2>Packages</h2>
          </br>
          <p><a href="https://docs.python.org/3/tutorial/modules.html#packages">Packages</a> are a way of structuring Python's module namespace by using "dotted module names"</p>
          <p>The import statement combines two operations:</p>
          <ul>
            <li>it searches for the named module</li>
            <li>it binds the results of that search to a name in the <em>local scope</em></li>
          </ul>
          <pre data-id="code-animation">
            <code class="python" data-trim type="text/template">
              # bar.py then bar/__init__.py will be considered, the first match executed and bound to 'bar'
              import bar

              import mymodule.foo  # implicitly executes mymodule.py, mymodule/__init__.py and mymodule/foo/__init__.py

              import numpy as np  # will be bound as 'np' instead of 'numpy'. N.B. __name__ is still 'numpy'

              import some.extremely.deep.path.Animal as Animal  # "sacrifice" the namespace in the name of convinience

              from sys import path  # execute sys and only import the 'path' attribute into local scope as 'path'

              # relative imports must be explicit in Python 3
              from .othermodule import something  # expects that current module and 'othermodule' are in the same
                                                  # package (containing __init__.py)

              from sys import *  # NO! Bad programming practice since 1879
            </code>
          </pre>
        </section>

        <section>
          <h2>Creating and maintaining a Python environment</h2>
          </br>
          <p>Python's official package repository is <a href="https://pypi.org"><em>PyPi (https://pypi.org)</em></a>, while Python's official package installer is <a href="https://pypi.org/project/pip/"><em>pip (https://pypi.org/project/pip/)</em></a></p>
          <p>A Python environment is the physical and logical arrangement of Python modules and packages. Several options exist:</p>
          <ul>
            <li>using a proper operating system :) (symlinks, real commercial support etc.)</li>
            <li>using <em>venv</em></li>
            <li>using higher level tools like <em>poetry</em></li>
            <li>using a mixture / cocktail of all of the above :)</li>
          </ul>
        </section>

        <section>
          <h2>Creating and maintaining a Python environment (cont...)</h2>
          </br>
          <p>Desired qualities for a flexible Python environment:</p>
          <ul>
            <li>easy to create and (un)load</li>
            <li>do not require extra privileges</li>
            <li>don't repeat yourself (DRY)</li>
            <li>easy to update without breaking the API</li>
            <li>easy to debug</li>
            <li>play nicely with the VCS (git)</li>
          </ul>          
        </section>

        <section>
          <h3>Creating and maintaining a Python environment (cont...)</h3>
          </br>
          <p>Exploting the operating system can be done by:</p>
          <ul>
            <li>(re)defining <em>PYTHONPATH</em></li>
            <li>using symlinks to point at packages placed at different locations</li>
        </section>

        <section>
          <h3>Creating and maintaining a Python environment (cont...)</h3>
          </br>
          <p>Using <em>venv</em> can be done by directly invoking <em>python</em>:</p>
          <pre data-id="code-animation">
            <code class="bash" data-trim type="text/template">
              # create a virtual environment
              python -m venv my_virtual_env
              python -m venv --system-site-packages my_virtual_env

              # load, use and unload the virtual environment
              source my_virtual_env/bin/activate
              pip install sqlalchemy
              # install package from a custom repository (https://artifactory.fifty.eu)
              pip install --index-url=https://artifactory.fifty.eu/artifactory/api/pypi/pypi/simple/ odin-data-access
              deactivate

              # one can alternatively use the python "wrapper" of the virtual env
              my_virtual_env/bin/python -m pip install sqlalchemy
            </code>
          </pre>
          <p><em>--system-site-packages</em> will keep the original <em>site-packages</em> folders at the end of <em>sys.path</em></p>
        </section>

        <section>
          <h3>Creating and maintaining a Python environment (cont...)</h3>
          </br>
          <p><em>Poetry</em> (<a href="https://python-poetry.org"><em>https://python-poetry.org</em></a>) is the prefered environment and dependency management tool at Statnett.</p>
          <pre data-id="code-animation">
            <code class="bash" data-trim type="text/template">
              # create project and a virtual environment from scratch
              poetry new my-project

              # ... or use Poetry with an existing one
              cd my-project
              poetry init

              # edit pyproject.toml for your needs (f.i. add dependencies, metadata ... etc),
              # create virtual environment and install dependencies
              poetry install
              # finally commit your poetry.lock file to version control

              # update all dependencies
              poetry update
            </code>
          </pre>
          <p>For more info: <a href="https://python-poetry.org/docs/basic-usage/"><em>https://python-poetry.org/docs/basic-usage/</em></a></p>
        </section>

        <section>
          <h2>Mutables vs. immutables</h2>
          <p>Immutable object is an object with a fixed value. Immutable objects include <em>bool, int, float, str, bytes</em> and <em>tuples</em>. Such an object cannot be altered. A new object has to be created if a different value has to be stored. They play an important role in places where a constant hash value is needed, for example as a key in a dictionary.</p>
          <p>All objects that are not immutable are... mutable. All <em>hashable objects</em> <b>should</b> be immutable or use <em>id()</em>.</p>
          <pre data-id="code-animation">
            <code class="python" data-trim type="text/template">
              i = 1
              id(i)  # returns f.i. 9788992
              i += 1  # same as i = i + 1
              id(i)  # returns a different value, hence - a brand new object

              s = 'Hello'
              s += ' World'  # s is now a different object
              s[0]  # 'H'
              s[0] = 'h'  # TypeError: 'str' object does not support item assignment

              t = (1, 4)  # tuple
              l = [1, 4]  # list
              hash(t)  # returns f.i. -6333845781340707986
              hash(l)  # TypeError: unhashable type: 'list'

              s = 'the long and winding road'
              s2 = 'the long and winding road'

              # check if s and s2 are the same object:
              id(s)  # Out: 139858905258704
              id(s2)  # Out: 139858926037472

              # the hash should be the same
              hash(s)  # Out: 7030216208569256362
              hash(s2)  # Out: 7030216208569256362
            </code>
          </pre>
        </section>

        <section>
          <h2>Functions</h2>
          </br>
          <p>A function is a sequence of program instructions that performs a specific task, packaged as a unit.</p>
          <p>Functions let you:</p>
          <ul>
            <li>reuse code across several programs / projects</li>
            <li>minimize code duplication</li>
            <li>devide larger programming tasks</li>
            <li>hide implementation details</li>
            <li>improve readability</li>
            <li>improve traceability</li>
          </ul>
          <p>Function calls bring some overhead pushing / popping function-data into / from stack.</p>
          <p>Important definitions (may have different meanings in different programming languages):</p>
          <ul>
            <li><em>parameter / formal parameter</em> - variable / data provided as input to the function</li>
            <li><em>argument / actual parameter</em> - local variable / data to the given function</li>
          </ul>
          <p>The keyword <em>def</em> introduces a function definition.</p>
        </section>

        <section>
          <h2>Functions (cont...)</h2>
          </br>
          <pre data-id="code-animation">
            <code class="python" data-trim type="text/template">
              def add(a, b):  # - function definition / header
                  """Function for adding integers"""  # - docstring
                  result = a + b
                  a = 5
                  return result  # - function that does not contain return, implicitly returns None

              a_param = 9
              b_param = -2
              add(a_param, b_param)  # Out: 7

              # integers are immutable and a_param will remain unchanged
              print(a_param)  # Out: 9


              def addl(a, b):
                  """Function for adding two lists"""
                  result = a + b
                  a += [5]  # in this case equal to: a.append(5)
                  return result

              a_param = [9]
              b_param = [-2]
              addl(a_param, b_param)  # Out: [9, -2]
              # lists are mutable and a_param will be changed
              print(a_param)  # Out: [9, 5]
            </code>
          </pre>
        </section>

        <section>
          <h2>Functions (cont ...)</h2>
          </br>
          <h3>Parameters and arguments</h3>
          <pre data-id="code-animation">
            <code class="python" data-trim type="text/template">
              def add(a, b):
                  """Function for adding integers"""
                  return a + b
              my_result = add(2, 5)  # positional arguments (parameters)
              my_result = add(b=5, a=2)  # keyword arguments (parameters)
              my_tuple = (2, 5)
              my_dict = {'b': 5, 'a': 2}
              my_result = add(*my_tuple)  # unpacked and assigned to the positional arguments
              my_result = add(**my_dict)  # unpacked and assigned to the kw. arguments

              def add(a, b=5):
                  """Function for adding integers"""
                  return a + b
              my_result = add(2)
              # ... and the rest of the examples above will work

              def add(a, *args, **kwargs):
                  """Function for adding integers"""
                  if args:
                      b = args[0]
                  elif 'b' in kwargs:
                      b = kwargs['b']
                  return a + b
              my_result = add(2, 5, 9, 11)  # 5 assigned to args[0]
              my_result = add(2, b=5, c=9, d=11)  # 5 assigned to kwargs['b']
              my_result = add(2, b=5, 9, 11)  # SyntaxError: positional argument follows keyword argument
            </code>
          </pre>
        </section>

        <section>
          <h2>Functions (cont ...)</h2>
          </br>
          <h3>Docstrings annotations and other hints</h3>
          <pre data-id="code-animation">
            <code class="python" data-trim type="text/template">
              def decrypt(password: str, edata: str) -> str:
                  """
                  Decrypts `edata` using `password`.

                  `edata` is in the following format:
                  enc-val$`version-num`$`bas64-salt`$`base64-encrypted_data`

                  :param password: The password to generate the key with
                  :type password: str

                  :param edata: The data to be decrypted
                  :type edata: str

                  :raises EtoolkitInstanceError: If the encryption format is unsupported

                  :return: The output string / decrypted data
                  :rtype: str
                  """
                  if not edata.startswith('enc-val$1$'):
                      raise EtoolkitInstanceError('Unsupported encryption format')
                  # some more code magic coming after....
                  # ...
                  # ..
                  return decrypted_str
            </code>
          </pre>
        </section>

        <section>
          <h2>Functions (cont ...)</h2>
          </br>
          <h3>Using <em>typing</em> for more advanced / flexible hinting</h3>
          <pre data-id="code-animation">
            <code class="python" data-trim type="text/template">
              import typing

              Basestring = typing.Union[str, bytes]

              def decrypt(password: Basestring, edata: str) -> str:
                  pass


              from typing import Union

              def decrypt(password: Union[str, bytes], edata: str) -> str:
                  """Generic documentation. No need for pass"""

              # Python >= 3.10 only
              def decrypt(password: str | bytes, edata: str) -> str:
                  """Generic documentation. No need for pass"""
            </code>
          </pre>
        </section>

        <section>
          <h2>Functions (cont ...)</h2>
          <h4>Functions as parameters / arguments, lambdas and returning multiple values</h4>
          <pre data-id="code-animation">
            <code class="python" data-trim type="text/template">
              def fetch_the_first_letter(input_str: str) -> str:
                  """Fetches the first letter of the string input_str or 'x'""""
                  try:
                      return input_str[0]
                  except Exception:
                      return 'x'
              letter_list = list(map(fetch_the_first_letter, ['foo', 'bar', 'test']))  # Out: ['f', 'b', 't']
            </code>
          </pre>
          <p>Small anonymous functions can be created with the <em>lambda</em> keyword</p>
          <pre data-id="code-animation">
            <code class="python" data-trim type="text/template">
              letter_list = list(map(lambda x: x[0], ['foo', 'bar', 'test']))  # Out: ['f', 'b', 't']
            </code>
          </pre>
          <p>Functions in Python are <em>callable objects</em>. Callable objects can be created by defining the <em>__call__</em> method. More on that later in the course...</p>
          <p>A function can return multiple values by implicitly returning a tuple:</p>
          <pre data-id="code-animation">
            <code class="python" data-trim type="text/template">
              def square_cube(x):
                  """returns x, x^2 and x^3"""
                  return x, x**2, x**3
              numbers = square_cube(5)  # Out: (5, 25, 125)
              num, sqnum, cbnum = square_cube(5)  # unpacking the tuple
            </code>
          </pre>
        </section>

        <section>
          <h2>Functions (cont ...)</h2>
          </br>
          <h3>Enclosing and nested functions</h3>
          <p>Can be used as:</p>
          <ul>
            <li>regular functions within functions</li>
            <li>dynamic function factories</li>
          </ul>
          <pre data-id="code-animation">
            <code class="python" data-trim type="text/template">
              def get_multiplier_of(base: int) -> str:
                  """the function enclosing its nested functions"""
    
                  def multiplier_function(x):
                      """a nested function"""
                      return base * x

                  return multiplier_function

              times3 = get_multiplier_of(3)
              times5 = get_multiplier_of(5)
              print(times3(3))  # Out: 9
              print(times5(3))  # Out: 15
            </code>
          </pre>
        </section>

        <section>
          <h2>Scopes in Python</h2>
          </br>
          <ul>
            <li><em>local</em> - assigned names are local unless declared <em>global</em></li>
            <li><em>enclosed</em> - the scope of the variable inside a function with a nested function</li>
            <li><em>global - global for the current module</em></li>
            <li><em>built-in</em></li>
          </ul>
          <p><em>locals()</em> and <em>globals()</em> return dicts of symbols for their respective scopes</p>
          <pre data-id="code-animation">
            <code class="python" data-trim type="text/template">
              num1, num2 = 7, 8  # module globals

              def print_numbers():
                  print(num1, num2)  # OK, these are module globals
                  num3 = 100
                  print(num3)  # prints 100, num3 is in the function (local) scope
                  global num4  # assignes / references num4 to / in the global scope
                  num4 = 99
                  id = 200  # new symbol in local scope
                  # id(num4)  # will not yield the expected result (raises TypeError)

                  def print_numbers2():
                      print(num3)  # OK, enclosed scope

                  print_numbers2()  # prints 100

              print_numbers()
              # print(num3)  # Raises NameError - why?
              print(num4)  # Prints 99 - why?
              # print_numbers2()  # Raises NameError
            </code>
          </pre>
        </section>

        <section>
          <h2>Decorators</h2>
          <p>Decorators can be used to modify the behavior of the objects they decorate. Decorators can be implemented either by using classes or by using nested functions.</p>
          <pre data-id="code-animation">
            <code class="python" data-trim type="text/template">
              def my_decorator(func):

                  def decorated():
                      print('Doing something before the decorated function')
                      retval = func()
                      print('Doing something after the decorated function')
                      return retval
                  return decorated

              def my_function():
                  print('Alice')

              my_function = my_decorator(my_function)
              my_function()
            </code>
          </pre>
          <p>... may be dificult to read / understand, while:</p>
          <pre data-id="code-animation">
            <code class="python" data-trim type="text/template">
              @my_decorator
              def my_function():
                  print('Alice')

              my_function()
            </code>
          </pre>
          <p>... may be easier</p>
        </section>

        <section>
          <h2>Decorators (cont ...)</h2>
          </br>
          <h3>A complete example</h3>
          <pre data-id="code-animation">
            <code class="python" data-trim type="text/template">
              import sys
              from functools import wraps

              def requires_access(access_secret: str):

                  def api_access_decorator(f):

                      @wraps(f)
                      def decorated(*args, **kwargs):
                          if 'secret' not in kwargs:
                              sys.exit('No secret provided')
                          if not kwargs['secret'] or kwargs['secret'] != access_secret:
                              sys.exit("Secret doesn't match")
                          # return f(args[0], **kwargs)
                          return f(*args, **kwargs)

                      return decorated

                  return api_access_decorator


              @requires_access(access_secret='b28cfeaa65b73cf')
              def sensitive_function(data, **kwargs):
                  """very sensitive function"""
                  db.save(data)
            </code>
          </pre>
        </section>

        <section>
          <h2>String formatting</h2>
          </br>
          <h3>The old ways...</h3>
          <pre data-id="code-animation">
            <code class="python" data-trim type="text/template">
              f = 6.57865
              i = 27
              s = 'another string'


              '%s - %d - %5.2f' % (s, i, f)  # Out: 'another string - 27 -  6.58'

              # still used in:
              logger.debug("%d - %s", event.id, message)


              '{} - {} - {:5.2f}'.format(s, i, f)  # implicit
              '{0} - {1} - {2:5.2f}'.format(s, i, f)  # explicit
              '{my_str} - {i} - {fl:5.2f}'.format(my_str=s, fl=f, i=i)  # keyword
              # Out: 'another string - 27 -  6.58'


              # modern Python >= 3.6 f-strings
              f'{s} - {i} - {f:5.2f}'  # Out: 'another string - 27 -  6.58'
            </code>
          </pre>
          <p>See <a href="https://docs.python.org/3/library/string.html#formatspec">https://docs.python.org/3/library/string.html#formatspec</a> for the complete format specification</p>
        </section>

      </div>
    </div>
    
	<script src="dist/reveal.js"></script>
	<script src="plugin/zoom/zoom.js"></script>
	<script src="plugin/notes/notes.js"></script>
	<script src="plugin/search/search.js"></script>
	<script src="plugin/markdown/markdown.js"></script>
	<script src="plugin/highlight/highlight.js"></script>
	<script>

	  // Also available as an ES module, see:
	  // https://revealjs.netlify.app/initialization/
	  Reveal.initialize({
		controls: true,
		progress: true,
		center: true,
		hash: true,

		// Learn about plugins: https://revealjs.netlify.app/plugins/
		plugins: [ RevealZoom, RevealNotes, RevealSearch, RevealMarkdown, RevealHighlight ]
	  });
      Reveal.configure({ pdfSeparateFragments: false });

	</script>

  </body>
</html>