Skip to content

Commit

Permalink
add the 'unicode' kwarg to number_to_si; use ascii in SI_PREFIX_MAP
Browse files Browse the repository at this point in the history
  • Loading branch information
jborbely committed Sep 14, 2022
1 parent 04efc29 commit 70e1bc8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion msl/qt/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
HOME_DIR = os.path.join(os.path.expanduser('~'), '.msl')
""":class:`str`: The default ``$HOME`` directory where all files used by the package are located."""

SI_PREFIX_MAP = {i: prefix for i, prefix in enumerate('yzafpn\u00b5m kMGTPEZY', start=-8)}
SI_PREFIX_MAP = {i: prefix for i, prefix in enumerate('yzafpnum kMGTPEZY', start=-8)}
""":class:`dict`: The SI prefixes used to form multiples of :math:`10^{3n}, n=` -8 .. 8"""
15 changes: 11 additions & 4 deletions msl/qt/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
)

from .constants import SI_PREFIX_MAP
from .characters import MICRO
from . import (
Qt,
QtGui,
Expand Down Expand Up @@ -189,7 +190,7 @@ def ensure_255(value):
return QtGui.QColor(*tuple(ensure_255(v) for v in args))


def number_to_si(number):
def number_to_si(number, unicode=True):
"""Convert a number to be represented with an SI prefix.
The hecto (h), deka (da), deci (d) and centi (c) prefixes are not used.
Expand All @@ -198,6 +199,8 @@ def number_to_si(number):
----------
number : :class:`int` or :class:`float`
The number to convert.
unicode : :class:`bool`, optional
Whether to use the unicode \u00B5 symbol for micro.
Returns
-------
Expand All @@ -224,7 +227,11 @@ def number_to_si(number):
return number, ''
if n > 8 or n < -8:
raise ValueError(f'The number {number} cannot be expressed with an SI prefix')
return number * 10 ** (-3 * n), SI_PREFIX_MAP[n]
if unicode and n == -2:
si = MICRO
else:
si = SI_PREFIX_MAP[n]
return number * 10 ** (-3 * n), si


def si_to_number(string):
Expand Down Expand Up @@ -259,8 +266,8 @@ def si_to_number(string):
return float(string_)

prefix = string_[-1]
if prefix == 'u':
prefix = '\u00b5'
if prefix == MICRO:
prefix = 'u'
for n, value in SI_PREFIX_MAP.items():
if prefix == value:
return float(string_[:-1]) * 10 ** (3 * n)
Expand Down
4 changes: 3 additions & 1 deletion tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Qt,
convert,
binding,
characters
)


Expand Down Expand Up @@ -402,7 +403,8 @@ def check(args, number, string):
check(convert.number_to_si(-12.3e-10), -1.23, 'n')
check(convert.number_to_si(1.23e-8), 12.3, 'n')
check(convert.number_to_si(-0.123e-7), -12.3, 'n')
check(convert.number_to_si(0.123e-5), 1.23, '\u00b5')
check(convert.number_to_si(0.123e-5), 1.23, characters.MICRO)
check(convert.number_to_si(0.123e-5, unicode=False), 1.23, 'u')
check(convert.number_to_si(-0.0123), -12.3, 'm')
check(convert.number_to_si(123.4), 123.4, '')
check(convert.number_to_si(0), 0, '')
Expand Down

0 comments on commit 70e1bc8

Please sign in to comment.