Skip to main content

Represent type-checked specifications as Python classes and incrementally build them.

Project description

Spec Classes

This is a small utility package that provides the spec_cls decorator that adds helper methods to allow users to mutate fields.

Installation

pip install spec-classes

Documentation

There is not much in the way of standalone documentation, but everything is documented fairly thoroughly in the source code, or you can install it and the view the inline documentation using:

>>> from spec_classes import spec_class

>>> help(spec_class)
A class decorator that adds `with_<field>`, `transform_<field>` and
`without_<field>` methods to a class in order to simplify the incremental
building and mutating of specification objects. By default these methods
return a new instance decorated class, with the appropriate field mutated.
...

Examples

@spec_class
class UnkeyedSpec:
    nested_scalar: int = 1
    nested_scalar2: str = 'original value'

@spec_class(key='key')
class KeyedSpec:
    key: str = 'key'
    nested_scalar: int = 1
    nested_scalar2: str = 'original value'


@spec_class(key='key')
class Spec:
    key: str = None
    scalar: int = None
    list_values: List[int] = None
    dict_values: Dict[str, int] = None
    set_values: Set[str] = None
    spec: UnkeyedSpec = None
    spec_list_items: List[UnkeyedSpec] = None
    spec_dict_items: Dict[str, UnkeyedSpec] = None
    keyed_spec_list_items: List[KeyedSpec] = None
    keyed_spec_dict_items: Dict[str, KeyedSpec] = None
    recursive: Spec = None


s = Spec()
spec.with_scalar(3).with_spec_list_item('x')....

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

spec-classes-0.3.0.tar.gz (35.2 kB view hashes)

Uploaded Source

Built Distribution

spec_classes-0.3.0-py3-none-any.whl (28.7 kB view hashes)

Uploaded Python 3

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