Python Advanced (7.9.2020 - 10.9.2020)

This is a training for a team which is already experienced in Python programming. There were some special requirements for the training, such as to loose a couple of words about AI/machinelearning and other topics, as well as a fuzzy “bring us advanced stuff”.

Preparation

Github for Exercises

Please create Github accounts and send them to training@faschingbauer.co.at. We will do exercises via a private repository on Github. (See the privacy statement.)

Agenda

Advanced Core Python Topics

Python is easy; one can solve nontrivial problems in only a few lines of code, in no time. This does not mean that you have to fully understand the language - which is good because this is what makes the language easy.

On the other hand, there’s always a line to cross where you wish you knew more. Here the more advanced core Python topics that shall be covered.

Finishing

Following is a recap of what has happened. File names are relative to the root of the Github repository we were working from.

Day 1

Rushed through basics,

Put a strong focus on Python features,

Day 2

  • Livehacking: Iteration and Generation, covering

    • List comprehensions

    • The range function

    • Writing generators using yield

    • Generator expressions

  • Livehacking OO

    • joerg-livehacking/oo.py

      • initially: Thermometer, ctor, get_temperature()

      • __init__, self, etc. (self can also read this)

      • th.__dict__

      • private, pros and cons

      • properties (r/w)

    • inheritance

      • joerg-livehacking/abstract-base-class.py, joerg-livehacking/abstract-base-class.cpp

      • insert one step without abc before it, and then show what abc can do

      • explain “check errors as early as can” ⟶ at ctor time, rather than at method call time.

  • Livehacking design patterns

    • joerg-livehacking/composite.py. Using the thermometer hierarchy, a “composite” thermometer was created. That thermometer uses (has) a set of concrete thermometers to calculate the average room temperature.

    • joerg-livehacking/adapter.py. Fictional scenario …

      • The Unser thermometer framework contains a number of thermometer implementations which all support the get_temperature_celsius() method.

      • A collaboration with a competitor is launched. That competitor has a similar set of thermometer implementations. The difference between Eana and Unser is that Eana thermometers do not support get_temperature_celsius(), but rather only get_temperature_fahrenheit().

      • We employ the adapter pattern and create one special thermometer in the Unser hierarchy

        class EanaAdapter(UnserThermometer):
            ...
        

Day 3

  • Revisit abstract base classes

    • Discuss duck typing.

    • Morph joerg-livehacking/abstract-base-class.py into joerg-livehacking/duck-typing.py.

    • Explaining how abc shifts duck errors from call to initialization

  • Visitor pattern. Not every pattern in the “Gang of Four” book should be considered a real pattern. For example, the Visitor pattern turn into an idiom for languages that don’t support generators.

    • joerg-livehacking/visitor.py. Classic OO implementation of the visitor pattern as a DFS traversal. Together with callbacks and all convolutions. Took sideways like,

      • show how __call__ makes a class callable.

      • show how __str__ and __repr__ work together in print()

    • joerg-livehacking/visitor-generator.py. “I don’t want to implement a visitor!”, poor user says. “I only want to iterate over the tree in DFS order!”

      Implement DFS iteration using yield from which delegates iteration into recursion.

  • TDD and Unit Testing theory; using excerpts from the Design Patterns deck of slides. Explain terminology; fixtures and such.

  • Start hacking on project. Agreed upon myself doing live hacking. Doing TDD.

    • Project/sensor/sensordata.py, and Project/tests/sensordata_tests.py. Prepare TDD; explain suites, cases, fixture, assertions.

    • While writing data classes (holding only attributes and no functionality), explain namedtuple. Use that to implement sensor.sensordata.HistoryData.

    • Slowly fix things, in a test driven way. Discuss, team giving input, all really fine.

Day 4

After Work Party

From my point of view, the training went really fine. Not everyone is equally satisfied with the outcome (we didn’t get to the AI topics, for example), but I have the impression that I brought it over.

As a gift to myself, I had reserved Thursday night at Gmundner Hütter. After the training I went back to Hoisn Wirt (which is where I stayed during the training - really fine), changed clothes, and started to climb the Traunstein via Zierlersteig.

See here for the description and pictures from this extraordinary hike.

Graph

