Skip to main content

UnitCorn 🦄

Units that convert, localize and format themselves. Born for weather dashboards, good for anything you can measure.

Formerly WeatherUnits. The library outgrew its name: lengths, masses, time, bytes and the whole derived-unit algebra are general-purpose, and the weather part lives in the config. pip install WeatherUnits still works, as a shim that installs UnitCorn and forwards import WeatherUnits to it with a DeprecationWarning — see compat/WeatherUnits.

pip install unitcorn

Features

🌈 Effortlessly define or extend your own units

☀️ Change nearly every part of unit display from a TOML config file. No need for additional code.

❄️ Localization can is as simple as adding .localize to a units variable or setting it to automatic

Requirements

  • Python 3.12 – 3.14, no other dependencies

How to use

📐 Formatting reference → — the format-spec mini-language, every parameter, how precision/digit_budget interact, and the known gotchas. Verified against the library; start there for anything display-related.

Unit Conversion

Conversion is as easy as using the desired unit's name or aliases as either an attribute or subscript

from unitcorn.temperature import Fahrenheit
>>> value = Fahrenheit(32)
>>> value
'32°'
>>> value.withUnit
'32°f'
>>> value['c'].withUnit
'0°c'
>>> value.celsius
'0°'

Using a Config file

Loading a config file

import unitcorn as uc
uc.config.read('units.toml')

A template config file can be found in src/unitcorn/config. An older .ini config still loads, and python -m unitcorn.config units.ini prints it as TOML. There are three sections in a valid config file

  1. LocalUnits: This is what defines what units you want to use for localization
  2. UnitDefaults: This section holds the default values that apply to every unit
  3. UnitProperties: Defines properties for individual units or unit systems as a whole

UnitDefaults

The basic rules are:

  1. Properties are specified without the leading '_'
  2. Units are specified with their lowercase names or types
  3. "" sets a property to empty; leaving the key out leaves it unset
  4. In [UnitProperties], each unit is an inline table: mmHg = { precision = 2, digit_budget = 5 }

Properties

  • precision: The number of digits allowed to be displayed past the decimal but will not exceed digit_budget. Example number:

    example 3.14159m
    0 3 m
    1 3.1 m
    2 3.14 m
    3 3.142 m

    (The last decimal is rounded, not truncated.)

  • digit_budget: The total number of digits that can be displayed while still showing the decimal. When the value doesn't fit, shorten decides how it is rescaled — either refitting to a different unit or dividing and appending a 10³ suffix.

    example 415.25mm
    2 41.53 cm
    3 415.2 mm
    4 415.2 mm
    5 415.2 mm

    ⚠️ Note the budget caps but does not grant: raising it above 3 does not buy more decimals here, because precision is separately capped by the class default and by the value's own decimal content. The intuitive 4 → 415.3mm, 5 → 415.25mm progression requires raising precision too. See the formatting reference.

  • unit: Override built in unit string

  • suffix: Override built in suffix string.Only used for shortening numbers

  • unit_symbol: Override built in symbol trailing the value.Only used for ° with degrees

  • title: Title to be used by other display programs

  • exp: Override exponent. I can't imagine why this would be used

  • show_unit: Show unit True: 5km False: 5

  • leading_zero: Display zero before values less than 1. True: 0.1in False: .1in auto (default): keeps the zero unless keeping it would cost the value its last meaningful digit — see the formatting reference for the exact rule

  • trailing_zeros: Pad decimals to a stable width instead of the value's own content. By default precision follows the value, so 29.92 shows 29.9 but 30.0 shows 30 — a live readout changes width as it crosses a whole number.

    value inHg(30.0) at precision=2, digit_budget=5
    off (default) 30.0
    precision 30.00
    fill 30.000
    3 30.000

    (At class defaults, with no spec at all, off gives a bare 30 — the derived precision is zero because the value has no decimals of its own. That is the drift this option exists to remove.)

    Always capped by digit_budget, so padding can never push a value over budget. True is an alias for precision.

  • true_zero: Keep an exact zero bare (0) even while trailing_zeros is padding everything else. Default True. A bare 0 says the value is actually zero, where 0.0 says "not zero, but rounds to it at this width" — Inch(0) gives 0 while Inch(0.004) gives 0.0. Set False where a column that never changes width matters more than the distinction, which is most places zero is an ordinary reading: precipitation, wind, lightning strikes. No effect unless trailing_zeros is on.

  • unit_spacer: The text placed between the value and its unit. ⚠️ This is a string, not a flag — False removes it (90°f), but True inserts the literal word (90°Truef). Leave it unset for the class default (a space), or give it the string you want.

  • k_separator: The character separating powers of 10³ — the , in 1,000. Not yet implemented

    value example
    True 1,000ft
    False 1000ft
  • shorten: Rescales a value that doesn't fit the digit_budget. Does one of two things depending on the unit type: refits to a neighbouring unit, or divides and appends a 10³ suffix.

    value example
    True 16.67min
    False 1000s

    (Time.Second(1000) — here it refits the unit. Light.Lux(1000) takes the other branch and gives 1.00k lux.)

  • slide: Scale units for the appropriate values. Not yet implemented

    value example
    True 1km
    False 1000m
  • cardinal: Show degrees as a cardinal direction rather than a number. shorten decides full vs abbreviated name

    value example
    True S
    False 180°

    With cardinal on, two axes work independently: shorten picks the form (letters vs words) and digit_budget picks the detail.

    The budget counts compass components, and the atom it counts depends on the form — a letter when abbreviated, a word when spelled out. So the same budget carries the same amount of information either way:

    digit_budget shorten=True shorten=False resolves to
    1 W West 4 points
    2 SW South West 8 points
    3 WSW West South West 16 points

    The budget is a cap, not a quota: a heading that lands on a cardinal doesn't pad to fill it. At a 2-component budget, 202.5° gives S / South, not a stretched two-component form.

  • key: Key to be used for other programs

  • size_hint: Override generated size hint string. Useful for when you know the expected max string length.

