Show More
@@ -1,54 +1,58 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """Decorators that don't go anywhere else. |
|
3 | 3 | |
|
4 | 4 | This module contains misc. decorators that don't really go with another module |
|
5 | 5 | in :mod:`IPython.utils`. Beore putting something here please see if it should |
|
6 | 6 | go into another topical module in :mod:`IPython.utils`. |
|
7 | 7 | """ |
|
8 | 8 | |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | # Copyright (C) 2008-2011 The IPython Development Team |
|
11 | 11 | # |
|
12 | 12 | # Distributed under the terms of the BSD License. The full license is in |
|
13 | 13 | # the file COPYING, distributed as part of this software. |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | # Imports |
|
18 | 18 | #----------------------------------------------------------------------------- |
|
19 | 19 | |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | # Code |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | |
|
24 | 24 | def flag_calls(func): |
|
25 | 25 | """Wrap a function to detect and flag when it gets called. |
|
26 | 26 | |
|
27 | 27 | This is a decorator which takes a function and wraps it in a function with |
|
28 | 28 | a 'called' attribute. wrapper.called is initialized to False. |
|
29 | 29 | |
|
30 | 30 | The wrapper.called attribute is set to False right before each call to the |
|
31 | 31 | wrapped function, so if the call fails it remains False. After the call |
|
32 | 32 | completes, wrapper.called is set to True and the output is returned. |
|
33 | 33 | |
|
34 | 34 | Testing for truth in wrapper.called allows you to determine if a call to |
|
35 | 35 | func() was attempted and succeeded.""" |
|
36 | ||
|
37 | # don't wrap twice | |
|
38 | if hasattr(func, 'called'): | |
|
39 | return func | |
|
36 | 40 | |
|
37 | 41 | def wrapper(*args,**kw): |
|
38 | 42 | wrapper.called = False |
|
39 | 43 | out = func(*args,**kw) |
|
40 | 44 | wrapper.called = True |
|
41 | 45 | return out |
|
42 | 46 | |
|
43 | 47 | wrapper.called = False |
|
44 | 48 | wrapper.__doc__ = func.__doc__ |
|
45 | 49 | return wrapper |
|
46 | 50 | |
|
47 | 51 | def undoc(func): |
|
48 | 52 | """Mark a function or class as undocumented. |
|
49 | 53 | |
|
50 | 54 | This is found by inspecting the AST, so for now it must be used directly |
|
51 | 55 | as @undoc, not as e.g. @decorators.undoc |
|
52 | 56 | """ |
|
53 | 57 | return func |
|
54 | 58 |
General Comments 0
You need to be logged in to leave comments.
Login now