Skip to main content

No project description provided

Project description

Physities

A Python library for representing and working with physical quantities and units. Provides dimensional analysis, unit conversion, and mathematical operations on physical measurements.

Installation

pip install physities

Or with Poetry:

poetry add physities

Quick Start

from physities.src.unit import Meter, Second, Kilometer, Hour

# Create composite unit types using operator syntax
MetersPerSecond = Meter / Second
KilometersPerHour = Kilometer / Hour

# Create values
v1 = MetersPerSecond(40)      # 40 m/s
v2 = KilometersPerHour(144)   # 144 km/h

# Convert between units
v3 = v2.convert(MetersPerSecond)  # 40 m/s

Available Units

Base SI Units

Dimension Base Unit
Length Meter
Mass Kilogram
Time Second
Temperature Kelvin
Amount Unity
Electric Current Ampere
Luminous Intensity Candela

Derived Units

Length

Gigameter, Megameter, Kilometer, Hectometer, Decameter, Decimeter, Centimeter, Millimeter, Micrometer, Nanometer, Foot, Yard, Inch, Mile, Furlong, Rod

Time

Nanosecond, Microsecond, Millisecond, Centisecond, Decisecond, Minute, Hour, Day, Week, Month, Year, Decade, Century, Millennium

Mass

Gigagram, Megagram, Tonne, Hectogram, Decagram, Gram, Decigram, Centigram, Milligram, Microgram, Nanogram, Pound, Ounce, Stone, Carat, Grain, Slug

Electric Current

Gigaampere, Megaampere, Kiloampere, Milliampere, Microampere, Nanoampere

Amount

Dozen, Moles, Pairs, Score

Area

Meter2, Kilometer2, Hectare, Centimeter2, Millimeter2, Foot2, Yard2, Inch2, Mile2, Acre

Volume

Meter3, Liter, Kiloliter, Milliliter, Centimeter3, Foot3, Gallon, Pint, Barrel

Examples

Creating and Using Units

from physities.src.unit import Meter, Second, Kilogram

# Create a velocity unit
Velocity = Meter / Second
v = Velocity(10)  # 10 m/s

# Create an acceleration unit
Acceleration = Meter / (Second ** 2)
a = Acceleration(9.8)  # 9.8 m/s²

# Create a force unit (Newton)
Newton = Kilogram * Meter / (Second ** 2)
force = Newton(100)  # 100 N

Unit Conversion

from physities.src.unit import Kilometer, Mile, Hour

# Create speed units
Kmh = Kilometer / Hour
Mph = Mile / Hour

# Convert between units
speed_kmh = Kmh(100)
speed_mph = speed_kmh.convert(Mph)

Mathematical Operations

from physities.src.unit import Meter, Second

Ms = Meter / Second

v1 = Ms(10)
v2 = Ms(20)

# Addition and subtraction (same units only)
v3 = v1 + v2  # 30 m/s
v4 = v2 - v1  # 10 m/s

# Multiplication and division with scalars
v5 = v1 * 2   # 20 m/s
v6 = v2 / 2   # 10 m/s

Creating Custom Units

from physities.src.unit import Unit
from physities.src.scale import Scale
from physities.src.dimension import Dimension

# Define a custom unit
class Furlong(Unit):
    scale = Scale(
        dimension=Dimension.new_length(),
        from_base_scale_conversions=(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0),
        rescale_value=201.168,  # 1 furlong = 201.168 meters
    )
    value = None

# Or derive from existing units
from physities.src.unit import Meter
MyUnit = 201.168 * Meter  # Equivalent to Furlong

Architecture

Physities uses a three-layer architecture:

  1. BaseDimension: Enum of 7 SI base dimensions
  2. Dimension: Frozen dataclass combining base dimensions into composite physical dimensions
  3. Scale: Frozen dataclass with dimension + conversion factors
  4. Unit + MetaUnit: MetaUnit metaclass enables operator overloading at the class level

The metaclass pattern allows elegant syntax like Meter / Second to create new unit types dynamically.

API Reference

Unit Class

The main class for working with physical quantities.

class Unit:
    scale: Scale      # The scale/unit definition
    value: float      # The numeric value

    def convert(self, target: Type[Unit]) -> Unit:
        """Convert to another unit of the same dimension."""

Scale Class

Defines unit conversion factors.

@dataclass(frozen=True, slots=True)
class Scale:
    dimension: Dimension
    from_base_scale_conversions: tuple[float, ...]
    rescale_value: float

Dimension Class

Represents physical dimensions.

@dataclass(frozen=True, slots=True)
class Dimension:
    dimensions: tuple[int, ...]

    @classmethod
    def new_length(cls) -> Dimension: ...
    @classmethod
    def new_mass(cls) -> Dimension: ...
    @classmethod
    def new_time(cls) -> Dimension: ...
    @classmethod
    def new_temperature(cls) -> Dimension: ...
    @classmethod
    def new_amount(cls) -> Dimension: ...
    @classmethod
    def new_electric_current(cls) -> Dimension: ...
    @classmethod
    def new_luminous_intensity(cls) -> Dimension: ...

