Bad Units
Bad Units is a system for working with units, that is bad.
Unit class
This is the most basic part of Bad Units. To make a new unit, you inherit from Unit and set the base_units_per and the unit_type. Example:
class Meter(Unit):
unit_type = "length"
base_units_per = 1
Those are the only two things you will ever need to set, Bad Units figures everything out for you. That's nice of it, maybe it isn't so bad after all? Oh, you just wait and see...
Convert between units with the .to() method:
print(Meter().to(Centimeter()))
Yes, that's right. The unit to be converted to is an instance of a class and not the class itself. What was I thinking when I made this?
You can add and subtract units:
print(Meter(5)+Centimeter(3)-Millimeter(42))
Compound Units
DO NOT USE FOR NOW
Bad Units also supports compound units. That means you can divide one unit by another, multiply them together, and create all kinds of messy physics-like expressions.
Example:
speed = Meter(10) / Second(2)
acceleration = speed / Second(5)
momentum = Meter(10) * Second(2)
You can even combine them in horrifying ways:
complex_unit = (Meter(10) / Second(2)) / (Second(5) / Meter(3))
print(complex_unit)
The output is… well, it looks like something that belongs in a physics exam. But don’t worry, Bad Units will still track your numerators and denominators correctly.
Adding and Subtracting Compound Units
Yes, you can add and subtract compound units — but only if they “make sense.” Bad Units will yell at you if you try to do something stupid, like add meters per second to kilograms:
valid = (Meter(10) / Second(2)) + (Centimeter(100) / Second(2))
invalid = (Meter(10) / Second(2)) + (Second(5) / Meter(3)) # This will throw a UnitError
Notice how the first one works after automatic conversion to base units. The second one is… well… it’s just plain bad.
Printing Units
When you print a unit or compound unit, Bad Units tries to make it look nice:
print(Meter(10) / Second(2))
# -> 10 Meter/2 Second
print((Meter(10)/Second(2)) / Second(5))
# -> (10 Meter/2 Second)/5 Second
If you’re lucky, it even formats nested compound units in a way that’s somewhat readable.
Why “Bad Units”?
Because it’s intentionally quirky.
- You deal with instances instead of classes for conversions.
- It complains if you do math that’s dimensionally invalid.
- Nested compound units can get ugly, fast.
And yet… somehow it works. Mostly. Maybe. Probably.
Example Usage
class Meter(Unit):
unit_type = "length"
base_units_per = 1
class Centimeter(Unit):
unit_type = "length"
base_units_per = 0.01
class Second(Unit):
unit_type = "time"
base_units_per = 1
# Simple addition
print(Meter(1) + Centimeter(100))
# Compound units
speed = Meter(10) / Second(2)
acceleration = speed / Second(5)
print(speed)
print(acceleration)
# Adding compatible compound units
speed2 = Centimeter(200) / Second(2)
print(speed + speed2)
Known Issues / Limitations
- Compound unit conversion is limited; nested or complex units may require manual conversions.
- Adding incompatible units (like meters/second + seconds/meter) will raise a
UnitError. - Printing deeply nested compounds can get visually messy.
- Units must be instances for conversions (
Meter().to(Centimeter())), not classes. - It’s “bad” on purpose. That’s part of the charm.
Metadata
Release files for bad-units 0.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| bad_units-0.4.1.tar.gz | 5.4 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| bad_units-0.4.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.1 kB
Release files / bad_units-0.4.1.tar.gz
| Download URL | bad_units-0.4.1.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1c9c2aa334c70c85d93bab8d7ed28530f22836a45ac6fd5aaefc45a827d5f7a3
|
|
BLAKE2b-256 checksum How to use checksums |
99f75d9b9d3f5dd3f2e450deb3bd0052c5f5faf44c5dfe96e38f4c01245209c8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.0
|
Release files / bad_units-0.4.1-py3-none-any.whl
| Download URL | bad_units-0.4.1-py3-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fe56130989eaad2645f27287199eff4b2897fdf86fde325d92864daf5bfd7aa3
|
|
BLAKE2b-256 checksum How to use checksums |
3f9177c003ea2bf370291efe315d378fc8b8931cdaa3bb17c72ecb2c3bd3ab56
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.0
|
Security for bad-units 0.4.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.