Skip to main content

Free your python code from 12-factor boilerplate.

Project description

petri: free your python code from 12-factor boilerplate.

Python Version

Python Version

Code Style

Code Style

Release

PyPI

Downloads

PyPI - Downloads

Build Status

Build Status

Docs

Documentation Status

Maintainability

Maintainability

License

License: Unlicense

Coverage

Test Coverage

Deps

Libraries.io dependency status for GitHub repo

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.

Sample petri usage

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.

Source Distribution

petri-0.23.2.tar.gz (10.1 kB view hashes)

Uploaded Source

Built Distribution

petri-0.23.2-py3-none-any.whl (8.9 kB view hashes)

Uploaded Python 3

Supported by

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