BaseDimension Enum

The 7 SI base dimensions:

class BaseDimension(Enum):
    LENGTH = 0
    MASS = 1
    TEMPERATURE = 2
    TIME = 3
    AMOUNT = 4
    ELECTRIC_CURRENT = 5
    LUMINOUS_INTENSITY = 6

Development

# Install dependencies
poetry install

# Run tests
pytest

# Run linting
ruff check .

License

MIT

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

physities-0.1.3.tar.gz (13.0 kB view details)

Uploaded Source

Built Distributions

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

physities-0.1.3-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

physities-0.1.3-cp312-cp312-win_amd64.whl (234.0 kB view details)

Uploaded CPython 3.12Windows x86-64

physities-0.1.3-cp312-cp312-manylinux_2_34_x86_64.whl (396.6 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

physities-0.1.3-cp312-cp312-macosx_11_0_arm64.whl (348.0 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

physities-0.1.3-cp311-cp311-win_amd64.whl (238.0 kB view details)

Uploaded CPython 3.11Windows x86-64

physities-0.1.3-cp311-cp311-manylinux_2_34_x86_64.whl (400.8 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

physities-0.1.3-cp311-cp311-macosx_11_0_arm64.whl (351.5 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

File details

Details for the file physities-0.1.3.tar.gz.

File metadata

  • Download URL: physities-0.1.3.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.11.15 Linux/6.14.0-1017-azure

File hashes

Hashes for physities-0.1.3.tar.gz
Algorithm Hash digest
SHA256 89defcc7011a78491d6998b5818f6e487976e460857199089724249812f261da
MD5 cf4d62d40c8857d5ba124a8418502dea
BLAKE2b-256 e1db172655b70f4e60101e49e738d4a41c2343474f6b2c028809061804c705ef

See more details on using hashes here.

File details

Details for the file physities-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: physities-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.11.15 Linux/6.14.0-1017-azure

File hashes

Hashes for physities-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a6e1839b8241f0445f6d9f2f523400ea020a6f04e36beb874f96fbb964eec52e
MD5 3a94c8597a6485254670c61071d79329
BLAKE2b-256 ef59f518ccf83951511bb40dd4bcc758589c148f27a2c64a9852be102f243022

See more details on using hashes here.

File details

Details for the file physities-0.1.3-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: physities-0.1.3-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 234.0 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for physities-0.1.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 fce05dc817b25b4e7d19fed53fed0df35ad1d7db38025b1d9c0b30dd0aedf47d
MD5 7698e54a9ba1be389141617993152ed4
BLAKE2b-256 d26f8fb0471c50b90fa3e7ab553a2e3bdcb3ca191c7bae0da123fbc96d7e5524

See more details on using hashes here.

File details

Details for the file physities-0.1.3-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for physities-0.1.3-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 2ee89d59b744ae1b196e7cee52ebe51817bb1dd80095ef4b66f8496749743064
MD5 40c43348fcfebe0d290de822dde953e2
BLAKE2b-256 07f1154e41d03ed8719630e71cceae903f5968d7d088027cc9b86a0952739d3e

See more details on using hashes here.

File details

Details for the file physities-0.1.3-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for physities-0.1.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d7e790319b73a12e9c77c225a86ec6e6d10c810a1f5de69f105dbe21a9262cc5
MD5 a8c41430bb5f0ba6b8e053b6fcd7b6bf
BLAKE2b-256 52bddcb492d2de5387b1c132312ede913834ebba5109bf55f9bdc83e07f49b45

See more details on using hashes here.

File details

Details for the file physities-0.1.3-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: physities-0.1.3-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 238.0 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for physities-0.1.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 861e4ba4bd2d33113d7097290f07b541841b2eb8866d320118603d31de6057b9
MD5 2d4e338453c2d880469041e3d0961aad
BLAKE2b-256 8b537e01c0820c371c4e4f665921bf1b7db338e77b6fee427b4d306bdc5e9791

See more details on using hashes here.

File details

Details for the file physities-0.1.3-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for physities-0.1.3-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 e796cab63f25b631f2bcd4d4b55520c96efec49c1e646f38e6f55b60932a11e7
MD5 c12b72a1bcda17a79a3af95f0506eda5
BLAKE2b-256 44f5d0e0c40ff3d5d21ae708b30f4de4c09b5370508c41aef52be3eb6ee98d54

See more details on using hashes here.

File details

Details for the file physities-0.1.3-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for physities-0.1.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 07971c51990306c5d5d1296e3a9463df8874b0fd4a0dc0f3c08ce5a2ff3d10ca
MD5 d338c2b11c958b9eb247120d207184ff
BLAKE2b-256 02f21f09c350c0d3a2a6c93ddcf2196bd10b404d6ddccd27b7f449285817c70a

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