Skip to main content

Showcase best practices for structuring a Python package.

Project description

ds-best-practice-example

pypi python Build Status codecov

Showcase best practices for structuring a simple DS-related Python package/application.

If you notice any best practice missing/incorrect, please do let me know and I will update them.

This example package will fit a linear regression on your input data. It comes with the diabetes data as a toy data.

Note that this example comes with many tools/components to help your development, but do enable/disable them based on your needs.

The recommend minimum tools I usually go for in most of my projects are

  • Poetry - for Python package management
  • Pre-commit - for formating and linting
  • hydra - for package configurations
  • logging - for logging runtime information
  • Pytest - for unit testing

The additional tools are

Related links

How to install this package?

The easiest way is to install it with pip.

pip install ds-best-practice-example

How to run this package?

You run this package via both command line and Python.

Note that all configurations are specified via hydra in this config yaml.

Please refer to the example notebook for more details.

(1) Run via command line

poetry run ezml

(2) Run via Python

import hydra

from ezml.data import prepare_data
from ezml.model import train_model
from ezml.diagnostic import diagnose_model

# Use Hydra config with Compose API
hydra.initialize(version_base=None, config_path="../input/conf")
cfg = hydra.compose(
    config_name="config",
    overrides=["data.file_path=../input/data/diabetes.csv", "model.file_path=../output/model/linreg_diabetes.joblib"]
)

# Train linear regression with default data
X_train, X_test, y_train, y_test = prepare_data(cfg)
model = train_model(cfg, X_train, y_train)
diagnose_model(X_test, y_test, model)

How to develop this package further?

  1. Update codes as needed.
    1. Usually I create and test codes in Jupyter notebook (under notebook folder) before manually adapting over to standard Python scripts.
  2. (Test locally) Test that codes are working as intended.
    1. Test locally (all in one go)
      1. poetry run tox
      2. Internally tox will run unit testing, document generation and build tests.
    2. Test locally (one by one)
      1. poetry run pytest for unit testing
      2. poetry run mike deploy vtest -m "test doc build" --ignore
      3. poetry run mike delete vtest -m "remove doc build" --ignore
      4. poetry run mkdocs serve to see docs locally
      5. poetry build
      6. poetry run twine check dist/* to test builds
    3. Test on cloud
      1. No need to do anything
      2. Follow later steps to push the codes to GitHub to trigger tests, as this repo has GitHub Workflows defined (in .github/workflows)
  3. Run pre-commit by committing codes.
    1. git add .
    2. git commit -m "a message"
    3. Resolve any errors from pre-commit manually.
  4. Bump the version number up
    1. E.g. for a patch, poetry run bump2version patch
  5. Rerun git add and commit to commit codes.
    1. Once happy with everything, git push the codes to cloud repo.
  6. GitHub Actions will be automatically triggered for testing and staging.
    1. Wait for GitHub Actions to complete, then check for a published package at https://test.pypi.org/project/ds-best-practice-example/
  7. Once all are done, trigger release build by tagging a commit with v* version number.
    1. A documentation will be automatically generated at https://cheeyeelim.github.io/ds-best-practice-example
    2. The package will be built and published to PyPI.
  8. Done!

How to create this project from scratch?

You can create it from scratch by following the steps below.

The steps assumed cookiecutter, pre-commit and poetry has been installed. Otherwise follow their respective installation instructions to install them.

# Setup project folder interactively
cookiecutter https://github.com/cheeyeelim/cookiecutter-pypackage.git

# Setup pre-commit
pre-commit install

# Update pyproject.toml with required packages
# Install Python packages
poetry install -E test -E dev -E doc

Credits

This package was created with Cookiecutter and the cheeyeelim/cookiecutter-pypackage project template.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ds_best_practice_example-0.1.1.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ds_best_practice_example-0.1.1-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file ds_best_practice_example-0.1.1.tar.gz.

File metadata

  • Download URL: ds_best_practice_example-0.1.1.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for ds_best_practice_example-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d60b1bc40344a1d3a07ee4dcf27ab4fe240ac37f28c082770d62bf5963d16d9b
MD5 1b98dbf9729db9ea8ea4e9398f647856
BLAKE2b-256 0080a089c72e55fb9d23b7aecebca7be86e8899bbb940458b375559b28a6c8cc

See more details on using hashes here.

File details

Details for the file ds_best_practice_example-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for ds_best_practice_example-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e7c6018a61578740d9420430d3ae96e1c7e0b7467587a020f21e988f6d4560dd
MD5 059ac08312da0001f86114fa400efab7
BLAKE2b-256 e983499bba2148c2cdda665f50fcaedcb3e8c541eae0b879a50ee2fc331a13ac

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page