Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Figa can read from multiple config sources including several file formats, environment variables, and even the Internet, allowing you to configure your project however you want.

Figa supports many sources, including:

  • Environment variables

  • Dict objects

  • JSON (.json)

  • HOCON (.hocon, .conf)

  • INI, CFG (.ini, .cfg)

  • YAML (.yaml, .yml)

  • TOML (.toml)

  • .properties (.properties)

  • Internet resources

$ pip install figa

Usage

import figa

@figa.config
class MyConfig:
    development = "~/config.yml"  # use YAML file for config when developing
    production = "env", "cfg_"  # use environment variables with cfg_ prefix in production

config = MyConfig("development")

>>> config.key == config["key"]  # config can be accessed using dots or indexing
True

Environment Detection

You can implement your own function that detects where to pull config values from.

@figa.config
class Config:
    development = "~/config.yml"  # use YAML file for config when developing
    production = "env", "cfg_"  # use environment variables with cfg_ prefix in production

    def get_env(self):
        if "ON_HEROKU" in figa.env:  # figa.env is shortcut for os.environ
            return "production"
        else:
            return "development"

>>> config = Config()  # if no environment is passed, get_env() will be called.

File Types

By default, the config file type will be guessed from the file extension. This can also be set explicitly:

@figa.config
class MyConfig:
    example = "ini", "./config.conf"
    # .conf would be detected as HOCON, but we set to INI

Default Values

Default values can be set that will be included on every environment.

@figa.config
class MyConfig:
    default = {"name": "My App"}

    dev = {"host": "localhost"}
    prod = {"host": "myapp.com"}

>>> dev_cfg = MyConfig("dev")
>>> prod_cfg = MyConfig("prod")
>>> dev_cfg.name == prod_cfg.name  # "name" config item is included in both
True

Required Values and Type Checking

In your Config class you can set required values and types that will be checked when the config is loaded. This helps ensure that your code doesn’t run with missing information.

@figa.config
class Config:
    # two required config values: `string` & `sub.number`
    __required__ = {
        "string": str,
        "sub": {
            "number": int
        }
    }

If any values are missing, an error will be raised:

@figa.config
class Config:
    # two required config values: `string` & `sub.number`
    __required__ = {
        "string": str,
        "sub": {
            "number": int
        }
    }

    missing_vals = {  # this config is missing sub.number
        "string": "hello, world",
        "sub": {}
    }

>>> cfg = Config("missing_vals")
ValueError: Missing required item 'sub.number'

Figa will automatically convert strings and numbers for you where possible.

@figa.config
class Config:
    __required__ = {
        "stringval": str,
        "numberval": int
    }

    values = {
        "stringval": 100,
        "numberval": "42"
    }

>>> cfg = Config("values")
>>> cfg.stringval
'100'
>>> cfg.numberval
42

This project is published under the MIT License. See LICENSE.md.

Metadata

Release files for figa 0.1a5

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

Source distribution (sdist)

Source distribution for figa 0.1a5
File Size Uploaded
figa-0.1a5.tar.gz 8.9 kB Details

Release files / figa-0.1a5.tar.gz

Download URL figa-0.1a5.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
56c51b49e40522b0f452c7d0e3b771bee1391a81159cd5399420ec5309989d2d
BLAKE2b-256 checksum
How to use checksums
742b90b7fa95900fb89f16b3f952e1c9e2a9281b6a83e9e0c0659fea83f04b6d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3
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