Skip to main content

Automatically deserialize complex objects from simple Python types

Project description

dragon

terramare

python: 3.6 | 3.7 | 3.8 license: MIT ci status coverage Checked with mypy Code style: black Conventional Commits

Automatically deserialize complex objects from simple Python types.

Highlights:

  • No boilerplate: terramare uses Python's standard type hints to determine how to construct instances of a class;
  • Format-agnostic: terramare takes simple Python types as input - pass it the output from json.load, toml.load, or yaml.load;
  • Non-invasive: terramare requires no modifications to your existing classes and functions beyond standard type hints;

Example

Deserializing a Simple Class

Consider the following simple class, defined using attrs for brevity:

>>> from typing import List
>>> import attr
>>> import terramare

>>> @attr.s(auto_attribs=True)
... class Example:
...     words: List[str]
...
...     def __str__(self):
...         return " ".join(self.words)

Deserializing an instance of the class from a dictionary is as simple as:

>>> print(terramare.deserialize_into(Example, {"words": ["hello", "world!"]}))
hello world!

Deserializing a More Complex Class

Consider the Person class defined below:

>>> from typing import NamedTuple, NewType, Sequence
>>> import attr
>>> import terramare

    # `terramare` handles NamedTuples
>>> class Location(NamedTuple):
...     longitude: float
...     latitude: float


    # `terramare` handles NewType aliases
>>> JobTitle = NewType("JobTitle", str)


    # `terramare` handles custom classes
>>> class Occupation:
...     def __init__(self, title: JobTitle, field: str):
...         self.title = title
...         self.field = field
...
...     def __eq__(self, other):
...         if isinstance(other, self.__class__):
...             return vars(self) == vars(other)
...         return False
...
...     def __repr__(self):
...         return "Occupation('{0.title}', '{0.field}')".format(self)


>>> @attr.s(auto_attribs=True)
... class Person:
...     name: str
...     age: int
...     friends: Sequence[str]
...
...     # `terramare` handles complex member variable types
...     location: Location
...     occupation: Occupation

Again, deserialization is a single function call:

>>> terramare.deserialize_into(
...     Person,
...     {
...         "name": "Alice",
...         "age": 20,
...         "friends": ["Bob", "Charlie"],
...         "location": [51.5074, 0.1278],
...         "occupation": {"title": "programmer", "field": "technology"}
...     }
... )
Person(name='Alice', age=20, friends=['Bob', 'Charlie'], location=Location(longitude=51.5074, latitude=0.1278), occupation=Occupation('programmer', 'technology'))

Installation

Install using pip:

pip install terramare

Alternatives

Check out:

  • pydantic - "Data validation and settings management using python type annotations". A much more mature library also using Python's standard type hints for deserialization that requires a little more integration with your code;
  • schematics - "...combine types into structures, validate them, and transform the shapes of your data based on simple descriptions". Uses custom types instead of Python's standard type hints;
  • cerberus - "...provides powerful yet simple and lightweight data validation functionality out of the box and is designed to be easily extensible, allowing for custom validation". Schema validation that doesn't change the type of the primitive value.

Project details


Download files

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

Source Distribution

terramare-0.2.0.tar.gz (19.0 kB view details)

Uploaded Source

Built Distribution

terramare-0.2.0-py3-none-any.whl (25.8 kB view details)

Uploaded Python 3

File details

Details for the file terramare-0.2.0.tar.gz.

File metadata

  • Download URL: terramare-0.2.0.tar.gz
  • Upload date:
  • Size: 19.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.2 CPython/3.6.10 Linux/4.19.78-coreos

File hashes

Hashes for terramare-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e08e68dc95623699b2b9b35b661423488ba7d35ab896f2f03de882e07c9eb221
MD5 154623b014bf96377389566efbabe7c7
BLAKE2b-256 58e24ff40827d853119f1a4ef55ee4fadaef3773978dccc6528724d04666bed1

See more details on using hashes here.

File details

Details for the file terramare-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: terramare-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 25.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.2 CPython/3.6.10 Linux/4.19.78-coreos

File hashes

Hashes for terramare-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 75acc62e3eff2cda3e179070a6b7ac1f06b8c21586ca76660712c3f17009ecb8
MD5 3125773c5dcdf6ead169357f0c4011a1
BLAKE2b-256 b0e775fef4a378b5440588c76c086d20d778f0e1612ee0a1c43a56f5f2a56ec2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page