Python Basic and Advanced (19.10.2020 - 22.10.2020)

Agenda

This is a preliminary list of very basic topics. The advanced part of the training is under discussion. Please send feedback/suggestions to training@faschingbauer.co.at.

  • Variables and Types

    • Numerical types such as integer, floating point, boolean

    • Sequential types such as list, tuple, string

    • Indexing, slicing and other operations on sequential types

    • Associative types such as dictionaries and sets

    • Mutable versus immutable

  • Control Flow

    • if

    • while

    • for: more about iteration

  • Functions

    • Why and how

    • Parameters passing: positional versus keyword parameters, default parameters

    • Return values

  • Exception Handling

    • The exception hierarchy in the standard library

    • How to define custom exception classes

    • Raising exceptions, and reacting on them

  • Strings — Advanced Features

    • String methods

    • Formatting

    • Encoding: what it is, and how Python solves encoding problems

  • File I/O

    • Opening and/or creating files

    • Reading and writing

  • Advanced Topics

    • Database Programming

    • Web Programming with Flask

Additional Topics, brought in by Thomas

  • Programmverweise mit import XY und Aufruf von XY.ZAB

  • JSON Handling

  • Exception Handling

  • with open(eventCodeFile, ‘r’) as f:

    definedLISAEvents = json.load(f)[‘event_ids’]

  • print (f’bla{variable}blubb’} : was macht das f hier? (habe gesehen, eine verbesserte Stringformatierung in Python3?)

  • CTYPES

  • Klassen und deren Aufruf im .py Code

  • Headerfiles (.h) verwenden

  • from lib import *

  • .lst Files (Listen?)

  • Socket module

  • Shell script inclusion

  • Code Snippet: “@cs.CALLBACKFUNC” was macht das?

Outcome

Day 2

  • Observation by Thomas: “as opposed to C# I need to define a function before I can use it”

    Longish answer …

    • Variables are names that refer to objects

    • def is only a statement: creates a function object, and points a name (the function name to it)

    • Cannot use a name that is not there

    • Show how to delete (and resurrect) the int type

  • Exercises: Lists, Loops, Functions

    • Exercise 2: uniq() in steps:

      1. Complicated iteration over outputsequence, only to determine membership. First with a flag, then with an else clause.

      2. in operator on outputsequence

      3. Performance: using a set for have-ness

      End result: uniq.py

      Generator version (can’t resist): uniq-generator.py

    • Exercise 3: join(). Sketch multiple ways to solve the problem.

      1. join-manual-index.py. Iterate over the input list, manually tracking indexes to determine if we are at the last element.

      2. join-manual-index-range.py. Iterate over a range object for index keeping, and use index based iteration on the input list.

      3. join-slicing.py. Another cumbersome but working approach: cut out [1:] from the input list.

      4. join-correct-sep.py. Add separator unconditionally after each element, and take correcting action finally.

      5. join-enumerate.py: use enumerate() to iterate over the input sequence and keep the indexes.

  • for loops

  • Generators: Iteration and Generators

    1. fibo-inline.py. Generate Fibonacci numbers in an infinite loop, and print them as we go. This is not modular (we’re not using a function), but it allows for infinity.

    2. fibo-function.py. Externalize Fibonacci generation into a function. This is modular, but we have to limit the amount of numbers generated (or we will run out of memory).

    3. fibo-generator.py. Using a function (err, a generator), and have infinity.

Day 3

  • Comprehension expressions

  • File IO

    • File I/O

    • The with statement: context managers

  • Group exercise: discussion

    A CAN bus log is gathered by software running on a device. That log contains CAN frames, basically. It has CSV format, suitable for analysis with Excel. One wants to automate the process of log analysis, at least to a certain extent.

    • Start with the csv module: read the log file, print the content of each row. Iteration, basically, and some tuple unpacking.

    • Sideway: encoding. The logfile is in cp1252 format.

Day 4