Python Advanced (2021-05-25 - 2021-05-27)¶
Notebook¶
- Das
with
Statement import zipfile
- Classes
- Functions, Positional and Keyword Arguments
- The
range()
Function - Functional Programming, Iteration,
yield
,map()
,filter()
, … - Tuple Unpacking and the Rest
- Decorators, etc.
NoneType
andNone
- File I/O
- Iteration,
yield
, Recursion OSError
,errno
- The
platform
Module,sys.path
os.path
- Exceptions und so (
assert()
) - Random Questions
Topics¶
jjjj
https://docs.python.org/3/library/exceptions.html
Predefined streams¶
import sys
line_count = 0
for line in sys.stdin:
line_count += 1
print(line_count, file=sys.stdout)
print('fertig, und das hier kommt aud stderr', file=sys.stderr)
PCAP Exam Syllabus¶
(Taken from here)
Exam block #1: Modules and Packages (12%)
Objectives covered by the block (6 items)
import variants; advanced qualifiying for nested modules
Comment:
os.path
dir(); sys.path variable
math: ceil(), floor(), trunc(), factorial(), hypot(), sqrt(); random: random(), seed(), choice(), sample()
platform: platform(), machine(), processor(), system(), version(), python_implementation(), python_version_tuple()
idea, __pycache__, __name__, public variables, __init__.py
searching for modules/packages; nested packages vs directory tree
Exam block #2: Exceptions (14%)
Objectives covered by the block (5 items)
except, except:-except; except:-else:, except (e1,e2)
the hierarchy of exceptions
raise, raise ex, assert
event classes, except E as e, arg property
self-defined exceptions, defining and using
Exam block #3: Strings (18%)
Objectives covered by the block (8 items)
ASCII, UNICODE, UTF-8, codepoints, escape sequences
ord(), chr(), literals
indexing, slicing, immutability
iterating through,
concatenating, multiplying, comparing (against strings and numbers)
in, not in
.isxxx(), .join(), .split()
.sort(), sorted(), .index(), .find(), .rfind()
Exam block #4: Object-Oriented Programming (34%)
Objectives covered by the block (12 items)
ideas: class, object, property, method, encapsulation, inheritance, grammar vs class, superclass, subclass
instance vs class variables: declaring, initializing
__dict__ property (objects vs classes)
private components (instance vs classes), name mangling
methods: declaring, using, self parameter
instrospection: hasattr() (objects vs classes), __name__, __module__, __bases__ properties
inheritance: single, multiple, isinstance(), overriding, not is and is operators
constructors: declaring and invoking
polymorphism
__name__, __module__, __bases__ properties, __str__() method
multiple inheritance, diamonds
Exam block #5: Miscellaneous (List Comprehensions, Lambdas, Closures, and I/O Operations) (22%)
Objectives covered by the block (9 items)
list comprehension: if operator, using list comprehensions
lambdas: defining and using lambdas, self-defined functions taking lambda as as arguments; map(), filter();
closures: meaning, defining, and using closures
I/O Operations: I/O modes, predefined streams, handles; text/binary modes
open(), errno and its values; close()
.read(), .write(), .readline(); readlines() (along with bytearray())
2021-06-09¶
#!/usr/bin/python
import io
import sys
from collections import defaultdict
def make_histogram(fileobj):
histogram = defaultdict(int)
while True:
c = fileobj.read(1)
if len(c) == 0:
break
# VERSION 1: not pythonic
# if c in histogram:
# histogram[c] += 1
# else:
# histogram[c] = 1
# VERSION 2: a little more pythonic
# count = histogram.setdefault(c, 0)
# histogram[c] += 1
# VERSION 3: most pythonic
histogram[c] += 1
return histogram
# f = io.StringIO('Jörg')
f = open(sys.argv[1], encoding='iso-8859-1')
for k, v in make_histogram(f).items():
print(f'{k} -> {v*"-"}')
Further Information¶
Python Tutorial: OS Module - Use Underlying Operating System Functionality, by Corey Schafer
Transforming Code into Beautiful, Idiomatic Python, by Raymond Hettinger
Python Tutorial: Unit Testing Your Code with the unittest Module, by Corey Schafer
Built in Super Heroes. David Beazley in an entertaining keynote to the “PyData Chicago 2016” conference. He has a number of very good and entertaining (and very advanced) videos. You have to spend an entire evening with him though.
The Fun of Reinvention - David Beazley - Pycon Israel 2017. David Beazley with an entertaining keynote about typing, type annotations, metaprogramming, contracts.
Iterations of Evolution: The Unauthorized Biography of the For-Loop. David Beazley giving lessons in history. How
for
evolved from the beginning until where we are today.Keynote - David Beazley. David Beazley live coding a Web Assembly interpreter. PyCon India 2019.
Raymond Hettinger - Super considered super! - PyCon 2015. Raymond Hettinger, revolving around
super()
and MRO.