cluster_python Python cluster_python_swdev Software Development cluster_python_basics Basics cluster_python_db Databases cluster_drafts Drafts cluster_drafts_ai Machine Learning, Artificial Intelligence python_python_1010_generators_yield Iteration and Generators python_basics_python_0270_functions Functions python_python_1010_generators_yield->python_basics_python_0270_functions python_basics_python_0200_sequential_types Sequential Datatypes python_python_1010_generators_yield->python_basics_python_0200_sequential_types python_basics_python_0220_for for Loops python_python_1010_generators_yield->python_basics_python_0220_for python_basics_python_0225_range The range Function python_python_1010_generators_yield->python_basics_python_0225_range python_swdev_python_1100_exceptions Exception Handling python_swdev_python_1050_oo Object Oriented Programming python_swdev_python_1100_exceptions->python_swdev_python_1050_oo python_swdev_python_1100_exceptions->python_basics_python_0270_functions python_basics_python_0140_variables Variables python_swdev_python_1100_exceptions->python_basics_python_0140_variables python_swdev_python_1200_modules Modules and Packages python_swdev_python_1200_modules->python_swdev_python_1050_oo python_swdev_python_1200_modules->python_basics_python_0270_functions python_swdev_python_1200_modules->python_basics_python_0140_variables python_basics_python_0450_dictionaries More on Dictionaries python_swdev_python_1050_oo->python_basics_python_0450_dictionaries python_swdev_python_1050_oo->python_basics_python_0270_functions python_basics_python_0450_dictionaries->python_basics_python_0220_for python_basics_python_0150_datatypes_overview_compound Compound Datatypes python_basics_python_0450_dictionaries->python_basics_python_0150_datatypes_overview_compound python_basics_python_0270_functions->python_basics_python_0140_variables python_basics_python_0150_datatypes_overview Datatypes python_basics_python_0270_functions->python_basics_python_0150_datatypes_overview python_basics_python_0200_sequential_types->python_basics_python_0150_datatypes_overview python_basics_python_0220_for->python_basics_python_0200_sequential_types python_basics_python_0150_datatypes_overview->python_basics_python_0140_variables python_basics_python_0150_datatypes_overview_compound->python_basics_python_0150_datatypes_overview python_basics_python_0500_files File I/O python_basics_python_0500_files->python_basics_python_0220_for python_basics_python_0330_strings_encoding Strings and Encoding python_basics_python_0500_files->python_basics_python_0330_strings_encoding python_basics_python_0300_strings More About Strings python_basics_python_0300_strings->python_basics_python_0200_sequential_types python_basics_python_0300_strings->python_basics_python_0150_datatypes_overview python_basics_python_0250_refs_flat_deep_copy References, (Im)mutability python_basics_python_0300_strings->python_basics_python_0250_refs_flat_deep_copy python_basics_python_0225_range->python_basics_python_0200_sequential_types python_basics_python_0225_range->python_basics_python_0220_for python_basics_python_0330_strings_encoding->python_basics_python_0300_strings python_basics_python_0250_refs_flat_deep_copy->python_basics_python_0140_variables python_basics_python_0250_refs_flat_deep_copy->python_basics_python_0150_datatypes_overview python_basics_python_0250_refs_flat_deep_copy->python_basics_python_0150_datatypes_overview_compound python_db_python_5510_dbapi2_sqlite3 DBAPI 2: sqlite3 python_db_python_5500_dbapi2 DBAPI 2 python_db_python_5510_dbapi2_sqlite3->python_db_python_5500_dbapi2 python_db_python_5500_dbapi2->python_python_1010_generators_yield python_db_python_5500_dbapi2->python_swdev_python_1200_modules python_db_python_5500_dbapi2->python_basics_python_0220_for drafts_mysql MySQL Python Binding drafts_mysql->python_db_python_5510_dbapi2_sqlite3 drafts_mysql->python_db_python_5500_dbapi2 drafts_pip Python Package Index drafts_import The import Statement (incomplete) drafts_pip->drafts_import drafts_special_methods Special Methods, Operator Overloading (incomplete) drafts_special_methods->python_swdev_python_1050_oo drafts_starargs Positional and Keyword Arguments drafts_starargs->python_basics_python_0270_functions drafts_closures Closures drafts_closures->python_basics_python_0270_functions drafts_context_manager Context Managers (the with Statement) (incomplete) drafts_context_manager->python_python_1010_generators_yield drafts_context_manager->python_basics_python_0140_variables drafts_decorators Decorators drafts_context_manager->drafts_decorators drafts_exceptions Exception Handling (incomplete) drafts_context_manager->drafts_exceptions drafts_decorators->python_basics_python_0270_functions drafts_decorators->drafts_special_methods drafts_decorators->drafts_starargs drafts_decorators->drafts_closures drafts_duck_typing Duck Typing vs. Strong Typing (incomplete) drafts_duck_typing->python_basics_python_0140_variables drafts_encoding Encoding (incomplete) drafts_encoding->python_basics_python_0500_files drafts_encoding->python_basics_python_0330_strings_encoding drafts_exceptions->python_swdev_python_1100_exceptions drafts_import->python_swdev_python_1200_modules drafts_installation Python Installation drafts_venv Virtual Environments drafts_venv->drafts_pip drafts_venv->drafts_import drafts_ai_k_means K-Means drafts_ai_k_means->drafts_venv drafts_ai_linear_regression Linear Regression drafts_ai_k_means->drafts_ai_linear_regression drafts_ai_machine_learning_intro Machine Learning: Concepts and Terminology drafts_ai_k_means->drafts_ai_machine_learning_intro drafts_ai_linear_regression->drafts_venv drafts_ai_linear_regression->drafts_ai_machine_learning_intro