Skip to main content

Automatically construct complex objects from simple Python types.

Project description

dragon

terramare

python: 3.6 | 3.7 | 3.8 | 3.9 license: MIT PyPI PyPI - Downloads docs: pages

ci status coverage Checked with mypy Code style: black Conventional Commits

Automatically construct 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;

Full documentation available at https://tomwatson1024.gitlab.io/terramare/.

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.structure({"words": ["hello", "world!"]}, into=Example))
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 [experimental]
>>> @terramare.auto
... 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.structure(
...     {
...         "name": "Alice",
...         "age": 20,
...         "friends": ["Bob", "Charlie"],
...         "location": [51.5074, 0.1278],
...         "occupation": {"title": "programmer", "field": "technology"}
...     },
...     into=Person,
... )
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.5.1.tar.gz (28.8 kB view details)

Uploaded Source

Built Distribution

terramare-0.5.1-py3-none-any.whl (34.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: terramare-0.5.1.tar.gz
  • Upload date:
  • Size: 28.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.6.15 Linux/5.4.109+

File hashes

Hashes for terramare-0.5.1.tar.gz
Algorithm Hash digest
SHA256 2ad7701a0e3da3ae4ec4e6c14810bc13c18f48e2d3953e7d70ccc2fcbf5b33ba
MD5 86575a3d8a18d2d93254902526a634b1
BLAKE2b-256 1fb7c8280c3a193dddccfc2b7634f64c71ff75c458236114450893aced75177f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: terramare-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 34.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.6.15 Linux/5.4.109+

File hashes

Hashes for terramare-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9c1b4966606e655bc352ec6254a61f8fe7b648b8f08a2ddf649aaff1a6cf955c
MD5 1788f62e93af9c1e860c43af008d9429
BLAKE2b-256 7d25d79057b25da272cc0f118bbbfc22fe49a07c0c8312d1a78d780b5d4f3c6f

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