Some questions on how to use Quantity's as objects #27
-
Hello, I have a use case for Quantiphy that I'm not sure how to handle. I enjoy this library and would like to expand and streamline the way I have been using it. My goal is to minimize the number of times/places where I have to type in something like I work with a codebase where the usual way to express physical units in a human-readable form has been to type in string formatters for each print or log statement:
The output from this is passable, but not great:
So the decimal places are OK and the commas help separate out large quantities, but it's still a bit tedious to read, and I have to do this every place in the code where a log or print statement is used to show a quantity. As a direct replacement for this, I started invoking
The output now takes care of the auto-scaling issue:
This is straightforward and there don't seem to be any situations where I can't use it. But it's basically the same amount of work as just hand-formatting the strings. The only benefit to using Quantiphy in this case is the auto-scaling. At some point I discovered that I could define a
This seems cleaner, less burdensome, easier to review in a PR, and becomes more efficient the more times I reference either variable in a print/log statement. But it doesn't always work. Here are a couple of examples. Example 1: Simple arithmetic on a
Example 2: I have a test that creates a bunch of dataclass instances representing test input vectors, pushes them onto a queue, then later unpacks them for use in another part of the code. Once the data has been consumed by one iteration of the test, I want to print out the results, which includes the values in the original dataclass, some of which have units that I want to be handled by Quantiphy. I would like to just define my dataclass fields as a Quantity with an assigned unit, and then later on populate their values. Something like this, I guess:
Is this possible? In general, how can I just create Quantities, with or without values, and pass them around as objects, to be manipulated by other code (like in formulas), and then have their values printed/logged correctly with their associated units? I admit I have not read all the docs, so maybe some of this is in there. Please point me to appropriate sections if I've missed something. Thanks in advance, Jeff |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
I increasingly find myself subclassing Quantity to support specific and common units. This started when I wanted to print dollar amounts, because the formatting for currency is idiosyncratic. So I would typically define something like this at the top of my code:
That simplifies printing a dollar amount:
Over time I found myself doing that more and more often. It seems like an approach that might help you. For example, you could create subclasses for types of quantities you encounter often:
Then, printing values might look like this:
Or, if you define the quantities up front, you might get this:
Finally, on your data class question. I am not conversant with dataclasses, but a quick glance at the documentation seems to suggest that you should use the Quantity subclasses rather than the Quantity objects for your type qualifiers. That is not all that helpful because the arguments will only be cast to the specified types if they are not already the desired type, and since Quantity subclasses float, the dataclass will not cast float arguments to the specified quantity. It will cast strings though, which is interesting. Anyway, you can
Hope that helps, |
Beta Was this translation helpful? Give feedback.
I increasingly find myself subclassing Quantity to support specific and common units. This started when I wanted to print dollar amounts, because the formatting for currency is idiosyncratic. So I would typically define something like this at the top of my code:
That simplifies printing a dollar amount:
Over time I found myself doing that more and more often. It seems like an approach that might help you. For example, you could create subclasses for types of quantit…