Skip to content
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

Allow units to be defined by bytes #55

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Changelog.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
Version 3.3.7
-------------

**2024-02-??**

* Allow units to be defined by `bytes`
(https://github.com/NCAS-CMS/cfunits/issues/54)

----

Version 3.3.6
-------------

Expand Down
4 changes: 4 additions & 0 deletions cfunits/test/test_Units.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@ def test_Units__dask_tokenize__(self):
self.assertEqual(tokenize(u), tokenize(Units("bad units")))
self.assertNotEqual(tokenize(u), tokenize(Units("worse units")))

def test_Units_bytes(self):
"""Tests initializing with bytes."""
self.assertEqual(Units("m"), Units(b"m"))


if __name__ == "__main__":
print("cfunits version:", cfunits.__version__)
Expand Down
8 changes: 8 additions & 0 deletions cfunits/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,9 +801,17 @@ def __init__(
# ----------------------------------------------------
ut_unit = _cached_ut_unit.get(units, None)
if ut_unit is None:
try:
# Try for units being bytes
units = units.decode("utf-8")
except AttributeError:
# units is not bytes
pass

ut_unit = _ut_parse(
_ut_system, _c_char_p(units.encode("utf-8")), _UT_ASCII
)

if not ut_unit:
ut_unit = None
self._isvalid = False
Expand Down
Loading