Unit Module¶
The unit module provides the core classes for working with physical quantities.
MetaUnit¶
- class physities.src.unit.unit.MetaUnit[source]¶
Bases:
typeMetaclass enabling operator overloading on Unit classes.
MetaUnit allows Unit types (not instances) to be combined using arithmetic operators to create new composite unit types. This enables syntax like
Meter / Secondto create a velocity unit type.- scale¶
The Scale defining this unit type’s dimension and conversion factors.
Examples
>>> # Create composite unit types >>> Velocity = Meter / Second >>> Acceleration = Meter / (Second ** 2) >>> Force = Kilogram * Acceleration
>>> # Scale a unit type >>> Kilometer = Meter * 1000
Unit¶
- class physities.src.unit.unit.Unit(value)[source]¶
Bases:
objectBase class for physical quantity values with units.
Unit represents a physical quantity with both a numeric value and a unit (defined by its Scale). It supports arithmetic operations that properly handle unit conversions and dimensional analysis.
- scale¶
The Scale defining this unit’s dimension and conversion factors.
- value¶
The numeric value in this unit.
Examples
>>> # Create values with units >>> distance = Meter(100) >>> time = Second(10)
>>> # Arithmetic operations >>> velocity = distance / time # Creates a m/s value >>> doubled = velocity * 2 >>> total = Meter(50) + Meter(30)
>>> # Unit conversion >>> km = Meter(1000).convert(Kilometer) # 1 km
>>> # Equality across units >>> Meter(1000) == Kilometer(1) # True
Pre-defined Units¶
The following units are available in physities.src.unit:
Base SI Units¶
Unit |
Dimension |
Description |
|---|---|---|
|
Length |
SI base unit of length |
|
Mass |
SI base unit of mass |
|
Time |
SI base unit of time |
|
Temperature |
SI base unit of temperature |
|
Electric Current |
SI base unit of current |
|
Amount |
Dimensionless unit |
|
Luminous Intensity |
SI base unit of luminosity |
Length Units¶
Gigameter, Megameter, Kilometer, Hectometer, Decameter,
Decimeter, Centimeter, Millimeter, Micrometer, Nanometer,
Foot, Yard, Inch, Mile, Furlong, Rod
Time Units¶
Nanosecond, Microsecond, Millisecond, Centisecond, Decisecond,
Minute, Hour, Day, Week, Month, Year, Decade,
Century, Millennium
Mass Units¶
Gigagram, Megagram, Tonne, Hectogram, Decagram, Gram,
Decigram, Centigram, Milligram, Microgram, Nanogram,
Pound, Ounce, Stone, Carat, Grain, Slug
Area Units¶
Meter2, Kilometer2, Hectare, Centimeter2, Millimeter2,
Foot2, Yard2, Inch2, Mile2, Acre
Volume Units¶
Meter3, Liter, Kiloliter, Milliliter, Centimeter3,
Foot3, Gallon, Pint, Barrel
Usage Examples¶
Creating and Using Units¶
from physities.src.unit import Meter, Second, Kilogram
# Simple quantities
distance = Meter(100)
time = Second(10)
# Composite units
Velocity = Meter / Second
speed = Velocity(10)
# Operations
total_distance = distance + Meter(50)
doubled = speed * 2
Unit Conversion¶
from physities.src.unit import Meter, Kilometer
m = Meter(1000)
km = m.convert(Kilometer)
print(km.value) # 1.0
Creating Custom Units¶
from physities.src.unit import Meter, Unit
from physities.src.scale import Scale
from physities.src.dimension import Dimension
# Using multiplication
Furlong = Meter * 201.168
# Using Scale directly
class Angstrom(Unit):
scale = Scale.new(
dimension=Dimension.new_length(),
from_base_scale_conversions=(1e-10, 1, 1, 1, 1, 1, 1),
)
value = None