Skip to main content

Serializable IPython Traitlets

Project description

Serializable IPython Traitlets

serializable-traitlets (imported as straitlets) is a Python 2/3 compatible library providing a restricted subset of the classes from IPython Traitlets. Within our restricted subset, we inherit all the benefits of using regular traitlets, including static type declarations, dynamic default generators, and attribute observers/validators.

By supporting only a limited (though still expressive) subset of Python objects, however, we gain the ability to serialize and deserialize instances of Serializable to and from various formats, including:

  1. JSON

  2. YAML

  3. base64-encoded strings

These properties make Serializables well-suited for configuration in environments where objects need to be transferred between processes.

straitlets also provides users the ability to specify example values for traits. If all traits of a Serializable class have examples (or default values) provided, then we can auto-generate an example for the parent class, and we can resursively generate examples for nested classes.

Usage

Basic Usage:

In [1]: from straitlets import Serializable, Integer, Dict, List
In [2]: class Foo(Serializable):
   ...:     my_int = Integer()
   ...:     my_dict = Dict()
   ...:     my_list = List()

In [3]: instance = Foo(my_int=3, my_dict={'a': [1, 2], 'b': (3, 4)}, my_list=[5, None])

In [4]: print(instance.to_json())
{"my_int": 3, "my_dict": {"a": [1, 2], "b": [3, 4]}, "my_list": [5, null]}

In [5]: print(instance.to_yaml())
my_dict:
a:
- 1
- 2
b:
- 3
- 4
my_int: 3
my_list:
- 5
- null

Autogenerating Example Values:

from straitlets import Serializable, Integer, Instance

class Point(Serializable):
    x = Integer().example(0)
    y = Integer().example(0)


class Vector(Serializable):
    # We can automatically generate example values for attributes
    # declared as Instances of Serializable.
    head = Instance(Point)

    # Per-attribute overrides are still supported.
    tail = Instance(Point).example(Point(x=1, y=3))

print(Vector.example_instance().to_yaml())
# head:
#   x: 0
#   y: 0
# tail:
#   x: 1
#   y: 3

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

straitlets-0.2.3.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

straitlets-0.2.3-py2.py3-none-any.whl (19.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file straitlets-0.2.3.tar.gz.

File metadata

  • Download URL: straitlets-0.2.3.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for straitlets-0.2.3.tar.gz
Algorithm Hash digest
SHA256 5abee1b3e6f773280acd0aa9a8ee1a8717903b06afdb68a01a1ca8b9fe4accc5
MD5 7e76bc87d83bbc4f813db50bb677267a
BLAKE2b-256 08ee5f456be848ff86ac29ec76ba26bf2f0ad0c841a4747064a1fbc09555d118

See more details on using hashes here.

Provenance

File details

Details for the file straitlets-0.2.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for straitlets-0.2.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6b549b976fdf30427019ce7eb72ffbb88803dfcd1b446137b291dfc2df7a1510
MD5 c870cffbbe1a54298afc68114abff5b1
BLAKE2b-256 782e06f0ddf09e8f455f300d51fa21a6ad0d7571c9d4a1e290c5bc9a970f36e1

See more details on using hashes here.

Provenance

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