|
1 | 1 | # Python-Object-Oriented-Programming---4th-edition |
2 | 2 | Code Repository for Python Object-Oriented Programming - 4th edition, Published by Packt |
| 3 | + |
| 4 | +The case study relies on a number of external packages. |
| 5 | +It's often best to start with a tool like `conda` |
| 6 | +to build virtual environments and download packages. |
| 7 | +This can also be done with other virtual environment managers. |
| 8 | + |
| 9 | +The following steps will help you build a working Python environment. Later, we'll add the additional |
| 10 | +packages used by the case study. |
| 11 | + |
| 12 | +1. Get **miniconda**. Find it here: https://docs.conda.io/en/latest/miniconda.html. |
| 13 | + |
| 14 | +2. Install **miniconda**. Find the instructions here: https://conda.io/projects/conda/en/latest/user-guide/install/index.html. |
| 15 | + For the most part, the instructions can be summarized as "double-click the installer." |
| 16 | + There are potential complications, so it can help to read through the the instructions. |
| 17 | + |
| 18 | +3. Use the **conda** tool to create a virtual environment that has Python. We'll add the required packages for this case study. |
| 19 | + |
| 20 | + ```sh |
| 21 | + % conda create -n CaseStudy python=3.9 |
| 22 | + % conda activate CaseStudy |
| 23 | + ``` |
| 24 | + |
| 25 | +4. Now that this is available, you can run Python. Try the following: |
| 26 | + |
| 27 | + ```sh |
| 28 | + % python |
| 29 | + >>> print("Hello, world!") |
| 30 | + Hello, world! |
| 31 | + >>> exit() |
| 32 | + ``` |
| 33 | + |
| 34 | +## Installing Other Components |
| 35 | + |
| 36 | +Now that you have a working Python environment, we can add some of packages we'll be using. |
| 37 | + |
| 38 | +```sh |
| 39 | + % conda install bs4 pytest pillow |
| 40 | +``` |
| 41 | + |
| 42 | +This will ask if you want to proceed. The answer is "y", for "yes." It will then download |
| 43 | +and install the four packages listed above, plus the packages they depend on. |
| 44 | + |
| 45 | +Not every package can be installed by **conda**, so PIP is sometimes needed. Specifically, |
| 46 | +we want to automate our testing with the `tox` tool, which isn't easily installed by **conda**. |
| 47 | + |
| 48 | +```sh |
| 49 | + % python -m pip install tox |
| 50 | +``` |
| 51 | + |
| 52 | +The file `environment.yml` has the exported environment used to produce this example. |
| 53 | + |
| 54 | +Once PIP has been run, **conda** can lose track of the extra installations. To make |
| 55 | +changes, it's often helpful to create a new conda environment with the packages available |
| 56 | +from **conda**, and then add PIP packages. |
| 57 | + |
| 58 | +Your versions may be slightly newer than the ones used by the author. |
| 59 | + |
| 60 | +## Multiple Python Version Setup |
| 61 | + |
| 62 | +The full test suite requires multiple versions of Python and the tox utility. |
| 63 | + |
| 64 | +There is some complexity when using Windows. |
| 65 | +See https://tox.readthedocs.io/en/latest/developers.html?highlight=windows#multiple-python-versions-on-windows |
| 66 | + |
| 67 | +The easiest way to do this is to create an additional conda environment, |
| 68 | + |
| 69 | +```sh |
| 70 | + conda create --name=tox-py38 python=3.8 |
| 71 | +``` |
| 72 | + |
| 73 | +This environment will have the needed Python run-time. |
| 74 | + |
| 75 | +For Windows, only, edit the `python3.8.bat` file to point to |
| 76 | +this environment's executables. Generally, the name supplied will |
| 77 | +be correct. |
| 78 | + |
| 79 | +## Running the Test Suite |
| 80 | + |
| 81 | +The test suite requires tox and Python 3.8 (see above for additional installs.) |
| 82 | + |
| 83 | +Use the following command to run all of the tests. |
| 84 | + |
| 85 | +```sh |
| 86 | + % make test |
| 87 | +``` |
| 88 | + |
| 89 | +To run tests for a specific chapter, you can change the |
| 90 | +working director and run `tox`. Here's an exception for |
| 91 | +Chapter 2. |
| 92 | + |
| 93 | +```sh |
| 94 | + % (cd ch_02; tox) |
| 95 | +``` |
| 96 | + |
| 97 | +This will change to the chapter 2 directory, `ch_02`, |
| 98 | +and run `tox` in that directory. |
| 99 | + |
| 100 | +For Windows, use `cmd`: |
| 101 | + |
| 102 | +```cmd |
| 103 | + C:\path\to\repo> cmd /c "CHDIR ch_02&&tox" |
| 104 | +``` |
| 105 | + |
| 106 | + |
| 107 | +## An Integrated Development Environment |
| 108 | + |
| 109 | +You can edit Python code with any text editor. It can be easier to use |
| 110 | +a sophisticated IDE, but some developers are happy with simple text editors. |
| 111 | +There's no "best" IDE for Python. While the author uses PyCharm and Komodo Edit, some people |
| 112 | +prefer VS Code, or Spyder. |
| 113 | + |
| 114 | +Good UML diagrams can be created with the http://draw.io diagram editor. This creates |
| 115 | +`drawio` text files that can saved as part of a project. It can export PNG files for publication. |
| 116 | +This is very easy to install and use. |
| 117 | + |
| 118 | +Another choice is to use plantuml. See https://plantuml.com. This can be incorporated |
| 119 | +into a markdown processing plug-in used by the PyCharm IDE. |
| 120 | +The plugin depends on **graphviz**, making the installation fairly complex. |
| 121 | + |
| 122 | +- Add the Markdown tool to PyCharm. |
| 123 | + |
| 124 | +- In the preferences for Markdown, install and enable PlantUML. |
| 125 | + |
| 126 | +- Use **conda** to install `graphviz` as well as installing the `plantuml-markdown` tools. |
| 127 | + The `markdown_py` application can create an HTML draft of a Markdown doc. |
| 128 | + It needs to be installed separately, if this is needed. |
| 129 | + |
| 130 | +- Update the OS environment settings to set the `GRAPHVIZ_DOT` environment variable to name the conda virtual environment. |
| 131 | + where `graphviz` was installed. |
| 132 | + The macOS and Linux users should update their `~/.zshrc` or `~/.bashrc` file, depending on which shell is in use. |
| 133 | + Windows users should set their system environment variables. |
| 134 | + |
| 135 | +- It may be necessary to create a `plantuml` shell script in `/usr/local/bin`. |
| 136 | + See https://github.com/mikitex70/plantuml-markdown for details on installation. |
| 137 | + |
| 138 | +The `plantuml` can be used to tranform UML files to PNG images. |
| 139 | + |
| 140 | +The Project Structure |
| 141 | +===================== |
| 142 | + |
| 143 | +Each chapter's code is in a separate directory, `ch_01`, `ch_02`, etc. |
| 144 | + |
| 145 | +Within the chapter, there's some combination of `docs`, `src`, and `tests` folders. |
| 146 | +There will also be a `pyproject.toml` file with parameters used to control tools |
| 147 | +like **tox**. |
| 148 | + |
| 149 | +Chapters and Case Study Content |
| 150 | +=============================== |
| 151 | + |
| 152 | +1. Object-Oriented Design. |
| 153 | + Creating the 4+1 views of the problem domain. |
| 154 | + |
| 155 | +2. Objects in Python. |
| 156 | + Core data model of samples and training data. |
| 157 | + |
| 158 | +3. When Objects Are Alike. |
| 159 | + Algorithm Alternatives for k-NN -- euclidean, manhattan, chebyshev, minkowski |
| 160 | + |
| 161 | +4. Expecting the Unexpected. |
| 162 | + Central authentication and authorization for a web service. |
| 163 | + |
| 164 | +5. When to Use Object-Oriented Programming. |
| 165 | + Input validations for training data as well as requests. |
| 166 | + Properties. Context Managers. |
| 167 | + |
| 168 | +6. Abstract Base Classes (abc’s) and Operator Overloading. |
| 169 | + Filtering and subsetting the training data to create test sets. Shuffling. Sorting. Random Selection. Filters. |
| 170 | + |
| 171 | +7. Python Data Structures. |
| 172 | + The ``@dataclass`` definitions and ``NamedTuple`` implementation choices. |
| 173 | + |
| 174 | +8. Functional Techniques. |
| 175 | + The essential k-NN algorithm as a functional design. |
| 176 | + Computing test results for different K values and distance algorithms. |
| 177 | + |
| 178 | +9. Strings and Serialization. |
| 179 | + JSON serialization and deserialization of training data, requests and responses. |
| 180 | + |
| 181 | +10. The Iterator Pattern. |
| 182 | + Revisiting the k-NN design to permit future flexibility. |
| 183 | + |
| 184 | +11. Common Design Patterns. (No case study.) |
| 185 | + |
| 186 | +12. Advanced Design Patterns. (No case study.) |
| 187 | + |
| 188 | +13. Testing Object-Oriented Programs. |
| 189 | + Using Test-Driven Develoment on a small ciphering algorithm. |
| 190 | + |
| 191 | +14. Concurrency. |
| 192 | + Compressing image files using Run-Length Encoding. |
0 commit comments