8000 Move cbook._check_getitem() to _api.check_getitem() · matplotlib/matplotlib@c725179 · GitHub
[go: up one dir, main page]

Skip to content

Commit c725179

Browse files
committed
Move cbook._check_getitem() to _api.check_getitem()
1 parent 9f9ea54 commit c725179

File tree

16 files changed

+52
-52
lines changed

16 files changed

+52
-52
lines changed

lib/matplotlib/_api.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,25 @@ def check_shape(_shape, **kwargs):
7171
f"with shape ({text_shape}). "
7272
f"Your input has shape {v.shape}."
7373
)
74+
75+
76+
def check_getitem(_mapping, **kwargs):
77+
"""
78+
*kwargs* must consist of a single *key, value* pair. If *key* is in
79+
*_mapping*, return ``_mapping[value]``; else, raise an appropriate
80+
ValueError.
81+
82+
Examples
83+
--------
84+
>>> _api.check_getitem({"foo": "bar"}, arg=arg)
85+
"""
86+
mapping = _mapping
87+
if len(kwargs) != 1:
88+
raise ValueError("check_getitem takes a single keyword argument")
89+
(k, v), = kwargs.items()
90+
try:
91+
return mapping[v]
92+
except KeyError:
93+
raise ValueError(
94+
"{!r} is not a valid value for {}; supported values are {}"
95+
.format(v, k, ', '.join(map(repr, mapping)))) from None

lib/matplotlib/axes/_axes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def get_title(self, loc="center"):
8686
titles = {'left': self._left_title,
8787
'center': self.title,
8888
'right': self._right_title}
89-
title = cbook._check_getitem(titles, loc=loc.lower())
89+
title = _api.check_getitem(titles, loc=loc.lower())
9090
return title.get_text()
9191

