Leanpub, 2020. — 1069 p.
This version was published on 2020-09-21.Fixtures and Mocking in PythonHow do you test Moon-landing?
How do you test a system …
Plan.
About me.
Goal.
Fixtures.
Fixtuers in Pytest.
Traditional xUnit fixtures.
Dependency Injection.
Temporary directory - tmpdir.
Capture STDOUT and STDERR - capsys.
Home-made fixture.
Home-made fixture - conftest.
Home-made fixture with tempdir.
Home-made fixture with yield.
Fixture Autouse.
Fixture Autouse with yield.
Fixture for MongoDB.
Test Doubles.
Test Doubles explained.
Verify behavior or state?
What is Mocking and Monkey Patching?
Situations.
Unit testing vs. Integration testing.
Experiment with mocking in various situations.
Examples are simple.
Hard coded path.
Manually Patching attribute.
Monkey Patching attribute.
Monkey Patching functions.
Monkey Patching dictionary items.
Mocking a whole class.
Mocking input/output.
Mocking input/output.
Mocking random numbers.
Exercises.
Work in pairs.
Exercise: test login expiration.
Solution: test login expiration.
Exercise: Record e-mail sending.
Solution: Record e-mail sending.
Exercise: Fixture database.
Exercise: One Dimentsional space-fight.
Exercise: web client.
Exercise: Open WeatherMap client.
Exercise: Mocking A Bank.
Testing the whole application.
Resources.
Retrospective.
Job searching help.
Solutions - game.
Solutions - Mocking the database access.
First stepsWhat is Python?
What is needed to write a program?
The source (code) of Python.
Python 2 vs. Python 3.
Installation.
Installation on Linux.
Installation on Apple Mac OSX.
Installation on MS Windows.
Editors, IDEs.
Documentation.
Program types.
Python on the command line.
First script - hello world.
Examples.
Comments.
Variables.
Exercise: Hello world.
What is programming?
What are the programming languages.
A written human language.
A programming language.
Words and punctuation matter!
Literals, Value Types in Python.
Floating point limitation.
Value Types in Numpy.
Rectangle (numerical operations).
Multiply string.
Add numbers.
Add strings.
Exercise: Calculations.
Solution: Calculations.
Second stepsModules.
A main function.
The main function - called.
Indentation.
Conditional main.
Input - Output I/O.
print in Python 2.
print in Python 3.
print in Python 2 as if it was Python 3.
Exception: SyntaxError: Missing parentheses in call.
Prompting for user input in Python 2.
Prompting for user input in Python 3.
Python2 input or raw_input?
Prompting both Python 2 and Python 3.
Add numbers entered by the user (oups).
Add numbers entered by the user (fixed).
How can I check if a string can be converted to a number?
Converting string to int.
Converting float to int.
Conditionals: if.
Conditionals: if - else.
Divide by 0.
Conditionals: if - else (other example).
Conditionals: else if.
Conditionals: elif.
Ternary operator (Conditional Operator).
Case or Switch in Python.
Exercise: Rectangle.
Exercise: Calculator.
Exercise: Age limit.
Exercise: What is this language?
Exercise: Standard Input.
Solution: Area of rectangle.
Solution: Calculator.
Solution: Calculator eval.
Solution: Age limit.
Solution: What is this language?
Command line arguments.
Command line arguments - len.
Command line arguments - exit.
Exercise: Rectangle (argv).
Exercise: Calculator (argv).
Solution: Area of rectangle (argv).
Solution: Calculator (argv).
Solution: Calculator eval.
Compilation vs. Interpretation.
Is Python compiled or interpreted?
Flake8 checking.
Pylint checking.
NumbersNumbers.
Operators for Numbers.
Integer division and the future.
Pseudo Random Number (unform distribution).
Fixed random numbers.
Rolling dice - randrange.
Random choice.
built-in method.
Exception: TypeError: ‘module’ object is not callable.
Fixing the previous code.
Exception: AttributeError: module ‘random’ has no attribute.
Exercise: Number guessing game - level 0.
Exercise: Fruit salad.
Solution: Number guessing game - level 0.
Solution: Fruit salad.
Comparison and Booleanif statement again.
Comparison operators.
Compare numbers, compare strings.
Do NOT Compare different types!
Complex if statement with boolean operators.
Boolean operators.
Boolean truth tables.
Boolean values: True and False.
Using True and False in variables.
Comparison returns True or False.
Assign comparisons to variables.
Flag.
Toggle.
Short circuit.
Short circuit fixed.
Does this value count as True or False?
True and False values in Python.
Incorrect use of conditions.
Exercise: compare numbers.
Exercise: compare strings.
Solution: compare numbers.
Solution: compare strings.
StringsSingle quoted and double quoted strings.
Long lines.
Triple quoted strings (multiline).
String length (len).
String repetition and concatenation.
A character in a string.
String slice (instead of substr).
Change a string.
How to change a string.
String copy.
String functions and methods (len, upper, lower).
index in string.
index in string with range.
rindex in string with range.
find in string.
Find all in the string.
in string.
index if in string.
Encodings: ASCII, Windows-1255, Unicode.
raw strings.
ord.
ord in a file.
chr - number to character.
Exercise: one string in another string.
Exercise: to ASCII CLI.
Exercise: from ASCII CLI.
Solution: one string in another string.
Solution: compare strings.
Solution: to ASCII CLI.
Solution: from ASCII CLI.
LoopsLoops: for-in and while.
for-in loop on strings.
for-in loop on list.
for-in loop on range.
Iterable, iterator.
for in loop with early end using break.
for in loop skipping parts using continue.
for in loop with break and continue.
while loop.
Infinite while loop.
While with complex expression.
While with break.
While True.
Duplicate input call.
Eliminate duplicate input call.
do while loop.
while with many continue calls.
Break out from multi-level loops.
Exit vs return vs break and continue.
Exercise: Print all the locations in a string.
Exercise: Number guessing game.
Exercise: Count unique characters.
Exercise: Convert for-loop to while-loop.
Solution: Print all the locations in a string.
Solution 1 for Number Guessing.
Solution 2 for Number Guessing (x).
Solution 3 for Number Guessing (s).
Solution for Number Guessing (debug).
Solution for Number Guessing (move).
Solution for Number Guessing (multi-game).
Solution: Count unique characters.
Solution: Convert for-loop to while-loop.
Formatted printingformat - sprintf.
Examples using format - indexing.
Examples using format with names.
Format columns.
Examples using format - alignment.
Format - string.
Format characters and types.
Format floating point number.
f-strings (formatted string literals).
printf using old %-syntax.
Format braces, bracket, and parentheses.
Examples using format with attributes of objects.
raw f-strings.
Lists.
Anything can be a list.
Any layout.
Lists.
List slice with steps.
Change a List.
Change with steps.
List assignment and list copy.
Shallow vs. Deep copy of lists.
join.
join list of numbers.
split.
for loop on lists.
in list.
Where is the element in the list?
Index improved.
[]. insert.
[]. append.
[].remove.
Remove the element by the index [].pop.
Remove first element of list.
Remove several elements of list by index.
Use list as a queue.
Queue using deque from collections.
Fixed size queue.
List as a stack.
stack with deque.
Exercies: Queue.
Exercise: Stack.
Exercise: MasterMind.
Solution: Queue with list.
Solution: Queue with deque.
Solution: Reverse Polish calculator (stack) with lists.
Solution: Reverse Polish calculator (stack) with deque.
Solution: MasterMind.
MasterMind to debug.
Debugging Queue.
sort.
sort numbers.
sort mixed.
key sort.
sort with sorted.
sort vs. sorted.
key sort with sorted.
Sorting characters of a string.
range.
Looping over index.
Enumerate lists.
List operators.
List of lists.
List assignment.
List documentation.
tuple.
Sort tuples.
Exercise: color selector menu.
Exercise: count digits.
Exercise: Create list.
Exercise: Count words.
Exercise: Check if number is prime.
Exercise: DNA sequencing.
Solution: menu.
Solution: count digits.
Solution: Create list.
Solution: Count words.
Solution: Check if number is prime.
Solution: DNA sequencing.
Solution: DNA sequencing with filter.
Solution: DNA sequencing with filter and lambda.
[].extend.
append vs. extend.
split and extend.
FilesFile types: Text vs Binary.
Open vs. Read vs. Load.
Binary files: Images.
Reading an Excel file.
Open and read file (easy but not recommended).
Open and read file using with (recommended).
Read file remove newlines.
Filename on the command line.
Filehandle with return.
Read all the lines into a list.
Read all the characters into a string (slurp).
Not existing file.
Open file exception handling.
Open many files - exception handling.
Writing to file.
Append to file.
Binary mode.
Does file exist? Is it a file?
Direct access of a line in a file.
Exercise: count numbers.
Exercise: strip newlines.
Exercise: print lines with Report:
Exercise: color selector.
Exercise: ROT13.
Exercise: Combine lists.
Solution: count numbers.
Solution: strip newlines.
Solution: print lines with Report:
Solution: color selector.
Solution: Combine lists.
Filehandle using with and not using it.
Dictionary (hash)What is a dictionary.
When to use dictionaries.
Dictionary.
keys.
Loop over keys.
Loop over dictionary keys.
Loop using items.
values.
Not existing key.
Get key.
Does the key exist?
Does the value exist?
Delete key.
List of dictionaries.
Shared dictionary.
immutable collection: tuple as dictionary key.
immutable numbers: numbers as dictionary key.
Sort dictionary by value.
Sort dictionary keys by value.
Insertion Order is kept.
Change order of keys in dictionary - OrderedDict.
Set order of keys in dictionary - OrderedDict.
Exercise: count characters.
Exercise: count words.
Exercise: count words from a file.
Exercise: Apache log.
Exercise: Combine lists again.
Exercise: counting DNA bases.
Exercise: Count Amino Acids.
Exercise: List of dictionaries.
Exercise: Dictinoary of dictionaries.
Exercise: Age limit with dictionaries.
Solution: count characters.
Default Dict.
Solution: count characters with default dict.
Solution: count words.
Solution: count words in file.
Solution: Apache log.
Solution: Combine lists again.
Solution: counting DNA bases.
Solution: Count Amino Acids.
Do not change dictionary in loop.
Setssets.
set operations.
Creating a set.
Creating an empty set.
Adding an element to a set (add).
Merging one set into another set (update).
set intersection.
set subset.
set symmetric difference.
set union.
set relative complement.
Functions (subroutines)Why use functions?
Defining simple function.
Passing positional parameters to a function.
Function parameters can be named.
Mixing positional and named parameters.
Default values, optional parameters, optional parameters.
Default value in first param.
Several defaults, using names.
Arbitrary number of arguments *.
Fixed parmeters before the others.
Arbitrary key-value pairs in parameters **.
Extra key-value pairs in parameters.
Every parameter option.
Duplicate declaration of functions (multiple signatures).
Pylint duplicate declaration.
Return more than one value.
Recursive factorial.
Recursive Fibonacci.
Non-recursive Fibonacci.
Unbound recursion.
Variable assignment and change - Immutable.
Variable assignment and change - Mutable.
Parameter passing of functions.
Passing references.
Function documentation.
Sum ARGV.
Copy-paste code.
Copy-paste code fixed.
Copy-paste code further improvement.
Palindrome.
Exercise: statistics.
Exercise: recursive.
Exercise: Tower of Hanoi.
Exercise: Merge and Bubble sort.
Exercise: Refactor previous solutions to use functions.
Solution: statistics.
Solution: recursive.
Solution: Tower of Hanoi.
Solution: Merge and Bubble sort.
ModulesBefore modules.
Create modules.
path to load modules from - The module search path.
sys.path - the module search path.
Flat project directory structure.
Absolute path.
Relative path.
Python modules are compiled.
How “import” and “from” work?
Runtime loading of modules.
Conditional loading of modules.
Duplicate importing of functions.
Script or library.
Script or library - import.
Script or library - from import.
assert to verify values.
mycalc as a self testing module.
doctest.
Scope of import.
Export import.
Export import with all.
import module.
Execute at import time.
Import multiple times.
Exercise: Number guessing.
Exercies: Scripts and modules.
Exercise: Module my_sum.
Exercise: Convert your script to module.
Exercise: Add doctests to your code.
Solution: Module my_sum.
Regular ExpressionsWhat are Regular Expressions (aka. Regexes)?
What are Regular Expressions good for?
Examples.
Where can I use it?
grep.
Regexes first match.
Match numbers.
Capture.
Capture more.
Capture even more.
findall.
findall with capture.
findall with capture more than one.
Any Character.
Match dot.
Character classes.
Common characer classes.
Negated character class.
Optional character.
Regex 0 or more quantifier.
Quantifiers.
Quantifiers limit.
Quantifiers on character classes.
Greedy quantifiers.
Minimal quantifiers.
Anchors.
Anchors on both end.
Match ISBN: numbers.
Matching a section.
Matching a section - minimal.
Matching a section negated character class.
DOTALL S (single line).
MULTILINE M.
Two regex with logical or.
Alternatives.
Grouping and Alternatives.
Internal variables.
More internal variables.
Regex DNA.
Regex IGNORECASE.
Regex VERBOSE X.
Substitution.
findall capture.
Fixing dates.
Duplicate numbers.
Remove spaces.
Replace string in assembly code.
Full example of previous.
Split with regex.
Exercises: Regexes part 1.
Exercise: Regexes part 2.
Exercise: Sort SNMP numbers.
Exercise: parse hours log file and give report.
Exercise: Parse ini file.
Exercise: Replace Python.
Exercise: Extract phone numbers.
Solution: Sort SNMP numbers.
Solution: parse hours log file and give report.
Solution: Processing INI file manually.
Solution: Processing config file.
Solution: Extract phone numbers.
Regular Expressions Cheat sheet.
Fix bad JSON.
Fix very bad JSON.
Raw string or escape.
Remove spaces regex.
Regex Unicode.
Anchors Other example.
PyCharmPyCharm Intro.
PyCharm configurations.
PyCharm Project.
PyCharm Files.
PyCharm - run code.
PyCharm Python console at the bottom left.
Refactoring example with PyCharm.
Python standard modulesSome Standard modules.
sys.
Writing to standard error (stderr).
Current directory (getcwd, pwd, chdir).
OS dir (mkdir, makedirs, remove, rmdir).
python which OS are we running on (os, platform).
Get process ID.
OS path.
Traverse directory tree - list directories recursively.
os.path.join.
Directory listing.
expanduser - handle tilde ~.
Listing specific files using glob.
External command with system.
subprocess.
subprocess in the background.
Accessing the system environment variables from Python.
Set env and run command.
shutil.
time.
sleep in Python.
timer.
Current date and time datetime now.
Converting string to datetime.
datetime arithmeticis.
Rounding datetime object to nearest second.
Signals and Python.
Sending Signal.
Catching Signal.
Catching Ctrl-C on Unix.
Catching Ctrl-C on Unix confirm.
Alarm signal and timeouts.
deep copy list.
deep copy dictionary.
Exercise: Catching Ctrl-C on Unix 2nd time.
Exercise: Signals.
Ctrl-z.
JSONJSON - JavaScript Object Notation.
dumps.
loads.
dump.
load.
Round trip.
Pretty print JSON.
Sort keys in JSON.
Set order of keys in JSON - OrderedDict.
Exercise: Counter in JSON.
Exercise: Phone book.
Exercise: Processes.
Solution: Counter in JSON.
Solution: Phone book.
Command line arguments with argparseModules to handle the command line.
argparse.
Basic usage of argparse.
Positional argument.
Many positional argument.
Convert to integers.
Convert to integer.
Named arguments.
Boolean Flags.
Short names.
Exercise: Command line parameters.
Exercise: argparse positional and named.
argparse print help explicitely.
Argparse xor - mutual exlucise - only one - exactly one.
Exception handlingHierarchy of calls.
Handling errors as return values.
Handling errors as exceptions.
A simple exception.
Working on a list.
Catch ZeroDivisionError exception.
Module to open files and calculate something.
File for exception handling example.
Open files - exception.
Handle divide by zero exception.
Handle files - exception.
Catch all the exceptions and show their type.
List exception types.
Exceptions.
How to raise an exception.
Stack trace.
Exercies: Exception int conversion.
Exercies: Raise Exception.
Solution: Exception int conversion (specific).
Solution: Exception int conversion (all other).
Solution: Raise Exception.
Classes - OOP - Object-Oriented ProgrammingWhy Object-Oriented Programming?
Generic Object-Oriented Programming terms.
OOP in Python.
OOP in Python (numbers, strings, lists).
OOP in Python (argparse).
Create a class.
Import module containing class.
Import class from module.
Initialize a class - constructor, attributes.
Attributes are not special.
Create Point class.
Initialize a class - constructor, attributes.
Methods.
Stringify class.
Inheritance.
Inheritance - another level.
Modes of method inheritance.
Modes of method inheritance - implicit.
Modes of method inheritance - override.
Modes of method inheritance - extend.
Modes of method inheritance - delegate - provide.
Composition - Line.
Some comments.
Class in function.
Serialization of instances with pickle.
Quick Class definition and usage.
Exercise: Add move_rad to based on radians.
Exercise: Improve previous examples.
Exercise: Polygon.
Exercise: Number.
Exercise: Library.
Exercise: Bookexchange.
Exercise: Represent turtle graphics.
Solution - Polygon.
PyPi - Python Package IndexWhat is PyPi?
Easy Install.
pip.
Upgrade pip.
PYTHONPATH.
Virtualenv.
Virtualenv for Python 3.
SQLite Database AccessSQLite.
Connecting to SQLite database.
Create TABLE in SQLite.
INSERT data into SQLite database.
SELECT data from SQLite database.
A counter.
MySQLInstall MySQL support.
Create database user (manually).
Create database (manually).
Create table (manually).
Connect to MySQL.
Connect to MySQL and Handle exception.
Select data.
Select more data.
Select all data fetchall.
Select some data fetchmany.
Select some data WHERE clause.
Select into dictionaries.
Insert data.
Update data.
Delete data.
Exercise MySQL.
Exercise: MySQL Connection.
Solution: MySQL Connection.
PostgreSQLPostgreSQL install.
Python and PostgreSQL.
PostgreSQL connect.
INSERT.
INSERT (from command line).
SELECT.
DELETE.
SQLAlchemySQLAlchemy hierarchy.
SQLAlchemy engine.
SQLAlchemy autocommit.
SQLAlchemy engine CREATE TABLE.
SQLAlchemy engine INSERT.
SQLAlchemy engine SELECT.
SQLAlchemy engine SELECT all.
SQLAlchemy engine SELECT fetchall.
SQLAlchemy engine SELECT aggregate.
SQLAlchemy engine SELECT IN.
SQLAlchemy engine SELECT IN with placeholders.
SQLAlchemy engine connection.
SQLAlchemy engine transaction.
SQLAlchemy engine using context managers.
Exercise: Create table.
SQLAlchemy Metada.
SQLAlchemy types.
SQLAlchemy ORM - Object Relational Mapping.
SQLAlchemy ORM create.
SQLAlchemy ORM schema.
SQLAlchemy ORM reflection.
SQLAlchemy ORM INSERT after automap.
SQLAlchemy ORM INSERT.
SQLAlchemy ORM SELECT.
SQLAlchemy ORM SELECT cross tables.
SQLAlchemy ORM SELECT and INSERT.
SQLAlchemy ORM UPDATE.
SQLAlchemy ORM logging.
Solution: Create table.
Exercise: Inspector.
SQLAlchemy CREATE and DROP.
SQLAlchemy Notes.
SQLAlchemy Meta SQLite CREATE.
SQLAlchemy Meta Reflection.
SQLAlchemy Meta INSERT.
SQLAlchemy Meta SELECT.
And more