This repo contains various examples for:
- PDM and Poetry-based dependencies management.
- pipx-based installation of poetry and pylint.
- pylint-based linting.
- pytest-based unit tests.
- Command-line arguments parsing.
- Logging.
- Executing
Callableobjects. - Type hints.
- Async IO
- Jupyter notebook.
- Dash-based Web application.
- Flask-based API.
- SQLAlchemy-based SQL Database access.
- Interfaces
- TLS-secured gRPC-based microservices.
I built this repo to become a better Python programmer by learning from well-written, lint-checked, unit-tested, " Pythonic" examples, and provide (for myself) an inventory of working examples that I can go back to when I forget how to do something in Python. :-)
The code uses and adapts various examples and tutorials that I found on the Internet from a variety of sites, hence I do not claim any authorship to any of the source code in this repo.
I strive to indicate the examples' sources in the modules' docstring.
There is none.
The code in the repo was written, debugged, and executed using JetBrains' PyCharm 2023.2.5 (Professional Edition).
Python 3.10 or above.
Install it on a Mac with:
brew install python@3.10PDM and Poetry can share the same pyproject.toml file.
brew install pdmNotes:
- brew also installed python@3.12 as a PDM dependency
pdm init
pdm add jupyter
pdm installbrew install pipx
pipx install poetry
pipx ensurepath
# open new terminal
poetry -hpoetry installpoetry run python main.py --example types-hintpoetry run jupyter notebookRun it with:
pdm run main.py --example type-hints --debug trueRun it with:
pdm run jupyter notebook- api
- web_app
- web_crawler
- gRPC-based microservices
The above apps use the app_logging module, hence add the Dash's application location to PYTHONPATH before running
them, for example:
-> % export PYTHONPATH="${PYTHONPATH}:/Users/bertrandrigaldies/Projects/python-by-example"The gRPC examples were the results of doing a code-along of this tutorial
To generate the protobuf bindings, and run the client and recommendations server by hand (outside of Docker):
For the server:
cd apps/grpc/recommendations
python -m grpc_tools.protoc -I ../protobufs --python_out=. \
--grpc_python_out=. ../protobufs/recommendations.protoFor the Web server:
cd apps/grpc/marketplace
python -m grpc_tools.protoc -I ../protobufs --python_out=. \
--grpc_python_out=. ../protobufs/recommendations.protoWhen running in Docker, the protobuf bindings are generated as part of the Docker image builds for both the client and recommendations server.
See the next section for the instructions on how to run the demo in Docker with docker-compose.
Build the Docker images for the client and recommendations server as shown below (see also the second comment line at the top of the Docker files):
- Build the client's CA and TLS public key
openssl req -x509 -nodes -newkey rsa:4096 -keyout ca.key -out ca.pem -subj /O=me- Build the client's Docker image
DOCKER_BUILDKIT=1 docker build . -f marketplace/Dockerfile -t marketplace --secret id=ca.key,src=ca.key- Build the recommendations server's Docker image
DOCKER_BUILDKIT=1 docker build . -f recommendations/Dockerfile -t recommendations --secret id=ca.key,src=ca.keyRun the dockerized demo with docker-compose as shown below:
docker-compose upShut down the application as shown below from the terminal where the demo was started (see above):
- Ctrl-C
docker-compose down
See setup_logging() in app_logging.py.
See parser.parse_args() in main.py.
See register_examples() in ./examples/registry/registry.py for the use if the Callable generic.
See run_examples() in ./examples/typehints/type_hints_example.py.
poetry add aiofiles
poetry add aiohttppoetry run python main.py --example async-io --debug true -p 3 -c 2
poetry run python main.py --example generators --debug truepoetry install pytestpoetry run python -m pytestpipx install pylintpylint **/*.py poetry add mypymypy **/*.pyIn order to address mypy errors such as:
error: Library stubs not installed for "pandas" [import-untyped]Install the supporting type stubs:
poetry add pandas-stubs
poetry add types-aiofiles
poetry add types-requestspoetry add flaskpoetry run flask --app ./apps/api/resp_api --debug run- View incomes
- Add income: Run the Python
post_income.pyprogram as shown below:
poetry run python ./apps/api/post_income.py The example uses the Dash framework, and tutorial.
poetry install dashpoetry run python ./apps/web_app/app_with_bootstrap_styling.pypoetry install sqlalchemypoetry add rediscd ./examples/cache
docker compose upPre-requisite: Install the Redis client
brew install redisredis-cli -h localhost -p 6379poetry install tqdmpoetry install progressbar2poetry add numpy