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

Skip to content

Commit b44e8f2

Browse files
committed
add support for docstring.hardcopy to format the docstrings less verbosesly for interactive use
svn path=/trunk/matplotlib/; revision=6526
1 parent a956383 commit b44e8f2

File tree

5 files changed

+91
-12
lines changed

5 files changed

+91
-12
lines changed

CHANGELOG

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
2008-12-08 Some of the changes Michael made to improve the output of
2+
the property tables in the rest docs broke of made
3+
difficult to use some of the interactive doc helpers, eg
4+
setp and getp. Having all the rest markup in the ipython
5+
shell also confused the docstrings. I added a new rc param
6+
docstring.harcopy, to format the docstrings differently for
7+
hardcopy and other use. Ther ArtistInspector could use a
8+
little refactoring now since there is duplication of effort
9+
between the rest out put and the non-rest output - JDH
10+
111
2008-12-08 Updated spectral methods (psd, csd, etc.) to scale one-sided
212
densities by a factor of 2 and, optionally, scale all densities
313
by the sampling frequency. This gives better MatLab

doc/matplotlibrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ savefig.dpi : 80 # figure dots per inch
294294
#svg.image_noscale : False # suppress scaling of raster data embedded in SVG
295295
#svg.embed_chars : True # embed character outlines in the SVG file
296296

297+
# docstring params
298+
docstring.hardcopy = True # set this when you want to generate hardcopy docstring
299+
297300
# Set the verbose flags. This controls how much information
298301
# matplotlib gives you at runtime and where it goes. The verbosity
299302
# levels are: silent, helpful, debug, debug-annoying. Any level is
@@ -311,5 +314,6 @@ savefig.dpi : 80 # figure dots per inch
311314
#
312315
# You can access the verbose instance in your code
313316
# from matplotlib import verbose.
317+
314318
#verbose.level : silent # one of silent, helpful, debug, debug-annoying
315319
#verbose.fileo : sys.stdout # a log filename, sys.stdout or sys.stderr

lib/matplotlib/artist.py

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import division
22
import re, warnings
3+
import matplotlib
34
import matplotlib.cbook as cbook
45
from transforms import Bbox, IdentityTransform, TransformedBbox, TransformedPath
56
from path import Path
@@ -640,9 +641,12 @@ def __init__(self, o):
640641
type) and it is your responsibility to make sure this is so.
641642
"""
642643
if cbook.iterable(o) and len(o): o = o[0]
644+
645+
self.oorig = o
643646
if not isinstance(o, type):
644647
o = type(o)
645648
self.o = o
649+
646650
self.aliasd = self.get_aliases()
647651

648652
def get_aliases(self):
@@ -735,7 +739,7 @@ def is_alias(self, o):
735739
if ds is None: return False
736740
return ds.startswith('alias for ')
737741

738-
def aliased_name(self, s, target):
742+
def aliased_name(self, s):
739743
"""
740744
return 'PROPNAME or alias' if *s* has an alias, else return
741745
PROPNAME.
@@ -745,12 +749,29 @@ def aliased_name(self, s, target):
745749
property, which does not, return 'transform'
746750
"""
747751

752+
if s in self.aliasd:
753+
return s + ''.join([' or %s' % x for x in self.aliasd[s].keys()])
754+
else:
755+
return s
756+
757+
758+
def aliased_name_rest(self, s, target):
759+
"""
760+
return 'PROPNAME or alias' if *s* has an alias, else return
761+
PROPNAME formatted for ReST
762+
763+
E.g. for the line markerfacecolor property, which has an
764+
alias, return 'markerfacecolor or mfc' and for the transform
765+
property, which does not, return 'transform'
766+
"""
767+
748768
if s in self.aliasd:
749769
aliases = ''.join([' or %s' % x for x in self.aliasd[s].keys()])
750770
else:
751771
aliases = ''
752772
return ':meth:`%s <%s>`%s' % (s, target, aliases)
753773

774+
754775
def pprint_setters(self, prop=None, leadingspace=2):
755776
"""
756777
If *prop* is *None*, return a list of strings of all settable properies
@@ -772,8 +793,36 @@ def pprint_setters(self, prop=None, leadingspace=2):
772793
attrs.sort()
773794
lines = []
774795

