Skip to main content

pyDictStore adds automated dictionary storage to properties eliminating the need for code bodies, property getters and setters. It also provides a property change event.

Project description

GitHub release PyPI PyPI - Python Version GitHub

What is pyDictStore

pyDictStore adds automated dictionary storage to properties eliminating the need for code bodies, property getters and setters. It also provides a property change event.

Minimum usage

The minimum usage of this library requires you to add the @storage decorator to your class. This will 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 propperty's 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. To do this requires ignoring normal setter conventions by using a return statement.

:warning: Warning: Overriding the output does not work if you return a value of None.

@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.

:bulb: Note: When the default value is instantiated the PropertyChanged event does not fire.

...event handler 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: ...

...event handler external from 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: ...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyDictStore-1.0.2.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyDictStore-1.0.2-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file pyDictStore-1.0.2.tar.gz.

File metadata

  • Download URL: pyDictStore-1.0.2.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.11

File hashes

Hashes for pyDictStore-1.0.2.tar.gz
Algorithm Hash digest
SHA256 0973ff457fb1f2db9f7e6e78419a5f69441d8c6d1b2285bbb48a4f22284d517a
MD5 1a3ca39dca30acc9aaad47488ec284eb
BLAKE2b-256 913cf7b7722dbc1c0cc83695dedbcb971a79b337fbec0536f1bc2048d3bbce77

See more details on using hashes here.

File details

Details for the file pyDictStore-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: pyDictStore-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.11

File hashes

Hashes for pyDictStore-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 355b8de6bdcbe170dabf5fe7ee52a1428e8337e19d4e0d1961f7f69c3a435a85
MD5 9bf7a5dd930010a9db9dbc28f3297ea3
BLAKE2b-256 3e8d19bb4a57484fe43a22deb0632af21a17140b8dc6edba0f2121d4bfbc505c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page