Closed sshmaxime closed 3 years ago
@yukihiko-shinoda up please
Hi, I'm sorry for late to notice this post, and thank you for your question.
Before updating README.md, can I confirm that the following example becomes your help to understand ?
config.yml:
place: Paris
weather: clear
museums:
- Louvre Museum
- Musée d'Orsay
- Musée de l'Orangerie
- Centre Pompidou
- Atelier des Lumières
- Louis Vuitton Foundation
test.py:
from dataclasses import dataclass, field
from yamldataclassconfig.config import YamlDataClassConfig
@dataclass
class Config(YamlDataClassConfig):
place: str = None
weather: str = None
museums: list[str] = field(default_factory=list)
CONFIG: Config = Config()
def main():
CONFIG.load()
print(CONFIG.place)
print(CONFIG.weather)
print(CONFIG.museums)
if __name__ == '__main__':
main()
Result:
$ python test.py
Paris
clear
['Louvre Museum', "Musée d'Orsay", "Musée de l'Orangerie", 'Centre Pompidou', 'Atelier des Lumières', 'Louis Vuitton Foundation']
If you got understand, it's much helps to let me know what point was difficult to understand.
Thanks @yukihiko-shinoda for the answer ! Well it brought an error. It is the same error I encountered myself when trying to make this work with arrays.
Traceback (most recent call last):
File "test.py", line 6, in <module>
class Config(YamlDataClassConfig):
File "test.py", line 9, in Config
museums: list[str] = field(default_factory=list)
TypeError: 'type' object is not subscriptable
Any ideas ?
Python: 3.8 yaml-dataclass-config: 1.5.0
Ok, I solved it using typing
import.
test.py:
from dataclasses import dataclass, field
from typing import List
from yamldataclassconfig.config import YamlDataClassConfig
@dataclass
class Config(YamlDataClassConfig):
place: str = None
weather: str = None
museums: List[str] = field(default_factory=list)
CONFIG: Config = Config()
def main():
CONFIG.load()
print(CONFIG.place)
print(CONFIG.weather)
print(CONFIG.museums)
if __name__ == '__main__':
main()
Are you using Python 3.7 ?
Good! It's better than my example. I tested by Python 3.9.
All good now ! Seems clear to me !
Hello, does it support yaml arrays ? If so, could you please add an example to do so in the README.md please ?