Data binding
Project description
bindr
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
Installation
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.
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file bindr-0.1.0.tar.gz
.
File metadata
- Download URL: bindr-0.1.0.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c58a600874e6bb6702e6e7f36889db13b40379f768ac7333dc36012a48c1f12 |
|
MD5 | f6edd6ebd0f1792d92edffc07eb0e734 |
|
BLAKE2b-256 | a9cc029147edc914b1b850b4138a905a7878da1f63bede88699792fd4f8e6886 |
File details
Details for the file bindr-0.1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: bindr-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3db02ef14b7036f9da4fe1637fdf4cd5fc2d638c5bdeeb1739f10d6c9c4fbb47 |
|
MD5 | ef5f9592d8f4a01c07d19aba9afa2db4 |
|
BLAKE2b-256 | 34d93a6b101084c78bfd672e0530ce328e25138eeb70d49ce61d2680d68745f0 |