Created
March 26, 2024 03:49
-
-
Save sjshuck/7333f724eda3398df0d08b2305b0ce7e to your computer and use it in GitHub Desktop.
dataclass-wizard demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
from dataclasses import dataclass, field | |
from typing import List | |
from dataclass_wizard import YAMLWizard | |
@dataclass | |
class Thing(YAMLWizard): | |
foo: int = 1 | |
bar: List[str] = field(default_factory = list) | |
def get_things() -> List[Thing]: | |
things = Thing.from_yaml_file('things.yaml') # Thing | List[Thing] | |
assert isinstance(things, list) | |
return things | |
def main() -> None: | |
bar_items = set( | |
bar_item | |
for thing in get_things() | |
for bar_item in thing.bar | |
) | |
print(bar_items) # {'something else', 'something further', 'something'} | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- foo: 42 | |
bar: | |
- something | |
- something else | |
- foo: 10 | |
# default bar is [] | |
- # default foo is 1 | |
bar: | |
- something further |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment