Skip to main content

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

Motivation

  • In order to have same code for dev/production, it all starts with an innocent settings.py.

  • In order to switch between them, it’s a good idea to use env vars…

  • But sometimes, you want to overrride a single variable.

  • But sometimes, you want to overrride several variables.

  • Plus, colored logs while developing are pretty.

  • Plus, structured logs in production look smart.

Features

  • [X] Sane defaults for logging:

    • [X] json logs for production.

    • [X] user-friendly (spaced) + colored for development.

    • [X] Enforce root logger’s formatting.

  • [X] Easy settings:

    • [X] Toggle between configurations using a signle env var.

    • [X] Define default configuration in case the env var is not present.

    • [X] Granular settings override using environment variables.

    • [X] Batch settings override by loading a .env.

  • Read package metadata (author, version, etc):

    • [X] Lazy-loaded to avoid reading files during imports.

    • [X] For installed packages only.

Install

Install using poetry poetry add petri or pip pip install petri or (for dev) pip install git+https://github.com/pwoolvett/petri.

Optionally, also install the color extra for colored logs using colorama.

Usage

Just 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):
    SQL_CONNECTION_STRING = 'sqlite:///database.db'  # example setting

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)

That’s it!. Watch the animation above for results running python -c “import my_package”

Optionally, define an environment variable named env_file, to override the settings:

  • Its value must be the path to a valid, existing file.

  • Its contents must have name=value pairs.

  • The names must be of the form [MY_PACKAGE]_[SETTING_NAME] (Watch the animation above).

To select which of your settings classes to use, you can:

  • Point the selector envvar (eg: for my-pkg, this would be MY_PKG_CONFIG=my_pkg.settings:Production), or

  • Use the default_config kwarg when instantiating petri.Petri (eg: use pkg = Petri(__file__, default_config=”my_pkg.settings:Production”) in the example above).

Of course, you can use both. Petri will attempt to load the selecto envvar, and if not found, default to the defined kwarg.


For more info, check the docs.

Release files for petri 0.24.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for petri 0.24.1
File Size Uploaded
petri-0.24.1.tar.gz 11.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for petri 0.24.1
File Interpreter ABI Platform
petri-0.24.1-py3-none-any.whl Python 3 none any Details

Total release size: 22.5 kB

Release files / petri-0.24.1.tar.gz

Download URL petri-0.24.1.tar.gz
Size 11.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a16d2e6d96dcce44d1caa61b7700c15a2b969bc1b5e33236f3e2e71d476a834c
BLAKE2b-256 checksum
How to use checksums
525cca4aabbc71f17cd85f398eb0d0b277c7b250762e5d29db71cecdd1251874
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.0a5 CPython/3.6.9 Linux/5.0.0-1020-azure

Release files / petri-0.24.1-py3-none-any.whl

Download URL petri-0.24.1-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1e5e9868b15aed1642f57ef32cafbba712607489a330249eee3f90cfc952cd0c
BLAKE2b-256 checksum
How to use checksums
3e654dedb791bbcec33d83d0889360b860b84e1f709a2710198cb18e6eee1bf5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.0a5 CPython/3.6.9 Linux/5.0.0-1020-azure

Release history Release notifications | RSS feed

This release

0.24.1 This release

2 release files

0.24.0

2 release files

0.22.2

2 release files

0.22.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page