mypythontools.property module — mypythontools 24-08-2021 documentation

# mypythontools.property module

Module contains MyProperty class that edit normal python property to add new features.

There is default setter, it’s possible to auto init values on class init and values in setter can be validated.

It’s possible to set function as a value and it’s evaluated during call.

Example of how can it be used is in module config.

## Examples:

    >>> class MyClass:
    ...     # Init all default values of defined properties
    ...     def __init__(self):
    ...        for j in vars(type(self)).values():
    ...            if type(j) is MyProperty:
    ...                setattr(self, j.private_name, j.init_function)
    ...
    ...     @MyProperty(int)  # New value will be validated whether it's int
    ...     def var() -> int:  # This is for type hints in IDE.
    ...         '''
    ...         Type:
    ...             int
    ...
    ...         Default:
    ...             123
    ...
    ...         This is docstrings (also visible in IDE, because not defined dynamically).'''
    ...
    ...         return 123  # This is initial value that can be edited.
    ...
    ...     @MyProperty()  # Even if you don't need any params, use empty brackets
    ...     def var2():
    ...         return 111
    ...
    >>> myobject = MyClass()
    >>> myobject.var
    123
    >>> myobject.var = 124
    >>> myobject.var
    124

class mypythontools.property.MyProperty(types=None, options=None, fget=None, fset=None, doc=None)

  Bases: `property`

  Python property on steroids. Check module docstrings for more info.

  default_fget(object)
  default_fset(object, content)