-
Notifications
You must be signed in to change notification settings - Fork 6
/
decorators.py
164 lines (141 loc) · 4.78 KB
/
decorators.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
# coding: utf8
"""
useful decorators
"""
__author__ = "Philippe Guglielmetti"
__copyright__ = "Copyright 2015, Philippe Guglielmetti"
__credits__ = ["http://include.aorcsik.com/2014/05/28/timeout-decorator/"]
__license__ = "LGPL + MIT"
import functools, sys, logging
_gettrace=getattr(sys, 'gettrace', None)
debugger = _gettrace and _gettrace()
logging.info('debugger '+('ACTIVE' if debugger else 'INACTIVE'))
#http://wiki.python.org/moin/PythonDecoratorLibrary
def memoize(obj):
"""speed up repeated calls to a function by caching its results in a dict index by params
:see: https://en.wikipedia.org/wiki/Memoization
"""
cache = obj.cache = {}
@functools.wraps(obj)
def memoizer(*args, **kwargs):
key = str(args) + str(kwargs)
if key not in cache:
cache[key] = obj(*args, **kwargs)
return cache[key]
return memoizer
import logging
def debug(func):
# Customize these messages
ENTRY_MESSAGE = 'Entering {}'
EXIT_MESSAGE = 'Exiting {}'
@functools.wraps(func)
def wrapper(*args, **kwds):
logger=logging.getLogger()
logger.info(ENTRY_MESSAGE.format(func.__name__))
level=logger.getEffectiveLevel()
logger.setLevel(logging.DEBUG)
f_result = func(*args, **kwds)
logger.setLevel(level)
logger.info(EXIT_MESSAGE.format(func.__name__))
return f_result
return wrapper
def nodebug(func):
@functools.wraps(func)
def wrapper(*args, **kwds):
logger=logging.getLogger()
level=logger.getEffectiveLevel()
logger.setLevel(logging.INFO)
f_result = func(*args, **kwds)
logger.setLevel(level)
return f_result
return wrapper
# http://include.aorcsik.com/2014/05/28/timeout-decorator/
# BUT read http://eli.thegreenplace.net/2011/08/22/how-not-to-set-a-timeout-on-a-computation-in-python
import multiprocessing
from multiprocessing.pool import ThreadPool
import _thread as thread
import threading
import weakref
thread_pool = None
def get_thread_pool():
global thread_pool
if thread_pool is None:
# fix for python <2.7.2
if not hasattr(threading.current_thread(), "_children"):
threading.current_thread()._children = weakref.WeakKeyDictionary()
thread_pool = ThreadPool(processes=1)
return thread_pool
def timeout(timeout):
def wrap_function(func):
if not timeout:
return func
@functools.wraps(func)
def __wrapper(*args, **kwargs):
try:
async_result = get_thread_pool().apply_async(func, args=args, kwds=kwargs)
return async_result.get(timeout)
except thread.error:
return func(*args, **kwargs)
return __wrapper
return wrap_function
#https://gist.github.com/goulu/45329ef041a368a663e5
from threading import Timer
from multiprocessing import TimeoutError
def itimeout(iterable,timeout):
"""timeout for loops
:param iterable: any iterable
:param timeout: float max running time in seconds
:yield: items in iterator until timeout occurs
:raise: multiprocessing.TimeoutError if timeout occured
"""
if False : # handle debugger better one day ...
n=100*timeout
for i,x in enumerate(iterable):
yield x
if i>n : break
else:
timer=Timer(timeout,lambda:None)
timer.start()
for x in iterable:
yield x
if timer.finished.is_set():
raise TimeoutError
timer.cancel() #don't forget it, otherwise the thread never finishes...
# https://www.artima.com/weblogs/viewpost.jsp?thread=101605
registry = {}
class MultiMethod(object):
def __init__(self, name):
self.name = name
self.typemap = {}
def __call__(self, *args):
types = tuple(arg.__class__ for arg in args) # a generator expression!
function = self.typemap.get(types)
if function is None:
raise TypeError("no match")
return function(*args)
def register(self, types, function):
if types in self.typemap:
raise TypeError("duplicate registration")
self.typemap[types] = function
def multimethod(*types):
"""
allows to overload functions for various parameter types
@multimethod(int, int)
def foo(a, b):
...code for two ints...
@multimethod(float, float):
def foo(a, b):
...code for two floats...
@multimethod(str, str):
def foo(a, b):
...code for two strings...
"""
def register(function):
name = function.__name__
mm = registry.get(name)
if mm is None:
mm = registry[name] = MultiMethod(name)
mm.register(types, function)
return mm
return register