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

Skip to content

Commit f80dba2

Browse files
committed
Formatting updates to the docs
svn path=/trunk/matplotlib/; revision=5320
1 parent 4695c53 commit f80dba2

5 files changed

Lines changed: 380 additions & 288 deletions

File tree

doc/devel/coding_guide.rst

Lines changed: 117 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Checking out the main source::
1818
Branch checkouts, eg the maintenance branch::
1919

2020
svn co https://matplotlib.svn.sourceforge.net/svnroot/matplotlib/branches/\
21-
v0_91_maint mpl91
21+
v0_91_maint mpl91 --username=youruser --password=yourpass
2222

2323
Committing changes
2424
==================
@@ -27,26 +27,25 @@ When committing changes to matplotlib, there are a few things to bear
2727
in mind.
2828

2929
* if your changes are non-trivial, please make an entry in the
30-
CHANGELOG
31-
* if you change the API, please document it in API_CHANGES, and
30+
:file:`CHANGELOG`
31+
* if you change the API, please document it in :file:`API_CHANGES`, and
3232
consider posting to mpl-devel
33-
* Are your changes python2.3 compatible? We are still trying to
34-
support 2.3, so avoid 2.4 only features like decorators until we
35-
remove 2.3 support
36-
* Can you pass examples/backend_driver.py? This is our poor man's
37-
unit test.
33+
* Are your changes python2.4 compatible? We are still trying to
34+
support 2.4, so avoid features new to 2.5
35+
* Can you pass :file:`examples/tests/backend_driver.py`? This is our
36+
poor man's unit test.
3837
* If you have altered extension code, do you pass
39-
unit/memleak_hawaii.py?
40-
* if you have added new files or directories, or reorganized
41-
existing ones, are the new files included in the match patterns in
42-
MANIFEST.in. This file determines what goes into the src
38+
:file:`unit/memleak_hawaii.py`?
39+
* if you have added new files or directories, or reorganized existing
40+
ones, are the new files included in the match patterns in
41+
:file:`MANIFEST.in`. This file determines what goes into the source
4342
distribution of the mpl build.
4443
* Keep the maintenance branch and trunk in sync where it makes sense.
45-
If there is a bug on both that needs fixing, use svnmerge.py to
46-
keep them in sync. http://www.orcaware.com/svn/wiki/Svnmerge.py. The
47-
basic procedure is:
44+
If there is a bug on both that needs fixing, use `svnmerge.py
45+
<http://www.orcaware.com/svn/wiki/Svnmerge.py>`_ to keep them in
46+
sync. The basic procedure is:
4847

49-
* install svnmerge.py in your PATH::
48+
* install ``svnmerge.py`` in your PATH::
5049

5150
wget http://svn.collab.net/repos/svn/trunk/contrib/client-side/\
5251
svnmerge/svnmerge.py
@@ -56,11 +55,22 @@ in mind.
5655
it. Make sure you svn upped on the trunk and have no local
5756
modifications, and then from the svn trunk do::
5857

59-
> svnmerge.py merge -rNNN1,NNN2
58+
> svnmerge.py merge
6059

61-
where the NNN* are the revision numbers. Ranges arealso acceptable.
62-
svnmergy.py automatically creates a file containing the commit messages,
63-
so you are ready to make the commit::
60+
If you wish to merge only specific revisions (in an unusual
61+
situation), do::
62+
63+
> svnmerge.py merge -rNNN1-NNN2
64+
65+
where the ``NNN`` are the revision numbers. Ranges are also
66+
acceptable.
67+
68+
The merge may have found some conflicts (code that must be
69+
manually resolved). Correct those conflicts, build matplotlib and
70+
test your choices.
71+
72+
``svnmerge.py`` automatically creates a file containing the commit
73+
messages, so you are ready to make the commit::
6474

6575
> svn commit -F svnmerge-commit-message.txt
6676

@@ -71,7 +81,7 @@ Style Guide
7181
Importing and name spaces
7282
=========================
7383

74-
For numpy, use::
84+
For `numpy <http://www.numpy.org>`_, use::
7585

7686
import numpy as np
7787
a = np.array([1,2,3])
@@ -80,11 +90,11 @@ For masked arrays, use::
8090

8191
from numpy import ma
8292

