Skip to main content

Physical Quantities

This package provides types for representing physical quantities, which have a magnitude and a unit.

An in-depth usage guide is available in the module's docstring.

Quick Start

Create units by creating an enum class subclassing QuantityUnit. The value of each item is scaled to its siblings, with an appropriate value chosen as the smallest representable quantity which forms the base precision when scaling quantities. For example, units of distance where the smallest possible quantity is 0.1mm:

from kodo.quantities import QuantityUnit


class Distance(QuantityUnit):

    MILLIMETERS = 10
    CENTIMETERS = 10 * MILLIMETERS
    METERS = 100 * CENTIMETERS
    KILOMETERS = 1000 * METERS

    SIXTEENTH_INCH = 15  # 1/16" == 1.5mm
    QUARTER_INCH = 4 * SIXTEENTH_INCH
    HALF_INCH = 8 * SIXTEENTH_INCH
    INCH = 16 * SIXTEENTH_INCH
    FOOT = 12 * INCH


class Time(QuantityUnit):

    MILLISECONDS = 1
    SECONDS = 1000 * MILLISECONDS
    MINUTES = 60 * SECONDS

Quantities are then created with the '@' operator, using a numeric value and the desired unit, for example 3m:

from kodo.quantities import Quantity

distance: Quantity[Distance] = 3 @ Distance.METERS

Quantities can be compared with, added to, and subtracted from other quantities of the same type. Note that while use of different types of quantities together will not be caught at run time, a type checker will report it as an error. The following examples use quantities of the Distance type, so all work:

assert (3 @ Distance.METERS) == (300 @ Distance.CENTIMETERS) == (3000 @ Distance.MILLIMETERS)
assert (3 @ Distance.METERS) + (2.5 @ Distance.METERS) == (5.5 @ Distance.METERS)

Scaling with multiplication and division must be with unitless values:

assert (3 @ Distance.METERS) * 2.0 == (6 @ Distance.METERS)
assert (3 @ Distance.METERS) / 2 == (1.5 @ Distance.METERS)

Division with the floor division operator '//' and finding the remainder with the modulus operator '%' work a little differently; the second argument must be another quantity:

# 10m divides into 3m three times…
assert (10 @ Distance.METERS) // (3 @ Distance.METERS) == 3

# … with 1m left over
assert (10 @ Distance.METERS) % (3 @ Distance.METERS) == (1 @ Distance.METERS)

Finally, for most uses the quantity will eventually have to be converted back to a unitless value of a known fixed unit, for example to pass to an external API, or store in a database.
This is done with the right shift operator '>>':

import time

def get_delay() -> Quantity[Time]:
    return 10000 @ Time.MILLISECONDS

# time.sleep() takes a single float argument for the sleep time in seconds
time.sleep( get_delay() >> Time.SECONDS )

If for some reason an integer value is needed instead, the floor division operator can be used as described above with a value of 1 of the desired unit. As a convenience the unit item itself can be used, matching the '>>' operator:

assert (3.6 @ Time.SECONDS) // (1 @ Time.SECONDS) is 3
assert (3.6 @ Time.SECONDS) // Time.SECONDS is 3

Metadata

Release files for kodo.quantities 0.1.2

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

Source distribution (sdist)

Source distribution for kodo.quantities 0.1.2
File Size Uploaded Provenance Actions
kodo_quantities-0.1.2.tar.gz 9.2 kB Without provenance Details for kodo_quantities-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for kodo.quantities 0.1.2
File Interpreter ABI Platform Actions
kodo_quantities-0.1.2-py3-none-any.whl Python 3 none any Details for kodo_quantities-0.1.2-py3-none-any.whl

Total release size: 19.7 kB

Release files / kodo_quantities-0.1.2.tar.gz

Download URL kodo_quantities-0.1.2.tar.gz
Size 9.2 kB
Tags Source
SHA-256 checksum
How to use checksums
9d9299e7a26a89f3e5949e09f79af5ae85d8e4914cc6571ef79aeb1fc215f551
BLAKE2b-256 checksum
How to use checksums
49ec26f1ab0f5d2a960435072829cf5e8aa16efb7896662cdcb8458640c1a195
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.6

Release files / kodo_quantities-0.1.2-py3-none-any.whl

Download URL kodo_quantities-0.1.2-py3-none-any.whl
Size 10.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4bfd3adb4add0b71c37f8a5c664bd12d2d13b6adfce9540692e11ae7e734f252
BLAKE2b-256 checksum
How to use checksums
d6895d5bad71fe0346a017a9f00670648eba5ee71fc1c7da043d90d686c735d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.6

Security for kodo.quantities 0.1.2

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.

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

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