1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
""" Allows you continually update a function """
# TODO: dependencies
_live_functions_dict = {}
class _live_function:
def __init__(self, func, dependency=None):
self.func = func
self.name = func.__name__
self.live = False
self.dependency = dependency
def __call__(self, *args, **kwargs):
self.live = True
if isinstance(self.dependency, self.__class__):
self.dependency.live = False
return self.func.__call__(*args, **kwargs)
def update(self, func, dependency=None):
self.func = func
if dependency:
self.dependency = dependency
return
def livefunction(f, dependency=None):
""" Wraps a function 'f' in a flexible/interactive way """
# Live functions can "depend" on others for switching from live or not
if dependency in _live_functions_dict:
dependenct = _live_functions_dict[dependency.__name__]
# Add / update a dictionary of all live functions
if f.__name__ not in _live_functions_dict:
_live_functions_dict[f.__name__] = _live_function(f, dependency)
else:
_live_functions_dict[f.__name__].update(f, dependency)
f = _live_functions_dict[f.__name__]
# If the function is "live" call it
if f.live: f.__call__()
return f
if __name__ == "__main__":
# debug
@livefunction
def part1():
return 10
@livefunction
def part2():
return 20
part1()
print(part1.__class__)
|