1
+ import inspect
2
+
1
3
from matplotlib import cbook
2
4
3
5
4
6
class Substitution (object ):
5
7
"""
6
- A decorator to take a function's docstring and perform string
7
- substitution on it.
8
+ A decorator that performs %-substitution on an object's docstring.
8
9
9
- This decorator should be robust even if func .__doc__ is None
10
- (for example, if -OO was passed to the interpreter)
10
+ This decorator should be robust even if ``obj .__doc__`` is None (for
11
+ example, if -OO was passed to the interpreter).
11
12
12
- Usage: construct a docstring.Substitution with a sequence or
13
- dictionary suitable for performing substitution; then
14
- decorate a suitable function with the constructed object. e.g.
13
+ Usage: construct a docstring.Substitution with a sequence or dictionary
14
+ suitable for performing substitution; then decorate a suitable function
15
+ with the constructed object, e.g.::
15
16
16
- sub_author_name = Substitution(author='Jason')
17
+ sub_author_name = Substitution(author='Jason')
17
18
18
- @sub_author_name
19
- def some_function(x):
20
- "%(author)s wrote this function"
19
+ @sub_author_name
20
+ def some_function(x):
21
+ "%(author)s wrote this function"
21
22
22
- # note that some_function.__doc__ is now "Jason wrote this function"
23
+ # note that some_function.__doc__ is now "Jason wrote this function"
23
24
24
- One can also use positional arguments.
25
+ One can also use positional arguments::
25
26
26
- sub_first_last_names = Substitution('Edgar Allen', 'Poe')
27
+ sub_first_last_names = Substitution('Edgar Allen', 'Poe')
27
28
28
- @sub_first_last_names
29
- def some_function(x):
30
- "%s %s wrote the Raven"
29
+ @sub_first_last_names
30
+ def some_function(x):
31
+ "%s %s wrote the Raven"
31
32
"""
32
33
def __init__ (self , * args , ** kwargs ):
33
- assert not ( len ( args ) and len ( kwargs )), \
34
- "Only positional or keyword args are allowed"
34
+ if args and kwargs :
35
+ raise TypeError ( "Only positional or keyword args are allowed" )
35
36
self .params = args or kwargs
36
37
37
38
def __call__ (self , func ):
38
- func .__doc__ = func .__doc__ and func .__doc__ % self .params
39
+ if func .__doc__ :
40
+ func .__doc__ %= self .params
39
41
return func
40
42
41
43
def update (self , * args , ** kwargs ):
42
- "Assume self.params is a dict and update it with supplied args"
44
+ """
45
+ Update ``self.params`` (which must be a dict) with the supplied args.
46
+ """
43
47
self .params .update (* args , ** kwargs )
44
48
45
49
@classmethod
@@ -55,6 +59,7 @@ def from_params(cls, params):
55
59
return result
56
60
57
61
62
+ @cbook .deprecated ("3.1" )
58
63
class Appender (object ):
59
64
"""
60
65
A function decorator that will append an addendum to the docstring
@@ -84,6 +89,7 @@ def __call__(self, func):
84
89
return func
85
90
86
91
92
+ @cbook .deprecated ("3.1" , alternative = "inspect.getdoc()" )
87
93
def dedent (func ):
88
94
"Dedent a docstring (if present)"
89
95
func .__doc__ = func .__doc__ and cbook .dedent (func .__doc__ )
@@ -98,17 +104,19 @@ def do_copy(target):
98
104
return target
99
105
return do_copy
100
106
101
- # create a decorator that will house the various documentation that
102
- # is reused throughout matplotlib
107
+
108
+ # Create a decorator that will house the various docstring snippets reused
109
+ # throughout Matplotlib.
103
110
interpd = Substitution ()
104
111
105
112
106
113
def dedent_interpd (func ):
107
- """A special case of the interpd that first performs a dedent on
108
- the incoming docstring"""
109
- return interpd (dedent ( func ) )
114
+ """Dedent *func*'s docstring, then interpolate it with ``interpd``."""
115
+ func . __doc__ = inspect . getdoc ( func . __doc__ )
116
+ return interpd (func )
110
117
111
118
119
+ @cbook .deprecated ("3.1" , alternative = "docstring.copy() and cbook.dedent()" )
112
120
def copy_dedent (source ):
113
121
"""A decorator that will copy the docstring from the source and
114
122
then dedent it"""
0 commit comments