Skip to main content

Data binding

Project description


PyPI python TravisCI Coveralls github license

Bind dictionary data into named tuples and dataclasses automatically for typed attribute access throughout the rest of your codebase.

from bindr import bind
from typing import Dict, List, NamedTuple
from yaml import safe_load

class Config(NamedTuple):
    class SMSServiceConfig(NamedTuple):
        host: str
        port: int
        username: str
        password: str

    class S3Config(NamedTuple):
        default_bucket: str
        default_region: str
        max_item_size: int

    support_emails: List[str]
    api_key: str
    timeout_ms: int
    pi: float
    sms_providers: List[SMSServiceConfig]
    s3_settings: S3Config
    accounts: Dict[str, str]

config = bind(Config, safe_load("config.yaml"))
config.s3_settings.max_item_size  # <-- int


Bindr is developed on GitHub and hosted on PyPI. You can fetch Bindr using a simple:

pip install bindr

Why does this exist?

Bindr is not meant to serve as a replacement for 12Factor methodology. There are certain niche cases where you might want to read in a structured file (such as JSON or YAML) and bind it directly to a typed object outside of application configuration (as demonstrated in the example above). In fact, application configuration created as a dictionary (perhaps from environment variables) is still a valid use case for a bound object.

Bindr exists as an alternative to the automatic binding syntax offered by PyYAML. The default object deserialization syntax in PyYAML is a leaky abstraction. Declarative data formats such as YAML should not be concerned with the details of how objects are deserialized in your application code.

Objects generated via Bindr will give you typed objects that can be passed around and verified by MyPy and hinted in PyCharm, which is a distinct advantage over accessing multiple levels deep of nested dictionaries.


MIT License

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for bindr, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size bindr-0.1.1-py2.py3-none-any.whl (5.2 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size bindr-0.1.1.tar.gz (5.3 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page