83-
(The earlier recommendation, 'import matplotlib.numerix.npyma as ma',
84-
was needed temporarily during the development of the maskedarray
85-
implementation as a separate package. As of numpy 1.1, it replaces the
86-
old implementation. Note: "from numpy import ma" works with numpy < 1.1
87-
*and* with numpy >= 1.1. "import numpy.ma as ma" works *only* with
93+
(The earlier recommendation, :samp:`import matplotlib.numerix.npyma as ma`,
94+
was needed temporarily during the development of the maskedarray
95+
implementation as a separate package. As of numpy 1.1, it replaces the
96+
old implementation. Note: ``from numpy import ma`` works with numpy < 1.1
97+
*and* with numpy >= 1.1. ``import numpy.ma as ma`` works *only* with
8898
numpy >= 1.1, so for now we must not use it.)
8999

90100
For matplotlib main module, use::
@@ -99,24 +109,25 @@ For matplotlib modules (or any other modules), use::
99109
if cbook.iterable(z):
100110
pass
101111

102-
We prefer this over the equivalent 'from matplotlib import cbook'
103-
because the latter is ambiguous whether cbook is a module or a
112+
We prefer this over the equivalent ``from matplotlib import cbook``
113+
because the latter is ambiguous whether ``cbook`` is a module or a
104114
function to the new developer. The former makes it explcit that
105115
you are importing a module or package.
106116

107117
Naming, spacing, and formatting conventions
108118
===========================================
109119

110120
In general, we want to hew as closely as possible to the standard
111-
coding guidelines for python written by Guido in
112-
http://www.python.org/dev/peps/pep-0008, though we do not do this
121+
coding guidelines for python written by Guido in `PEP 0008
122+
<http://www.python.org/dev/peps/pep-0008>`_, though we do not do this
113123
throughout.
114124

115-
* functions and class methods: lower or lower_underscore_separated
125+
* functions and class methods: ``lower`` or
126+
``lower_underscore_separated``
116127

117-
* attributes and variables: lower or lowerUpper
128+
* attributes and variables: ``lower`` or ``lowerUpper``
118129

119-
* classes: Upper or MixedCase
130+
* classes: ``Upper`` or ``MixedCase``
120131

121132
Personally, I prefer the shortest names that are still readable.
122133

@@ -129,27 +140,32 @@ Please avoid spurious invisible spaces at the ends of lines.
129140
a file.)
130141

131142
Keep docstrings uniformly indented as in the example below, with
132-
nothing to the left of the triple quotes. The dedent() function
133-
is needed to remove excess indentation only if something will be
134-
interpolated into the docstring, again as in the example above.
143+
nothing to the left of the triple quotes. The
144+
:func:`matplotlib.cbook.dedent` function is needed to remove excess
145+
indentation only if something will be interpolated into the docstring,
146+
again as in the example above.
135147

136148
Limit line length to 80 characters. If a logical line needs to be
137-
longer, use parentheses to break it; do not use an escaped
138-
newline. It may be preferable to use a temporary variable
139-
to replace a single long line with two shorter and more
140-
readable lines.
149+
longer, use parentheses to break it; do not use an escaped newline.
150+
It may be preferable to use a temporary variable to replace a single
151+
long line with two shorter and more readable lines.
141152

