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 poetry add petri or pip pip install petri.
Optionally, also install the color extra for colored logs using colorama.
Usage
Define configuration setting(s) and instantiate petri.Petri:
# my_package/__init__.py
from petri import Petri
from petri.settings import BaseSettings
from petri.loggin import LogFormatter, LogLevel
class Settings(BaseSettings):
class Config:
env_prefix = "MY_PKG_" # VERY IMPORTANT!!!
# define common settings here
class Production(Settings):
LOG_FORMAT = LogFormatter.JSON
LOG_LEVEL = LogLevel.TRACE
class Development(Settings):
LOG_FORMAT = LogFormatter.COLOR # requires colorama
LOG_LEVEL = LogLevel.WARNING
pkg = Petri(__file__)
# demo logs
pkg.log.info("log away info level",mode=pkg.settings, version=pkg.meta.version)
pkg.log.warning("this will show", kewl=True)
- [OPTIONAL] Define an environment variable named env_file, to feed
additional envs. Its value must be the path to a valid, existing file.
Select which of your settings classes to use, by doing one of the folowing:
Point the selector envvar (eg: for a-pkg, this would be A_PKG_CONFIG=a_pkg.settings:Production, see petri.Petri.pkg_2_envvar), 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.
Additional Info
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.
For more info, check the docs.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.