Added data_file example.
This commit is contained in:
parent
5919c1bf8b
commit
64def0c826
1 changed files with 64 additions and 0 deletions
64
wobbl_tools/examples/data_file.py
Normal file
64
wobbl_tools/examples/data_file.py
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
"""
|
||||||
|
Example on how to use the data_file module.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from wobbl_tools.data_file import load_dataclass_json
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ExampleData:
|
||||||
|
example_int: int=256
|
||||||
|
example_str: str="Hello, World!"
|
||||||
|
|
||||||
|
|
||||||
|
class Main:
|
||||||
|
def __init__(self):
|
||||||
|
self.data = load_dataclass_json(
|
||||||
|
dataclass=ExampleData,
|
||||||
|
file_path="example_data.json",
|
||||||
|
app=self,
|
||||||
|
builtin_save=True,
|
||||||
|
builtin_change_event=True
|
||||||
|
)
|
||||||
|
|
||||||
|
self.data.set_attribute_change_event(self.on_data_change)
|
||||||
|
|
||||||
|
print(self.data)
|
||||||
|
"""
|
||||||
|
Output on first run:
|
||||||
|
ExampleData(example_int=256, example_str='Hello, World!')
|
||||||
|
|
||||||
|
Output on second run:
|
||||||
|
ExampleData(example_int=512, example_str='Bye, World!')
|
||||||
|
"""
|
||||||
|
|
||||||
|
self.data.example_int = 512
|
||||||
|
self.data.example_str = "Bye, World!"
|
||||||
|
"""
|
||||||
|
Output on first run:
|
||||||
|
Data changed: example_int 512
|
||||||
|
Data changed: example_str Bye, World!
|
||||||
|
|
||||||
|
Output on second run:
|
||||||
|
Data changed: example_int 512
|
||||||
|
Data changed: example_str Bye, World!
|
||||||
|
"""
|
||||||
|
|
||||||
|
self.data.save("example_data.json")
|
||||||
|
|
||||||
|
def on_data_change(self, key, value):
|
||||||
|
print("Data changed:", key, value)
|
||||||
|
|
||||||
|
"""
|
||||||
|
If this function doesn't return True, the event would be "canceled".
|
||||||
|
"""
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app = Main()
|
||||||
|
|
Loading…
Add table
Reference in a new issue