Skip to content

Instantly share code, notes, and snippets.

@thanakijwanavit
Last active January 28, 2021 07:23
Show Gist options
  • Save thanakijwanavit/e022b52cb06e9c4637b82b7e557d5c9a to your computer and use it in GitHub Desktop.
Save thanakijwanavit/e022b52cb06e9c4637b82b7e557d5c9a to your computer and use it in GitHub Desktop.
example for dataclasses_json
from dataclasses import dataclass
from dataclasses_json import dataclass_json, Undefined
from dataclasses_json.undefined import UndefinedParameterError
from dataclasses_jsonschema import JsonSchemaMixin
from typing import Optional
@dataclass_json(undefined=Undefined.RAISE)
@dataclass
class Info(JsonSchemaMixin):
conf_one: float
# conf_two: str
conf_three: bool
optional_conf: Optional[str]
@dataclass_json
@dataclass
class ConfStructure:
version: int
info: Info
my_conf = {
'version': 1,
'info': {
'conf_one': 2.5,
'conf_two': 'foo',
'conf_three': False,
'optional_conf': 'bar'
}
}
try:
ConfStructure.from_dict(my_conf).to_dict()
except KeyError as e:
print('theres a missing parameter')
except UndefinedParameterError as e:
print('extra parameters')
ConfStructure.from_dict(my_conf).to_dict()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment