UnitValues 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- unitvalues-0.1.0/LICENSE +21 -0
- unitvalues-0.1.0/PKG-INFO +195 -0
- unitvalues-0.1.0/README.md +181 -0
- unitvalues-0.1.0/pyproject.toml +18 -0
- unitvalues-0.1.0/setup.cfg +4 -0
- unitvalues-0.1.0/src/UnitValues.egg-info/PKG-INFO +195 -0
- unitvalues-0.1.0/src/UnitValues.egg-info/SOURCES.txt +9 -0
- unitvalues-0.1.0/src/UnitValues.egg-info/dependency_links.txt +1 -0
- unitvalues-0.1.0/src/UnitValues.egg-info/top_level.txt +1 -0
- unitvalues-0.1.0/src/unitvalue/__init__.py +1 -0
- unitvalues-0.1.0/src/unitvalue/units.py +776 -0
unitvalues-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Brody Howard
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: UnitValues
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: 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.
|
|
5
|
+
Author-email: Brody Howard <brody.howard@mail.utoronto.ca>
|
|
6
|
+
Project-URL: Homepage, https://github.com/BroRocket/unitvalue/
|
|
7
|
+
Project-URL: Issues, https://github.com/BroRocket/unitvalue/issues
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
|
|
15
|
+
# UnitValue Package
|
|
16
|
+
## Introduction
|
|
17
|
+
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.
|
|
18
|
+
|
|
19
|
+
This package is ideal for applications that require precise unit conversions and tracking of units alongside numerical values.
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
To install the UnitValue package, you can use the following command:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install UnitValue
|
|
26
|
+
```
|
|
27
|
+
## Features
|
|
28
|
+
|
|
29
|
+
- Create `UnitValue` objects with specified units and values.
|
|
30
|
+
- Convert between different units within the same dimension (e.g., meters to kilometers).
|
|
31
|
+
- Support for different measurement systems (e.g., metric and imperial).
|
|
32
|
+
- Arithmetic operations with unit handling (addition, subtraction, multiplication, division).
|
|
33
|
+
- Error handling for unsupported units and invalid operations.
|
|
34
|
+
|
|
35
|
+
## Class and Methods Overview
|
|
36
|
+
|
|
37
|
+
`UnitValue`
|
|
38
|
+
Attributes:
|
|
39
|
+
- value (float): The numerical value.
|
|
40
|
+
- unit (str): The unit associated with the value.
|
|
41
|
+
- system (str): The measurement system (e.g., "METRIC", "IMPERIAL").
|
|
42
|
+
- dimension (str): The dimension of the unit (e.g., "LENGTH", "MASS").
|
|
43
|
+
Methods:
|
|
44
|
+
- __init__(self, value: float, unit: str): Initializes a UnitValue object.
|
|
45
|
+
- create_unit(cls, unit: str, value: float=0) -> 'UnitValue': Creates unit object.
|
|
46
|
+
- to(self, target_unit: str) -> 'UnitValue': Converts the current value to the specified unit.
|
|
47
|
+
- convert_base_metric(self) -> 'UnitValue': Convert the current value to it's base SI/Metric unit.
|
|
48
|
+
- 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.
|
|
49
|
+
- available_units(cls, system: str="", dimension: str="") -> str: Get available units for the specified measurement system and dimension.
|
|
50
|
+
- unit_from_string(cls, unit_str:str) -> 'UnitValue': Create a UnitValue object from a formatted string (e.g., '10 m').
|
|
51
|
+
- from_dict(cls, data: dict) -> 'UnitValue': Create a UnitValue object from a dictionary.
|
|
52
|
+
- to_dict(self) -> dict: Convert the UnitValue object to a dictionary.
|
|
53
|
+
Properties:
|
|
54
|
+
- get_unit: returns unit string.
|
|
55
|
+
- get_dimension: return dimension string.
|
|
56
|
+
- get_system: returns system string.
|
|
57
|
+
|
|
58
|
+
## Supported Units and Conversions
|
|
59
|
+
|
|
60
|
+
The package supports a wide range of units, including but not limited to:
|
|
61
|
+
|
|
62
|
+
### METRIC:
|
|
63
|
+
DISTANCE: ['m', 'km', 'cm', 'mm']
|
|
64
|
+
PRESSURE: ['kg/ms^2', 'MPa', 'bar', 'kPa', 'hPa', 'Pa']
|
|
65
|
+
MASS: ['kg', 'tonne', 'g']
|
|
66
|
+
VELOCITY: ['m/s', 'km/s', 'km/h', 'cm/s', 'mm/s']
|
|
67
|
+
DENSITY: ['kg/m^3', 't/m^3', 'g/m^3']
|
|
68
|
+
VOLUME: ['m^3', 'L', 'cm^3', 'mL', 'mm^3']
|
|
69
|
+
AREA: ['m^2', 'km^2', 'cm^2', 'mm^2']
|
|
70
|
+
TEMPERATURE: ['K', 'c']
|
|
71
|
+
MASS FLOW RATE: ['kg/s', 't/s', 'kg/min', 'g/s']
|
|
72
|
+
ENERGY: ['kgm^2/s^2', 'MJ', 'kJ', 'Nm', 'J', 'eV']
|
|
73
|
+
TIME: ['s', 'h', 'min', 'ms']
|
|
74
|
+
MOMENTUM: ['kgm/s', 'Ns']
|
|
75
|
+
FREQUENCY: ['/s', 'Hz']
|
|
76
|
+
ACCELERATION: ['m/s^2', 'g']
|
|
77
|
+
FORCE: ['kgm/s^2', 'N', 'gcm/s^2']
|
|
78
|
+
ENERGY PER UNIT MASS: ['m^2/s^2']
|
|
79
|
+
MASS PER LENGTH: ['kg/m', 'kg/cm', 'g/cm']
|
|
80
|
+
MASS PER AREA: ['kg/m^2', 'g/cm^2']
|
|
81
|
+
VOLUMETRIC FLOW RATE: ['m^3/s', 'cm^3/s']
|
|
82
|
+
DYNAMIC VISCOCITY: ['kg/ms', 'g/cms']
|
|
83
|
+
KINEMATIC VISCOCITY: ['m^2/s', 'cm^2/s']
|
|
84
|
+
MASS FLUX: ['kg/m^2s']
|
|
85
|
+
|
|
86
|
+
### IMPERIAL:
|
|
87
|
+
DISTANCE: ['in', 'mi', 'yd', 'ft']
|
|
88
|
+
PRESSURE: ['psi', 'psf']
|
|
89
|
+
MASS: ['lb', 'ton', 'slug', 'st', 'oz']
|
|
90
|
+
VELOCITY: ['ft/s', 'mi/s', 'mph', 'in/s']
|
|
91
|
+
DENSITY: ['lb/in^3', 'lb/ft^3', 'lb/yd^3']
|
|
92
|
+
VOLUME: ['gal', 'yd^3', 'ft^3', 'in^3']
|
|
93
|
+
AREA: ['in^2', 'mi^2', 'yd^2', 'ft^2']
|
|
94
|
+
TEMPERATURE: ['f', 'R']
|
|
95
|
+
MASS FLOW RATE: ['lb/s', 'ton/s', 'st/s', 'oz', 'lb/min']
|
|
96
|
+
ENERGY: ['ftlb', 'kcal', 'cal']
|
|
97
|
+
TIME: ['s', 'h', 'min', 'ms']
|
|
98
|
+
MOMENTUM: ['slugft/s', 'lbft/s']
|
|
99
|
+
FREQUENCY: ['rpm']
|
|
100
|
+
ACCELERATION: ['ft/s^2']
|
|
101
|
+
FORCE: ['lbf']
|
|
102
|
+
ENERGY PER UNIT MASS: ['ft^2/s^2']
|
|
103
|
+
MASS PER UNIT LENGTH: ['lb/ft', 'oz/in']
|
|
104
|
+
MASS PER AREA: ['lb/ft^2']
|
|
105
|
+
VOLUMETRIC FLOW RATE: ['ft^3/s', 'gal/s']
|
|
106
|
+
DYNAMIC VISCOCITY: ['lb/fts']
|
|
107
|
+
KINEMATIC VISCOCITY: ['ft^2/s']
|
|
108
|
+
MASS FLUX: ['lb/ft^2s']
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
## Example Usage
|
|
112
|
+
|
|
113
|
+
### Creating a UnitValue
|
|
114
|
+
|
|
115
|
+
To create a `UnitValue` object, use the `create_dimensioned_quantity` function:
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
from unitvalue import UnitValue
|
|
119
|
+
|
|
120
|
+
# Create a UnitValue object with a specified unit and value
|
|
121
|
+
distance = UnitValue.create_unit('meter', 100)
|
|
122
|
+
# Or initialize instance yourself
|
|
123
|
+
distance = UnitValue("METRIC", "DISTANCE", "m", 100)
|
|
124
|
+
# or initialize a unit from a string
|
|
125
|
+
distance = UnitValue.unit_from_string('100 m')
|
|
126
|
+
# or initialize from a dictionary
|
|
127
|
+
data = {'system':'METRIC', 'dimension':'DISTANCE', 'unit':'m', 'value':100}
|
|
128
|
+
distance = UnitValue.from_dict(data)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Arithmetic Operations
|
|
132
|
+
|
|
133
|
+
`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
|
|
134
|
+
|
|
135
|
+
### Converting Units
|
|
136
|
+
|
|
137
|
+
You can convert the unit of a `UnitValue` object using the `to` method:
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
# Convert the distance to kilometers
|
|
141
|
+
distance.to(unit='kilometer')
|
|
142
|
+
print(distance) # Output: 0.1 kilometer
|
|
143
|
+
|
|
144
|
+
# Convert to base metric unit (Useful for Scinetifc calculations)
|
|
145
|
+
distance.convert_base_metric()
|
|
146
|
+
print(distance)
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Basic Example
|
|
150
|
+
```python
|
|
151
|
+
from unitvalue import UnitValue
|
|
152
|
+
|
|
153
|
+
length1 = UnitValue.create_unit('meter', 50)
|
|
154
|
+
length2 = UnitValue.create_unit('meter', 30)
|
|
155
|
+
|
|
156
|
+
# Addition
|
|
157
|
+
total_length = length1 + length2
|
|
158
|
+
print(total_length) # Output: 80 m
|
|
159
|
+
|
|
160
|
+
# Subtraction
|
|
161
|
+
remaining_length = length1 - length2
|
|
162
|
+
print(remaining_length) # Output: 20 m
|
|
163
|
+
|
|
164
|
+
# Multiplication by a scalar
|
|
165
|
+
double_length = length1 * 2
|
|
166
|
+
print(double_length) # Output: 100 m
|
|
167
|
+
|
|
168
|
+
# Division by a scalar
|
|
169
|
+
half_length = length1 / 2
|
|
170
|
+
print(half_length) # Output: 25 m
|
|
171
|
+
|
|
172
|
+
# Multiplication between UnitValue Objects
|
|
173
|
+
area = lenght1 * length2
|
|
174
|
+
print(area) # Output: 150 m^2
|
|
175
|
+
|
|
176
|
+
# Divivsion between UnitValue Objects
|
|
177
|
+
l = area / length2
|
|
178
|
+
print(l) # Output: 50 m
|
|
179
|
+
|
|
180
|
+
# UnitValue object to the power
|
|
181
|
+
werid unit = lenght1**3.5
|
|
182
|
+
print(weird_unit) # Output: 125000 m^3
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Contributing
|
|
186
|
+
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.
|
|
187
|
+
1. Fork the repository.
|
|
188
|
+
2. Create a new branch (git checkout -b feature-branch).
|
|
189
|
+
3. Make your changes.
|
|
190
|
+
4. Commit your changes (git commit -am 'Add new feature').
|
|
191
|
+
5. Push to the branch (git push origin feature-branch).
|
|
192
|
+
6. Open a Pull Request.
|
|
193
|
+
|
|
194
|
+
## License
|
|
195
|
+
This package is distributed under the MIT License. See LICENSE for more information.
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# UnitValue Package
|
|
2
|
+
## Introduction
|
|
3
|
+
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.
|
|
4
|
+
|
|
5
|
+
This package is ideal for applications that require precise unit conversions and tracking of units alongside numerical values.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
To install the UnitValue package, you can use the following command:
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
pip install UnitValue
|
|
12
|
+
```
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- Create `UnitValue` objects with specified units and values.
|
|
16
|
+
- Convert between different units within the same dimension (e.g., meters to kilometers).
|
|
17
|
+
- Support for different measurement systems (e.g., metric and imperial).
|
|
18
|
+
- Arithmetic operations with unit handling (addition, subtraction, multiplication, division).
|
|
19
|
+
- Error handling for unsupported units and invalid operations.
|
|
20
|
+
|
|
21
|
+
## Class and Methods Overview
|
|
22
|
+
|
|
23
|
+
`UnitValue`
|
|
24
|
+
Attributes:
|
|
25
|
+
- value (float): The numerical value.
|
|
26
|
+
- unit (str): The unit associated with the value.
|
|
27
|
+
- system (str): The measurement system (e.g., "METRIC", "IMPERIAL").
|
|
28
|
+
- dimension (str): The dimension of the unit (e.g., "LENGTH", "MASS").
|
|
29
|
+
Methods:
|
|
30
|
+
- __init__(self, value: float, unit: str): Initializes a UnitValue object.
|
|
31
|
+
- create_unit(cls, unit: str, value: float=0) -> 'UnitValue': Creates unit object.
|
|
32
|
+
- to(self, target_unit: str) -> 'UnitValue': Converts the current value to the specified unit.
|
|
33
|
+
- convert_base_metric(self) -> 'UnitValue': Convert the current value to it's base SI/Metric unit.
|
|
34
|
+
- 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.
|
|
35
|
+
- available_units(cls, system: str="", dimension: str="") -> str: Get available units for the specified measurement system and dimension.
|
|
36
|
+
- unit_from_string(cls, unit_str:str) -> 'UnitValue': Create a UnitValue object from a formatted string (e.g., '10 m').
|
|
37
|
+
- from_dict(cls, data: dict) -> 'UnitValue': Create a UnitValue object from a dictionary.
|
|
38
|
+
- to_dict(self) -> dict: Convert the UnitValue object to a dictionary.
|
|
39
|
+
Properties:
|
|
40
|
+
- get_unit: returns unit string.
|
|
41
|
+
- get_dimension: return dimension string.
|
|
42
|
+
- get_system: returns system string.
|
|
43
|
+
|
|
44
|
+
## Supported Units and Conversions
|
|
45
|
+
|
|
46
|
+
The package supports a wide range of units, including but not limited to:
|
|
47
|
+
|
|
48
|
+
### METRIC:
|
|
49
|
+
DISTANCE: ['m', 'km', 'cm', 'mm']
|
|
50
|
+
PRESSURE: ['kg/ms^2', 'MPa', 'bar', 'kPa', 'hPa', 'Pa']
|
|
51
|
+
MASS: ['kg', 'tonne', 'g']
|
|
52
|
+
VELOCITY: ['m/s', 'km/s', 'km/h', 'cm/s', 'mm/s']
|
|
53
|
+
DENSITY: ['kg/m^3', 't/m^3', 'g/m^3']
|
|
54
|
+
VOLUME: ['m^3', 'L', 'cm^3', 'mL', 'mm^3']
|
|
55
|
+
AREA: ['m^2', 'km^2', 'cm^2', 'mm^2']
|
|
56
|
+
TEMPERATURE: ['K', 'c']
|
|
57
|
+
MASS FLOW RATE: ['kg/s', 't/s', 'kg/min', 'g/s']
|
|
58
|
+
ENERGY: ['kgm^2/s^2', 'MJ', 'kJ', 'Nm', 'J', 'eV']
|
|
59
|
+
TIME: ['s', 'h', 'min', 'ms']
|
|
60
|
+
MOMENTUM: ['kgm/s', 'Ns']
|
|
61
|
+
FREQUENCY: ['/s', 'Hz']
|
|
62
|
+
ACCELERATION: ['m/s^2', 'g']
|
|
63
|
+
FORCE: ['kgm/s^2', 'N', 'gcm/s^2']
|
|
64
|
+
ENERGY PER UNIT MASS: ['m^2/s^2']
|
|
65
|
+
MASS PER LENGTH: ['kg/m', 'kg/cm', 'g/cm']
|
|
66
|
+
MASS PER AREA: ['kg/m^2', 'g/cm^2']
|
|
67
|
+
VOLUMETRIC FLOW RATE: ['m^3/s', 'cm^3/s']
|
|
68
|
+
DYNAMIC VISCOCITY: ['kg/ms', 'g/cms']
|
|
69
|
+
KINEMATIC VISCOCITY: ['m^2/s', 'cm^2/s']
|
|
70
|
+
MASS FLUX: ['kg/m^2s']
|
|
71
|
+
|
|
72
|
+
### IMPERIAL:
|
|
73
|
+
DISTANCE: ['in', 'mi', 'yd', 'ft']
|
|
74
|
+
PRESSURE: ['psi', 'psf']
|
|
75
|
+
MASS: ['lb', 'ton', 'slug', 'st', 'oz']
|
|
76
|
+
VELOCITY: ['ft/s', 'mi/s', 'mph', 'in/s']
|
|
77
|
+
DENSITY: ['lb/in^3', 'lb/ft^3', 'lb/yd^3']
|
|
78
|
+
VOLUME: ['gal', 'yd^3', 'ft^3', 'in^3']
|
|
79
|
+
AREA: ['in^2', 'mi^2', 'yd^2', 'ft^2']
|
|
80
|
+
TEMPERATURE: ['f', 'R']
|
|
81
|
+
MASS FLOW RATE: ['lb/s', 'ton/s', 'st/s', 'oz', 'lb/min']
|
|
82
|
+
ENERGY: ['ftlb', 'kcal', 'cal']
|
|
83
|
+
TIME: ['s', 'h', 'min', 'ms']
|
|
84
|
+
MOMENTUM: ['slugft/s', 'lbft/s']
|
|
85
|
+
FREQUENCY: ['rpm']
|
|
86
|
+
ACCELERATION: ['ft/s^2']
|
|
87
|
+
FORCE: ['lbf']
|
|
88
|
+
ENERGY PER UNIT MASS: ['ft^2/s^2']
|
|
89
|
+
MASS PER UNIT LENGTH: ['lb/ft', 'oz/in']
|
|
90
|
+
MASS PER AREA: ['lb/ft^2']
|
|
91
|
+
VOLUMETRIC FLOW RATE: ['ft^3/s', 'gal/s']
|
|
92
|
+
DYNAMIC VISCOCITY: ['lb/fts']
|
|
93
|
+
KINEMATIC VISCOCITY: ['ft^2/s']
|
|
94
|
+
MASS FLUX: ['lb/ft^2s']
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
## Example Usage
|
|
98
|
+
|
|
99
|
+
### Creating a UnitValue
|
|
100
|
+
|
|
101
|
+
To create a `UnitValue` object, use the `create_dimensioned_quantity` function:
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
from unitvalue import UnitValue
|
|
105
|
+
|
|
106
|
+
# Create a UnitValue object with a specified unit and value
|
|
107
|
+
distance = UnitValue.create_unit('meter', 100)
|
|
108
|
+
# Or initialize instance yourself
|
|
109
|
+
distance = UnitValue("METRIC", "DISTANCE", "m", 100)
|
|
110
|
+
# or initialize a unit from a string
|
|
111
|
+
distance = UnitValue.unit_from_string('100 m')
|
|
112
|
+
# or initialize from a dictionary
|
|
113
|
+
data = {'system':'METRIC', 'dimension':'DISTANCE', 'unit':'m', 'value':100}
|
|
114
|
+
distance = UnitValue.from_dict(data)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Arithmetic Operations
|
|
118
|
+
|
|
119
|
+
`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
|
|
120
|
+
|
|
121
|
+
### Converting Units
|
|
122
|
+
|
|
123
|
+
You can convert the unit of a `UnitValue` object using the `to` method:
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
# Convert the distance to kilometers
|
|
127
|
+
distance.to(unit='kilometer')
|
|
128
|
+
print(distance) # Output: 0.1 kilometer
|
|
129
|
+
|
|
130
|
+
# Convert to base metric unit (Useful for Scinetifc calculations)
|
|
131
|
+
distance.convert_base_metric()
|
|
132
|
+
print(distance)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Basic Example
|
|
136
|
+
```python
|
|
137
|
+
from unitvalue import UnitValue
|
|
138
|
+
|
|
139
|
+
length1 = UnitValue.create_unit('meter', 50)
|
|
140
|
+
length2 = UnitValue.create_unit('meter', 30)
|
|
141
|
+
|
|
142
|
+
# Addition
|
|
143
|
+
total_length = length1 + length2
|
|
144
|
+
print(total_length) # Output: 80 m
|
|
145
|
+
|
|
146
|
+
# Subtraction
|
|
147
|
+
remaining_length = length1 - length2
|
|
148
|
+
print(remaining_length) # Output: 20 m
|
|
149
|
+
|
|
150
|
+
# Multiplication by a scalar
|
|
151
|
+
double_length = length1 * 2
|
|
152
|
+
print(double_length) # Output: 100 m
|
|
153
|
+
|
|
154
|
+
# Division by a scalar
|
|
155
|
+
half_length = length1 / 2
|
|
156
|
+
print(half_length) # Output: 25 m
|
|
157
|
+
|
|
158
|
+
# Multiplication between UnitValue Objects
|
|
159
|
+
area = lenght1 * length2
|
|
160
|
+
print(area) # Output: 150 m^2
|
|
161
|
+
|
|
162
|
+
# Divivsion between UnitValue Objects
|
|
163
|
+
l = area / length2
|
|
164
|
+
print(l) # Output: 50 m
|
|
165
|
+
|
|
166
|
+
# UnitValue object to the power
|
|
167
|
+
werid unit = lenght1**3.5
|
|
168
|
+
print(weird_unit) # Output: 125000 m^3
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Contributing
|
|
172
|
+
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.
|
|
173
|
+
1. Fork the repository.
|
|
174
|
+
2. Create a new branch (git checkout -b feature-branch).
|
|
175
|
+
3. Make your changes.
|
|
176
|
+
4. Commit your changes (git commit -am 'Add new feature').
|
|
177
|
+
5. Push to the branch (git push origin feature-branch).
|
|
178
|
+
6. Open a Pull Request.
|
|
179
|
+
|
|
180
|
+
## License
|
|
181
|
+
This package is distributed under the MIT License. See LICENSE for more information.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "UnitValues"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
authors = [
|
|
5
|
+
{ name="Brody Howard", email="brody.howard@mail.utoronto.ca" },
|
|
6
|
+
]
|
|
7
|
+
description = "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."
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
requires-python = ">=3.8"
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Programming Language :: Python :: 3",
|
|
12
|
+
"License :: OSI Approved :: MIT License",
|
|
13
|
+
"Operating System :: OS Independent",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
[project.urls]
|
|
17
|
+
Homepage = "https://github.com/BroRocket/unitvalue/"
|
|
18
|
+
Issues = "https://github.com/BroRocket/unitvalue/issues"
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: UnitValues
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: 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.
|
|
5
|
+
Author-email: Brody Howard <brody.howard@mail.utoronto.ca>
|
|
6
|
+
Project-URL: Homepage, https://github.com/BroRocket/unitvalue/
|
|
7
|
+
Project-URL: Issues, https://github.com/BroRocket/unitvalue/issues
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
|
|
15
|
+
# UnitValue Package
|
|
16
|
+
## Introduction
|
|
17
|
+
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.
|
|
18
|
+
|
|
19
|
+
This package is ideal for applications that require precise unit conversions and tracking of units alongside numerical values.
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
To install the UnitValue package, you can use the following command:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install UnitValue
|
|
26
|
+
```
|
|
27
|
+
## Features
|
|
28
|
+
|
|
29
|
+
- Create `UnitValue` objects with specified units and values.
|
|
30
|
+
- Convert between different units within the same dimension (e.g., meters to kilometers).
|
|
31
|
+
- Support for different measurement systems (e.g., metric and imperial).
|
|
32
|
+
- Arithmetic operations with unit handling (addition, subtraction, multiplication, division).
|
|
33
|
+
- Error handling for unsupported units and invalid operations.
|
|
34
|
+
|
|
35
|
+
## Class and Methods Overview
|
|
36
|
+
|
|
37
|
+
`UnitValue`
|
|
38
|
+
Attributes:
|
|
39
|
+
- value (float): The numerical value.
|
|
40
|
+
- unit (str): The unit associated with the value.
|
|
41
|
+
- system (str): The measurement system (e.g., "METRIC", "IMPERIAL").
|
|
42
|
+
- dimension (str): The dimension of the unit (e.g., "LENGTH", "MASS").
|
|
43
|
+
Methods:
|
|
44
|
+
- __init__(self, value: float, unit: str): Initializes a UnitValue object.
|
|
45
|
+
- create_unit(cls, unit: str, value: float=0) -> 'UnitValue': Creates unit object.
|
|
46
|
+
- to(self, target_unit: str) -> 'UnitValue': Converts the current value to the specified unit.
|
|
47
|
+
- convert_base_metric(self) -> 'UnitValue': Convert the current value to it's base SI/Metric unit.
|
|
48
|
+
- 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.
|
|
49
|
+
- available_units(cls, system: str="", dimension: str="") -> str: Get available units for the specified measurement system and dimension.
|
|
50
|
+
- unit_from_string(cls, unit_str:str) -> 'UnitValue': Create a UnitValue object from a formatted string (e.g., '10 m').
|
|
51
|
+
- from_dict(cls, data: dict) -> 'UnitValue': Create a UnitValue object from a dictionary.
|
|
52
|
+
- to_dict(self) -> dict: Convert the UnitValue object to a dictionary.
|
|
53
|
+
Properties:
|
|
54
|
+
- get_unit: returns unit string.
|
|
55
|
+
- get_dimension: return dimension string.
|
|
56
|
+
- get_system: returns system string.
|
|
57
|
+
|
|
58
|
+
## Supported Units and Conversions
|
|
59
|
+
|
|
60
|
+
The package supports a wide range of units, including but not limited to:
|
|
61
|
+
|
|
62
|
+
### METRIC:
|
|
63
|
+
DISTANCE: ['m', 'km', 'cm', 'mm']
|
|
64
|
+
PRESSURE: ['kg/ms^2', 'MPa', 'bar', 'kPa', 'hPa', 'Pa']
|
|
65
|
+
MASS: ['kg', 'tonne', 'g']
|
|
66
|
+
VELOCITY: ['m/s', 'km/s', 'km/h', 'cm/s', 'mm/s']
|
|
67
|
+
DENSITY: ['kg/m^3', 't/m^3', 'g/m^3']
|
|
68
|
+
VOLUME: ['m^3', 'L', 'cm^3', 'mL', 'mm^3']
|
|
69
|
+
AREA: ['m^2', 'km^2', 'cm^2', 'mm^2']
|
|
70
|
+
TEMPERATURE: ['K', 'c']
|
|
71
|
+
MASS FLOW RATE: ['kg/s', 't/s', 'kg/min', 'g/s']
|
|
72
|
+
ENERGY: ['kgm^2/s^2', 'MJ', 'kJ', 'Nm', 'J', 'eV']
|
|
73
|
+
TIME: ['s', 'h', 'min', 'ms']
|
|
74
|
+
MOMENTUM: ['kgm/s', 'Ns']
|
|
75
|
+
FREQUENCY: ['/s', 'Hz']
|
|
76
|
+
ACCELERATION: ['m/s^2', 'g']
|
|
77
|
+
FORCE: ['kgm/s^2', 'N', 'gcm/s^2']
|
|
78
|
+
ENERGY PER UNIT MASS: ['m^2/s^2']
|
|
79
|
+
MASS PER LENGTH: ['kg/m', 'kg/cm', 'g/cm']
|
|
80
|
+
MASS PER AREA: ['kg/m^2', 'g/cm^2']
|
|
81
|
+
VOLUMETRIC FLOW RATE: ['m^3/s', 'cm^3/s']
|
|
82
|
+
DYNAMIC VISCOCITY: ['kg/ms', 'g/cms']
|
|
83
|
+
KINEMATIC VISCOCITY: ['m^2/s', 'cm^2/s']
|
|
84
|
+
MASS FLUX: ['kg/m^2s']
|
|
85
|
+
|
|
86
|
+
### IMPERIAL:
|
|
87
|
+
DISTANCE: ['in', 'mi', 'yd', 'ft']
|
|
88
|
+
PRESSURE: ['psi', 'psf']
|
|
89
|
+
MASS: ['lb', 'ton', 'slug', 'st', 'oz']
|
|
90
|
+
VELOCITY: ['ft/s', 'mi/s', 'mph', 'in/s']
|
|
91
|
+
DENSITY: ['lb/in^3', 'lb/ft^3', 'lb/yd^3']
|
|
92
|
+
VOLUME: ['gal', 'yd^3', 'ft^3', 'in^3']
|
|
93
|
+
AREA: ['in^2', 'mi^2', 'yd^2', 'ft^2']
|
|
94
|
+
TEMPERATURE: ['f', 'R']
|
|
95
|
+
MASS FLOW RATE: ['lb/s', 'ton/s', 'st/s', 'oz', 'lb/min']
|
|
96
|
+
ENERGY: ['ftlb', 'kcal', 'cal']
|
|
97
|
+
TIME: ['s', 'h', 'min', 'ms']
|
|
98
|
+
MOMENTUM: ['slugft/s', 'lbft/s']
|
|
99
|
+
FREQUENCY: ['rpm']
|
|
100
|
+
ACCELERATION: ['ft/s^2']
|
|
101
|
+
FORCE: ['lbf']
|
|
102
|
+
ENERGY PER UNIT MASS: ['ft^2/s^2']
|
|
103
|
+
MASS PER UNIT LENGTH: ['lb/ft', 'oz/in']
|
|
104
|
+
MASS PER AREA: ['lb/ft^2']
|
|
105
|
+
VOLUMETRIC FLOW RATE: ['ft^3/s', 'gal/s']
|
|
106
|
+
DYNAMIC VISCOCITY: ['lb/fts']
|
|
107
|
+
KINEMATIC VISCOCITY: ['ft^2/s']
|
|
108
|
+
MASS FLUX: ['lb/ft^2s']
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
## Example Usage
|
|
112
|
+
|
|
113
|
+
### Creating a UnitValue
|
|
114
|
+
|
|
115
|
+
To create a `UnitValue` object, use the `create_dimensioned_quantity` function:
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
from unitvalue import UnitValue
|
|
119
|
+
|
|
120
|
+
# Create a UnitValue object with a specified unit and value
|
|
121
|
+
distance = UnitValue.create_unit('meter', 100)
|
|
122
|
+
# Or initialize instance yourself
|
|
123
|
+
distance = UnitValue("METRIC", "DISTANCE", "m", 100)
|
|
124
|
+
# or initialize a unit from a string
|
|
125
|
+
distance = UnitValue.unit_from_string('100 m')
|
|
126
|
+
# or initialize from a dictionary
|
|
127
|
+
data = {'system':'METRIC', 'dimension':'DISTANCE', 'unit':'m', 'value':100}
|
|
128
|
+
distance = UnitValue.from_dict(data)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Arithmetic Operations
|
|
132
|
+
|
|
133
|
+
`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
|
|
134
|
+
|
|
135
|
+
### Converting Units
|
|
136
|
+
|
|
137
|
+
You can convert the unit of a `UnitValue` object using the `to` method:
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
# Convert the distance to kilometers
|
|
141
|
+
distance.to(unit='kilometer')
|
|
142
|
+
print(distance) # Output: 0.1 kilometer
|
|
143
|
+
|
|
144
|
+
# Convert to base metric unit (Useful for Scinetifc calculations)
|
|
145
|
+
distance.convert_base_metric()
|
|
146
|
+
print(distance)
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Basic Example
|
|
150
|
+
```python
|
|
151
|
+
from unitvalue import UnitValue
|
|
152
|
+
|
|
153
|
+
length1 = UnitValue.create_unit('meter', 50)
|
|
154
|
+
length2 = UnitValue.create_unit('meter', 30)
|
|
155
|
+
|
|
156
|
+
# Addition
|
|
157
|
+
total_length = length1 + length2
|
|
158
|
+
print(total_length) # Output: 80 m
|
|
159
|
+
|
|
160
|
+
# Subtraction
|
|
161
|
+
remaining_length = length1 - length2
|
|
162
|
+
print(remaining_length) # Output: 20 m
|
|
163
|
+
|
|
164
|
+
# Multiplication by a scalar
|
|
165
|
+
double_length = length1 * 2
|
|
166
|
+
print(double_length) # Output: 100 m
|
|
167
|
+
|
|
168
|
+
# Division by a scalar
|
|
169
|
+
half_length = length1 / 2
|
|
170
|
+
print(half_length) # Output: 25 m
|
|
171
|
+
|
|
172
|
+
# Multiplication between UnitValue Objects
|
|
173
|
+
area = lenght1 * length2
|
|
174
|
+
print(area) # Output: 150 m^2
|
|
175
|
+
|
|
176
|
+
# Divivsion between UnitValue Objects
|
|
177
|
+
l = area / length2
|
|
178
|
+
print(l) # Output: 50 m
|
|
179
|
+
|
|
180
|
+
# UnitValue object to the power
|
|
181
|
+
werid unit = lenght1**3.5
|
|
182
|
+
print(weird_unit) # Output: 125000 m^3
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Contributing
|
|
186
|
+
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.
|
|
187
|
+
1. Fork the repository.
|
|
188
|
+
2. Create a new branch (git checkout -b feature-branch).
|
|
189
|
+
3. Make your changes.
|
|
190
|
+
4. Commit your changes (git commit -am 'Add new feature').
|
|
191
|
+
5. Push to the branch (git push origin feature-branch).
|
|
192
|
+
6. Open a Pull Request.
|
|
193
|
+
|
|
194
|
+
## License
|
|
195
|
+
This package is distributed under the MIT License. See LICENSE for more information.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
unitvalue
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .units import *
|