GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — push2-python/push2_python/classes.py at master · ffont/push2-python Expand file tree/ Copy pathclasses.pyMore file actions 56 lines (47 loc) · 2.46 KB / Copy pathclasses.pyFile metadata and controls56 lines (47 loc) · 2.46 KB 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 54 55 56 import weakref import functools import time def function_call_interval_limit(interval): """Decorator that makes sure the decorated function is only executed once in the given time interval (in seconds). It stores the last time the decorated function was executed and if it was less than "interval" seconds ago, a dummy function is returned instead. This decorator also check at runtime if the first argument of the decorated function call has the porperty "function_call_interval_limit_overwrite" exists. If that is the cases, it uses its value as interval rather than the "interval" value passed in the decorator definition. """ def decorator(func): @functools.wraps(func) def wrapper(*args, **kwargs): current_time = time.time() last_time_called_key = '_last_time_called_{0}'.format(func.__name__) if not hasattr(function_call_interval_limit, last_time_called_key): setattr(function_call_interval_limit, last_time_called_key, current_time) return func(*args, **kwargs) try: # First argument in the func call should be class instance (i.e. self), try to get interval # definition from calss at runtime so it is adjustable new_interval = args[0].function_call_interval_limit_overwrite interval = new_interval except AttributeError: # If property "function_call_interval_limit_overwrite" not found in class instance, just use the interval # given in the decorator definition pass if current_time - getattr(function_call_interval_limit, last_time_called_key) >= interval: setattr(function_call_interval_limit, last_time_called_key, current_time) return func(*args, **kwargs) else: return lambda *args: None return wrapper return decorator class AbstractPush2Section(object): """Abstract class to be inherited when implementing the interfacing with specific sections of Push2. It implements an init method which gets a reference to the main Push2 object and adds a property method to get it de-referenced. """ main_push2_object = None def __init__(self, main_push_object): self.main_push_object = weakref.ref(main_push_object) @property def push(self): return self.main_push_object() # Return de-refernced main Push2 object |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |