Skip to main content

Yaml to dataclass loader

Project description

Bentoudev.dataclass

CI (on push) PyPI version

Yaml to dataclass loader. Validates objects based on type information.

Supports folowing types:

  • classes marked as dataclass (from dataclasses)
  • int, str, float, list
  • Enum (from enum)
  • Optional, List (from typing)
  • Dict, Union (from typing)
  • forward references to not yet known types (see example), including self-referencing

Install

pip install bentoudev.dataclass

Documentation

Work in progress, for now, check out examples below or browse the source code.

Example

@dataclass
class Person:
    name: str
    age: int
    money: float

yaml_content = (
    'name: John\n'
    'age: 30\n'
    'money: 400.50'
)

obj = load_yaml_dataclass(Person, 'Person', yaml_content)

assert obj.name == 'John'

Inline loaders

If you need to load complex class from a single value (like string), you can use @inline_loader attribute

import bentoudev.dataclass.yaml_loader
import bentoudev.dataclass.base

@dataclass
@inline_loader(source_type=str, field_name='name')
class ObjFromStr:
    name: str
    foo: int
    bar: float

@dataclass
class Container:
    value: ObjFromStr

obj = load_yaml_dataclass(Container, 'pretty file name', 'value: ThisIsMyName')

assert obj.value.name == 'ThisIsMyName'

Forward references to external types

Sometimes you might want to load dataclass that forward references foreign types, from other modules, in form of a string. In order to support such types, loader must be supplied with list of them.

@dataclass
class MyDataclass:
    foo: Optional['my_namespace.project.model.my_ext_dataclass']

local_types = base.get_types_from_modules([__name__, 'my_namespace.project.model.my_ext_dataclass'])

my_obj: MyDataclass = yaml_loader.load_yaml_dataclass(MyDataclass, 'pretty file name', yaml_content, ext_types=local_types)

Self referencing types

Additionaly to external types, self referencing is also supported

from dataclasses import dataclass
import bentoudev.dataclass.yaml_loader as yaml_loader

@dataclass
class MyDataclass:
    my_string: str
    self_nested: Optional['MyDataclass']
    list_of_sth: List[str]
    user_data: Dict[str, str]

yaml_content = (
    'my_string: foo\n'
    'self_nested:\n'
    '  my_string: bar\n'
    '  list_of_sth: inline_value\n'
    'list_of_sth:\n'
    '- first\n'
    '- second\n'
    'user_data:\n'
    '  anything: goes'
)

my_obj: MyDataclass = yaml_loader.load_yaml_dataclass(MyDataclass, 'pretty file name', yaml_content)

Remember lines

Additional information about source from which obj/field was loaded can be enabled by using @track_source attribute, or setting always_track_source parameter to True (disabled by default, but recomended). Such information is then used to print prettier errors in DataclassLoadError.

class EKind(Enum):
    FIRST = 1
    SECOND = 2

@dataclass
class SomeClass:
    kind: EKind

try:
    obj =  yaml_loader.load_yaml_dataclass(SomeClass, '[SomeClass] my_file.yml', 'kind: THIRD', always_track_source=True)
except DataclassLoadError as err:
    print(err)

Outputs:

error: Got 'THIRD' when expecting enum 'EKind' with one of values: FIRST, SECOND
in "[SomeClass] my_file.yml", line 1, column 1:
kind: THIRD
^ (line: 1)

If you desire to retrieve this information and print error yourself, access it's source field in error, or use injected methods get_root_source or get_field_source.

try:
    obj =  yaml_loader.load_yaml_dataclass(SomeClass, 'broken_file.yml', broken_yaml_content, always_track_source=True)
    field_src = obj.get_field_source('my_field_name')
    print(f"Value location line '{field_src.line_number}', column '{field_src.column_number}'")
except DataclassLoadError as err:
    print(f"Error location line '{err.source.line_number}', column '{err.source.column_number}'")

Additionaly, you can control how many lines are loaded for code snippet and in which format line numbers are presented via error_code_snippet_lines and error_format (Pretty or MSVC compatible).

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

bentoudev.dataclass-1.0.0.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

bentoudev.dataclass-1.0.0-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file bentoudev.dataclass-1.0.0.tar.gz.

File metadata

  • Download URL: bentoudev.dataclass-1.0.0.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for bentoudev.dataclass-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a6daddfea9838a143cdc00387725d29a613952662e030a86c9c28acb84c081c9
MD5 e8d4b204376abdc96923fb6f453f5b77
BLAKE2b-256 1b2bb6be7bbe0a83b0c4f635564871a0d9624c4b54b8b0cd9775236f267276a7

See more details on using hashes here.

File details

Details for the file bentoudev.dataclass-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for bentoudev.dataclass-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b191440501be9585f578c6fd57d642cdbbd18ab2c000f04efc6fffbf16ac1ebd
MD5 fca1d76c9400fed59398d57d5c65a5bb
BLAKE2b-256 ab1e0ff23b407f7106a7970d3b84eb2aaff7a01609fe2a7b3f61eb6034b5b8b3

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