Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b43356c

Browse files
committed
added coding guide
svn path=/trunk/matplotlib/; revision=2957
1 parent 7fb83cd commit b43356c

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

CODING_GUIDE

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ in mind.
2121
running simple_plot.py or image_demo.py with --Numeric, --numarray
2222
and --numpy (Note, someone should add examples to
2323
backend_driver.py which explicitly require numpy, numarray and
24-
Numeric so we can automatically catch these
24+
Numeric so we can automatically catch these)
2525

26-
* Can you pass examples/backend_driver.py . This is our poor man's
27-
unit test
26+
* Can you pass examples/backend_driver.py? This is our poor man's
27+
unit test.
2828

2929
* If you have altered extension code, do you pass
30-
unit/memleak_hawaii.py
30+
unit/memleak_hawaii.py?
3131

3232

3333
== Naming conventions ==
3434

35-
functions and class methods : undercase_separated_lower
35+
functions and class methods : lower or lower_underscore_separated
3636

3737
attributes and variables : lower or lowerUpper
3838

@@ -42,7 +42,7 @@ Personally, I prefer the shortest names that are still readable.
4242

4343
== kwargs processing ==
4444

45-
Matplotlib makes extensive use of kwargs so pass through
45+
Matplotlib makes extensive use of **kwargs for pass through
4646
customizations from one function to another, eg the pylab plot ->
4747
Axes.plot pass through. As a general rule, the use of **kwargs should
4848
be reserved for pass-through keyword arguments, eg
@@ -51,16 +51,18 @@ be reserved for pass-through keyword arguments, eg
5151
# do some thing with x, k1
5252
return some_other_func(..., **kwargs)
5353

54-
If I intend for all the kwargs to be used in somefunc alone, I just
55-
use the key/value keyword args in the function definition rather than
56-
the **kwargs idiom. In some cases I want to consume some keys and
57-
pass through the others, in which case I pop the ones I want to use
58-
locally and pass on the rest, eg I pop scalex and scaley in Axes.plot
59-
and assume the rest are Line2D args. Whenever you mutate a kwargs
60-
dictionary, you must first copy it since the user may be explitly
61-
passing in a dictionary which is used across many function calls, eg,
62-
in Axes.plot
54+
If I intend for all the keyword args to be used in somefunc alone, I
55+
just use the key/value keyword args in the function definition rather
56+
than the **kwargs idiom. In some cases I want to consume some keys
57+
and pass through the others, in which case I pop the ones I want to
58+
use locally and pass on the rest, eg I pop scalex and scaley in
59+
Axes.plot and assume the rest are Line2D keyword arguments. Whenever
60+
you mutate a kwargs dictionary (eg by popping it), you must first copy
61+
it since the user may be explitly passing in a dictionary which is
62+
used across many function calls. As an example of a copy, pop,
63+
passthrough usage, see Axes.plot:
6364

65+
def plot(self, *args, **kwargs):
6466
kwargs = kwargs.copy()
6567
scalex = popd(kwargs, 'scalex', True)
6668
scaley = popd(kwargs, 'scaley', True)
@@ -70,14 +72,15 @@ in Axes.plot
7072
self.add_line(line)
7173
lines.append(line)
7274

73-
popd is a matplotlib.cbook function.
75+
popd is a matplotlib.cbook function to pop an item from a dictionary
76+
with a default value if the item doesn't exist
7477

7578
Note there is a use case when kwargs are meant to be used locally in
76-
the function, but you still need the **kwargs idiom. That is when you
77-
want to use *args to allow variable numbers of non keyword args. In
78-
this case, python will not allow you to use named keyword args after
79-
the *args usage, so you will be forces to use kwargs. An example is
80-
matplotlib.contour.ContourLabeler.clabel
79+
the function (not passed on), but you still need the **kwargs idiom.
80+
That is when you want to use *args to allow variable numbers of
81+
non-keyword args. In this case, python will not allow you to use
82+
named keyword args after the *args usage, so you will be forced to use
83+
**kwargs. An example is matplotlib.contour.ContourLabeler.clabel
8184

8285
def clabel(self, *args, **kwargs):
8386
fontsize = kwargs.get('fontsize', None)
@@ -98,7 +101,7 @@ matplotlib uses artist instrospection of docstrings to support
98101
properties. All properties that you want to support through setp and
99102
getp should have a set_property and get_property method in the Artist
100103
class. Yes this is not ideal given python properties or enthought
101-
traits but it is a historical legacy for now. The setter methods use
104+
traits, but it is a historical legacy for now. The setter methods use
102105
the docstring with the ACCEPTS token to indicate the type of argument
103106
the method accepts. Eg in matplotlib.lines.Line2D
104107

@@ -120,8 +123,8 @@ requirements are:
120123
1) single point of configuration so changes to the properties don't
121124
require multiple docstring edits
122125

123-
2) as automated as possible so that as setters and getters change
124-
the docs are updated automagically.
126+
2) as automated as possible so that as properties change the docs
127+
are updated automagically.
125128

126129
I have added a matplotlib.artist.kwdocd to faciliate this. This
127130
combines python string interpolation in the docstring with the
@@ -149,4 +152,11 @@ matplotlib.axes.Axes.plot
149152
pass
150153
plot.__doc__ = plot.__doc__ % artist.kwdocd
151154

155+
Note there is a problem for Artist __init__ methods, eg
156+
Patch.__init__ which supports Patch kwargs, since the artist inspector
157+
cannot work until the class is fully defined and we can't modify the
158+
Patch.__init__.__doc__ docstring outside the class definition. I have
159+
made some manual hacks in this case which violates the "single entry
160+
point" requirement above; hopefully we'll find a more elegant solution
161+
before too long
152162

0 commit comments

Comments
 (0)