Free your python code from 12-factor boilerplate.
Project description
petri: free your python code from 12-factor boilerplate.
Python Version |
|
Code Style |
|
Release |
|
Downloads |
|
Build Status |
|
Docs |
|
Maintainability |
|
License |
|
Coverage |
|
Deps |
Summary
Importing petri equips your app/pacakage with:
Dotenv file handling using python-dotenv.
Package metadata (for installed packages), using importlib_metadata.
Settings using pydantic.
Logging config using structlog.
Environment switching (prod/dev/test) handling via ENV environment variable.
Install
Install using poetry or pip:
Poetry:
poetry add petri
pip:
pip install petri
Usage
[OPTIONAL] Define an environment variable named env_file, to feed additional envs. Its value must be the path to a valid, existing file.
Define dev/prod/test settings:
from petri.settings import BaseSettings class Settings(BaseSettings): class Config: # pylint: disable=missing-docstring,too-few-public-methods env_prefix = "A_PKG_" class Production(Settings): ENV = "production" class Development(Settings): ENV = "development" class Testing(Settings): ENV = "testing"
IMPORTANT: In your base class, define Config.env_prefix. For example, a package named a-pkg turns into A_PKG_. The code used should be compatible with: Config.env_prefix=[package_name].upper().replace(‘-’ ,’_’)+’_’.
Select which class of setting to use, by doing one of the folowing:
Set the envvar [package_name].replace(“-”, “_”).upper() + “_CONFIG” to a defined settings class (eg: A_PKG_CONFIG=a_pkg.settings:Production), or
Use the default_config kwarg when instantiating petri.Petri (See Below)
Of course, you can use both. Petri will attempt to load said env, and if not found, default to the defined kwarg.
Instantiate petri.Petri form your package’s __init__.py, like so:
"""A package: sample petri usage""" from petri import Petri pkg = Petri(__file__) __version__ = "1.2.3"
This allows petri to:
Load env_file’s contents, if defined.
Provide your package’s metadata (version, author, etc), available in pkg.meta (lazy-loaded to avoid reading metadata files unnecessarily).
Activate and instantiate a settings class, according to environment var and default, available in pkg.settings (https://pydantic-docs.helpmanual.io/#id5)
Configure and expose a logger, available in pkg.log, which uses configuration defined in your settings.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.