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

Skip to content

Commit 9ef6065

Browse files
committed
Use lru_cache in *_class_factory.
1 parent 4818c27 commit 9ef6065

File tree

4 files changed

+38
-72
lines changed

4 files changed

+38
-72
lines changed

lib/matplotlib/axes/_subplots.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import functools
12
import warnings
23

34
from matplotlib import docstring
@@ -183,9 +184,8 @@ def _make_twin_axes(self, *kl, **kwargs):
183184
self._twinned_axes.join(self, ax2)
184185
return ax2
185186

186-
_subplot_classes = {}
187-
188187

188+
@functools.lru_cache(None)
189189
def subplot_class_factory(axes_class=None):
190190
# This makes a new class that inherits from SubplotBase and the
191191
# given axes_class (which is assumed to be a subclass of Axes).
@@ -194,15 +194,10 @@ def subplot_class_factory(axes_class=None):
194194
# not have to be created for every type of Axes.
195195
if axes_class is None:
196196
axes_class = Axes
197+
return type("%sSubplot" % axes_class.__name__,
198+
(SubplotBase, axes_class),
199+
{'_axes_class': axes_class})
197200

198-
new_class = _subplot_classes.get(axes_class)
199-
if new_class is None:
200-
new_class = type(str("%sSubplot") % (axes_class.__name__),
201-
(SubplotBase, axes_class),
202-
{'_axes_class': axes_class})
203-
_subplot_classes[axes_class] = new_class
204-
205-
return new_class
206201

207202
# This is provided for backward compatibility
208203
Subplot = subplot_class_factory()

lib/matplotlib/tests/test_axes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,7 @@ def __init__(self):
17791779
def _as_mpl_axes(self):
17801780
# implement the matplotlib axes interface
17811781
return PolarAxes, {'theta_offset': self.theta_offset}
1782+
17821783
prj = Polar()
17831784
prj2 = Polar()
17841785
prj2.theta_offset = np.pi
@@ -1793,7 +1794,7 @@ def _as_mpl_axes(self):
17931794

17941795
# testing axes creation with gca
17951796
ax = plt.gca(projection=prj)
1796-
assert type(ax) == maxes._subplots._subplot_classes[PolarAxes]
1797+
assert type(ax) == maxes._subplots.subplot_class_factory(PolarAxes)
17971798
ax_via_gca = plt.gca(projection=prj)
17981799
assert ax_via_gca is ax
17991800
# try getting the axes given a different polar projection
@@ -1814,7 +1815,7 @@ def _as_mpl_axes(self):
18141815

18151816
# testing axes creation with subplot
18161817
ax = plt.subplot(121, projection=prj)
1817-
assert type(ax) == maxes._subplots._subplot_classes[PolarAxes]
1818+
assert type(ax) == maxes._subplots.subplot_class_factory(PolarAxes)
18181819
plt.close()
18191820

18201821

lib/mpl_toolkits/axes_grid1/parasite_axes.py

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import six
1+
import functools
22

