anton

anton.yaml.yaml_conf(cls=None, /, *, conf_path: Union[str, Path, PathLike], init: bool = True, repr: bool = True, eq: bool = True, order: bool = False, unsafe_hash: bool = False, frozen: bool = False)

A super easy to use decorator that wraps the dataclasses.dataclass decorator to provide auto instantiation from yaml definitions with runtime type checking of values.

Parameters:
  • cls – A python class defintion with all the fields. (Refer to the docs of dataclasses.)

  • conf_path – Path to the yaml file containing the appropriate definition.

  • init – If true (the default), a __init__() method will be generated. If the class already defines __init__(), this parameter is ignored.

  • repr – If true (the default), a __repr__() method will be generated.

  • eq – If true (the default), an __eq__() method will be generated.

  • order – If true (the default is False), __lt__(), __le__(), __gt__(), and __ge__() methods will be generated.

  • unsafe_hash – If False (the default), a __hash__() method is generated according to how eq and frozen are set.

  • frozen – If True (the default is False), assigning to fields will generate an exception.

Returns:

A dataclass definition equipped with auto instantiation from YAML files and runtime type checking.

Note

Except conf_path all other arguments to anton.yaml.yaml_conf() are directly passed on to dataclasses.dataclass().

Examples

>>> import tempfile
>>> from dataclasses import dataclass
>>> from anton import yaml_conf
>>>
>>> @dataclass
... class Point:
...     x: int
...     y: int
...
>>> @dataclass
... class LineSegment:
...     first_point: Point
...     second_point: Point
...
>>> temp_file = tempfile.NamedTemporaryFile()
>>> _ = temp_file.write(
... b"""
... integer: 23
... string: "Hello world"
... point:
...     x: 0
...     y: 0
... line_segment:
...     first_point:
...         x: 10
...         y: 10
...     second_point:
...         x: 10
...         y: 10
... """)
>>> temp_file.flush()
>>>
>>> @yaml_conf(conf_path=temp_file.name)
... class ExampleClass:
...     integer: int
...     string: str
...     point: Point
...     line_segment: LineSegment
...
>>> ExampleClass()
ExampleClass(integer=23, string='Hello world', point=Point(x=0, y=0), line_segment=LineSegment(first_point=Point(x=10, y=10), second_point=Point(x=10, y=10)))
anton.json.json_conf(cls=None, /, *, conf_path: Union[str, Path, PathLike], init: bool = True, repr: bool = True, eq: bool = True, order: bool = False, unsafe_hash: bool = False, frozen: bool = False)

A super easy to use decorator that wraps the dataclasses.dataclass decorator to provide auto instantiation from json definitions with runtime type checking of values.

Parameters:
  • cls

    A python class defintion with all the fields. (Refer to the docs of dataclasses.)

  • conf_path – Path to the json file containing the appropriate definition.

  • init – If true (the default), a __init__() method will be generated. If the class already defines __init__(), this parameter is ignored.

  • repr – If true (the default), a __repr__() method will be generated.

  • eq – If true (the default), an __eq__() method will be generated.

  • order – If true (the default is False), __lt__(), __le__(), __gt__(), and __ge__() methods will be generated.

  • unsafe_hash – If False (the default), a __hash__() method is generated according to how eq and frozen are set.

  • frozen – If True (the default is False), assigning to fields will generate an exception.

Returns:

A dataclass definition equipped with auto instantiation from JSON files and runtime type checking.

Note

Except conf_path all other arguments to anton.json.json_conf() are directly passed on to dataclasses.dataclass().

Examples

>>> import tempfile
>>> from dataclasses import dataclass
>>> from anton import json_conf
>>>
>>> @dataclass
... class Point:
...     x: int
...     y: int
...
>>> @dataclass
... class LineSegment:
...     first_point: Point
...     second_point: Point
...
>>> temp_file = tempfile.NamedTemporaryFile()
>>> _ = temp_file.write(
... b"""{
...     "integer": 23,
...     "string": "Hello world",
...     "point": {
...         "x": 0,
...         "y": 0
...     },
...     "line_segment": {
...         "first_point": {
...             "x": 10,
...             "y": 10
...         },
...         "second_point": {
...             "x": 10,
...             "y": 10
...         }
...     }
... }
... """)
>>> temp_file.flush()
>>>
>>> @json_conf(conf_path=temp_file.name)
... class ExampleClass:
...     integer: int
...     string: str
...     point: Point
...     line_segment: LineSegment
...
>>> ExampleClass()
ExampleClass(integer=23, string='Hello world', point=Point(x=0, y=0), line_segment=LineSegment(first_point=Point(x=10, y=10), second_point=Point(x=10, y=10)))