项目作者: chrisrink10

项目描述 :
Bind data to Python data classes and named tuples
高级语言: Python
项目地址: git://github.com/chrisrink10/bindr.git
创建时间: 2018-11-20T00:24:17Z
项目社区:https://github.com/chrisrink10/bindr

开源协议:MIT License

下载


bindr

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.

  1. from bindr import bind
  2. from typing import Dict, List, NamedTuple
  3. from yaml import safe_load
  4. class Config(NamedTuple):
  5. class SMSServiceConfig(NamedTuple):
  6. host: str
  7. port: int
  8. username: str
  9. password: str
  10. class S3Config(NamedTuple):
  11. default_bucket: str
  12. default_region: str
  13. max_item_size: int
  14. support_emails: List[str]
  15. api_key: str
  16. timeout_ms: int
  17. pi: float
  18. sms_providers: List[SMSServiceConfig]
  19. s3_settings: S3Config
  20. accounts: Dict[str, str]
  21. config = bind(Config, safe_load("config.yaml"))
  22. config.s3_settings.max_item_size # <-- int

Installation

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

  1. 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