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