8000 space_sep=bool <- sep=string, and adapt tests · matplotlib/matplotlib@3049a2d · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3049a2d

Browse files
committed
space_sep=bool <- sep=string, and adapt tests
1 parent e371e5a commit 3049a2d

File tree

2 files changed

+44
-37
lines changed

2 files changed

+44
-37
lines changed

lib/matplotlib/tests/test_ticker.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ def test_params(self, input, expected):
585585
UNIT = u's' # seconds
586586
DIGITS = u'0123456789' # %timeit showed 10-20% faster search than set
587587

588-
# Case 0: unit='' (default) and space_sep=True (default).
588+
# Case 0: unit='' (default) and sep=' ' (default).
589589
# 'expected' already corresponds to this reference case.
590590
exp_outputs = (_s for _s in expected) # simple copy of 'expected'
591591
formatters = (
@@ -596,7 +596,7 @@ def test_params(self, input, expected):
596596
for _formatter, _exp_output in zip(formatters, exp_outputs):
597597
assert _formatter(input) == _exp_output
598598

599-
# Case 1: unit=UNIT and space_sep=True (default).
599+
# Case 1: unit=UNIT and sep=' ' (default).
600600
# Append a unit symbol to the reference case.
601601
# Beware of the values in [1, 1000), where there is no prefix!
602602
exp_outputs = (_s + u" " + UNIT if _s[-1] in DIGITS # case w/o prefix
@@ -609,28 +609,34 @@ def test_params(self, input, expected):
609609
for _formatter, _exp_output in zip(formatters, exp_outputs):
610610
assert _formatter(input) == _exp_output
611611

612-
# Case 2: unit=UNIT and space_sep=False.
613-
# Remove the space separator from the reference case.
614-
exp_outputs = (_s.replace(" ", "") + UNIT for _s in expected)
615-
formatters = (
616-
mticker.EngFormatter(unit=UNIT, space_sep=False), # places=None
617-
mticker.EngFormatter(unit=UNIT, places=0, space_sep=False),
618-
mticker.EngFormatter(unit=UNIT, places=2, space_sep=False)
619-
)
620-
for _formatter, _exp_output in zip(formatters, exp_outputs):
621-
assert _formatter(input) == _exp_output
622-
623-
# Case 3: unit='' (default) and space_sep=False.
624-
# Remove the space separator from the reference case and append
625-
# a unit symbol to it.
626-
exp_outputs = (_s.replace(" ", "") for _s in expected)
627-
formatters = (
628-
mticker.EngFormatter(space_sep=False), # places=None (default)
629-
mticker.EngFormatter(places=0, space_sep=False),
630-
mticker.EngFormatter(places=2, space_sep=False)
631-
)
632-
for _formatter, _exp_output in zip(formatters, exp_outputs):
633-
assert _formatter(input) == _exp_output
612+
# Test several non default separators: no separator, a narrow
613+
# no-break space (unicode character) and an extravagant string.
614+
for _sep in ("","\u202f", "@_@"):
615+
# Case 2x: unit=UNIT and sep=_sep.
616+
# Remove the space separator from the reference case.
617+
exp_outputs = (_s + _sep + UNIT if _s[-1] in DIGITS # no prefix
618+
else _s.replace(" ", _sep) + UNIT
619+
for _s in expected)
620+
#exp_outputs = list(_s.replace(" ", _sep) + UNIT for _s in expected)
621+
formatters = (
622+
mticker.EngFormatter(unit=UNIT, sep=_sep), # places=None
623+
mticker.EngFormatter(unit=UNIT, places=0, sep=_sep),
624+
mticker.EngFormatter(unit=UNIT, places=2, sep=_sep)
625+
)
626+
for _formatter, _exp_output in zip(formatters, exp_outputs):
627+
assert _formatter(input) == _exp_output
628+
629+
# Case 3x: unit='' (default) and sep=_sep.
630+
# Remove the space separator from the reference case and append
631+
# a unit symbol to it.
632+
exp_outputs = (_s.replace(" ", _sep) for _s in expected)
633+
formatters = (
634+
mticker.EngFormatter(sep=_sep), # places=None (default)
635+
mticker.EngFormatter(places=0, sep=_sep),
636+
mticker.EngFormatter(places=2, sep=_sep)
637+
)
638+
for _formatter, _exp_output in zip(formatters, exp_outputs):
639+
assert _formatter(input) == _exp_output
634640

635641

636642
class TestPercentFormatter(object):

lib/matplotlib/ticker.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,8 +1193,9 @@ class EngFormatter(Formatter):
11931193
specified in digits after the decimal point (there will be between
11941194
one and three digits before the decimal point).
11951195
1196-
`space_sep` controls if there is a space between the number and the
1197-
prefix/unit. For example, '3.14 mV' if True and '3.14mV' if False.
1196+
`sep` is the separator (a string) that is used between the number
1197+
and the prefix/unit. For example, '3.14 mV' if `sep` is " " (default)
1198+
and '3.14mV' if `sep` is "".
11981199
"""
11991200
# The SI engineering prefixes
12001201
ENG_PREFIXES = {
@@ -1217,32 +1218,32 @@ class EngFormatter(Formatter):
12171218
24: "Y"
12181219
}
12191220

1220-
def __init__(self, unit="", places=None, space_sep=True):
1221+
def __init__(self, unit="", places=None, sep=" "):
12211222
"""
12221223
Parameters
12231224
----------
1224-
unit: str (default: u"")
1225+
unit: string (default: "")
12251226
Unit symbol to use.
12261227
12271228
places: int (default: None)
12281229
Precision, i.e. number of digits after the decimal point.
12291230
If it is None, falls back to the floating point format '%g'.
12301231
1231-
space_sep: boolean (default: True)
1232-
If True, a (single) space is used between the value and the
1233-
prefix/unit, else the prefix/unit is directly appended to the
1234-
value.
1232+
sep: string (default: " ")
1233+
String used between the value and the prefix/unit. Beside the
1234+
default behavior, some other useful use cases may be:
1235+
* sep="" to append directly the prefix/unit to the value;
1236+
* sep="\u00a0" to use a no-break space;
1237+
* sep="\u202f" to use a narrow no-break space.
12351238
"""
12361239
self.unit = unit
12371240
self.places = places
1238-
if space_sep is True:
1239-
self.sep = u" " # 1 space
1240-
else:
1241-
self.sep = u"" # no space
1241+
self.sep = sep
12421242

12431243
def __call__(self, x, pos=None):
12441244
s = "%s%s" % (self.format_eng(x), self.unit)
1245-
s = s.strip() # Remove separator when there is neither prefix nor unit
1245+
# Remove the trailing separator when there is neither prefix nor unit
1246+
s = s.strip(self.sep)
12461247
return self.fix_minus(s)
12471248

12481249
def format_eng(self, num):

0 commit comments

Comments
 (0)
0