Skip to content

Commit a53f170

Browse files
committed
Byte and Bit display as 'B' and 'b' (closes #93)
The base unit display names were the only ones that didn't use abbreviated form. Byte('Byte'/'Bytes') -> 'B' and Bit('Bit'/'Bits') -> 'b', consistent with KiB, MiB, kB, etc. Breaking change noted in NEWS.rst. Five tests updated to expect the new strings.
1 parent 79a323f commit a53f170

5 files changed

Lines changed: 18 additions & 10 deletions

File tree

NEWS.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ Breaking Changes
4949
``setup.py`` and ``setup.py.in`` are gone. Installation is
5050
``pip install bitmath``. Source builds use ``python -m build``.
5151

52+
**Byte and Bit display names**
53+
``Byte`` and ``Bit`` now display as ``B`` and ``b`` respectively,
54+
matching the abbreviated style of every other unit. Code that
55+
compares formatted strings (e.g. ``"{unit}"`` in a format template
56+
or the output of ``str()`` / ``repr()``) against the literal words
57+
``"Byte"`` or ``"Bit"`` will need to be updated. The class names
58+
themselves are unchanged.
59+
5260

5361
Library Improvements
5462
====================

bitmath/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ def __abs__(self):
10221022
class Byte(Bitmath):
10231023
"""Byte based types fundamentally operate on self._bit_value"""
10241024
def _setup(self):
1025-
return (2, 0, 'Byte', 'Bytes')
1025+
return (2, 0, 'B', 'B')
10261026

10271027
######################################################################
10281028
# NIST Prefixes for Byte based types
@@ -1167,7 +1167,7 @@ def _set_prefix_value(self):
11671167
self.prefix_value = self._to_prefix_value(self._bit_value)
11681168

11691169
def _setup(self):
1170-
return (2, 0, 'Bit', 'Bits')
1170+
return (2, 0, 'b', 'b')
11711171

11721172
def _norm(self, value):
11731173
"""Normalize the input value into the fundamental unit for this prefix

tests/test_context_manager.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_with_format(self):
4747
]
4848

4949
str_reps = [
50-
"101.00-Byte",
50+
"101.00-B",
5151
"202.00-KiB",
5252
"303.00-MB",
5353
"404.00-GiB",
@@ -68,7 +68,7 @@ def test_with_format(self):
6868

6969
def test_print_byte_plural(self):
7070
"""Byte(3.0) prints out units in plural form"""
71-
expected_result = "3Bytes"
71+
expected_result = "3B"
7272
fmt_str = "{value:.1g}{unit}"
7373
three_Bytes = bitmath.Byte(3.0)
7474

@@ -78,7 +78,7 @@ def test_print_byte_plural(self):
7878

7979
def test_print_byte_plural_fmt_in_mgr(self):
8080
"""Byte(3.0) prints out units in plural form, setting the fmt str in the mgr"""
81-
expected_result = "3Bytes"
81+
expected_result = "3B"
8282

8383
with bitmath.format(fmt_str="{value:.1g}{unit}", plural=True):
8484
three_Bytes = bitmath.Byte(3.0)

tests/test_context_manager_thread_safe.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ def plural_worker(expect_plural):
9393
with bitmath.format(plural=expect_plural):
9494
barrier.wait()
9595
result = str(bitmath.Byte(3.0))
96-
if expect_plural and result != "3.0 Bytes":
96+
if expect_plural and result != "3.0 B":
9797
errors.put(AssertionError(
98-
"plural thread: expected '3.0 Bytes', got %r" % result))
99-
elif not expect_plural and result != "3.0 Byte":
98+
"plural thread: expected '3.0 B', got %r" % result))
99+
elif not expect_plural and result != "3.0 B":
100100
errors.put(AssertionError(
101-
"singular thread: expected '3.0 Byte', got %r" % result))
101+
"singular thread: expected '3.0 B', got %r" % result))
102102
except Exception as exc:
103103
errors.put(exc)
104104

tests/test_representation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def test_change_format_string(self):
142142

143143
def test_print_byte_singular(self):
144144
"""Byte(1.0) prints out units in singular form"""
145-
expected_result = "1Byte"
145+
expected_result = "1B"
146146
fmt_str = "{value:.2g}{unit}"
147147
one_Byte = bitmath.Byte(1.0)
148148
actual_result = one_Byte.format(fmt_str)

0 commit comments

Comments
 (0)