Skip to content

Commit

Permalink
Accept Micro symbol as input prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
avylove committed Sep 2, 2024
1 parent f6615ba commit 400930b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ SI (Decimal) Prefixes
| q | Quecto | |10^-30| |
+--------+--------+----------+

.. note::
Prefixed uses the lower case Greek letter mu ('μ'), U+03BC, to represent the Micro
prefix, but will accept input using the Micro sign ('µ'), U+00B5. This complies
with the preference defined in `Unicode Technical Report #25`_ section 2.5.


IEC (Binary) Prefixes
^^^^^^^^^^^^^^^^^^^^^

Expand All @@ -249,6 +255,7 @@ IEC (Binary) Prefixes
| K | Kibi | |2^10| |
+--------+------+--------+

.. _Unicode Technical Report #25: https://www.unicode.org/reports/tr25
.. _SI (decimal): https://en.wikipedia.org/wiki/Metric_prefix
.. _IEC (binary): https://en.wikipedia.org/wiki/Binary_prefix
.. _float: https://docs.python.org/3/library/functions.html#float
Expand Down
8 changes: 8 additions & 0 deletions doc/prefixes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ SI (Decimal) Prefixes
| q | Quecto | |10^-30| |
+--------+--------+----------+

.. note::
Prefixed uses the lower case Greek letter mu ('μ'), U+03BC, to represent the Micro
prefix, but will accept input using the Micro sign ('µ'), U+00B5. This complies
with the preference defined in `Unicode Technical Report #25`_ section 2.5.


IEC (Binary) Prefixes
^^^^^^^^^^^^^^^^^^^^^

Expand All @@ -80,6 +86,8 @@ IEC (Binary) Prefixes
| K | Kibi | |2^10| |
+--------+------+--------+

.. _Unicode Technical Report #25: https://www.unicode.org/reports/tr25

.. |10^30| replace:: 10\ :sup:`30`\
.. |10^27| replace:: 10\ :sup:`27`\
.. |10^24| replace:: 10\ :sup:`24`\
Expand Down
1 change: 1 addition & 0 deletions doc/spelling_wordlist.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
μ
µ
Atto
Exa
Exbi
Expand Down
9 changes: 8 additions & 1 deletion prefixed/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@
# spec_type: Single non-numeric character
r'(?P<type>\D)?$'
)

# pylint: disable-next=wrong-spelling-in-comment
# \xce\xbc and \xc2\xb5 included for micro for Python 2.7 strings
# Support for both Greek letter mu and legacy micro symbol
RE_PREFIX = re.compile(
r'(?P<value>[-+]?\d+\.?(?:\d+)?(?:[eE]?\d)?) ?(?P<prefix>(?:[a-zA-Z\u03bc]|\xce\xbc)i?)$'
r'(?P<value>[-+]?\d+\.?(?:\d+)?(?:[eE]?\d)?) ?'
r'(?P<prefix>(?:[a-zA-Z\u03bc\u00B5]|\xce\xbc|\xc2\xb5)i?)$'
)

SI_SMALL = {
Expand Down Expand Up @@ -272,6 +277,8 @@ def __new__(cls, value=0.0):
prefix = match.group('prefix')
if prefix[-1] == 'i':
magnitude = IEC_MAGNITUDE.get(prefix[0])
elif prefix in {'µ', u'µ'}: # pylint: disable=duplicate-value # Python 2.7
magnitude = SI_MAGNITUDE.get('μ')
else:
magnitude = SI_MAGNITUDE.get(prefix)

Expand Down
19 changes: 15 additions & 4 deletions tests/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ def test_str(self):
self.assertEqual(str(Float(1.0)), '1.0')


# pylint: disable=expression-not-assigned
class TestFloatFormatting(unittest.TestCase):
class TestFloatFormatting(unittest.TestCase): # pylint: disable=too-many-public-methods
"""
Tests for prefixed.Float input and output
"""
Expand Down Expand Up @@ -261,7 +260,7 @@ def test_input_output_si_large(self):

def test_input_output_si_small(self):
"""
Large (>1) numbers input matches output
Small (<1) numbers input matches output
"""

for num in ('100.00m', '10.00m', '1.00m',
Expand Down Expand Up @@ -317,7 +316,18 @@ def test_input_output_iec(self):
self.assertEqual(format(Float('+' + num), '.2k'), num)
self.assertEqual(format(Float('+' + num), '.2m'), short_form)

def test_unicode(self):
def test_micro(self):
"""
Micro and mu are both valid for input
"""

micro = '100.01µ'
mu = '100.01μ'

self.assertEqual(format(Float(mu), '.2h'), mu)
self.assertEqual(format(Float(micro), '.2h'), mu)

def test_unicode_iec(self):
"""
For Python 2, test Unicode strings behave the same
"""
Expand Down Expand Up @@ -486,6 +496,7 @@ def test_deprecated(self):
self.assertEqual(format(Float(2048), u'.2J'), '2.00K')


# pylint: disable=expression-not-assigned
class TestFloatMath(unittest.TestCase):
"""
Tests for prefixed.Float math
Expand Down

0 comments on commit 400930b

Please sign in to comment.