Skip to main content

kofiko - Code-First Configuration approach for Python

Overview

kofiko = (Ko)de-(Fi)rst (Ko)nfiguration

See my blog post about it.

Define application configuration as Python classes:

@config_section
class GeneralConfig:
    env = "default"
    port = 8001
    weights = [1.1, 2.2]
    bool_to_str = {
        True: "Yes",
        False: "No"
    }

Override default values using one or more of the following:

  1. Customization functions

     @config_custom
     def prod():
         GeneralConfig.env = "prod"
         GeneralConfig.port = 8080
    
  2. .INI files:

    [GENERAL]
    bool_to_str = No:Nyet
    WEIGHTS = 3,4,5.1
    
  3. Environment variables:

    myapp_GENERAL_ENV=prod 
    myapp_general_bool_to_str=True:si
    

Call configure() to activate:

kofiko.configure()

And use static members of configured classes directly from your code.

print(GeneralConfig.env)            # prod
print(GeneralConfig.bool_to_str)    # {True: 'Si', False: 'Nyet'}

No external dependencies.

How Kofiko works:

  • Configuration should be defined as classes with static attributes. Each class is considered as a configuration section, each class is a configuration option.
  • Once kofiko.configure() is called, kofiko will override the default values defined for each attribute from values in the following order: (1) registered customization functions (2) provided .INI files (3) env. vars (the first override found takes place)
  • Kofiko will automatically derive the type of configuration options from their default values. This also works for lists and dicts.
  • Kofiko supports the following types: string, int, float, bool, list, dict.  
  • Lookup for configuration in env. vars is expecting the following format by default: prefix_section_option (prefix can be omitted).
  • Lookup for configuration in ini files and env. vars is NOT case sensitive by default.  
  • Configuration classes can reside everywhere in your code. They should be registered using the @config_section decorator or explicitly with a call to kofiko.register_section()
  • If you use decorators, you should make sure that the modules that contain configuration classes and customization functions are loaded, before the call to kofiko.configure(). You can do that by performing import on those modules or calling kofiko.register_module().
  • You can modify values in kofiko.Settings before configure() to control Kofiko's behavior, e.g. env. var lookup prefix; lookup case-sensitivity; whether dicts should be appended or replaced; separators for parsing lists and dicts and more.
  • Specific terms like "Config", "Settings", can be omitted from the names of sections when overriding those in .ini or env. vars. For example: DatabaseConfig.port attribute can be overridden from database_port env var.
  • kofiko.configure() returns a dict of all values modified by overrides. Might be useful to log this.

Requirements

Kofiko has no 3rd party requirements other than Python 3.6 or later.

Getting Started

Kofiko is available via PyPi and can be installed using:

pip install kofiko

to-do:

  • Command-Line arguments layer
  • Env-var interpolation
  • Support more types e.g dates
  • Control override order

License:

Apache-2.0

Contributing

Our project welcomes external contributions. Please refer to CONTRIBUTING.md for further details.

Metadata

Release files for kofiko 1.0.7

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

Source distribution (sdist)

Source distribution for kofiko 1.0.7
File Size Uploaded Provenance
kofiko-1.0.7.tar.gz 6.7 kB Without provenance Details

Release files / kofiko-1.0.7.tar.gz

Download URL kofiko-1.0.7.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
2907974bf63429b51ed8e63ca5dafa3c894eb73c36976d95b95ba651257e1027
BLAKE2b-256 checksum
How to use checksums
11092421eabf3e59b7a0d4c9bc12eca902930ee122dc7bd14067b35d4fc6a599
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/40.6.2 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.8

Security for kofiko 1.0.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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