Skip to content

Conversation

@rju
Copy link

@rju rju commented Dec 18, 2025

This pull request adds the feature to add custom units programmatically. See also #58

Reiner Jung added 2 commits October 14, 2025 10:15
especially with countable amounts, like currency, cups etc. they are not
interchangable and units can be used to distinguish them.
Copy link
Contributor

@davidhassell davidhassell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @rju,

Thank you! I've make some suggestions/asked some questions, but it looks good and works as far as I can tell by playing on the command line (the proper units test will tell all :)).

Would you like add yourself as a contributor at docs/source/contributing.rst?

# --------------------------------------------------------------------
# Function to decode Udunits status codes to text
# --------------------------------------------------------------------
def decode_status(status):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def decode_status(status):
def _decode_status(status):

... and doc string, please!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide an example of when this would ever get called? I see that we only ever call _ut_format if there already an integer self._ut_unit, so I'm wondering how the former could go wrong?

return out.decode("utf-8")

@classmethod
def new_unit(cls, name):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doc string please!

@@ -0,0 +1,59 @@
#!/usr/bin/env python3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to recast this a new method in cfunits.cfunits.test.UnitsTest?

The tests need be framed with self.ssertTrue, self.assertFalse, self.assertEqual, etc.

out = _string_buffer.value
else:
raise ValueError(f"Can't format unit {self!r}")
raise ValueError(f"Cannot format unit {self!r} cause: {decode_status(_ut_get_status())}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise ValueError(f"Cannot format unit {self!r} cause: {decode_status(_ut_get_status())}")
raise ValueError(f"Cannot format unit {self!r} cause: {_decode_status(_ut_get_status())}")

@davidhassell davidhassell linked an issue Jan 8, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow users to add custom units

2 participants