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

Skip to content

Commit 529cfa1

Browse files
committed
Make _define_aliases a class decorator.
1 parent b442049 commit 529cfa1

File tree

5 files changed

+50
-51
lines changed

5 files changed

+50
-51
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2805,14 +2805,13 @@ def _str_lower_equal(obj, s):
28052805
return isinstance(obj, six.string_types) and obj.lower() == s
28062806

28072807

2808-
def _define_aliases(local_d, alias_d):
2809-
"""Define property aliases.
2808+
def _define_aliases(alias_d, cls=None):
2809+
"""Class decorator for defining property aliases.
28102810
2811-
Use in a class definition as ::
2811+
Use as ::
28122812
2813-
cbook._define_aliases(locals(), {
2814-
"property": ["alias", ...], ...
2815-
})
2813+
@cbook._define_aliases({"property": ["alias", ...], ...})
2814+
class C: ...
28162815
28172816
For each property, if the corresponding ``get_property`` is defined in the
28182817
class so far, an alias named ``get_alias`` will be defined; the same will
@@ -2822,6 +2821,8 @@ class so far, an alias named ``get_alias`` will be defined; the same will
28222821
The alias map is stored as the ``_alias_map`` attribute on the class and
28232822
can be used by `~.normalize_kwargs`.
28242823
"""
2824+
if cls is None:
2825+
return functools.partial(_define_aliases, alias_d)
28252826

28262827
def make_alias(name): # Enfore a closure over *name*.
28272828
def method(self, *args, **kwargs):
@@ -2831,14 +2832,15 @@ def method(self, *args, **kwargs):
28312832
for prop, aliases in alias_d.items():
28322833
exists = False
28332834
for prefix in ["get_", "set_"]:
2834-
if prefix + prop in local_d:
2835+
if prefix + prop in vars(cls):
28352836
exists = True
28362837
for alias in aliases:
28372838
method = make_alias(prefix + prop)
28382839
method.__name__ = prefix + alias
28392840
method.__doc__ = "alias for `{}`".format(prefix + prop)
2840-
local_d[prefix + alias] = method
2841+
setattr(cls, prefix + alias, method)
28412842
if not exists:
28422843
raise ValueError("property {} does not exist".format(prop))
28432844

2844-
local_d["_alias_map"] = alias_d
2845+
cls._alias_map = alias_d
2846+
return cls

lib/matplotlib/collections.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
CIRCLE_AREA_FACTOR = 1.0 / np.sqrt(np.pi)
2929

3030

31+
@cbook._define_aliases({
32+
"antialiased": ["antialiaseds"],
33+
"edgecolor": ["edgecolors"],
34+
"facecolor": ["facecolors"],
35+
"linestyle": ["linestyles", "dashes"],
36+
"linewidth": ["linewidths", "lw"],
37+
})
3138
class Collection(artist.Artist, cm.ScalarMappable):
3239
"""
3340
Base class for Collections. Must be subclassed to be usable.
@@ -799,13 +806,6 @@ def update_from(self, other):
799806
# self.update_dict = other.update_dict # do we need to copy this? -JJL
800807
self.stale = True
801808

802-
cbook._define_aliases(locals(), {
803-
"antialiased": ["antialiaseds"],
804-
"edgecolor": ["edgecolors"],
805-
"facecolor": ["facecolors"],
806-
"linestyle": ["linestyles", "dashes"],
807-
"linewidth": ["linewidths", "lw"],
808-
})
809809

810810
# these are not available for the object inspector until after the
811811
# class is built so we define an initial set here for the init

lib/matplotlib/lines.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,25 @@ def _slice_or_none(in_v, slc):
229229
'markevery=%s' % (markevery,))
230230

231231

