pyDictStore

GitHub release PyPI PyPI - Python Version GitHub

What is pyDictStore

pyDictStore add automated dictionary storage to properties eliminating the need for code bodies for property getters and setters with change event

Minimum usage

The minimum usage of this library requires you to ad the decoragotr @storage to your class. This will automatically automatically wrap your properties with the auto storage capabilities. The default value applied to any property is None. To override this you will need to apply the @default decorator to your propperties getter.

@storage
class ExampleClass(): 
    @property
    def exampleProperty(self): ...
    @exampleProperty.setter
    def exampleProperty(self,value): ...

...with default value

@storage
class ExampleClass(): 
    @property
    @default(10)
    def exampleProperty(self) -> int: ...
    @exampleProperty.setter
    def exampleProperty(self,value) -> None: ...

Overriding the getter and setter

getter

If the getter returns a value other than None it will override the value pulled by pyDictStore. The example below will result in the property always returning 12.

@storage
class ExampleClass(): 
    @property
    @default(10)
    def exampleProperty(self) -> int: 
        return 12

setter

Overriding the setter allows you to modify the value that is saved into storage. This is helpful if you need to perform logic against the value being passed in or if you want to force the storage type such as parsing an integer from a string or storing a boolean value as an integer. The critical aspect which is abnormal for a typical setter is that you need to return a value. The one limitation is that you can not pass a None value.

@storage
class ExampleClass():
    @property 
    @default(10)
    def exampleProperty(self) -> int: ...
    @exampleProperty.setter
    def exampleProperty(self,value) -> None: 
        return value * 3

Event Handling

When the setter of a property is called it will raise a PropertyChanged Event within your class. This provides you the instance of the class that raised the event, the name of the property, the previous value and the new value.

Its important to note that when the default value is instantiated in the property that the event will not fire.

...within class

@storage
class ExampleClass(): 
    def __init__(self) -> None:
        self.PropertyChanged += self.onPropertyChanged


    @staticmethod
    def onPropertyChanged(sender, name:str, oldValue, newValue):
        ... #Your Custom Action here

    @property
    @default(10)
    def exampleProperty(self) -> int: ...
    @exampleProperty.setter
    def exampleProperty(self,value) -> None: ...

...outside of class

def onPropertyChanged(sender, name:str, oldValue, newValue):
    ... #Your Custom Action here

@storage
class ExampleClass(): 
    def __init__(self) -> None:
        self.PropertyChanged += onPropertyChanged

    @property
    @default(10)
    def exampleProperty(self) -> int: ...
    @exampleProperty.setter
    def exampleProperty(self,value) -> None: ...

Library functions

def isDefault(obj: object, prop: str) -> bool:

Checks if an objects property is equal to its default value

def getDefault(obj: object, prop: str) -> object:

Returns the default value of an objects property