8000 Declare all property aliases together. · matplotlib/matplotlib@08a92e5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 08a92e5

Browse files
committed
Declare all property aliases together.
1 parent a5fb352 commit 08a92e5

File tree

5 files changed

+83
-212
lines changed

5 files changed

+83
-212
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2764,3 +2764,37 @@ def _topmost_artist(
27642764
in reverse order.
27652765
"""
27662766
return _cached_max(reversed(artists))
2767+
2768+
2769+
def _define_aliases(local_d, alias_d):
2770+
"""Define property aliases.
2771+
2772+
Use in a class definition as ::
2773+
2774+
cbook._define_aliases(locals(), {
2775+
"property": ["alias", ...], ...
2776+
})
2777+
2778+
For each property, if the corresponding ``get_property`` is defined in the
2779+
class so far, an alias named ``get_alias`` will be defined; the same will
2780+
be done for setters. If neither the getter nor the setter exists, an
2781+
exception will be raised.
2782+
"""
2783+
2784+
def make_alias(name): # Enfore a closure over *name*.
2785+
def method(self, *args, **kwargs):
2786+
return getattr(self, name)(*args, **kwargs)
2787+
method.__doc__ = "alias for {}".format(name)
2788+
return method
2789+
2790+
for prop, aliases in alias_d.items():
2791+
exists = False
2792+
for prefix in ["get_", "set_"]:
2793+
if prefix + prop in local_d:
2794+
exists = True
2795+
for alias in aliases:
2796+
method = make_alias(prefix + prop)
2797+
method.__name__ = str(prefix + alias) # Py2 compat.
2798+
local_d[prefix + alias] = method
2799+
if not exists:
2800+
raise ValueError("property {} does not exist".format(prop))

lib/matplotlib/collections.py

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -517,14 +517,6 @@ def set_linewidth(self, lw):
517517
self._us_lw, self._us_linestyles)
518518
self.stale = True
519519

520-
def set_linewidths(self, lw):
521-
"""alias for set_linewidth"""
522-
return self.set_linewidth(lw)
523-
524-
def set_lw(self, lw):
525-
"""alias for set_linewidth"""
526-
return self.set_linewidth(lw)
527-
528520
def set_linestyle(self, ls):
529521
"""
530522
Set the linestyle(s) for the collection.
@@ -652,14 +644,6 @@ def _bcast_lwls(linewidths, dashes):
652644

653645
return linewidths, dashes
654646

655 67E6 -
def set_linestyles(self, ls):
656-
"""alias for set_linestyle"""
657-
return self.set_linestyle(ls)
658-
659-
def set_dashes(self, ls):
660-
"""alias for set_linestyle"""
661-
return self.set_linestyle(ls)
662-
663647
def set_antialiased(self, aa):
664648
"""
665649
Set the antialiasing state for rendering.
@@ -671,10 +655,6 @@ def set_antialiased(self, aa):
671655
self._antialiaseds = np.atleast_1d(np.asarray(aa, bool))
672656
self.stale = True
673657

674-
def set_antialiaseds(self, aa):
675-
"""alias for set_antialiased"""
676-
return self.set_antialiased(aa)
677-
678658
def set_color(self, c):
679659
"""
680660
Set both the edgecolor and the facecolor.
@@ -716,21 +696,15 @@ def set_facecolor(self, c):
716696
self._original_facecolor = c
717697
self._set_facecolor(c)
718698

719-
def set_facecolors(self, c):
720-
"""alias for set_facecolor"""
721-
return self.set_facecolor(c)
722-
723699
def get_facecolor(self):
724700
return self._facecolors
725-
get_facecolors = get_facecolor
726701

727702
def get_edgecolor(self):
728703
if (isinstance(self._edgecolors, six.string_types)
729704
and self._edgecolors == str('face')):
730705
return self.get_facecolors()
731706
else:
732707
return self._edgecolors
733-
get_edgecolors = get_edgecolor
734708

735709
def _set_edgecolor(self, c):
736710
set_hatch_color = True
@@ -776,10 +750,6 @@ def set_edgecolor(self, c):
776750
self._original_edgecolor = c
777751
self._set_edgecolor(c)
778752

779-
def set_edgecolors(self, c):
780-
"""alias for set_edgecolor"""
781-
return self.set_edgecolor(c)
782-
783753
def set_alpha(self, alpha):
784754
"""
785755
Set the alpha tranparencies of the collection. *alpha* must be
@@ -797,13 +767,11 @@ def set_alpha(self, alpha):
797767
self._set_facecolor(self._original_facecolor)
798768
self._set_edgecolor(self._original_edgecolor)
799769

800-
def get_linewidths(self):
770+
def get_linewidth(self):
801771
return self._linewidths
802-
get_linewidth = get_linewidths
803772

804-
def get_linestyles(self):
773+
def get_linestyle(self):
805774
return self._linestyles
806-
get_dashes = get_linestyle = get_linestyles
807775

808776
def update_scalarmappable(self):
809777
"""
@@ -848,6 +816,14 @@ def update_from(self, other):
848816
# self.update_dict = other.update_dict # do we need to copy this? -JJL
849817
self.stale = True
850818

819+
cbook._define_aliases(locals(), {
820+
"antialiased": ["antialiaseds"],
821+
"edgecolor": ["edgecolors"],
822+
"facecolor": ["facecolors"],
823+
"linestyle": ["linestyles", "dashes"],
824+
"linewidth": ["linewidths", "lw"],
825+
})
826+
851827
# these are not available for the object inspector until after the
852828
# class is built so we define an initial set here for the init
853829
# function and they will be overridden after object defn

lib/matplotlib/lines.py

Lines changed: 13 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import numpy as np
1515

16-
from . import artist, colors as mcolors, docstring, rcParams
16+
from . import artist, cbook, colors as mcolors, docstring, rcParams
1717
from .artist import Artist, allow_rasterization
1818
from .cbook import (
1919
_to_unmasked_float_array, iterable, is_numlike, ls_mapper, ls_mapper_r,
@@ -1268,79 +1268,6 @@ def _get_rgba_face(self, alt=False):
12681268
def _get_rgba_ln_color(self, alt=False):
12691269
return mcolors.to_rgba(self._color, self._alpha)
12701270

1271-
# some aliases....
1272-
def set_aa(self, val):
1273-
'alias for set_antialiased'
1274-
self.set_antialiased(val)
1275-
1276-
def set_c(self, val):
1277-
'alias for set_color'
1278-
self.set_color(val)
1279-
1280-
def set_ls(self, val):
1281-
"""alias for set_linestyle"""
1282-
self.set_linestyle(val)
1283-
1284-
def set_lw(self, val):
1285-
"""alias for set_linewidth"""
1286-
self.set_linewidth(val)
1287-
1288-
def set_mec(self, val):
1289-
"""alias for set_markeredgecolor"""
1290-
self.set_markeredgecolor(val)
1291-
1292-
def set_mew(self, val):
1293-
"""alias for set_markeredgewidth"""
1294-
self.set_markeredgewidth(val)
1295-
1296-
def set_mfc(self, val):
1297-
"""alias for set_markerfacecolor"""
1298-
self.set_markerfacecolor(val)
1299-
1300-
def set_mfcalt(self, val):
1301-
"""alias for set_markerfacecoloralt"""
1302-
self.set_markerfacecoloralt(val)
1303-
1304-
def set_ms(self, val):
1305-
"""alias for set_markersize"""
1306-
self.set_markersize(val)
1307-
1308-
def get_aa(self):
1309-
"""alias for get_antialiased"""
1310-
return self.get_antialiased()
1311-
1312-
def get_c(self):
1313-
"""alias for get_color"""
1314-
return self.get_color()
1315-
1316-
def get_ls(self):
1317-
"""alias for get_linestyle"""
1318-
return self.get_linestyle()
1319-
1320-
def get_lw(self):
1321-
"""alias for get_linewidth"""
1322-
return self.get_linewidth()
1323-
1324-
def get_mec(self):
1325-
"""alias for get_markeredgecolor"""
1326-
return self.get_markeredgecolor()
1327-
1328-
def get_mew(self):
1329-
"""alias for get_markeredgewidth"""
1330-
return self.get_markeredgewidth()
1331-
1332-
def get_mfc(self):
1333-
"""alias for get_markerfacecolor"""
1334-
return self.get_markerfacecolor()
1335-
1336-
def get_mfcalt(self, alt=False):
1337-
"""alias for get_markerfacecoloralt"""
1338-
return self.get_markerfacecoloralt()
1339-
1340-
def get_ms(self):
1341-
"""alias for get_markersize"""
1342-
return self.get_markersize()
1343-
13441271
def set_dash_joinstyle(self, s):
13451272
"""
13461273
Set the join style for dashed linestyles
@@ -1424,6 +1351,18 @@ def is_dashed(self):
14241351
'return True if line is dashstyle'
14251352
return self._linestyle in ('--', '-.', ':')
14261353

1354+
cbook._define_aliases(locals(), {
1355+
"antialiased": ["aa"],
1356+
"color": ["c"],
1357+
"linestyle": ["ls"],
1358+
"linewidth": ["lw"],
1359+
"markeredgecolor": ["mec"],
1360+
"markeredgewidth": ["mew"],
1361+
"markerfacecolor": ["mfc"],
1362+
"markerfacecoloralt": ["mfcalt"],
1363+
"markersize": ["ms"],
1364+
})
1365+
14271366

14281367
class VertexSelector(object):
14291368
"""

lib/matplotlib/patches.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,6 @@ def set_antialiased(self, aa):
255255
self._antialiased = aa
256256
self.stale = True
257257

258-
def set_aa(self, aa):
259-
"""alias for set_antialiased"""
260-
return self.set_antialiased(aa)
261-
262258
def _set_edgecolor(self, color):
263259
set_hatch_color = True
264260
if color is None:
@@ -283,10 +279,6 @@ def set_edgecolor(self, color):
283279
self._original_edgecolor = color
284280
self._set_edgecolor(color)
285281

286-
def set_ec(self, color):
287-
"""alias for set_edgecolor"""
288-
return self.set_edgecolor(color)
289-
290282
def _set_facecolor(self, color):
291283
if color is None:
292284
color = mpl.rcParams['patch.facecolor']
@@ -303,10 +295,6 @@ def set_facecolor(self, color):
303295
self._original_facecolor = color
304296
self._set_facecolor(color)
305297

306-
def set_fc(self, color):
307-
"""alias for set_facecolor"""
308-
return self.set_facecolor(color)
309-
310298
def set_color(self, c):
311299
"""
312300
Set both the edgecolor and the facecolor.
@@ -355,10 +343,6 @@ def set_linewidth(self, w):
355343
offset, ls, self._linewidth)
356344
self.stale = True
357345

358-
def set_lw(self, lw):
359-
"""alias for set_linewidth"""
360-
return self.set_linewidth(lw)
361-
362346
def set_linestyle(self, ls):
363347
"""
364348
Set the patch linestyle
@@ -399,10 +383,6 @@ def set_linestyle(self, ls):
399383
offset, ls, self._linewidth)
400384
self.stale = True
401385

402-
def set_ls(self, ls):
403-
"""alias for set_linestyle"""
404-
return self.set_linestyle(ls)
405-
406386
def set_fill(self, b):
407387
"""
408388
Set whether to fill the patch
@@ -556,6 +536,14 @@ def get_path(self):
556536
def get_window_extent(self, renderer=None):
557537
return self.get_path().get_extents(self.get_transform())
558538

539+
cbook._define_aliases(locals(), {
540+
"antialiased": ["aa"],
541+
"edgecolor": ["ec"],
542+
"facecolor": ["fc"],
543+
"linewidth": ["lw"],
544+
"linestyle": ["ls"],
545+
})
546+
559547

560548
patchdoc = artist.kwdoc(Patch)
561549
for k in ('Rectangle', 'Circle', 'RegularPolygon', 'Polygon', 'Wedge', 'Arrow',

0 commit comments

Comments
 (0)
0