Every code example below is executed against the library as part of verifying this README — they define real, working unit classes.

Defining Your Own Unit

There are currently three types of unit, Static, Scaling, and Derived

Static Unit

The most simple to define. Temperature is an example of a static unit. Similar units of the same are a subclass of the unit type. In the example below, Fahrenheit, Celsius, and Kelvin are subclasses of Temperature. Convert to other similar units with a single private function sharing the name of the desired unit.

from unitcorn.base import Measurement, UnitType

@UnitType  # Unit types are defined with this decorator.
class Temperature(Measurement):  # For static unit types, the class inherits Measurement.

    # Shared unit properties are defined here or in a provided config file.
    _unit_symbol = '°'

    # Properties for converting units are defined in the main parent class.
    @property
    def fahrenheit(self):
        # Unless all the subunits are defined in the same file, importing the desired
        # class locally is currently necessary.
        return Fahrenheit(self._fahrenheit())

    # Unit abbreviations are defined somewhere below the converting property.
    f = fahrenheit

    @property
    def celsius(self):
        return Celsius(self._celsius())
    c = celsius

class Fahrenheit(Temperature):
    # Individual unit strings are defined as protected class variables
    # for each unit or within a config file.
    _unit = 'f'

    # This method is what is called to convert Fahrenheit to Celsius.
    def _celsius(self):
        # Since Measurement is a float subclass, math can be done on self.
        return (self - 32) / 1.8

    def _fahrenheit(self):
        # Currently, a function needs to be defined for even converting to the
        # same unit to prevent errors.
        return self

class Celsius(Temperature):
    _unit = 'c'

    def _celsius(self):
        return self

    def _fahrenheit(self):
        return (self * 1.8) + 32

Scaling Unit

Scaling units have quite a bit going on. A scaling unit can be reduced to a single unit through a multiplier, this is the base unit. Multiple unit systems can be defined within one scale. It is also to split systems separate scales as long as there is a function within each system that converts from one base unit to the other.

  • SystemVariant: Marks a unit as falling outside the regular scale
  • Synonym: Denotes that a unit is the same as an already defined one but uses another name. Synonym classes inherit their identically valued class
  • Dimension: A metaclass for the shared dimension class (class Length(metaclass=Dimension, symbol='L')), which the per-system classes then inherit
  • baseUnit=: A class keyword naming the base unit for the system, used when converting to a non-standard SystemVariant
  • system=: A class keyword assigning the class to metric, imperial, or both
  • Scale: Uses the metaclass EnumMeta to define the scaling factors for a ScalingMeasurement class along with the multipliers for any SystemVariants and the base unit

To define a scaling measurement (one that has multiple units within the same unit system, think Centimeter, Meter, Kilometer) the unit system class inherits ScalingMeasurement. The main difference from Measurement is the _Scale class defined inside the main parent class. In the example below, two unit systems (US and SI) are defined in the same scale. Most US units have an SI basis, so this can be done fairly often. However, it is sometimes necessary to split them for readability.

Unified System

from unitcorn.base import UnitType, Synonym, ScalingMeasurement
from unitcorn.base import Scale, SystemVariant, both

