Python Basic and Advanced (19.10.2020 - 22.10.2020)#
Preparation#
Workspace Setup#
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
ifwhilefor: 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 XYund Aufruf vonXY.ZABJSON 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
.pyCodeHeaderfiles (
.h) verwendenfrom lib import *.lstFiles (Listen?)Socket module
Shell script inclusion
Code Snippet:
@cs.CALLBACKFUNCwas macht das?
Outcome#
Notebook#
Links
Download notebook:
Notebook.ipynb
Day 1#
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
defis 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
inttype
-
Complicated iteration over outputsequence, only to determine membership. First with a flag, then with an
elseclause.inoperator on outputsequencePerformance: using a
setfor have-ness
End result: uniq.py
Generator version (can’t resist): uniq-generator.py
-
Sketch multiple ways to solve the problem.
join-manual-index.py. Iterate over the input list, manually tracking indexes to determine if we are at the last element.
join-manual-index-range.py. Iterate over a
rangeobject for index keeping, and use index based iteration on the input list.join-slicing.py. Another cumbersome but working approach: cut out
[1:]from the input list.join-correct-sep.py. Add separator unconditionally after each element, and take correcting action finally.
join-enumerate.py: use enumerate() to iterate over the input sequence and keep the indexes.
forloopsThe range Function, and the iterator protocol
Generators: Iteration, Generators, And yield
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.
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).
fibo-generator.py. Using a function (err, a generator), and have infinity.
Day 3#
Comprehension expressions
List comprehension, from More About Lists. Code: list-comprehension.py.
Generator expression. Code: generator-expression.py.
Dictionary comprehension. Code: dictionary-comprehension.py.
File IO
The
withstatement: 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
csvmodule: read the log file, print the content of each row. Iteration, basically, and some tuple unpacking.Sideway: encoding. The logfile is in
cp1252format.
Day 4#
Class
Messageto hold rows (includedatetime.datetimeconversion). Code: csvlog/unified/message.py.Function to read CSV into a list of
Messageobjects. Code: csvlog/unified/csv_Reader.pyOne main program that prints CAN messages. Code: csvlog/unified/print-log.py
Cut out a time interval from a larger list
Show how CAN bus programming is done in Linux and Python, using the
socketmodule.Excerpt from my Grazer Linuxtage 2014 talk: “CAN-Bus mit Linux und Python” (
PDF, Youtube Video).Code: csvlog/joerg/can-recv.py
Links#
Tutorials#
Transforming Code into Beautiful, Idiomatic Python. Raymond Hettinger, emphasizing on his favorite phrase “There must be a better way”. (Hettinger is a “Python Core Developer”.)
Modern dictionaries: Raymond Hettinger again, this time emphasizing on dictionaries, even more than I did. (Hehe, I just discovered that he’s bringing my quick hash table explanation to a conclusive end. Hard stuff for the unaware though.)
Unit Testing: Corey Schafer (he has a number of really good and short tutorial videos; look out for him as you search).
Generators Tutorial: Corey Schafer again, this time 11 minutes on generators. (I couldn’t resist.)
CAN Bus#
Grazer Linuxtage 2014: “CAN-Bus mit Linux und Python”