-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
ENH: EngFormatter new kwarg 'sep' #6542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
5a550f2
722f5d6
3c221ff
122cfa2
07f022d
e371e5a
3049a2d
e0a2ec8
1621a2d
c82dcff
f09a1b4
dc9409b
0195beb
3ec1dbd
cd2ac88
7f1422c
aba7a7f
b75f20d
41a69da
556ec20
a9af431
0bde03a
c98ba91
60b95ab
ac42b94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…n class and init docstrings
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1184,19 +1184,8 @@ class EngFormatter(Formatter): | |
""" | ||
Formats axis values using engineering prefixes to represent powers | ||
of 1000, plus a specified unit, e.g., 10 MHz instead of 1e7. | ||
|
||
`unit` is a string containing the abbreviated name of the unit, | ||
suitable for use with single-letter representations of powers of | ||
1000. For example, 'Hz' or 'm'. | ||
|
||
`places` is the precision with which to display the number, | ||
specified in digits after the decimal point (there will be between | ||
one and three digits before the decimal point). | ||
|
||
`sep` is the separator (a string) that is used between the number | ||
and the prefix/unit. For example, '3.14 mV' if `sep` is " " (default) | ||
and '3.14mV' if `sep` is "". | ||
""" | ||
|
||
# The SI engineering prefixes | ||
ENG_PREFIXES = { | ||
-24: "y", | ||
|
@@ -1222,20 +1211,26 @@ def __init__(self, unit="", places=None, sep=" "): | |
""" | ||
Parameters | ||
---------- | ||
unit: string (default: "") | ||
Unit symbol to use. | ||
|
||
places: int (default: None) | ||
Precision, i.e. number of digits after the decimal point. | ||
If it is None, falls back to the floating point format '%g'. | ||
|
||
sep: string (default: " ") | ||
String used between the value and the prefix/unit. Beside the | ||
default behavior, some other useful use cases may be: | ||
* sep="" to append directly the prefix/unit to the value; | ||
* sep="\\u2009" to use a thin space; | ||
* sep="\\u202f" to use a narrow no-break space; | ||
* sep="\\u00a0" to use a no-break space. | ||
unit : str (default: "") | ||
Unit symbol to use, suitable for use with single-letter | ||
representations of powers of 1000. For example, 'Hz' or 'm'. | ||
|
||
places : int (default: None) | ||
Precision with which to display the number, specified in | ||
digits after the decimal point (there will be between one | ||
and three digits before the decimal point). If it is None, | ||
falls back to the floating point format '%g'. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "%g" defaults to a precision of 6. Does that mean that the default is simply places=6? Otherwise the statement is a bit unclear. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, if I am correct, "%g" results in up to 6 significant digits. This means that with it, the equivalent value for Edit: updated docstring in ec92520 |
||
|
||
sep : str (default: " ") | ||
Separator used between the value and the prefix/unit. For | ||
example, one get '3.14 mV' if ``sep`` is " " (default) and | ||
'3.14mV' if ``sep`` is "". Besides the default behavior, some | ||
other useful options may be: | ||
|
||
* ``sep=""`` to append directly the prefix/unit to the value; | ||
* ``sep="\\u2009"`` to use a thin space; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, named entities ("\N{...}") are more self-documenting. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed by 8616a04 |
||
* ``sep="\\u202f"`` to use a narrow no-break space; | ||
* ``sep="\\u00a0"`` to use a no-break space. | ||
""" | ||
self.unit = unit | ||
self.places = places | ||
|
@@ -1244,7 +1239,8 @@ def __init__(self, unit="", places=None, sep=" "): | |
def __call__(self, x, pos=None): | ||
s = "%s%s" % (self.format_eng(x), self.unit) | ||
# Remove the trailing separator when there is neither prefix nor unit | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be rewritten as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well regex are black magic to me, but as you kindly gave the answer plus the tests still passed locally, here it is with 033111f ;). |
||
s = s.strip(self.sep) | ||
if len(self.sep) > 0 and s.endswith(self.sep): | ||
s = s[:-len(self.sep)] | ||
return self.fix_minus(s) | ||
|
||
def format_eng(self, num): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
\N{THIN SPACE}
here too? (not a big deal)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in ec92520