Databind is a library inspired by Jackson-databind to describe and bind data models for object-oriented programming.
Project description
databind.core
Databind is a library inspired by Jackson-databind to describe and bind data models for
object-oriented programming. The databind.core
package provides the abstractions to
generalize the (de-) serialization process such that it can be implemented for arbitrary
data formats.
Databind requires Python 3.6+ because of it's dependency on class-member type hints and
the dataclasses
module (for which there exists a backport from Python 3.7 to 3.6 on
PyPI).
Quickstart
from databind.core import datamodel, field
from typing import Optional
@datamodel
class Person:
""" Class that represents a person's details. """
name: str
age: Optional[int] = field(default=None)
address: Optional[str] = field(default=None)
Then you'll need to pick a serialization library. Below is an example for databind.json
:
from databind import json
person = json.from_str(Person, '{"name": "John Wick", "age": 55}')
assert isinstance(person, Person)
assert person.name == 'John Wick'
print(json.to_str(person))
Databind also makes it easy to define configurable plugin systems:
import abc
from databind.core import datamodel, interface, implementation
from databind.json import from_json, to_json
@interface
class Authenticator(metaclass=abc.ABCMeta):
@abc.abstractmethod
def start_oauth2_session(self) -> 'OAuth2Session':
...
@datamodel
@implementation('github')
class GithubAuthenticator(Authenticator):
client_id: str
client_secret: str
# ...
github = GithubAuthenticator('id', 'secret')
payload = {'type': 'github', 'client_id': 'id', 'client_secret': 'secret'}
assert to_json(github, Authenticator) == payload
assert from_json(Authenticator, payload) == github
Copyright © 2020 – Niklas Rosenstein
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
Hashes for databind.core-0.8.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a07d1fcf5c83bc900ac024e1739384a309edd3fdec381bb34e6b2934c9d37095 |
|
MD5 | be416b748accd636f7337d64ed9d5e07 |
|
BLAKE2b-256 | d93258c5565e9819e60074a30e66ab32a0704243af3a996ea7be408341b11bac |