33
from matplotlib import (
44
artist as martist, collections as mcoll, transforms as mtransforms,
@@ -40,32 +40,21 @@ def cla(self):
4040
self.yaxis.set_zorder(2.5)
4141

4242

43-
_parasite_axes_classes = {}
43+
@functools.lru_cache(None)
4444
def parasite_axes_class_factory(axes_class=None):
4545
if axes_class is None:
4646
axes_class = Axes
4747

48-
new_class = _parasite_axes_classes.get(axes_class)
49-
if new_class is None:
50-
def _get_base_axes_attr(self, attrname):
51-
return getattr(axes_class, attrname)
48+
def _get_base_axes_attr(self, attrname):
49+
return getattr(axes_class, attrname)
5250

53-
new_class = type(str("%sParasite" % (axes_class.__name__)),
54-
(ParasiteAxesBase, axes_class),
55-
{'_get_base_axes_attr': _get_base_axes_attr})
56-
_parasite_axes_classes[axes_class] = new_class
51+
return type("%sParasite" % axes_class.__name__,
52+
(ParasiteAxesBase, axes_class),
53+
{'_get_base_axes_attr': _get_base_axes_attr})
5754

58-
return new_class
5955

6056
ParasiteAxes = parasite_axes_class_factory()
6157

62-
# #class ParasiteAxes(ParasiteAxesBase, Axes):
63-
64-
# @classmethod
65-
# def _get_base_axes_attr(cls, attrname):
66-
# return getattr(Axes, attrname)
67-
68-
6958

7059
class ParasiteAxesAuxTransBase(object):
7160
def __init__(self, parent_axes, aux_transform, viewlim_mode=None,
@@ -189,30 +178,22 @@ def apply_aspect(self, position=None):
189178
#ParasiteAxes.apply_aspect()
190179

191180

192-
193-
_parasite_axes_auxtrans_classes = {}
181+
@functools.lru_cache(None)
194182
def parasite_axes_auxtrans_class_factory(axes_class=None):
195183
if axes_class is None:
196184
parasite_axes_class = ParasiteAxes
197185
elif not issubclass(axes_class, ParasiteAxesBase):
198186
parasite_axes_class = parasite_axes_class_factory(axes_class)
199187
else:
200188
parasite_axes_class = axes_class
201-
202-
new_class = _parasite_axes_auxtrans_classes.get(parasite_axes_class)
203-
if new_class is None:
204-
new_class = type(str("%sParasiteAuxTrans" % (parasite_axes_class.__name__)),
205-
(ParasiteAxesAuxTransBase, parasite_axes_class),
206-
{'_parasite_axes_class': parasite_axes_class,
207-
'name': 'parasite_axes'})
208-
_parasite_axes_auxtrans_classes[parasite_axes_class] = new_class
209-
210-
return new_class
211-
212-
213-
ParasiteAxesAuxTrans = parasite_axes_auxtrans_class_factory(axes_class=ParasiteAxes)
189+
return type("%sParasiteAuxTrans" % parasite_axes_class.__name__,
190+
(ParasiteAxesAuxTransBase, parasite_axes_class),
191+
{'_parasite_axes_class': parasite_axes_class,
192+
'name': 'parasite_axes'})
214193

215194

195+
ParasiteAxesAuxTrans = parasite_axes_auxtrans_class_factory(
196+
axes_class=ParasiteAxes)
216197

217198

218199
def _get_handles(ax):
@@ -391,33 +372,29 @@ def get_tightbbox(self, renderer, call_axes_locator=True):
391372
return _bbox
392373

393374

394-
_host_axes_classes = {}
375+
@functools.lru_cache(None)
395376
def host_axes_class_factory(axes_class=None):
396377
if axes_class is None:
397378
axes_class = Axes
398379

399-
new_class = _host_axes_classes.get(axes_class)
400-
if new_class is None:
401-
def _get_base_axes(self):
402-
return axes_class
380+
def _get_base_axes(self):
381+
return axes_class
403382

404-
def _get_base_axes_attr(self, attrname):
405-
return getattr(axes_class, attrname)
383+
def _get_base_axes_attr(self, attrname):
384+
return getattr(axes_class, attrname)
406385

407-
new_class = type(str("%sHostAxes" % (axes_class.__name__)),
408-
(HostAxesBase, axes_class),
409-
{'_get_base_axes_attr': _get_base_axes_attr,
410-
'_get_base_axes': _get_base_axes})
386+
return type("%sHostAxes" % axes_class.__name__,
387+
(HostAxesBase, axes_class),
388+
{'_get_base_axes_attr': _get_base_axes_attr,
389+
'_get_base_axes': _get_base_axes})
411390

412-
_host_axes_classes[axes_class] = new_class
413-
414-
return new_class
415391

416392
def host_subplot_class_factory(axes_class):
417393
host_axes_class = host_axes_class_factory(axes_class=axes_class)
418394
subplot_host_class = subplot_class_factory(host_axes_class)
419395
return subplot_host_class
420396

397+
421398
HostAxes = host_axes_class_factory(axes_class=Axes)
422399
SubplotHost = subplot_class_factory(HostAxes)
423400

lib/mpl_toolkits/axisartist/floating_axes.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""
22
An experimental support for curvilinear grid.
33
"""
4-
import six
5-
from six.moves import zip
4+
5+
import functools
66

77
# TODO :
88
# see if tick_iterator method can be simplified by reusing the parent method.
@@ -506,19 +506,12 @@ def adjust_axes_lim(self):
506506
self.set_ylim(ymin-dy, ymax+dy)
507507

508508

509-
510-
_floatingaxes_classes = {}
511-
509+
@functools.lru_cache(None)
512510
def floatingaxes_class_factory(axes_class):
511+
return type("Floating %s" % axes_class.__name__,
512+
(FloatingAxesBase, axes_class),
513+
{'_axes_class_floating': axes_class})
513514

514-
new_class = _floatingaxes_classes.get(axes_class)
515-
if new_class is None:
516-
new_class = type(str("Floating %s" % (axes_class.__name__)),
517-
(FloatingAxesBase, axes_class),
518-
{'_axes_class_floating': axes_class})
519-
_floatingaxes_classes[axes_class] = new_class
520-
521-
return new_class
522515

523516
from .axislines import Axes
524517
from mpl_toolkits.axes_grid1.parasite_axes import host_axes_class_factory

0 commit comments

Comments
 (0)