@UnitType
# `baseUnit` names the unit everything reduces to; `system` assigns the class
# to metric, imperial, or both.
class Pressure(ScalingMeasurement, baseUnit='Pascal', system=both):

    # The scale for the unit system is defined with by a class named _Scale inheriting.
    class _Scale(Scale):
        # Every unit is named here starting with the smallest unit and assigned a
        # scaling multiplier.  Scaling multipliers here must be an integer.
        Pascal = 1
        Decapascal = 10
        Hectopascal = 10

        # A base unit is also defined denoted by Base = class.__name__.
        # Base value must be a string.
        Base = 'Pascal'

        # System variants are units that fall outside of the normal scale
        # The multiplier value for variants must be a float and be relative
        # to the base unit.  For example, 1 Bar is 1000 Pascals and
        # 1 MillimeterOfMercury is 1/0.00750062 or 133.3 Pascals.
        Bar = 1000.
        Atmosphere = 101325.
        PoundsPerSquareInch = 6894.757293168
        MillimeterOfMercury = 1 / 0.00750062
        InchOfMercury = 1 / 0.00029530

    @property
    def pascal(self):
        return Pascal(self)

    @property
    def hectopascal(self):
        return Hectopascal(self)

class Pascal(Pressure):
    _unit = 'Pa'

class Hectopascal(Pressure):
    _unit = 'hPa'

class Atmosphere(Pascal, SystemVariant):
    _unit = 'atm'

@Synonym
class Millibar(Hectopascal):
    _unit = 'mBar'
    _digit_budget = 4

Split system

from unitcorn.base import ScalingMeasurement, Scale, Dimension
from unitcorn.base import metric, imperial


# The shared dimension is a plain class using the Dimension metaclass. It owns
# the conversion properties; the per-system classes below supply the scales.
class Length(metaclass=Dimension, symbol='L'):

    @property
    def meter(self):
        return Meter(self)
    m = meter

    @property
    def foot(self):
        return Foot(self)
    ft = foot


class ImperialLength(ScalingMeasurement, Length, system=imperial, baseUnit='Foot'):

    class _Scale(Scale):
        Line = 1
        Inch = 12
        Foot = 12
        Yard = 3
        Mile = 1760
        Base = 'Foot'

    # Since the multipliers are defined in _Scale, changeScale() uses one
    # parameter to change the scale within the same system.
    def _foot(self):
        return self.changeScale(self._Scale.Foot)

    # This function is called to convert from ImperialLength to MetricLength.  Once its
    # converted to MetricLength, changeScale() is called within the MetricLength class
    # to get to the final unit.
    def _meter(self):
        return self._foot() * 0.3048


class Foot(ImperialLength):
    _unit = 'ft'


class MetricLength(ScalingMeasurement, Length, system=metric, baseUnit='Meter'):

    class _Scale(Scale):
        Millimeter = 1
        Centimeter = 10
        Decimeter = 10
        Meter = 10
        Decameter = 10
        Hectometer = 10
        Kilometer = 10
        Base = 'Meter'

    def _meter(self):
        return self.changeScale(self._Scale.Meter)

    # As with the pervious class, this method is called to convert from MetricLength
    # to ImperialLength.
    def _foot(self):
        return self._meter() * 3.280839895013123


class Meter(MetricLength):
    _unit = 'm'

Derived Unit

DerivedMeasurement is essentially a measurement with multiple units, eg, m/s [meters per second], mph [miles per hour]. A derived value is one number in its unit, like every measurement. .numerator and .denominator describe it as "this much of the numerator unit per ONE of the denominator unit", so MilesPerHour(20).numerator is 20 mi and .denominator is 1 hr. Building one from an amount over an interval still works and gives the rate: Hourly(Millimeter(6), Hour(3)) is 2 mm/hr. A value that has to remember the window it covers (rain in the last 24 hours) carries period=timedelta(...) beside its timestamp; conversion never touches it.

Rates and accumulations share a number and differ in meaning (mm/h and mm, kW and kWh), so the class has to say which one it is.

Dimension algebra. * and / between measurements of different dimensions work on exponent vectors over length, mass, time and temperature:

from unitcorn import Length, Mass, Time
Meter, Foot, Millimeter, Hour, Kilogram = Length.Meter, Length.Foot, Length.Millimeter, Time.Hour, Mass.Kilogram

Meter(10) / Meter(2)                  # 5.0, a plain float
Millimeter(6) / Hour(3)               # 2 mm/hr, keeps the units it was given
Meter(5) * Meter(5)                   # 25 m², an Area
Foot(3) * Meter(1)                    # 0.9144 m², across systems
Kilogram(2) / (Meter(2) * Meter(2) * Meter(2))   # 0.25 kg·m⁻³

The result is a named type when one is registered for the dimension (length, mass, time, pressure, speed), a plain float when it is dimensionless, and otherwise a Compound, which prints its coherent SI unit and takes part in further algebra. Exponents run from -3 to 3. A temperature reading (Celsius(5)) does not multiply or divide, since 20 °C × 2 is not 40 °C; use Kelvin. Unary -/+, %, //, round and scalar * and / keep the class.

