Added chapters and list of exercises

This commit is contained in:
Rick van Hattem 2022-04-30 00:45:54 +02:00
commit 1c106d776c
No known key found for this signature in database
GPG Key ID: E81444E9CE1F695D
37 changed files with 124 additions and 0 deletions

View File

@ -0,0 +1,4 @@
Chapter 1 - getting started
=======================================================================================================================
Chapter 1 has no exercises.

View File

View File

@ -0,0 +1,8 @@
Chapter 2 - interactive python
=======================================================================================================================
1. The `rlcompleter` enhancement we created currently only handles dictionaries. Try and extend the code so it supports lists, strings, and tuples as well.
2. Add colors to the completer (hint: use `colorama` for the coloring).
3. Instead of manually completing using our own object introspection, try and use the `jedi` library for autocompletion, which does static code analysis.
4. Try to create a `Hello` `<ipywidget>` so the name of the person can be edited through a notebook without code changes.
5. Try and create a script that will look for a given pattern through all of your previous ipython sessions.

View File

View File

@ -0,0 +1,4 @@
Chapter 3 - pythonic syntax
=======================================================================================================================
Chapter 3 has no exercises.

View File

View File

@ -0,0 +1,6 @@
Chapter 4 - design patterns
=======================================================================================================================
- Create a SortedDict collection that takes a keyfunc to decide the sort order.
- Create a SortedList collection that has O(log(n)) inserts and always returns a sorted list during each iteration.
- Create a Borg pattern that has a state per subclass.

View File

View File

@ -0,0 +1,6 @@
Chapter 5 - functional programming
=======================================================================================================================
1. Implement the quicksort algorithm.
2. Write a groupby function that isnt affected by sorting.
3. Write a groupby function that returns lists of results instead of generators.

View File

View File

@ -0,0 +1,10 @@
Chapter 6 - decorators
=======================================================================================================================
1. Extend the `track` function to monitor execution time.
2. Extend the `track` function with min/max/average execution time and call count.
3. Modify the memoization function to function with unhashable types.
4. Modify the memoization function to have a cache per function instead of a global one.
5. Create a version of `functools.cached_property` that can be recalculated as needed.
6. Create a single-dispatch decorator that considers all or a configurable number of arguments instead of only the first one.
7. Enhance the `type_check` decorator to include additional checks such as requiring a number to be greater than or less than a given value.

View File

View File

@ -0,0 +1,7 @@
Chapter 7 - generators and coroutines
=======================================================================================================================
1. Create a generator similar to `itertools.islice()` that allows for a negative step so you can execute `some_list[20:10:-1]`.
2. Create a class that wraps a generator so it becomes sliceable by using `itertools.islice()` internally.
3. Write a generator for the Fibonacci numbers.
4. Write a generator that uses the sieve of Eratosthenes to generate prime numbers.

View File

@ -0,0 +1,8 @@
Chapter 8 - metaclasses
=======================================================================================================================
1. Create a metaclass to test if attributes/methods are available.
2. Create a metaclass to test if specific classes are inherited.
3. Build a metaclass that wraps every method with a decorator (could be useful for logging/de- bugging purposes), something with a signature like this:
class SomeClass(metaclass=WrappingMeta, wrapper=some_wrapper):

View File

View File

@ -0,0 +1,6 @@
Chapter 9 - documentation
=======================================================================================================================
1. Type hint a complex `dict`
2. Type hint nested types
3. Type hint recursive types

View File

View File

@ -0,0 +1,8 @@
Chapter 10 - testing and logging
=======================================================================================================================
1. Create a function that tests the doctests of a given function/class.
2. For a greater challenge, create a function that recursively tests all doctests of every function and class in a given module.
3. Create a `py.test` plugin that checks if all tested files have file-level documentation. Hint: use `pytest_collect_file`.
4. Create a custom `tox` environment to run `flake8` or `mypy` on your project.
5. Create a `LoggerAdapter` that combines multiple messages into a single message based on some task ID.

View File

View File

@ -0,0 +1,6 @@
Chapter 11 - debugging
=======================================================================================================================
1. Execute code with a timeout so you can see where your application is stalling
2. Measure the duration of the execution
3. Show how often that specific bit of code has been executed

View File

View File

@ -0,0 +1,6 @@
Chapter 12 - performance
=======================================================================================================================
1. Try to create a decorator that monitors each run of a function and warns you if the memory usage grows each run.
2. Try to create a decorator that monitors the runtime of a function and warns you if it deviates too much from the previous run. Optionally, you could make the function generate a (running) average runtime as well.
3. Try to create a memory manager for your classes that warns you when more than a configured number of instances remain in memory. If you never expect more than 5 instances of a certain class, you can warn the user when that number is exceeded.

View File

View File

@ -0,0 +1,6 @@
Chapter 13 - async io
=======================================================================================================================
1. Try to create a `asyncio` base class that automatically registers all instances for easy closing/destructuring when you are done
2. Create an `asyncio` wrapper class for a synchronous process such as file or network operations using executors

View File

View File

@ -0,0 +1,10 @@
Chapter 14 - multithreading and multiprocessing
=======================================================================================================================
1. See if you can make an echo server and client as separate processes. Even though we did not cover `multiprocessing.Pipe()`, I trust you can work with it regardless. It can be created through `a, b = multiprocessing.Pipe()` and you can use it with `a.send()` or `b.send()` and `a.recv()` or `b.recv()`.
2. Read all files in a directory and sum the size of the files by reading each file using `concurrent.futures`. If you want an extra challenge, walk through the directories recursively by letting the thread/process queue new items while running.
3. Read all files in a directory and sum the size of the files by reading each file using `threading` or `multiprocessing`
4. As above, but walk through the directories recursively by letting the thread/process queue new items while running.
5. Create a pool of workers that keeps waiting for items to be queued through `multiprocessing.Queue()`.
6. Convert the pool above to a safe RPC (remote procedure call) type operation.
7. Apply your functional programming skills and calculate something in a parallel way. Perhaps parallel sorting?

View File

@ -0,0 +1,5 @@
Chapter 15 - scientific python
=======================================================================================================================
1. Create a datashader plot.
2. Make the datashater plot interactive using a Jupyter notebook.

View File

View File

@ -0,0 +1,4 @@
Chapter 16 - machine learning
=======================================================================================================================
1. Extract data or information from this chapters summary by applying one of the NLP algorithms.

View File

View File

@ -0,0 +1,5 @@
Chapter 17 - c and cpp extensions
=======================================================================================================================
1. Try to sort a list of numbers using `ctypes`, `CFFI`, and with a native extension. You can use the `qsort` function in `stdlib`.
2. Try to make the `custom_sum` function we created safer by adding proper errors for overflow/underflow issues. Additionally, catch the errors when summing multiple numbers that only overflow or underflow in summation.

View File

View File

@ -0,0 +1,6 @@
Chapter 18 - packaging
=======================================================================================================================
1. Create a `setuptools` command to bump the version in your package
2. Extend the version bumping command by interactively asking for a major, minor, or patch upgrade
3. Try and convert existing projects from `setup.py` to a `pyproject.toml` structure

View File

9
README.rst Normal file
View File

@ -0,0 +1,9 @@
Mastering Python Second Edition Exercises
=======================================================================================================================
This repository contains both example exercise solutions and user submitted solutions.
You are always free to submit your solution, so feel free to jump in and see if you can come up with an improvement
or alternative option!
Naturally code style is a big part of the exercise, so please make sure your code is well formatted and indented.