796+
for prop, path in attrs:
797+
accepts = self.get_valid_values(prop)
798+
name = self.aliased_name(prop)
799+
800+
lines.append('%s%s: %s' %(pad, name, accepts))
801+
return lines
802+
803+
def pprint_setters_rest(self, prop=None, leadingspace=2):
804+
"""
805+
If *prop* is *None*, return a list of strings of all settable properies
806+
and their valid values. Format the output for ReST
807+
808+
If *prop* is not *None*, it is a valid property name and that
809+
property will be returned as a string of property : valid
810+
values.
811+
"""
812+
if leadingspace:
813+
pad = ' '*leadingspace
814+
else:
815+
pad = ''
816+
if prop is not None:
817+
accepts = self.get_valid_values(prop)
818+
return '%s%s: %s' %(pad, prop, accepts)
819+
820+
attrs = self._get_setters_and_targets()
821+
attrs.sort()
822+
lines = []
823+
775824
########
776-
names = [self.aliased_name(prop, target) for prop, target in attrs]
825+
names = [self.aliased_name_rest(prop, target) for prop, target in attrs]
777826
accepts = [self.get_valid_values(prop) for prop, target in attrs]
778827

779828
col0_len = max([len(n) for n in names])
@@ -796,7 +845,7 @@ def pprint_setters(self, prop=None, leadingspace=2):
796845

797846
for prop, path in attrs:
798847
accepts = self.get_valid_values(prop)
799-
name = self.aliased_name(prop, path)
848+
name = self.aliased_name_rest(prop, path)
800849

801850
lines.append('%s%s: %s' %(pad, name, accepts))
802851
return lines
@@ -805,20 +854,27 @@ def pprint_getters(self):
805854
"""
806855
Return the getters and actual values as list of strings.
807856
"""
808-
getters = [name for name in dir(self.o)
857+
858+
o = self.oorig
859+
getters = [name for name in dir(o)
809860
if name.startswith('get_')
810-
and callable(getattr(self.o, name))]
861+
and callable(getattr(o, name))]
862+
#print getters
811863
getters.sort()
812864
lines = []
813865
for name in getters:
814-
func = getattr(self.o, name)
866+
func = getattr(o, name)
815867
if self.is_alias(func): continue
868+
816869
try: val = func()
817870
except: continue
818871
if getattr(val, 'shape', ()) != () and len(val)>6:
819872
s = str(val[:6]) + '...'
820873
else:
821874
s = str(val)
875+
s = s.replace('\n', ' ')
876+
if len(s)>50:
877+
s = s[:50] + '...'
822878
name = self.aliased_name(name[4:])
823879
lines.append(' %s = %s' %(name, s))
824880
return lines
@@ -898,16 +954,16 @@ def getp(o, property=None):
898954
insp = ArtistInspector(o)
899955

900956
if property is None:
901-
print '\n'.join(insp.pprint_getters())
957+
ret = insp.pprint_getters()
958+
print '\n'.join(ret)
902959
return
903960

904961
func = getattr(o, 'get_' + property)
905-
return func()
906962

907-
def get(o, *args, **kwargs):
908-
return getp(o, *args, **kwargs)
909-
get.__doc__ = getp.__doc__
963+
return func()
910964

965+
# alias
966+
get = getp
911967

912968
def setp(h, *args, **kwargs):
913969
"""
@@ -984,7 +1040,11 @@ def setp(h, *args, **kwargs):
9841040
return [x for x in cbook.flatten(ret)]
9851041

9861042
def kwdoc(a):
987-
return '\n'.join(ArtistInspector(a).pprint_setters(leadingspace=2))
1043+
hardcopy = matplotlib.rcParams['docstring.hardcopy']
1044+
if hardcopy:
1045+
return '\n'.join(ArtistInspector(a).pprint_setters_rest(leadingspace=2))
1046+
else:
1047+
return '\n'.join(ArtistInspector(a).pprint_setters(leadingspace=2))
9881048

9891049
kwdocd = dict()
9901050
kwdocd['Artist'] = kwdoc(Artist)

lib/matplotlib/rcsetup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,8 @@ def __call__(self, s):
511511
'svg.image_inline' : [True, validate_bool], # write raster image data directly into the svg file
512512
'svg.image_noscale' : [False, validate_bool], # suppress scaling of raster data embedded in SVG
513513
'svg.embed_char_paths' : [True, validate_bool], # True to save all characters as paths in the SVG
514+
515+
'docstring.hardcopy' : [False, validate_bool], # set this when you want to generate hardcopy docstring
514516
'plugins.directory' : ['.matplotlib_plugins', str], # where plugin directory is locate
515517

516518
'path.simplify' : [False, validate_bool],

matplotlibrc.template

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,9 @@ numerix : %(numerix)s # numpy, Numeric or numarray
323323
#svg.image_noscale : False # suppress scaling of raster data embedded in SVG
324324
#svg.embed_char_paths : True # embed character outlines in the SVG file
325325

326+
# docstring params
327+
#docstring.hardcopy = False # set this when you want to generate hardcopy docstring
328+
326329
# Set the verbose flags. This controls how much information
327330
# matplotlib gives you at runtime and where it goes. The verbosity
328331
# levels are: silent, helpful, debug, debug-annoying. Any level is

0 commit comments

Comments
 (0)