Skip to main content

Very, very bad unit system for Python

Project description

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_unit_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

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bad_units-0.3.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bad_units-0.3.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file bad_units-0.3.0.tar.gz.

File metadata

  • Download URL: bad_units-0.3.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for bad_units-0.3.0.tar.gz
Algorithm Hash digest
SHA256 659da32fc886d2e0c6bf7220cbf652ee13dc03271ae07715e1345f06163f7d75
MD5 eb5500307d7ac896fbabf59a42fa8ef2
BLAKE2b-256 93607da5e80442fefc7d7b320a7e2b969d84726bb8c5db942851de29634c66e0

See more details on using hashes here.

File details

Details for the file bad_units-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: bad_units-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for bad_units-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 130c5bc89259874cec0c5d1e4fa2fa86986ece049dec64bdb42f717e5ae7594a
MD5 8605e802ab90a35a80d946d20738cd4c
BLAKE2b-256 e2a29ee5c44d9f837d106c4ad8ff22a04a33fa376121edc6a7e353a8c89e33a7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page