design-patterns
design-patterns copied to clipboard
Some of the most common design patterns implemented in Python.
design-patterns
Some of the most common design patterns implemented in Python.
Installation
This project uses pyenv and pyenv-virtualenv to manage the Python virtual environment, and poetry to manage the project dependencies.
If you don't have python 3.x.x, you have to install it. For example, I'm using 3.7.9.
pyenv install 3.7.9
Create a virtual environment and activate it.
pyenv virtualenv 3.7.9 design_patterns
pyenv activate design_patterns
Install all the dependencies from the poetry.lock file.
poetry install
Usage
Every python file contains an implementation of a design pattern and a simple example that can help you understand where the pattern might be useful.
For example
python observer.py
python strategy.py
# etc...
Tests
If you want you can run all tests with:
poetry run pytest --verbose
You can also test the MVC pattern with:
cd mvc
poetry run python model_view_controller.py
# or simply
python mvc/model_view_controller.py
Troubleshooting
If you use pyenv and get the error "No module named '_ctypes'" on Ubuntu, you are probably missing the libffi-dev package. See this answer.
If you get Error: pg_config executable not found. on Ubuntu, install the libpq-dev package. See here.