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

Skip to content

Commit 21b5b3e

Browse files
authored
Merge pull request #12038 from anntzer/ai2
Modernize ArtistInspector a bit...
2 parents b8d06c4 + e13d42c commit 21b5b3e

1 file changed

Lines changed: 34 additions & 83 deletions

File tree

lib/matplotlib/artist.py

Lines changed: 34 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,6 @@
1111
from .path import Path
1212
from .transforms import (Bbox, IdentityTransform, Transform, TransformedBbox,
1313
TransformedPatchPath, TransformedPath)
14-
# Note, matplotlib artists use the doc strings for set and get
15-
# methods to enable the introspection methods of setp and getp. Every
16-
# set_* method should have a docstring containing the line
17-
#
18-
# ACCEPTS: [ legal | values ]
19-
#
20-
# and aliases for setters and getters should have a docstring that
21-
# starts with 'alias for ', as in 'alias for set_somemethod'
22-
#
23-
# You may wonder why we use so much boiler-plate manually defining the
24-
# set_alias and get_alias functions, rather than using some clever
25-
# python trick. The answer is that I need to be able to manipulate
26-
# the docstring, and there is no clever way to do that in python 2.2,
27-
# as far as I can see - see
28-
#
29-
# https://mail.python.org/pipermail/python-list/2004-October/242925.html
3014

3115

3216
def allow_rasterization(draw):
@@ -1181,7 +1165,7 @@ def __init__(self, o):
11811165
o = o[0]
11821166

11831167
self.oorig = o
1184-
if not inspect.isclass(o):
1168+
if not isinstance(o, type):
11851169
o = type(o)
11861170
self.o = o
11871171

@@ -1220,12 +1204,9 @@ def get_valid_values(self, attr):
12201204
"""
12211205
Get the legal arguments for the setter associated with *attr*.
12221206
1223-
This is done by querying the docstring of the function *set_attr*
1224-
for a line that begins with "ACCEPTS" or ".. ACCEPTS":
1225-
1226-
e.g., for a line linestyle, return
1227-
"[ ``'-'`` | ``'--'`` | ``'-.'`` | ``':'`` | ``'steps'`` | ``'None'``
1228-
]"
1207+
This is done by querying the docstring of the setter for a line that
1208+
begins with "ACCEPTS:" or ".. ACCEPTS:", and then by looking for a
1209+
numpydoc-style documentation for the setter's first argument.
12291210
"""
12301211