9292
def set_title(self, label, fontdict=None, loc=None, pad=None, *, y=None,
@@ -149,7 +149,7 @@ def set_title(self, label, fontdict=None, loc=None, pad=None, *, y=None,
149149
titles = {'left': self._left_title,
150150
'center': self.title,
151151
'right': self._right_title}
152-
title = cbook._check_getitem(titles, loc=loc.lower())
152+
title = _api.check_getitem(titles, loc=loc.lower())
153153
default = {
154154
'fontsize': rcParams['axes.titlesize'],
155155
'fontweight': rcParams['axes.titleweight'],
@@ -7195,7 +7195,7 @@ def magnitude_spectrum(self, x, Fs=None, Fc=None, window=None,
71957195
pad_to=pad_to, sides=sides)
71967196
freqs += Fc
71977197

7198-
yunits = cbook._check_getitem(
7198+
yunits = _api.check_getitem(
71997199
{None: 'energy', 'default': 'energy', 'linear': 'energy',
72007200
'dB': 'dB'},
72017201
scale=scale)

lib/matplotlib/axes/_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3002,10 +3002,10 @@ def ticklabel_format(self, *, axis='both', style='', scilimits=None,
30023002
raise ValueError("scilimits must be a sequence of 2 integers"
30033003
) from err
30043004
STYLES = {'sci': True, 'scientific': True, 'plain': False, '': None}
3005-
is_sci_style = cbook._check_getitem(STYLES, style=style)
3005+
is_sci_style = _api.check_getitem(STYLES, style=style)
30063006
axis_map = {**{k: [v] for k, v in self._get_axis_map().items()},
30073007
'both': self._get_axis_list()}
3008-
axises = cbook._check_getitem(axis_map, axis=axis)
3008+
axises = _api.check_getitem(axis_map, axis=axis)
30093009
try:
30103010
for axis in axises:
30113011
if is_sci_style is not None:

lib/matplotlib/axis.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,7 +2048,7 @@ def set_label_position(self, position):
20482048
----------
20492049
position : {'top', 'bottom'}
20502050
"""
2051-
self.label.set_verticalalignment(cbook._check_getitem({
2051+
self.label.set_verticalalignment(_api.check_getitem({
20522052
'top': 'baseline', 'bottom': 'top',
20532053
}, position=position))
20542054
self.label_position = position
@@ -2340,7 +2340,7 @@ def set_label_position(self, position):
23402340
"""
23412341
self.label.set_rotation_mode('anchor')
23422342
self.label.set_horizontalalignment('center')
2343-
self.label.set_verticalalignment(cbook._check_getitem({
2343+
self.label.set_verticalalignment(_api.check_getitem({
23442344
'left': 'bottom', 'right': 'top',
23452345
}, position=position))
23462346
self.label_position = position
@@ -2425,7 +2425,7 @@ def set_offset_position(self, position):
24252425
position : {'left', 'right'}
24262426
"""
24272427
x, y = self.offsetText.get_position()
2428-
x = cbook._check_getitem({'left': 0, 'right': 1}, position=position)
2428+
x = _api.check_getitem({'left': 0, 'right': 1}, position=position)
24292429

24302430
self.offsetText.set_ha(position)
24312431
self.offsetText.set_position((x, y))

lib/matplotlib/backends/backend_cairo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"cairo backend requires that pycairo>=1.11.0 or cairocffi "
2525
"is installed") from err
2626

27-
from .. import cbook, font_manager
27+
from .. import _api, cbook, font_manager
2828
from matplotlib.backend_bases import (
2929
_Backend, _check_savefig_extra_args, FigureCanvasBase, FigureManagerBase,
3030
GraphicsContextBase, RendererBase)
@@ -358,7 +358,7 @@ def set_alpha(self, alpha):
358358
# one for False.
359359

360360
def set_capstyle(self, cs):
361-
self.ctx.set_line_cap(cbook._check_getitem(self._capd, capstyle=cs))
361+
self.ctx.set_line_cap(_api.check_getitem(self._capd, capstyle=cs))
362362
self._capstyle = cs
363363

364364
def set_clip_rectangle(self, rectangle):
@@ -401,7 +401,7 @@ def get_rgb(self):
401401
return self.ctx.get_source().get_rgba()[:3]
402402

403403
def set_joinstyle(self, js):
404-
self.ctx.set_line_join(cbook._check_getitem(self._joind, joinstyle=js))
404+
self.ctx.set_line_join(_api.check_getitem(self._joind, joinstyle=js))
405405
self._joinstyle = js
406406

407407
def set_linewidth(self, w):

lib/matplotlib/backends/backend_ps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ def _print_ps(
817817
papertype = papertype.lower()
818818
_api.check_in_list(['auto', *papersize], papertype=papertype)
819819

820-
orientation = cbook._check_getitem(
820+
orientation = _api.check_getitem(
821821
_Orientation, orientation=orientation.lower())
822822

823823
printer = (self._print_figure_tex

lib/matplotlib/cbook/__init__.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,28 +2281,6 @@ def type_name(tp):
22812281
type_name(type(v))))
22822282

22832283

2284-
def _check_getitem(_mapping, **kwargs):
2285-
"""
2286-
*kwargs* must consist of a single *key, value* pair. If *key* is in
2287-
*_mapping*, return ``_mapping[value]``; else, raise an appropriate
2288-
ValueError.
2289-
2290-
Examples
2291-
--------
2292-
>>> cbook._check_getitem({"foo": "bar"}, arg=arg)
2293-
"""
2294-
mapping = _mapping
2295-
if len(kwargs) != 1:
2296-
raise ValueError("_check_getitem takes a single keyword argument")
2297-
(k, v), = kwargs.items()
2298-
try:
2299-
return mapping[v]
2300-
except KeyError:
2301-
raise ValueError(
2302-
"{!r} is not a valid value for {}; supported values are {}"
2303-
.format(v, k, ', '.join(map(repr, mapping)))) from None
2304-
2305-
23062284
class _classproperty:
23072285
"""
23082286
Like `property`, but also triggers on access via the class, and it is the

lib/matplotlib/collections.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1619,7 +1619,7 @@ def set_orientation(self, orientation=None):
16191619
orientation : {'horizontal', 'vertical'}
16201620
"""
16211621
try:
1622-
is_horizontal = cbook._check_getitem(
1622+
is_horizontal = _api.check_getitem(
16231623
{"horizontal": True, "vertical": False},
16241624
orientation=orientation)
16251625
except ValueError:

lib/matplotlib/colorbar.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ def __init__(self, ax, cmap=None,
461461
self.values = values
462462
self.boundaries = boundaries
463463
self.extend = extend
464-
self._inside = cbook._check_getitem(
464+
self._inside = _api.check_getitem(
465465
{'neither': slice(0, None), 'both': slice(1, -1),
466466
'min': slice(1, None), 'max': slice(0, -1)},
467467
extend=extend)
@@ -1372,10 +1372,10 @@ def remove(self):
13721372

13731373
def _normalize_location_orientation(location, orientation):
13741374
if location is None:
1375-
location = cbook._check_getitem(
1375+
location = _api.check_getitem(
13761376
{None: "right", "vertical": "right", "horizontal": "bottom"},
13771377
orientation=orientation)
1378-
loc_settings = cbook._check_getitem({
1378+
loc_settings = _api.check_getitem({
13791379
"left": {"location": "left", "orientation": "vertical",
13801380
"anchor": (1.0, 0.5), "panchor": (0.0, 0.5), "pad": 0.10},
13811381
"right": {"location": "right", "orientation": "vertical",

lib/matplotlib/mathtext.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import numpy as np
2525
from PIL import Image
2626

27-
from matplotlib import cbook, colors as mcolors, rcParams, _mathtext
27+
from matplotlib import _api, cbook, colors as mcolors, rcParams, _mathtext
2828
from matplotlib.ft2font import FT2Image, LOAD_NO_HINTING
2929
from matplotlib.font_manager import FontProperties
3030
# Backcompat imports, all are deprecated as of 3.4.
@@ -444,7 +444,7 @@ def _parse_cached(self, s, dpi, prop, force_standard_ps_fonts):
444444

445445
fontset_class = (
446446
_mathtext.StandardPsFonts if force_standard_ps_fonts
447-
else cbook._check_getitem(
447+
else _api.check_getitem(
448448
self._font_type_mapping, fontset=prop.get_math_fontfamily()))
449449
backend = self._backend_mapping[self._output]()
450450
font_output = fontset_class(prop, backend)

lib/matplotlib/offsetbox.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,7 @@ def __init__(self, loc,
10951095
self.set_child(child)
10961096

10971097
if isinstance(loc, str):
1098-
loc = cbook._check_getitem(self.codes, loc=loc)
1098+
loc = _api.check_getitem(self.codes, loc=loc)
10991099

11001100
self.loc = loc
11011101
self.borderpad = borderpad

lib/matplotlib/quiver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ def draw(self, renderer):
352352
self.stale = False
353353

354354
def _set_transform(self):
355-
self.set_transform(cbook._check_getitem({
355+
self.set_transform(_api.check_getitem({
356356
"data": self.Q.axes.transData,
357357
"axes": self.Q.axes.transAxes,
358358
"figure": self.Q.axes.figure.transFigure,

lib/matplotlib/scale.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def __init__(self, base, nonpositive='clip'):
200200
if base <= 0 or base == 1:
201201
raise ValueError('The log base cannot be <= 0 or == 1')
202202
self.base = base
203-
self._clip = cbook._check_getitem(
203+
self._clip = _api.check_getitem(
204204
{"clip": True, "mask": False}, nonpositive=nonpositive)
205205

206206
def __str__(self):

lib/matplotlib/testing/jpl_units/UnitDbl.py

Lines changed: 3 additions & 3 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import operator
44

5-
from matplotlib import cbook
5+
from matplotlib import _api
66

77

88
class UnitDbl:
@@ -48,7 +48,7 @@ def __init__(self, value, units):
4848
- value The numeric value of the UnitDbl.
4949
- units The string name of the units the value is in.
5050
"""
51-
data = cbook._check_getitem(self.allowed, units=units)
51+
data = _api.check_getitem(self.allowed, units=units)
5252
self._value = float(value * data[0])
5353
self._units = data[1]
5454

@@ -68,7 +68,7 @@ def convert(self, units):
6868
"""
6969
if self._units == units:
7070
return self._value
71-
data = cbook._check_getitem(self.allowed, units=units)
71+
data = _api.check_getitem(self.allowed, units=units)
7272
if self._units != data[1]:
7373
raise ValueError(f"Error trying to convert to different units.\n"
7474
f" Invalid conversion requested.\n"

lib/mpl_toolkits/axisartist/axis_artist.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090

9191
import numpy as np
9292

93-
from matplotlib import cbook, rcParams
93+
from matplotlib import _api, cbook, rcParams
9494
import matplotlib.artist as martist
9595
import matplotlib.text as mtext
9696

@@ -416,7 +416,7 @@ def get_text(self):
416416
top=("bottom", "center"))
417417

418418
def set_default_alignment(self, d):
419-
va, ha = cbook._check_getitem(self._default_alignments, d=d)
419+
va, ha = _api.check_getitem(self._default_alignments, d=d)
420420
self.set_va(va)
421421
self.set_ha(ha)
422422

@@ -426,7 +426,7 @@ def set_default_alignment(self, d):
426426
top=180)
427427

428428
def set_default_angle(self, d):
429-
self.set_rotation(cbook._check_getitem(self._default_angles, d=d))
429+
self.set_rotation(_api.check_getitem(self._default_angles, d=d))
430430

431431
def set_axis_direction(self, d):
432432
"""
@@ -807,7 +807,7 @@ def set_ticklabel_direction(self, tick_direction):
807807
----------
808808
tick_direction : {"+", "-"}
809809
"""
810-
self._ticklabel_add_angle = cbook._check_getitem(
810+
self._ticklabel_add_angle = _api.check_getitem(
811811
{"+": 0, "-": 180}, tick_direction=tick_direction)
812812

813813
def invert_ticklabel_direction(self):
@@ -826,7 +826,7 @@ def set_axislabel_direction(self, label_direction):
826826
----------
827827
tick_direction : {"+", "-"}
828828
"""
829-
self._axislabel_add_angle = cbook._check_getitem(
829+
self._axislabel_add_angle = _api.check_getitem(
830830
{"+": 0, "-": 180}, label_direction=label_direction)
831831

832832
def get_transform(self):

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ def set_proj_type(self, proj_type):
10151015
----------
10161016
proj_type : {'persp', 'ortho'}
10171017
"""
1018-
self._projection = cbook._check_getitem({
1018+
self._projection = _api.check_getitem({
10191019
'persp': proj3d.persp_transformation,
10201020
'ortho': proj3d.ortho_transformation,
10211021
}, proj_type=proj_type)

0 commit comments

Comments
 (0)
0