Wrap a function to detect and flag when it gets called. This is a decorator which takes a function and wraps it in a function with a 'called' attribute. wrapper.called is initialized to False. The wrapper.called attribute is set to False right before each call to the wrapped functi
(func)
| 146 | |
| 147 | |
| 148 | def flag_calls(func): |
| 149 | """Wrap a function to detect and flag when it gets called. |
| 150 | |
| 151 | This is a decorator which takes a function and wraps it in a function with |
| 152 | a 'called' attribute. wrapper.called is initialized to False. |
| 153 | |
| 154 | The wrapper.called attribute is set to False right before each call to the |
| 155 | wrapped function, so if the call fails it remains False. After the call |
| 156 | completes, wrapper.called is set to True and the output is returned. |
| 157 | |
| 158 | Testing for truth in wrapper.called allows you to determine if a call to |
| 159 | func() was attempted and succeeded.""" |
| 160 | |
| 161 | # don't wrap twice |
| 162 | if hasattr(func, "called"): |
| 163 | return func |
| 164 | |
| 165 | def wrapper(*args, **kw): |
| 166 | wrapper.called = False |
| 167 | out = func(*args, **kw) |
| 168 | wrapper.called = True |
| 169 | return out |
| 170 | |
| 171 | wrapper.called = False |
| 172 | wrapper.__doc__ = func.__doc__ |
| 173 | return wrapper |
| 174 | |
| 175 | |
| 176 | def activate_pylab(): |
no outgoing calls
no test coverage detected