4
4
import six
5
5
6
6
from matplotlib import cbook
7
- import sys
8
- import types
9
7
10
8
11
9
class Substitution (object ):
12
- """
13
- A decorator to take a function's docstring and perform string
14
- substitution on it.
10
+ """A decorator that performs %-substitution on an object's docstring.
15
11
16
- This decorator should be robust even if func .__doc__ is None
17
- (for example, if -OO was passed to the interpreter)
12
+ This decorator should be robust even if obj .__doc__ is None (for example,
13
+ if -OO was passed to the interpreter)
18
14
19
- Usage: construct a docstring.Substitution with a sequence or
20
- dictionary suitable for performing substitution; then
21
- decorate a suitable function with the constructed object. e.g.
15
+ Usage: construct a docstring.Substitution with a sequence or dictionary
16
+ suitable for performing substitution; then decorate a suitable function
17
+ with the constructed object, e.g.::
22
18
23
- sub_author_name = Substitution(author='Jason')
19
+ sub_author_name = Substitution(author='Jason')
24
20
25
- @sub_author_name
26
- def some_function(x):
27
- "%(author)s wrote this function"
21
+ @sub_author_name
22
+ def some_function(x):
23
+ "%(author)s wrote this function"
28
24
29
- # note that some_function.__doc__ is now "Jason wrote this function"
25
+ # note that some_function.__doc__ is now "Jason wrote this function"
30
26
31
- One can also use positional arguments.
27
+ One can also use positional arguments::
32
28
33
- sub_first_last_names = Substitution('Edgar Allen', 'Poe')
29
+ sub_first_last_names = Substitution('Edgar Allen', 'Poe')
34
30
35
- @sub_first_last_names
36
- def some_function(x):
37
- "%s %s wrote the Raven"
31
+ @sub_first_last_names
32
+ def some_function(x):
33
+ "%s %s wrote the Raven"
38
34
"""
39
35
def __init__ (self , * args , ** kwargs ):
40
- assert not ( len ( args ) and len ( kwargs )), \
41
- "Only positional or keyword args are allowed"
36
+ if args and kwargs :
37
+ raise ValueError ( "Only positional or keyword args are allowed" )
42
38
self .params = args or kwargs
43
39
44
40
def __call__ (self , func ):
45
- func .__doc__ = func .__doc__ and func .__doc__ % self .params
41
+ if func .__doc__ :
42
+ if six .PY2 :
43
+ getattr (func , "im_func" , func ).__doc__ %= self .params
44
+ else :
45
+ func .__doc__ %= self .params
46
46
return func
47
47
48
48
def update (self , * args , ** kwargs ):
49
- "Assume self.params is a dict and update it with supplied args"
49
+ """ Assume self.params is a dict and update it with supplied args."" "
50
50
self .params .update (* args , ** kwargs )
51
51
52
52
@classmethod
53
+ @cbook .deprecated ("2.2" )
53
54
def from_params (cls , params ):
54
55
"""
55
56
In the case where the params is a mutable sequence (list or
@@ -62,6 +63,7 @@ def from_params(cls, params):
62
63
return result
63
64
64
65
66
+ @cbook .deprecated ("2.2" )
65
67
class Appender (object ):
66
68
"""
67
69
A function decorator that will append an addendum to the docstring
@@ -86,43 +88,52 @@ def __init__(self, addendum, join=''):
86
88
self .join = join
87
89
88
90
def __call__ (self , func ):
89
- docitems = [ func .__doc__ , self . addendum ]
90
- func . __doc__ = func .__doc__ and self .join .join (docitems )
91
+ if func .__doc__ :
92
+ func .__doc__ = self .join .join ([ func . __doc__ , self . addendum ] )
91
93
return func
92
94
93
95
96
+ @cbook .deprecated ("2.2" )
94
97
def dedent (func ):
95
- "Dedent a docstring (if present)"
96
- func .__doc__ = func .__doc__ and cbook .dedent (func .__doc__ )
98
+ """Dedent a docstring (if present)."""
99
+ if func .__doc__ :
100
+ if six .PY2 :
101
+ getattr (func , "im_func" , func ).__doc__ = cbook .dedent (func .__doc__ )
102
+ else :
103
+ func .__doc__ = cbook .dedent (func .__doc__ )
97
104
return func
98
105
99
106
107
+ @cbook .deprecated ("2.2" )
100
108
def copy (source ):
101
- "Copy a docstring from another source function (if present)"
102
- def do_copy (target ):
109
+ """A decorator that copies the docstring from the source (if present)."" "
110
+ def decorator (target ):
103
111
if source .__doc__ :
104
112
target .__doc__ = source .__doc__
105
113
return target
106
- return do_copy
114
+ return decorator
107
115
108
- # create a decorator that will house the various documentation that
109
- # is reused throughout matplotlib
116
+ # Create a decorator that will house the various documentation that is reused
117
+ # throughout Matplotlib.
110
118
interpd = Substitution ()
111
119
112
120
113
121
def dedent_interpd (func ):
114
- """A special case of the interpd that first performs a dedent on
115
- the incoming docstring"""
116
- if isinstance (func , types .MethodType ) and not six .PY3 :
117
- func = func .im_func
118
- return interpd (dedent (func ))
122
+ """Decorator that dedents and interpolates an object's docstring.
123
+ """
124
+ if func .__doc__ :
125
+ if six .PY2 :
126
+ getattr (func , "im_func" , func ).__doc__ = cbook .dedent (func .__doc__ )
127
+ else :
128
+ func .__doc__ = cbook .dedent (func .__doc__ )
129
+ return interpd (func )
119
130
120
131
132
+ @cbook .deprecated ("2.2" )
121
133
def copy_dedent (source ):
122
- """A decorator that will copy the docstring from the source and
123
- then dedent it"""
124
- # note the following is ugly because "Python is not a functional
125
- # language" - GVR. Perhaps one day, functools.compose will exist.
126
- # or perhaps not.
127
- # http://mail.python.org/pipermail/patches/2007-February/021687.html
128
- return lambda target : dedent (copy (source )(target ))
134
+ """A decorator that copies the dedented docstring from the source."""
135
+ def decorator (func ):
136
+ if source .__doc__ :
137
+ dedent (source )
138
+ return func
139
+ return decorator
0 commit comments