12311212
name = 'set_%s' % attr
@@ -1258,7 +1239,6 @@ def _get_setters_and_targets(self):
12581239
Get the attribute strings and a full path to where the setter
12591240
is defined for all setters in an object.
12601241
"""
1261-
12621242
setters = []
12631243
for name in dir(self.o):
12641244
if not name.startswith('set_'):
@@ -1283,10 +1263,8 @@ def _replace_path(self, source_class):
12831263
Changes the full path to the public API path that is used
12841264
in sphinx. This is needed for links to work.
12851265
"""
1286-
12871266
replace_dict = {'_base._AxesBase': 'Axes',
1288-
'_axes.Axes': 'Axes'}
1289-
1267+
'_axes.Axes': 'Axes'}
12901268
for key, value in replace_dict.items():
12911269
source_class = source_class.replace(key, value)
12921270
return source_class
@@ -1296,50 +1274,36 @@ def get_setters(self):
12961274
Get the attribute strings with setters for object. e.g., for a line,
12971275
return ``['markerfacecolor', 'linewidth', ....]``.
12981276
"""
1299-
13001277
return [prop for prop, target in self._get_setters_and_targets()]
13011278

13021279
def is_alias(self, o):
1303-
"""
1304-
Return *True* if method object *o* is an alias for another
1305-
function.
1306-
"""
1280+
"""Return whether method object *o* is an alias for another method."""
13071281
ds = o.__doc__
13081282
if ds is None:
13091283
return False
13101284
return ds.startswith('Alias for ')
13111285

13121286
def aliased_name(self, s):
13131287
"""
1314-
return 'PROPNAME or alias' if *s* has an alias, else return
1315-
PROPNAME.
1288+
Return 'PROPNAME or alias' if *s* has an alias, else return 'PROPNAME'.
13161289
13171290
e.g., for the line markerfacecolor property, which has an
13181291
alias, return 'markerfacecolor or mfc' and for the transform
1319-
property, which does not, return 'transform'
1292+
property, which does not, return 'transform'.
13201293
"""
1321-
1322-
if s in self.aliasd:
1323-
return s + ''.join([' or %s' % x
1324-
for x in sorted(self.aliasd[s])])
1325-
else:
1326-
return s
1294+
aliases = ''.join(' or %s' % x for x in sorted(self.aliasd.get(s, [])))
1295+
return s + aliases
13271296

13281297
def aliased_name_rest(self, s, target):
13291298
"""
1330-
return 'PROPNAME or alias' if *s* has an alias, else return
1331-
PROPNAME formatted for ReST
1299+
Return 'PROPNAME or alias' if *s* has an alias, else return 'PROPNAME',
1300+
formatted for ReST.
13321301
13331302
e.g., for the line markerfacecolor property, which has an
13341303
alias, return 'markerfacecolor or mfc' and for the transform
1335-
property, which does not, return 'transform'
1304+
property, which does not, return 'transform'.
13361305
"""
1337-
1338-
if s in self.aliasd:
1339-
aliases = ''.join([' or %s' % x
1340-
for x in sorted(self.aliasd[s])])
1341-
else:
1342-
aliases = ''
1306+
aliases = ''.join(' or %s' % x for x in sorted(self.aliasd.get(s, [])))
13431307
return ':meth:`%s <%s>`%s' % (s, target, aliases)
13441308

13451309
def pprint_setters(self, prop=None, leadingspace=2):
@@ -1387,52 +1351,43 @@ def pprint_setters_rest(self, prop=None, leadingspace=4):
13871351
accepts = self.get_valid_values(prop)
13881352
return '%s%s: %s' % (pad, prop, accepts)
13891353

1390-
attrs = self._get_setters_and_targets()
1391-
attrs.sort()
1354+
attrs = sorted(self._get_setters_and_targets())
13921355
lines = []
13931356

1394-
########
13951357
names = [self.aliased_name_rest(prop, target)
13961358
for prop, target in attrs]
13971359
accepts = [self.get_valid_values(prop) for prop, target in attrs]
13981360

13991361
col0_len = max(len(n) for n in names)
14001362
col1_len = max(len(a) for a in accepts)
1401-
1402-
lines.append('')
1403-
lines.append(pad + '.. table::')
1404-
lines.append(pad + ' :class: property-table')
1405-
pad += ' '
1406-
1407-
table_formatstr = pad + '=' * col0_len + ' ' + '=' * col1_len
1408-
1409-
lines.append('')
1410-
lines.append(table_formatstr)
1411-
lines.append(pad + 'Property'.ljust(col0_len + 3) +
1412-
'Description'.ljust(col1_len))
1413-
lines.append(table_formatstr)
1414-
1415-
lines.extend([pad + n.ljust(col0_len + 3) + a.ljust(col1_len)
1416-
for n, a in zip(names, accepts)])
1417-
1418-
lines.append(table_formatstr)
1419-
lines.append('')
1420-
return lines
1363+
table_formatstr = pad + ' ' + '=' * col0_len + ' ' + '=' * col1_len
1364+
1365+
return [
1366+
'',
1367+
pad + '.. table::',
1368+
pad + ' :class: property-table',
1369+
'',
1370+
table_formatstr,
1371+
pad + ' ' + 'Property'.ljust(col0_len)
1372+
+ ' ' + 'Description'.ljust(col1_len),
1373+
table_formatstr,
1374+
*[pad + ' ' + n.ljust(col0_len) + ' ' + a.ljust(col1_len)
1375+
for n, a in zip(names, accepts)],
1376+
table_formatstr,
1377+
'',
1378+
]
14211379

14221380
def properties(self):
1423-
"""
1424-
return a dictionary mapping property name -> value
1425-
"""
1381+
"""Return a dictionary mapping property name -> value."""
14261382
o = self.oorig
14271383
getters = [name for name in dir(o)
14281384
if name.startswith('get_') and callable(getattr(o, name))]
14291385
getters.sort()
1430-
d = dict()
1386+
d = {}
14311387
for name in getters:
14321388
func = getattr(o, name)
14331389
if self.is_alias(func):
14341390
continue
1435-
14361391
try:
14371392
with warnings.catch_warnings():
14381393
warnings.simplefilter('ignore')
@@ -1441,14 +1396,10 @@ def properties(self):
14411396
continue
14421397
else:
14431398
d[name[4:]] = val
1444-
14451399
return d
14461400

14471401
def pprint_getters(self):
1448-
"""
1449-
Return the getters and actual values as list of strings.
1450-
"""
1451-
1402+
"""Return the getters and actual values as list of strings."""
14521403
lines = []
14531404
for name, val in sorted(self.properties().items()):
14541405
if getattr(val, 'shape', ()) != () and len(val) > 6:

0 commit comments

Comments
 (0)