142153
Please do not commit lines with trailing white space, as it causes
143-
noise in svn diffs. If you are an emacs user, the following in your
144-
.emacs will cause emacs to strip trailing white space on save for
145-
python, C and C++::
154+
noise in svn diffs.
155+
156+
If you are an emacs user, the following in your ``.emacs`` will cause
157+
emacs to strip trailing white space upon saving for python, C and C++:
158+
159+
.. code-block:: cl
146160
147161
; and similarly for c++-mode-hook and c-mode-hook
148162
(add-hook 'python-mode-hook
149163
(lambda ()
150164
(add-hook 'write-file-functions 'delete-trailing-whitespace)))
151165
152-
for older versions of emacs (emacs<22) you need to do::
166+
for older versions of emacs (emacs<22) you need to do:
167+
168+
.. code-block:: cl
153169
154170
(add-hook 'python-mode-hook
155171
(lambda ()
@@ -160,34 +176,37 @@ Keyword argument processing
160176

161177
Matplotlib makes extensive use of ``**kwargs`` for pass through
162178
customizations from one function to another. A typical example is in
163-
pylab.text, The definition of the pylab text function is a simple
164-
pass-through to axes.Axes.text::
179+
:func:`matplotlib.pylab.text`. The definition of the pylab text
180+
function is a simple pass-through to
181+
:meth:`matplotlib.axes.Axes.text`::
165182

166183
# in pylab.py
167184
def text(*args, **kwargs):
168185
ret = gca().text(*args, **kwargs)
169186
draw_if_interactive()
170187
return ret
171188

172-
axes.Axes.text in simplified form looks like this, ie it just passes
173-
them on to text.Text.__init__::
189+
:meth:`~matplotlib.axes.Axes.text` in simplified form looks like this,
190+
i.e., it just passes them on to :meth:`matplotlib.text.Text.__init__`::
174191

175192
# in axes.py
176193
def text(self, x, y, s, fontdict=None, withdash=False, **kwargs):
177194
t = Text(x=x, y=y, text=s, **kwargs)
178195

179-
and Text.__init__ (again with liberties for illustration) just passes
180-
them on to the artist.Artist.update method::
196+
and :meth:`~matplotlib.text.Text.__init__` (again with liberties for
197+
illustration) just passes them on to the
198+
:meth:`matplotlib.artist.Artist.update` method::
181199

182200
# in text.py
183201
def __init__(self, x=0, y=0, text='', **kwargs):
184202
Artist.__init__(self)
185203
self.update(kwargs)
186204

187-
'update' does the work looking for methods named like 'set_property'
188-
if 'property' is a keyword argument. Ie, noone looks at the keywords,
189-
they just get passed through the API to the artist constructor which
190-
looks for suitably named methods and calls them with the value.
205+
``update`` does the work looking for methods named like
206+
``set_property`` if ``property`` is a keyword argument. I.e., no one
207+
looks at the keywords, they just get passed through the API to the
208+
artist constructor which looks for suitably named methods and calls
209+
them with the value.
191210

192211
As a general rule, the use of ``**kwargs`` should be reserved for
193212
pass-through keyword arguments, as in the examaple above. If I intend
@@ -197,9 +216,10 @@ rather than the ``**kwargs`` idiom.
197216

198217
In some cases I want to consume some keys and pass through the others,
199218
in which case I pop the ones I want to use locally and pass on the
200-
rest, eg I pop scalex and scaley in Axes.plot and assume the rest are
201-
Line2D keyword arguments. As an example of a pop, passthrough
202-
usage, see Axes.plot::
219+
rest, eg., I pop ``scalex`` and ``scaley`` in
220+
:meth:`~matplotlib.axes.Axes.plot` and assume the rest are
221+
:meth:`~matplotlib.lines.Line2D` keyword arguments. As an example of
222+
a pop, passthrough usage, see :meth:`~matplotlib.axes.Axes.plot`::
203223

204224
# in axes.py
205225
def plot(self, *args, **kwargs):
@@ -211,16 +231,17 @@ usage, see Axes.plot::
211231
self.add_line(line)
212232
lines.append(line)
213233

214-
The matplotlib.cbook function popd() is rendered
215-
obsolete by the pop() dictionary method introduced in Python 2.3,
234+
The :mod:`matplotlib.cbook` function :func:`~matplotlib.cbook.popd` is rendered
235+
obsolete by the :func:`~dict.pop` dictionary method introduced in Python 2.3,
216236
so it should not be used for new code.
217237

218-
Note there is a use case when kwargs are meant to be used locally in
219-
the function (not passed on), but you still need the ``**kwargs`` idiom.
220-
That is when you want to use ``*args`` to allow variable numbers of
221-
non-keyword args. In this case, python will not allow you to use
222-
named keyword args after the ``*args`` usage, so you will be forced to use
223-
``**kwargs``. An example is matplotlib.contour.ContourLabeler.clabel::
238+
Note there is a use case when ``kwargs`` are meant to be used locally
239+
in the function (not passed on), but you still need the ``**kwargs``
240+
idiom. That is when you want to use ``*args`` to allow variable
241+
numbers of non-keyword args. In this case, python will not allow you
242+
to use named keyword args after the ``*args`` usage, so you will be
243+
forced to use ``**kwargs``. An example is
244+
:meth:`matplotlib.contour.ContourLabeler.clabel`::
224245

225246
# in contour.py
226247
def clabel(self, *args, **kwargs):
@@ -238,51 +259,54 @@ Documentation and Docstrings
238259
============================
239260

240261
matplotlib uses artist instrospection of docstrings to support
241-
properties. All properties that you want to support through setp and
242-
getp should have a set_property and get_property method in the Artist
243-
class. Yes, this is not ideal given python properties or enthought
244-
traits, but it is a historical legacy for now. The setter methods use
245-
the docstring with the ACCEPTS token to indicate the type of argument
246-
the method accepts. Eg in matplotlib.lines.Line2D::
262+
properties. All properties that you want to support through ``setp``
263+
and ``getp`` should have a ``set_property`` and ``get_property``
264+
method in the :class:`~matplotlib.artist.Artist` class. Yes, this is
265+
not ideal given python properties or enthought traits, but it is a
266+
historical legacy for now. The setter methods use the docstring with
267+
the ACCEPTS token to indicate the type of argument the method accepts.
268+
Eg. in :class:`matplotlib.lines.Line2D`::
247269

248270
# in lines.py
249271
def set_linestyle(self, linestyle):
250272
"""
251273
Set the linestyle of the line
252-
274+
253275
ACCEPTS: [ '-' | '--' | '-.' | ':' | 'steps' | 'None' | ' ' | '' ]
254276
"""
255277

256-
Since matplotlib uses a lot of pass through kwargs, eg in every
257-
function that creates a line (plot, semilogx, semilogy, etc...), it
258-
can be difficult for the new user to know which kwargs are supported.
259-
I have developed a docstring interpolation scheme to support
260-
documentation of every function that takes a ``**kwargs``. The
261-
requirements are:
278+
Since matplotlib uses a lot of pass through ``kwargs``, eg. in every
279+
function that creates a line (:func:`~matplotlib.pyplot.plot`,
280+
:func:`~matplotlib.pyplot.semilogx`,
281+
:func:`~matplotlib.pyplot.semilogy`, etc...), it can be difficult for
282+
the new user to know which ``kwargs`` are supported. I have developed a
283+
docstring interpolation scheme to support documentation of every
284+
function that takes a ``**kwargs``. The requirements are:
262285

263286
1. single point of configuration so changes to the properties don't
264-
require multiple docstring edits
287+
require multiple docstring edits.
265288

266289
2. as automated as possible so that as properties change the docs
267290
are updated automagically.
268291

269-
I have added a matplotlib.artist.kwdocd and kwdoc() to faciliate this.
270-
They combines python string interpolation in the docstring with the
271-
matplotlib artist introspection facility that underlies setp and getp.
272-
The kwdocd is a single dictionary that maps class name to a docstring
273-
of kwargs. Here is an example from matplotlib.lines::
292+
I have added a :attr:`matplotlib.artist.kwdocd` and
293+
:func:`matplotlib.artist.kwdoc` to faciliate this. They combine
294+
python string interpolation in the docstring with the matplotlib
295+
artist introspection facility that underlies ``setp`` and ``getp``. The
296+
``kwdocd`` is a single dictionary that maps class name to a docstring of
297+
``kwargs``. Here is an example from :mod:`matplotlib.lines`::
274298

275299
# in lines.py
276300
artist.kwdocd['Line2D'] = artist.kwdoc(Line2D)
277301

278-
Then in any function accepting Line2D passthrough kwargs, eg
279-
matplotlib.axes.Axes.plot::
302+
Then in any function accepting :class:`~matplotlib.lines.Line2D`
303+
passthrough ``kwargs``, eg. :meth:`matplotlib.axes.Axes.plot`::
280304

281305
# in axes.py
282306
def plot(self, *args, **kwargs):
283307
"""
284308
Some stuff omitted
285-
309+
286310
The kwargs are Line2D properties:
287311
%(Line2D)s
288312

@@ -294,13 +318,14 @@ matplotlib.axes.Axes.plot::
294318
pass
295319
plot.__doc__ = cbook.dedent(plot.__doc__) % artist.kwdocd
296320

297-
Note there is a problem for Artist __init__ methods, eg Patch.__init__
298-
which supports Patch kwargs, since the artist inspector cannot work
321+
Note there is a problem for :class:`~matplotlib.artist.Artist`
322+
``__init__`` methods, eg. :meth:`matplotlib.patches.Patch.__init__`,
323+
which supports ``Patch`` ``kwargs``, since the artist inspector cannot work
299324
until the class is fully defined and we can't modify the
300-
Patch.__init__.__doc__ docstring outside the class definition. I have
325+
``Patch.__init__.__doc__`` docstring outside the class definition. I have
301326
made some manual hacks in this case which violates the "single entry
302327
point" requirement above; hopefully we'll find a more elegant solution
303-
before too long
328+
before too long.
304329

305330
********
306331
Licenses
@@ -314,4 +339,4 @@ main code base, though we are considering an alternative way of
314339
distributing L/GPL code through an separate channel, possibly a
315340
toolkit. If you include code, make sure you include a copy of that
316341
code's license in the license directory if the code's license requires
317-
you to distribute the license with it.
342+
you to distribute the license with it.

0 commit comments

Comments
 (0)