From 64def0c8266daa12832baeb97be931fe1f93d595 Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Wed, 25 Dec 2024 17:38:23 +0100 Subject: [PATCH] Added data_file example. --- wobbl_tools/examples/data_file.py | 64 +++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 wobbl_tools/examples/data_file.py diff --git a/wobbl_tools/examples/data_file.py b/wobbl_tools/examples/data_file.py new file mode 100644 index 0000000..6a0ecb2 --- /dev/null +++ b/wobbl_tools/examples/data_file.py @@ -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() +