232+
@cbook._define_aliases({
233+
"antialiased": ["aa"],
234+
"color": ["c"],
235+
"linestyle": ["ls"],
236+
"linewidth": ["lw"],
237+
"markeredgecolor": ["mec"],
238+
"markeredgewidth": ["mew"],
239+
"markerfacecolor": ["mfc"],
240+
"markerfacecoloralt": ["mfcalt"],
241+
"markersize": ["ms"],
242+
})
232243
class Line2D(Artist):
233244
"""
234245
A line - the line can have both a solid linestyle connecting all
235246
the vertices, and a marker at each vertex. Additionally, the
236247
drawing of the solid line is influenced by the drawstyle, e.g., one
237248
can create "stepped" lines in various styles.
238-
239-
240249
"""
250+
241251
lineStyles = _lineStyles = { # hidden names deprecated
242252
'-': '_draw_solid',
243253
'--': '_draw_dashed',
@@ -1347,18 +1357,6 @@ def is_dashed(self):
13471357
'return True if line is dashstyle'
13481358
return self._linestyle in ('--', '-.', ':')
13491359

1350-
cbook._define_aliases(locals(), {
1351-
"antialiased": ["aa"],
1352-
"color": ["c"],
1353-
"linestyle": ["ls"],
1354-
"linewidth": ["lw"],
1355-
"markeredgecolor": ["mec"],
1356-
"markeredgewidth": ["mew"],
1357-
"markerfacecolor": ["mfc"],
1358-
"markerfacecoloralt": ["mfcalt"],
1359-
"markersize": ["ms"],
1360-
})
1361-
13621360

13631361
class VertexSelector(object):
13641362
"""

lib/matplotlib/patches.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
split_bezier_intersecting_with_closedpath, split_path_inout)
1717
from .path import Path
1818

19+
@cbook._define_aliases({
20+
"antialiased": ["aa"],
21+
"edgecolor": ["ec"],
22+
"facecolor": ["fc"],
23+
"linewidth": ["lw"],
24+
"linestyle": ["ls"],
25+
})
1926
class Patch(artist.Artist):
2027
"""
2128
A patch is a 2D artist with a face color and an edge color.
@@ -538,14 +545,6 @@ def get_path(self):
538545
def get_window_extent(self, renderer=None):
539546
return self.get_path().get_extents(self.get_transform())
540547

541-
cbook._define_aliases(locals(), {
542-
"antialiased": ["aa"],
543-
"edgecolor": ["ec"],
544-
"facecolor": ["fc"],
545-
"linewidth": ["lw"],
546-
"linestyle": ["ls"],
547-
})
548-
549548

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

lib/matplotlib/text.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,19 @@ def _get_textbox(text, renderer):
116116
return x_box, y_box, w_box, h_box
117117

118118

119+
@cbook._define_aliases({
120+
"family": ["fontfamily"],
121+
"fontproperties": ["font_properties"],
122+
"horizontalalignment": ["ha"],
123+
"multialignment": ["ma"],
124+
"name": ["fontname"],
125+
"size": ["fontsize"],
126+
"stretch": ["fontstretch"],
127+
"style": ["fontstyle"],
128+
"variant": ["fontvariant"],
129+
"verticalalignment": ["va"],
130+
"weight": ["fontweight"],
131+
})
119132
class Text(Artist):
120133
"""
121134
Handle storing and drawing of text in window or data coordinates.
@@ -1154,19 +1167,6 @@ def set_name(self, fontname): # One-way alias only: the getter differs.
11541167
"""alias for set_family"""
11551168
return self.set_family(fontname)
11561169

1157-
cbook._define_aliases(locals(), {
1158-
"family": ["fontfamily"],
1159-
"fontproperties": ["font_properties"],
1160-
"horizontalalignment": ["ha"],
1161-
"multialignment": ["ma"],
1162-
"name": ["fontname"],
1163-
"size": ["fontsize"],
1164-
"stretch": ["fontstretch"],
1165-
"style": ["fontstyle"],
1166-
"variant": ["fontvariant"],
1167-
"verticalalignment": ["va"],
1168-
"weight": ["fontweight"],
1169-
})
11701170

11711171
docstring.interpd.update(Text=artist.kwdoc(Text))
11721172
docstring.dedent_interpd(Text.__init__)

0 commit comments

Comments
 (0)