Temperature readings and changes. Celsius(30) - Celsius(20) is a CelsiusChange(10), which is also an 18 change in FahrenheitChange: a change converts by a factor, never an offset. A reading plus or minus a change is a reading (Fahrenheit(68) + CelsiusChange(10) is 86 °F). Two readings do not add, because 20 °C + 10 °C and 68 °F + 50 °F are not the same temperature; inside with summingReadings(): they add in the left operand's unit (for a mean). A bare number next to a reading still means the same unit.

Energy, power and area are units in their own right: Energy (J, kJ, MJ, GJ, Wh, kWh, MWh, cal, BTU), Power (W, kW, MW, GW, hp) and Area (m², cm², km², ha, in², ft², mi², ac). Power.Kilowatt(2) * Time.Hour(3) is Energy (6 kWh), Energy.KilowattHour(6) / Time.Hour(3) is Power, and Meter(2) * Meter(3) is an Area. Light.Irradiance (W/m²) is power per area: Irradiance(500) * Area.SquareMeter(20) is Power (10 kW), and Power / Area is an Irradiance. Illuminance (lux) has no dimension and does not multiply.

Note: Still to do: several different units in the numerator or denominator at once, and compound units beyond what the exponent vector names (Compound prints them in SI).

Roughly what DerivedMeasurement does, for orientation (illustrative, not the real source):

# Illustrative sketch, not the real source.
class DerivedMeasurement(Measurement):
    _numerator: Measurement
    _denominator: Measurement

    def __init__(self, numerator, denominator):
        self._numerator = numerator
        self._denominator = denominator
        Measurement.__init__(self, numerator / denominator)

And defining your own:

from unitcorn import Length, Time
from unitcorn.base import DerivedMeasurement, UnitType


@UnitType
# The numerator and denominator dimensions are declared as class keywords.
class DistanceOverTime(DerivedMeasurement, numerator=Length, denominator=Time):

    # Currently all variations that will be used have to be defined as properties
    @property
    def mih(self):
        converted = DistanceOverTime(self._numerator.mi, self._denominator.hr)

        # Without this override, the unit would be displayed as 'mi/hr'
        converted._suffix = 'mph'
        return converted

    @property
    def ms(self):
        return DistanceOverTime(self._numerator.m, self._denominator.s)

    @property
    def kmh(self):
        return DistanceOverTime(self._numerator.km, self._denominator.hr)

    mph = mih


@UnitType
class Precipitation(DistanceOverTime):

    @property
    def inh(self):
        return Precipitation(self._numerator.inch, self._denominator.hr)

    @property
    def mmh(self):
        return Precipitation(self._numerator.mm, self._denominator.hr)


@UnitType
class PrecipitationRate(Precipitation):
    pass


# Fixing the denominator is a one-liner: no __init__ override needed.
class Daily(PrecipitationRate, denominator=Time.Day): ...


class Hourly(PrecipitationRate, denominator=Time.Hour): ...

License

UnitCorn is licensed under the GNU Lesser General Public License v3.0 only (LICENSE.txt, with the GPLv3 in COPYING.txt), so you can use it from software under any license; changes to UnitCorn itself must stay under the LGPLv3. Releases published before this change stay MIT (LICENSE-MIT-HISTORIC.txt).

Metadata

Release files for unitcorn 0.9.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for unitcorn 0.9.1
File Size Uploaded Provenance Actions
unitcorn-0.9.1.tar.gz 102.8 kB With provenance Details for unitcorn-0.9.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for unitcorn 0.9.1
File Interpreter ABI Platform Actions
unitcorn-0.9.1-py3-none-any.whl Python 3 none any Details for unitcorn-0.9.1-py3-none-any.whl

Total release size: 218.6 kB

Release files / unitcorn-0.9.1.tar.gz

Download URL unitcorn-0.9.1.tar.gz
Size 102.8 kB
Tags Source
SHA-256 checksum
How to use checksums
ac403977ad30688b889cdef1db2ae1fe2ec44b6625f8fe018c3ca67311225a72
BLAKE2b-256 checksum
How to use checksums
abdedcb3fbc71ec83678b34b463f75688ba64440f9ad566e959c3a35e107e370
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

Release files / unitcorn-0.9.1-py3-none-any.whl

Download URL unitcorn-0.9.1-py3-none-any.whl
Size 115.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ea42983d11835c69792ead5a3d3f836d8e7d55c8882c3a770108d9f92c35b98a
BLAKE2b-256 checksum
How to use checksums
c6f2bf82b5205d9308f575f4547de544e9ec427f9225e36672b7dc2141d90751
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

Security for unitcorn 0.9.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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for unitcorn-0.9.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.9.1 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page