A small package for using units in Python. Allows for computation with dimensioned qauntities and allows you to convert between different units. Do anything you would do with regular floats or integers in Python except now with units. I think you'll find this implementation more elegant then most other unit packages.
Project description
UnitValue Package
Introduction
The UnitValue package is designed to allow easy manipulation and conversion of numerical values associated with units. It currently supports both Imperial and Metric systems and includes commonly used units, with the flexibility to add more units as needed. It sworks with numpy as well.
This package is ideal for applications that require precise unit conversions and tracking of units alongside numerical values.
Installation
To install the UnitValue package, you can use the following command:
pip install UnitValue
Features
- Create
UnitValueobjects with specified units and values. - Convert between different units within the same dimension (e.g., meters to kilometers).
- Support for different measurement systems (e.g., metric and imperial).
- Arithmetic operations with unit handling (addition, subtraction, multiplication, division).
- Error handling for unsupported units and invalid operations.
Class and Methods Overview
UnitValue
Attributes:
- value (float): The numerical value.
- unit (str): The unit associated with the value.
- system (str): The measurement system (e.g., "METRIC", "IMPERIAL").
- dimension (str): The dimension of the unit (e.g., "LENGTH", "MASS"). Methods:
- init(self, value: float, unit: str): Initializes a UnitValue object.
- create_unit(cls, unit: str, value: float=0) -> 'UnitValue': Creates unit object.
- to(self, target_unit: str) -> 'UnitValue': Converts the current value to the specified unit.
- convert_base_metric(self) -> 'UnitValue': Convert the current value to it's base SI/Metric unit.
- add_custom_unitadd_custom_unit(cls, system: str, dimension:str, unit:str, conversion_factor: float) -> None: Allows you to add your own units to your instance of the package.
- available_units(cls, system: str="", dimension: str="") -> str: Get available units for the specified measurement system and dimension.
- unit_from_string(cls, unit_str:str) -> 'UnitValue': Create a UnitValue object from a formatted string (e.g., '10 m').
- from_dict(cls, data: dict) -> 'UnitValue': Create a UnitValue object from a dictionary.
- to_dict(self) -> dict: Convert the UnitValue object to a dictionary. Properties:
- get_unit: returns unit string.
- get_dimension: return dimension string.
- get_system: returns system string.
Supported Units and Conversions
The package supports a wide range of units, including but not limited to:
METRIC:
DISTANCE: ['m', 'km', 'cm', 'mm']
PRESSURE: ['kg/ms^2', 'MPa', 'bar', 'kPa', 'hPa', 'Pa']
MASS: ['kg', 'tonne', 'g']
VELOCITY: ['m/s', 'km/s', 'km/h', 'cm/s', 'mm/s']
DENSITY: ['kg/m^3', 't/m^3', 'g/m^3']
VOLUME: ['m^3', 'L', 'cm^3', 'mL', 'mm^3']
AREA: ['m^2', 'km^2', 'cm^2', 'mm^2']
TEMPERATURE: ['K', 'c']
MASS FLOW RATE: ['kg/s', 't/s', 'kg/min', 'g/s']
ENERGY: ['kgm^2/s^2', 'MJ', 'kJ', 'Nm', 'J', 'eV']
TIME: ['s', 'h', 'min', 'ms']
MOMENTUM: ['kgm/s', 'Ns']
FREQUENCY: ['/s', 'Hz']
ACCELERATION: ['m/s^2', 'g']
FORCE: ['kgm/s^2', 'N', 'gcm/s^2']
ENERGY PER UNIT MASS: ['m^2/s^2']
MASS PER LENGTH: ['kg/m', 'kg/cm', 'g/cm']
MASS PER AREA: ['kg/m^2', 'g/cm^2']
VOLUMETRIC FLOW RATE: ['m^3/s', 'cm^3/s']
DYNAMIC VISCOCITY: ['kg/ms', 'g/cms']
KINEMATIC VISCOCITY: ['m^2/s', 'cm^2/s']
MASS FLUX: ['kg/m^2s']
IMPERIAL:
DISTANCE: ['in', 'mi', 'yd', 'ft']
PRESSURE: ['psi', 'psf']
MASS: ['lb', 'ton', 'slug', 'st', 'oz']
VELOCITY: ['ft/s', 'mi/s', 'mph', 'in/s']
DENSITY: ['lb/in^3', 'lb/ft^3', 'lb/yd^3']
VOLUME: ['gal', 'yd^3', 'ft^3', 'in^3']
AREA: ['in^2', 'mi^2', 'yd^2', 'ft^2']
TEMPERATURE: ['f', 'R']
MASS FLOW RATE: ['lb/s', 'ton/s', 'st/s', 'oz', 'lb/min']
ENERGY: ['ftlb', 'kcal', 'cal']
TIME: ['s', 'h', 'min', 'ms']
MOMENTUM: ['slugft/s', 'lbft/s']
FREQUENCY: ['rpm']
ACCELERATION: ['ft/s^2']
FORCE: ['lbf']
ENERGY PER UNIT MASS: ['ft^2/s^2']
MASS PER UNIT LENGTH: ['lb/ft', 'oz/in']
MASS PER AREA: ['lb/ft^2']
VOLUMETRIC FLOW RATE: ['ft^3/s', 'gal/s']
DYNAMIC VISCOCITY: ['lb/fts']
KINEMATIC VISCOCITY: ['ft^2/s']
MASS FLUX: ['lb/ft^2s']
Example Usage
Creating a UnitValue
To create a UnitValue object, use the create_dimensioned_quantity function:
from unitvalue import UnitValue
# Create a UnitValue object with a specified unit and value
distance = UnitValue.create_unit('meter', 100)
# Or initialize instance yourself
distance = UnitValue("METRIC", "DISTANCE", "m", 100)
# or initialize a unit from a string
distance = UnitValue.unit_from_string('100 m')
# or initialize from a dictionary
data = {'system':'METRIC', 'dimension':'DISTANCE', 'unit':'m', 'value':100}
distance = UnitValue.from_dict(data)
Arithmetic Operations
UnitValue objects support arithmetic operations, maintaining unit consistency (The units do not even need to be in the same system or magnitude for you to perform arithmetic on them as the package will handle this). It is important to know all arithmetic operations return a value in the base metric units
Converting Units
You can convert the unit of a UnitValue object using the to method:
# Convert the distance to kilometers
distance.to(unit='kilometer')
print(distance) # Output: 0.1 kilometer
# Convert to base metric unit (Useful for Scinetifc calculations)
distance.convert_base_metric()
print(distance)
Basic Example
from unitvalue import UnitValue
length1 = UnitValue.create_unit('meter', 50)
length2 = UnitValue.create_unit('meter', 30)
# Addition
total_length = length1 + length2
print(total_length) # Output: 80 m
# Subtraction
remaining_length = length1 - length2
print(remaining_length) # Output: 20 m
# Multiplication by a scalar
double_length = length1 * 2
print(double_length) # Output: 100 m
# Division by a scalar
half_length = length1 / 2
print(half_length) # Output: 25 m
# Multiplication between UnitValue Objects
area = lenght1 * length2
print(area) # Output: 150 m^2
# Divivsion between UnitValue Objects
l = area / length2
print(l) # Output: 50 m
# UnitValue object to the power
werid unit = lenght1**3.5
print(weird_unit) # Output: 125000 m^3
Contributing
Contributions are welcome! If you find any issues or have ideas for enhancements, feel free to open an issue or submit a pull request on GitHub.
- Fork the repository.
- Create a new branch (git checkout -b feature-branch).
- Make your changes.
- Commit your changes (git commit -am 'Add new feature').
- Push to the branch (git push origin feature-branch).
- Open a Pull Request.
License
This package is distributed under the MIT License. See LICENSE for more information.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file unitvalues-0.1.0.tar.gz.
File metadata
- Download URL: unitvalues-0.1.0.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fdbd9148d0c4e3f1d6bd099f3cee7d5167703a0cd7037b5647cdbf034a28fbb4
|
|
| MD5 |
47fb2a995b229e09acfacc3929080804
|
|
| BLAKE2b-256 |
bf3a2c44b11316a2a8d93b55729420d8520458ed71dc7f5b27ec47ab92aa49df
|
File details
Details for the file UnitValues-0.1.0-py3-none-any.whl.
File metadata
- Download URL: UnitValues-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2487b732fe76db4d4e59354575f6c746b04540b7b72a41e3a983d243eb057ceb
|
|
| MD5 |
8f084c7f57f8e212cfe377f6be7c1d12
|
|
| BLAKE2b-256 |
24ef7a3b8859ae8a8c06ce032b3d674fd0457041065c9455e1926f9a08112959
|