mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-25 06:41:32 +00:00
Implement decorator to lazy initialise a property
This commit is contained in:
parent
e98c41d3cf
commit
06c2fc97ad
@ -2,3 +2,4 @@
|
||||
# All Rights Reserved. See LICENSE file for details.
|
||||
|
||||
from .rate_limiter import RateLimiter
|
||||
from .lazy_initialise import LazyInitialise
|
||||
|
19
lib/tfw/decorators/lazy_initialise.py
Normal file
19
lib/tfw/decorators/lazy_initialise.py
Normal file
@ -0,0 +1,19 @@
|
||||
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
||||
# All Rights Reserved. See LICENSE file for details.
|
||||
|
||||
|
||||
class LazyInitialise:
|
||||
"""
|
||||
Decorator that replaces a function with the value
|
||||
it calculates on the first call.
|
||||
"""
|
||||
def __init__(self, func):
|
||||
self.func = func
|
||||
self.__doc__ = func.__doc__
|
||||
|
||||
def __get__(self, instance, owner):
|
||||
if instance is None:
|
||||
raise TypeError('Cannot get object property from class!')
|
||||
value = self.func(instance)
|
||||
setattr(instance, self.func.__name__, value)
|
||||
return value
|
Loading…
Reference in New Issue
Block a user