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

Skip to content

Commit 52c6c77

Browse files
committed
Make _define_aliases a class decorator.
1 parent f789c2a commit 52c6c77

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
@@ -2769,14 +2769,13 @@ def _topmost_artist(
27692769
return _cached_max(reversed(artists))
27702770

27712771

2772-
def _define_aliases(local_d, alias_d):
2773-
"""Define property aliases.
2772+
def _define_aliases(alias_d, cls=None):
2773+
"""Class decorator for defining property aliases.
27742774
2775-
Use in a class definition as ::
2775+
Use as ::
27762776
2777-
cbook._define_aliases(locals(), {
2778-
"property": ["alias", ...], ...
2779-
})
2777+
@cbook._define_aliases({"property": ["alias", ...], ...})
2778+
class C: ...
27802779
27812780
For each property, if the corresponding ``get_property`` is defined in the
27822781
class so far, an alias named ``get_alias`` will be defined; the same will
@@ -2786,6 +2785,8 @@ class so far, an alias named ``get_alias`` will be defined; the same will
27862785
The alias map is stored as the ``_alias_map`` attribute on the class and
27872786
can be used by `~.normalize_kwargs`.
27882787
"""
2788+
if cls is None:
2789+
return functools.partial(_define_aliases, alias_d)
27892790

27902791
def make_alias(name): # Enfore a closure over *name*.
27912792
def method(self, *args, **kwargs):
@@ -2795,14 +2796,15 @@ def method(self, *args, **kwargs):
27952796
for prop, aliases in alias_d.items():
27962797
exists = False
27972798
for prefix in ["get_", "set_"]:
2798-
if prefix + prop in local_d:
2799+
if prefix + prop in vars(cls):
27992800
exists = True
28002801
for alias in aliases:
28012802
method = make_alias(prefix + prop)
28022803
method.__name__ = prefix + alias
28032804
method.__doc__ = "alias for `{}`".format(prefix + prop)
2804-
local_d[prefix + alias] = method
2805+
setattr(cls, prefix + alias, method)
28052806
if not exists:
28062807
raise ValueError("property {} does not exist".format(prop))
28072808

2808-
local_d["_alias_map"] = alias_d
2809+
cls._alias_map = alias_d
2810+
return cls

lib/matplotlib/collections.py

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

3939

40+
@cbook._define_aliases({
41+
"antialiased": ["antialiaseds"],
42+
"edgecolor": ["edgecolors"],
43+
"facecolor": ["facecolors"],
44+
"linestyle": ["linestyles", "dashes"],
45+
"linewidth": ["linewidths", "lw"],
46+
})
4047
class Collection(artist.Artist, cm.ScalarMappable):
4148
"""
4249
Base class for Collections. Must be subclassed to be usable.
@@ -811,13 +818,6 @@ def update_from(self, other):
811818
# self.update_dict = other.update_dict # do we need to copy this? -JJL
812819
self.stale = True
813820

814-
cbook._define_aliases(locals(), {
815-
"antialiased": ["antialiaseds"],
816-
"edgecolor": ["edgecolors"],
817-
"facecolor": ["facecolors"],
818-
"linestyle": ["linestyles", "dashes"],
819-
"linewidth": ["linewidths", "lw"],
820-
})
821821

822822
# these are not available for the object inspector until after the
823823
# 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
@@ -230,15 +230,25 @@ def _slice_or_none(in_v, slc):
230230
'markevery=%s' % (markevery,))
231231

232232

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

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

13661364
class VertexSelector(object):
13671365
"""

lib/matplotlib/patches.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424
from matplotlib.bezier import make_path_regular, concatenate_paths
2525

2626

27+
@cbook._define_aliases({
28+
"antialiased": ["aa"],
29+
"edgecolor": ["ec"],
30+
"facecolor": ["fc"],
31+
"linewidth": ["lw"],
32+
"linestyle": ["ls"],
33+
})
2734
class Patch(artist.Artist):
2835
"""
2936
A patch is a 2D artist with a face color and an edge color.
@@ -524,14 +531,6 @@ def get_path(self):
524531
def get_window_extent(self, renderer=None):
525532
return self.get_path().get_extents(self.get_transform())
526533

527-
cbook._define_aliases(locals(), {
528-
"antialiased": ["aa"],
529-
"edgecolor": ["ec"],
530-
"facecolor": ["fc"],
531-
"linewidth": ["lw"],
532-
"linestyle": ["ls"],
533-
})
534-
535534

536535
patchdoc = artist.kwdoc(Patch)
537536
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
@@ -125,6 +125,19 @@ def _get_textbox(text, renderer):
125125
return x_box, y_box, w_box, h_box
126126

127127

128+
@cbook._define_aliases({
129+
"family": ["fontfamily"],
130+
"fontproperties": ["font_properties"],
131+
"horizontalalignment": ["ha"],
132+
"multialignment": ["ma"],
133+
"name": ["fontname"],
134+
"size": ["fontsize"],
135+
"stretch": ["fontstretch"],
136+
"style": ["fontstyle"],
137+
"variant": ["fontvariant"],
138+
"verticalalignment": ["va"],
139+
"weight": ["fontweight"],
140+
})
128141
class Text(Artist):
129142
"""
130143
Handle storing and drawing of text in window or data coordinates.
@@ -1166,19 +1179,6 @@ def set_name(self, fontname): # One-way alias only: the getter differs.
11661179
"""alias for set_family"""
11671180
return self.set_family(fontname)
11681181

1169-
cbook._define_aliases(locals(), {
1170-
"family": ["fontfamily"],
1171-
"fontproperties": ["font_properties"],
1172-
"horizontalalignment": ["ha"],
1173-
"multialignment": ["ma"],
1174-
"name": ["fontname"],
1175-
"size": ["fontsize"],
1176-
"stretch": ["fontstretch"],
1177-
"style": ["fontstyle"],
1178-
"variant": ["fontvariant"],
1179-
"verticalalignment": ["va"],
1180-
"weight": ["fontweight"],
1181-
})
11821182

11831183
docstring.interpd.update(Text=artist.kwdoc(Text))
11841184
docstring.dedent_interpd(Text.__init__)

0 commit comments

Comments
 (0)