Coverage for backpack/__init__.py: 100%
13 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-30 23:12 +0000
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-30 23:12 +0000
1''' Utilities for AWS Panorama application development. '''
3__version__ = '0.3.2'
4__author__ = 'Janos Tolgyesi'
6import functools
8def lazy_property(func):
9 ''' Caches the return value of a function, and turns it into a property.
11 Intended to be used as a function decorator::
13 >>> class Foo:
14 >>> @lazy_property
15 >>> def bar(self):
16 >>> print('expensive calculation')
17 >>> return 'bar'
18 >>> foo = Foo()
19 >>> foo.bar()
20 expensive calculation
21 'bar'
22 >>> foo.bar()
23 'bar'
24 '''
25 attrib_name = '_' + func.__name__
26 @property
27 @functools.wraps(func)
28 def lazy_wrapper(instance, *args, **kwargs):
29 if hasattr(instance, attrib_name):
30 return getattr(instance, attrib_name)
31 value = func(instance, *args, **kwargs)
32 object.__setattr__(instance, attrib_name, value)
33 return value
34 return lazy_wrapper