neurone_loader.lazy

Classes

neurone_loader.lazy.Lazy([fget, fset, fdel, doc]) Return a lazy property attribute.

Functions

neurone_loader.lazy.preloadable(cls) Use this as a decorator for a class that contains properties constructed with lazy.Lazy.

Provides the Lazy decorator to construct properties that are evaluated only once and the preloadable decorator to enable optional preloading of all lazy properties on initialization.

class neurone_loader.lazy.Lazy(fget=None, fset=None, fdel=None, doc=None)[source]

Return a lazy property attribute.

This decorator can be used exactly like the property function to turn a function into an attribute, the difference being the following: A function decorated with property is evaluated every time the attribute is accessed. A function decorated with lazy.Lazy is only evaluated once and the result is stored as a private attribute. Subsequently the private attribute is returned when the property constructed with lazy.Lazy is accessed. The lazy property can also be set manually or deleted, just like every other attribute. When the lazy attribute is deleted and then accessed again, the property function is called again and the result stored as a private attribute.

Example:
>>> class Test:
>>>     @Lazy
>>>     def lazy_attribute(self):
>>>         print('lazy function called')
>>>         return 'lazy return'
>>>
>>>     @property
>>>     def property_attribute(self):
>>>         print('property function called')
>>>         return 'property return'
>>>
>>> test_object = Test()
>>> print(test_object.property_attribute)
property function called
property return
>>> print(test_object.property_attribute) # A property function is evaluated on every call
property function called
property return
>>> print(test_object.lazy_attribute) # The lazy function is evaluated on first call
lazy function called
lazy return
>>> print(test_object.lazy_attribute) # but not on subsequent calls
lazy return
>>> del test_object.lazy_attribute    # When deleted the attribute is reset and the
>>> print(test_object.lazy_attribute) # function is evaluated again on next call
lazy function called
lazy return

See also

Decorate your class with the lazy.preloadable attribute to enable optional preloading of all lazy attributes on initialization.

neurone_loader.lazy.preloadable(cls)[source]

Use this as a decorator for a class that contains properties constructed with lazy.Lazy. A class decorated like this can be initialized with preload=True to call every lazy property once and store it’s return value. Optionally the preload function can be used to do the same. It can also be used to reload all lazy properties without deleting them first.

Example:
>>> @preloadable
>>> class Test:
>>>     @Lazy
>>>     def lazy_attribute(self):
>>>         print('lazy function called')
>>>         return 'lazy return'
>>>
>>> test_object = Test(preload=True) # The lazy property is evaluated on initialization
lazy function called
>>> print(test_object.lazy_attribute) # The stored attribute is returned
lazy return
>>> del test_object.lazy_attribute    # When deleted the attribute is reset and the
>>> print(test_object.lazy_attribute) # function is evaluated again on next call
lazy function called
lazy return
>>> test_object.preload() # All properties are reloaded even though already stored
lazy function called