From 1c106d776c69c7b92a24fad23e97388b2105d435 Mon Sep 17 00:00:00 2001 From: Rick van Hattem Date: Sat, 30 Apr 2022 00:45:54 +0200 Subject: [PATCH] Added chapters and list of exercises --- CH_01_getting_started/README.rst | 4 ++++ CH_01_getting_started/__init__.py | 0 CH_02_interactive_python/README.rst | 8 ++++++++ CH_02_interactive_python/__init__.py | 0 CH_03_pythonic_syntax/README.rst | 4 ++++ CH_03_pythonic_syntax/__init__.py | 0 CH_04_design_patterns/README.rst | 6 ++++++ CH_04_design_patterns/__init__.py | 0 CH_05_functional_programming/README.rst | 6 ++++++ CH_05_functional_programming/__init__.py | 0 CH_06_decorators/README.rst | 10 ++++++++++ CH_06_decorators/__init__.py | 0 CH_07_generators_and_coroutines/README.rst | 7 +++++++ CH_07_generators_and_coroutines/__init__.py | 0 CH_08_metaclasses/README.rst | 8 ++++++++ CH_08_metaclasses/__init__.py | 0 CH_09_documentation/README.rst | 6 ++++++ CH_09_documentation/__init__.py | 0 CH_10_testing_and_logging/README.rst | 8 ++++++++ CH_10_testing_and_logging/__init__.py | 0 CH_11_debugging/README.rst | 6 ++++++ CH_11_debugging/__init__.py | 0 CH_12_performance/README.rst | 6 ++++++ CH_12_performance/__init__.py | 0 CH_13_async_io/README.rst | 6 ++++++ CH_13_async_io/__init__.py | 0 CH_14_multithreading_and_multiprocessing/README.rst | 10 ++++++++++ CH_14_multithreading_and_multiprocessing/__init__.py | 0 CH_15_scientific_python/README.rst | 5 +++++ CH_15_scientific_python/__init__.py | 0 CH_16_machine_learning/README.rst | 4 ++++ CH_16_machine_learning/__init__.py | 0 CH_17_c_and_cpp_extensions/README.rst | 5 +++++ CH_17_c_and_cpp_extensions/__init__.py | 0 CH_18_packaging/README.rst | 6 ++++++ CH_18_packaging/__init__.py | 0 README.rst | 9 +++++++++ 37 files changed, 124 insertions(+) create mode 100644 CH_01_getting_started/README.rst create mode 100644 CH_01_getting_started/__init__.py create mode 100644 CH_02_interactive_python/README.rst create mode 100644 CH_02_interactive_python/__init__.py create mode 100644 CH_03_pythonic_syntax/README.rst create mode 100644 CH_03_pythonic_syntax/__init__.py create mode 100644 CH_04_design_patterns/README.rst create mode 100644 CH_04_design_patterns/__init__.py create mode 100644 CH_05_functional_programming/README.rst create mode 100644 CH_05_functional_programming/__init__.py create mode 100644 CH_06_decorators/README.rst create mode 100644 CH_06_decorators/__init__.py create mode 100644 CH_07_generators_and_coroutines/README.rst create mode 100644 CH_07_generators_and_coroutines/__init__.py create mode 100644 CH_08_metaclasses/README.rst create mode 100644 CH_08_metaclasses/__init__.py create mode 100644 CH_09_documentation/README.rst create mode 100644 CH_09_documentation/__init__.py create mode 100644 CH_10_testing_and_logging/README.rst create mode 100644 CH_10_testing_and_logging/__init__.py create mode 100644 CH_11_debugging/README.rst create mode 100644 CH_11_debugging/__init__.py create mode 100644 CH_12_performance/README.rst create mode 100644 CH_12_performance/__init__.py create mode 100644 CH_13_async_io/README.rst create mode 100644 CH_13_async_io/__init__.py create mode 100644 CH_14_multithreading_and_multiprocessing/README.rst create mode 100644 CH_14_multithreading_and_multiprocessing/__init__.py create mode 100644 CH_15_scientific_python/README.rst create mode 100644 CH_15_scientific_python/__init__.py create mode 100644 CH_16_machine_learning/README.rst create mode 100644 CH_16_machine_learning/__init__.py create mode 100644 CH_17_c_and_cpp_extensions/README.rst create mode 100644 CH_17_c_and_cpp_extensions/__init__.py create mode 100644 CH_18_packaging/README.rst create mode 100644 CH_18_packaging/__init__.py create mode 100644 README.rst diff --git a/CH_01_getting_started/README.rst b/CH_01_getting_started/README.rst new file mode 100644 index 0000000..57e6da4 --- /dev/null +++ b/CH_01_getting_started/README.rst @@ -0,0 +1,4 @@ +Chapter 1 - getting started +======================================================================================================================= + +Chapter 1 has no exercises. diff --git a/CH_01_getting_started/__init__.py b/CH_01_getting_started/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/CH_02_interactive_python/README.rst b/CH_02_interactive_python/README.rst new file mode 100644 index 0000000..6569ba3 --- /dev/null +++ b/CH_02_interactive_python/README.rst @@ -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` `` 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. diff --git a/CH_02_interactive_python/__init__.py b/CH_02_interactive_python/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/CH_03_pythonic_syntax/README.rst b/CH_03_pythonic_syntax/README.rst new file mode 100644 index 0000000..d34f023 --- /dev/null +++ b/CH_03_pythonic_syntax/README.rst @@ -0,0 +1,4 @@ +Chapter 3 - pythonic syntax +======================================================================================================================= + +Chapter 3 has no exercises. diff --git a/CH_03_pythonic_syntax/__init__.py b/CH_03_pythonic_syntax/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/CH_04_design_patterns/README.rst b/CH_04_design_patterns/README.rst new file mode 100644 index 0000000..37b87e9 --- /dev/null +++ b/CH_04_design_patterns/README.rst @@ -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. diff --git a/CH_04_design_patterns/__init__.py b/CH_04_design_patterns/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/CH_05_functional_programming/README.rst b/CH_05_functional_programming/README.rst new file mode 100644 index 0000000..cfefb24 --- /dev/null +++ b/CH_05_functional_programming/README.rst @@ -0,0 +1,6 @@ +Chapter 5 - functional programming +======================================================================================================================= + +1. Implement the quicksort algorithm. +2. Write a groupby function that isn’t affected by sorting. +3. Write a groupby function that returns lists of results instead of generators. diff --git a/CH_05_functional_programming/__init__.py b/CH_05_functional_programming/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/CH_06_decorators/README.rst b/CH_06_decorators/README.rst new file mode 100644 index 0000000..ba70c6b --- /dev/null +++ b/CH_06_decorators/README.rst @@ -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. diff --git a/CH_06_decorators/__init__.py b/CH_06_decorators/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/CH_07_generators_and_coroutines/README.rst b/CH_07_generators_and_coroutines/README.rst new file mode 100644 index 0000000..ba0ff7c --- /dev/null +++ b/CH_07_generators_and_coroutines/README.rst @@ -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. diff --git a/CH_07_generators_and_coroutines/__init__.py b/CH_07_generators_and_coroutines/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/CH_08_metaclasses/README.rst b/CH_08_metaclasses/README.rst new file mode 100644 index 0000000..fd04f7c --- /dev/null +++ b/CH_08_metaclasses/README.rst @@ -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): diff --git a/CH_08_metaclasses/__init__.py b/CH_08_metaclasses/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/CH_09_documentation/README.rst b/CH_09_documentation/README.rst new file mode 100644 index 0000000..68bd5fe --- /dev/null +++ b/CH_09_documentation/README.rst @@ -0,0 +1,6 @@ +Chapter 9 - documentation +======================================================================================================================= + +1. Type hint a complex `dict` +2. Type hint nested types +3. Type hint recursive types diff --git a/CH_09_documentation/__init__.py b/CH_09_documentation/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/CH_10_testing_and_logging/README.rst b/CH_10_testing_and_logging/README.rst new file mode 100644 index 0000000..3e24b79 --- /dev/null +++ b/CH_10_testing_and_logging/README.rst @@ -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. diff --git a/CH_10_testing_and_logging/__init__.py b/CH_10_testing_and_logging/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/CH_11_debugging/README.rst b/CH_11_debugging/README.rst new file mode 100644 index 0000000..7cdb291 --- /dev/null +++ b/CH_11_debugging/README.rst @@ -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 diff --git a/CH_11_debugging/__init__.py b/CH_11_debugging/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/CH_12_performance/README.rst b/CH_12_performance/README.rst new file mode 100644 index 0000000..c65f7b3 --- /dev/null +++ b/CH_12_performance/README.rst @@ -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. diff --git a/CH_12_performance/__init__.py b/CH_12_performance/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/CH_13_async_io/README.rst b/CH_13_async_io/README.rst new file mode 100644 index 0000000..54f6d0b --- /dev/null +++ b/CH_13_async_io/README.rst @@ -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 + diff --git a/CH_13_async_io/__init__.py b/CH_13_async_io/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/CH_14_multithreading_and_multiprocessing/README.rst b/CH_14_multithreading_and_multiprocessing/README.rst new file mode 100644 index 0000000..0edeccf --- /dev/null +++ b/CH_14_multithreading_and_multiprocessing/README.rst @@ -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? diff --git a/CH_14_multithreading_and_multiprocessing/__init__.py b/CH_14_multithreading_and_multiprocessing/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/CH_15_scientific_python/README.rst b/CH_15_scientific_python/README.rst new file mode 100644 index 0000000..28fedfb --- /dev/null +++ b/CH_15_scientific_python/README.rst @@ -0,0 +1,5 @@ +Chapter 15 - scientific python +======================================================================================================================= + +1. Create a datashader plot. +2. Make the datashater plot interactive using a Jupyter notebook. diff --git a/CH_15_scientific_python/__init__.py b/CH_15_scientific_python/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/CH_16_machine_learning/README.rst b/CH_16_machine_learning/README.rst new file mode 100644 index 0000000..58445d4 --- /dev/null +++ b/CH_16_machine_learning/README.rst @@ -0,0 +1,4 @@ +Chapter 16 - machine learning +======================================================================================================================= + +1. Extract data or information from this chapter’s summary by applying one of the NLP algorithms. diff --git a/CH_16_machine_learning/__init__.py b/CH_16_machine_learning/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/CH_17_c_and_cpp_extensions/README.rst b/CH_17_c_and_cpp_extensions/README.rst new file mode 100644 index 0000000..17a1190 --- /dev/null +++ b/CH_17_c_and_cpp_extensions/README.rst @@ -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. diff --git a/CH_17_c_and_cpp_extensions/__init__.py b/CH_17_c_and_cpp_extensions/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/CH_18_packaging/README.rst b/CH_18_packaging/README.rst new file mode 100644 index 0000000..180dd04 --- /dev/null +++ b/CH_18_packaging/README.rst @@ -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 diff --git a/CH_18_packaging/__init__.py b/CH_18_packaging/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..0ae34e5 --- /dev/null +++ b/README.rst @@ -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.