TypedUnit 0.0.3__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.
- typedunit-0.0.3/.flake8 +6 -0
- typedunit-0.0.3/.github/dependabot.yml +11 -0
- typedunit-0.0.3/.github/workflows/deploy_PyPi.yml +20 -0
- typedunit-0.0.3/.github/workflows/deploy_anaconda.yml +21 -0
- typedunit-0.0.3/.github/workflows/deploy_coverage.yml +25 -0
- typedunit-0.0.3/.github/workflows/deploy_documentation.yml +24 -0
- typedunit-0.0.3/.gitignore +28 -0
- typedunit-0.0.3/LICENSE +21 -0
- typedunit-0.0.3/PKG-INFO +262 -0
- typedunit-0.0.3/README.rst +202 -0
- typedunit-0.0.3/TypedUnit/__init__.py +9 -0
- typedunit-0.0.3/TypedUnit/_version.py +34 -0
- typedunit-0.0.3/TypedUnit/directories.py +34 -0
- typedunit-0.0.3/TypedUnit/helper.py +20 -0
- typedunit-0.0.3/TypedUnit/units.py +106 -0
- typedunit-0.0.3/TypedUnit/utils.py +0 -0
- typedunit-0.0.3/TypedUnit.egg-info/PKG-INFO +262 -0
- typedunit-0.0.3/TypedUnit.egg-info/SOURCES.txt +39 -0
- typedunit-0.0.3/TypedUnit.egg-info/dependency_links.txt +1 -0
- typedunit-0.0.3/TypedUnit.egg-info/requires.txt +17 -0
- typedunit-0.0.3/TypedUnit.egg-info/top_level.txt +1 -0
- typedunit-0.0.3/docs/Makefile +19 -0
- typedunit-0.0.3/docs/examples/README.rst +6 -0
- typedunit-0.0.3/docs/examples/example_0.py +27 -0
- typedunit-0.0.3/docs/examples/example_1.py +27 -0
- typedunit-0.0.3/docs/images/logo.png +0 -0
- typedunit-0.0.3/docs/make.bat +35 -0
- typedunit-0.0.3/docs/source/_static/default.css +19 -0
- typedunit-0.0.3/docs/source/_static/thumbnail.png +0 -0
- typedunit-0.0.3/docs/source/code.rst +7 -0
- typedunit-0.0.3/docs/source/conf.py +167 -0
- typedunit-0.0.3/docs/source/gallery/index.rst +71 -0
- typedunit-0.0.3/docs/source/index.rst +13 -0
- typedunit-0.0.3/docs/source/references.rst +6 -0
- typedunit-0.0.3/docs/source/sg_execution_times.rst +40 -0
- typedunit-0.0.3/docs/source/theory.rst +5 -0
- typedunit-0.0.3/meta.yaml +59 -0
- typedunit-0.0.3/pyproject.toml +86 -0
- typedunit-0.0.3/setup.cfg +4 -0
- typedunit-0.0.3/tests/__init__.py +0 -0
- typedunit-0.0.3/tests/test_0.py +432 -0
typedunit-0.0.3/.flake8
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
name: Deploy to PyPi
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [ "master" ]
|
|
6
|
+
tags:
|
|
7
|
+
- '*'
|
|
8
|
+
pull_request:
|
|
9
|
+
branches: [ "master" ]
|
|
10
|
+
|
|
11
|
+
permissions:
|
|
12
|
+
contents: write
|
|
13
|
+
|
|
14
|
+
jobs:
|
|
15
|
+
pure_package:
|
|
16
|
+
uses: MartinPdeS/MPSActions/.github/workflows/publish_pure_package_to_PyPi.yml@master
|
|
17
|
+
with:
|
|
18
|
+
python-version: "3.11"
|
|
19
|
+
secrets:
|
|
20
|
+
PYPI_API_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
name: Deploy Conda Packages
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [ "master" ]
|
|
6
|
+
tags: '*'
|
|
7
|
+
pull_request:
|
|
8
|
+
branches: [ "master" ]
|
|
9
|
+
|
|
10
|
+
permissions:
|
|
11
|
+
contents: write
|
|
12
|
+
|
|
13
|
+
jobs:
|
|
14
|
+
ManyLinux_x86_64:
|
|
15
|
+
uses: MartinPdeS/MPSActions/.github/workflows/publish_compiled_package_to_anaconda.yml@master
|
|
16
|
+
with:
|
|
17
|
+
python-versions: '["3.11", "3.12", "3.13"]'
|
|
18
|
+
apt-package: ""
|
|
19
|
+
secrets:
|
|
20
|
+
ANACONDA_API_TOKEN: ${{ secrets.ANACONDA_API_TOKEN }}
|
|
21
|
+
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Simple workflow for deploying static content to GitHub Pages
|
|
2
|
+
name: Deploy coverage
|
|
3
|
+
|
|
4
|
+
on:
|
|
5
|
+
push:
|
|
6
|
+
branches: [ "master" ]
|
|
7
|
+
tags: '*'
|
|
8
|
+
pull_request:
|
|
9
|
+
branches: [ "master" ]
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
|
|
13
|
+
permissions:
|
|
14
|
+
contents: write
|
|
15
|
+
pages: write
|
|
16
|
+
id-token: write
|
|
17
|
+
|
|
18
|
+
jobs:
|
|
19
|
+
ManyLinux_x86_64:
|
|
20
|
+
uses: MartinPdeS/MPSActions/.github/workflows/publish_coverage.yml@master
|
|
21
|
+
with:
|
|
22
|
+
python-version: "3.11"
|
|
23
|
+
apt-package: xvfb
|
|
24
|
+
secrets:
|
|
25
|
+
_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Simple workflow for deploying static content to GitHub Pages
|
|
2
|
+
name: Documentation
|
|
3
|
+
|
|
4
|
+
on:
|
|
5
|
+
push:
|
|
6
|
+
branches: [ "master" ]
|
|
7
|
+
tags: '*'
|
|
8
|
+
pull_request:
|
|
9
|
+
branches: [ "master" ]
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
|
|
13
|
+
permissions:
|
|
14
|
+
contents: write
|
|
15
|
+
pages: write
|
|
16
|
+
id-token: write
|
|
17
|
+
|
|
18
|
+
jobs:
|
|
19
|
+
ManyLinux_x86_64:
|
|
20
|
+
uses: MartinPdeS/MPSActions/.github/workflows/publish_documentation.yml@master
|
|
21
|
+
with:
|
|
22
|
+
python-version: "3.11"
|
|
23
|
+
package-name: "TypedUnit"
|
|
24
|
+
apt-package: xvfb jq
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# General patterns
|
|
2
|
+
*.DS_store
|
|
3
|
+
*.ipynb_checkpoints*
|
|
4
|
+
*__pycache__*
|
|
5
|
+
*.so
|
|
6
|
+
*.a
|
|
7
|
+
.coverage*
|
|
8
|
+
*.pytest_cache
|
|
9
|
+
htmlcov
|
|
10
|
+
dist
|
|
11
|
+
build
|
|
12
|
+
*.egg*
|
|
13
|
+
*.whl
|
|
14
|
+
CMakeCache.txt
|
|
15
|
+
CMakeFiles
|
|
16
|
+
install_manifest.txt
|
|
17
|
+
cmake_install.cmake
|
|
18
|
+
wheelhouse
|
|
19
|
+
|
|
20
|
+
# Docs-specific patterns
|
|
21
|
+
docs/source/gallery/*
|
|
22
|
+
!docs/source/gallery/index.rst
|
|
23
|
+
PyOptik/data/**/test*
|
|
24
|
+
PyOptik/data/**/example*
|
|
25
|
+
|
|
26
|
+
# Override for documentation_build
|
|
27
|
+
!documentation_build
|
|
28
|
+
!documentation_build/**
|
typedunit-0.0.3/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Martin de Sivry
|
|
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.
|
typedunit-0.0.3/PKG-INFO
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: TypedUnit
|
|
3
|
+
Version: 0.0.3
|
|
4
|
+
Summary: A package wrapping pint for handling typed units in Python.
|
|
5
|
+
Author-email: Martin Poinsinet de Sivry-Houle <martin.poinsinet.de.sivry@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2020 Martin de Sivry
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/MartinPdeS/TypedUnit
|
|
29
|
+
Project-URL: Documentation, https://martinpdes.github.io/TypedUnit/
|
|
30
|
+
Project-URL: Repository, https://github.com/MartinPdeS/TypedUnit.git
|
|
31
|
+
Keywords: typed units,pint,physical quantities
|
|
32
|
+
Classifier: Programming Language :: Python
|
|
33
|
+
Classifier: Programming Language :: Python :: 3
|
|
34
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
35
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
36
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
37
|
+
Classifier: Development Status :: 4 - Beta
|
|
38
|
+
Classifier: Topic :: Scientific/Engineering :: Physics
|
|
39
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
40
|
+
Classifier: Intended Audience :: Science/Research
|
|
41
|
+
Requires-Python: >=3.10
|
|
42
|
+
Description-Content-Type: text/x-rst
|
|
43
|
+
License-File: LICENSE
|
|
44
|
+
Requires-Dist: pint
|
|
45
|
+
Requires-Dist: numpy
|
|
46
|
+
Requires-Dist: matplotlib
|
|
47
|
+
Provides-Extra: testing
|
|
48
|
+
Requires-Dist: pytest<9.0,>=7.4; extra == "testing"
|
|
49
|
+
Requires-Dist: pytest-cov<7,>=2; extra == "testing"
|
|
50
|
+
Requires-Dist: pytest-json-report~=1.5; extra == "testing"
|
|
51
|
+
Requires-Dist: flake8==7.3.0; extra == "testing"
|
|
52
|
+
Requires-Dist: coverage~=7.6; extra == "testing"
|
|
53
|
+
Provides-Extra: documentation
|
|
54
|
+
Requires-Dist: numpydoc~=1.8; extra == "documentation"
|
|
55
|
+
Requires-Dist: sphinx>=6; extra == "documentation"
|
|
56
|
+
Requires-Dist: sphinx-gallery~=0.17; extra == "documentation"
|
|
57
|
+
Requires-Dist: sphinx-rtd-theme<4,>=2; extra == "documentation"
|
|
58
|
+
Requires-Dist: pydata-sphinx-theme~=0.15; extra == "documentation"
|
|
59
|
+
Dynamic: license-file
|
|
60
|
+
|
|
61
|
+
TypedUnits
|
|
62
|
+
===========
|
|
63
|
+
|
|
64
|
+
|python| |docs| |coverage| |PyPi| |PyPi_download|
|
|
65
|
+
|
|
66
|
+
**Type-safe physical units for Python**
|
|
67
|
+
|
|
68
|
+
TypedUnits is a Python library that extends Pint with type safety and validation capabilities. It allows you to create unit-aware types that work seamlessly with Python's type system and provide runtime validation for physical quantities.
|
|
69
|
+
|
|
70
|
+
**Key Features:**
|
|
71
|
+
|
|
72
|
+
- **Type Safety**: Use `isinstance()` checks with unit types like `Energy`, `Time`, `Power`
|
|
73
|
+
- **Automatic Validation**: Function decorators for unit validation based on type annotations
|
|
74
|
+
- **Pydantic Integration**: Full compatibility with Pydantic dataclasses and models
|
|
75
|
+
- **Normal Pint Usage**: Keep your existing Pint code - just add type safety on top
|
|
76
|
+
|
|
77
|
+
----
|
|
78
|
+
|
|
79
|
+
**Quick Example**
|
|
80
|
+
|
|
81
|
+
.. code-block:: python
|
|
82
|
+
|
|
83
|
+
from TypedUnits import Energy, Time, Power, validate, ureg
|
|
84
|
+
|
|
85
|
+
# Normal Pint instantiation
|
|
86
|
+
energy = 100 * ureg.joule
|
|
87
|
+
time = 10 * ureg.second
|
|
88
|
+
|
|
89
|
+
# But now isinstance works!
|
|
90
|
+
assert isinstance(energy, Energy) # True
|
|
91
|
+
assert isinstance(time, Time) # True
|
|
92
|
+
|
|
93
|
+
@validate_units
|
|
94
|
+
def calculate_power(energy: Energy, time: Time) -> Power:
|
|
95
|
+
"""Function with automatic unit validation."""
|
|
96
|
+
return (energy / time).to(ureg.watt)
|
|
97
|
+
|
|
98
|
+
# This works
|
|
99
|
+
power = calculate_power(energy, time)
|
|
100
|
+
print(f"Power: {power}")
|
|
101
|
+
|
|
102
|
+
# This fails with clear error message
|
|
103
|
+
length = 5 * ureg.meter
|
|
104
|
+
# calculate_power(length, time) # TypeError: Expected Energy, got Length
|
|
105
|
+
|
|
106
|
+
----
|
|
107
|
+
|
|
108
|
+
**Installation**
|
|
109
|
+
|
|
110
|
+
.. code-block:: bash
|
|
111
|
+
|
|
112
|
+
pip install TypedUnits
|
|
113
|
+
|
|
114
|
+
----
|
|
115
|
+
|
|
116
|
+
**Basic Usage**
|
|
117
|
+
|
|
118
|
+
**Unit Type Checking**
|
|
119
|
+
|
|
120
|
+
.. code-block:: python
|
|
121
|
+
|
|
122
|
+
from TypedUnits import Energy, Length, Mass, ureg
|
|
123
|
+
|
|
124
|
+
# Create quantities the normal way
|
|
125
|
+
kinetic_energy = 0.5 * 10 * ureg.kilogram * (5 * ureg.meter / ureg.second)**2
|
|
126
|
+
potential_energy = 10 * ureg.kilogram * 9.81 * ureg.meter / ureg.second**2 * 2 * ureg.meter
|
|
127
|
+
distance = 100 * ureg.meter
|
|
128
|
+
mass = 5 * ureg.kilogram
|
|
129
|
+
|
|
130
|
+
# Type checking works
|
|
131
|
+
assert isinstance(kinetic_energy, Energy)
|
|
132
|
+
assert isinstance(potential_energy, Energy)
|
|
133
|
+
assert isinstance(distance, Length)
|
|
134
|
+
assert isinstance(mass, Mass)
|
|
135
|
+
|
|
136
|
+
**Function Validation**
|
|
137
|
+
|
|
138
|
+
.. code-block:: python
|
|
139
|
+
|
|
140
|
+
from TypedUnits import validate_units, Energy, Time, Power
|
|
141
|
+
|
|
142
|
+
@validate_units
|
|
143
|
+
def calculate_work(power: Power, time: Time) -> Energy:
|
|
144
|
+
"""Calculate work from power and time."""
|
|
145
|
+
return (power * time).to(ureg.joule)
|
|
146
|
+
|
|
147
|
+
power = 1500 * ureg.watt
|
|
148
|
+
time = 3600 * ureg.second # 1 hour
|
|
149
|
+
work = calculate_work(power, time)
|
|
150
|
+
print(f"Work done: {work.to(ureg.kilowatt_hour)}")
|
|
151
|
+
|
|
152
|
+
**Pydantic Integration**
|
|
153
|
+
|
|
154
|
+
.. code-block:: python
|
|
155
|
+
|
|
156
|
+
from TypedUnits import Energy, Length, Mass
|
|
157
|
+
from pydantic.dataclasses import dataclass
|
|
158
|
+
|
|
159
|
+
@dataclass
|
|
160
|
+
class Projectile:
|
|
161
|
+
kinetic_energy: Energy
|
|
162
|
+
height: Length
|
|
163
|
+
mass: Mass
|
|
164
|
+
|
|
165
|
+
# Automatic validation on creation
|
|
166
|
+
projectile = Projectile(
|
|
167
|
+
kinetic_energy=500 * ureg.joule,
|
|
168
|
+
height=10 * ureg.meter,
|
|
169
|
+
mass=2 * ureg.kilogram
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
----
|
|
173
|
+
|
|
174
|
+
**Available Unit Types**
|
|
175
|
+
|
|
176
|
+
TypedUnits includes common physical unit types:
|
|
177
|
+
|
|
178
|
+
- **Mechanical**: `Energy`, `Power`, `Force`, `Pressure`
|
|
179
|
+
- **Spatial**: `Length`, `Area`, `Volume`, `Angle`
|
|
180
|
+
- **Temporal**: `Time`, `Frequency`
|
|
181
|
+
- **Thermal**: `Temperature`, `ThermalConductivity`
|
|
182
|
+
- **Electrical**: `Current`, `Voltage`, `Resistance`, `Capacitance`
|
|
183
|
+
- **Optical**: `RefractiveIndex`, `Wavelength`, `ElectricField`
|
|
184
|
+
- **Mass**: `Mass`, `Density`, `MolarMass`
|
|
185
|
+
|
|
186
|
+
----
|
|
187
|
+
|
|
188
|
+
**Advanced Features**
|
|
189
|
+
|
|
190
|
+
**Custom Unit Types**
|
|
191
|
+
|
|
192
|
+
.. code-block:: python
|
|
193
|
+
|
|
194
|
+
from TypedUnits import create_unit_type
|
|
195
|
+
|
|
196
|
+
# Create your own unit types
|
|
197
|
+
MagneticField = create_unit_type('MagneticField', '[magnetic_flux_density]')
|
|
198
|
+
|
|
199
|
+
field = 1.5 * ureg.tesla
|
|
200
|
+
assert isinstance(field, MagneticField)
|
|
201
|
+
|
|
202
|
+
**Flexible Validation**
|
|
203
|
+
|
|
204
|
+
.. code-block:: python
|
|
205
|
+
|
|
206
|
+
from TypedUnits import validate_enhanced
|
|
207
|
+
|
|
208
|
+
@validate_enhanced(strict_mode=False, convert_types=True)
|
|
209
|
+
def flexible_function(energy: Energy, count: int) -> str:
|
|
210
|
+
"""Function with flexible validation options."""
|
|
211
|
+
return f"Energy per item: {energy / count}"
|
|
212
|
+
|
|
213
|
+
----
|
|
214
|
+
|
|
215
|
+
**Requirements**
|
|
216
|
+
|
|
217
|
+
- Python ≥ 3.8
|
|
218
|
+
- Pint ≥ 0.20
|
|
219
|
+
- Pydantic ≥ 2.0 (optional, for dataclass integration)
|
|
220
|
+
|
|
221
|
+
----
|
|
222
|
+
|
|
223
|
+
**Testing**
|
|
224
|
+
|
|
225
|
+
.. code-block:: bash
|
|
226
|
+
|
|
227
|
+
git clone https://github.com/MartinPdeS/TypedUnits.git
|
|
228
|
+
cd TypedUnits
|
|
229
|
+
pip install -e ".[testing]"
|
|
230
|
+
pytest
|
|
231
|
+
|
|
232
|
+
----
|
|
233
|
+
|
|
234
|
+
**Contributing**
|
|
235
|
+
|
|
236
|
+
TypedUnits is open source and contributions are welcome! Whether you're fixing bugs, adding new unit types, or improving documentation, your help is appreciated.
|
|
237
|
+
|
|
238
|
+
**Author:** `Martin Poinsinet de Sivry-Houle <https://github.com/MartinPdeS>`_
|
|
239
|
+
|
|
240
|
+
**Email:** `martin.poinsinet-de-sivry@polymtl.ca <mailto:martin.poinsinet-de-sivry@polymtl.ca?subject=TypedUnits>`_
|
|
241
|
+
|
|
242
|
+
----
|
|
243
|
+
|
|
244
|
+
.. |python| image:: https://img.shields.io/pypi/pyversions/typedunits.svg
|
|
245
|
+
:target: https://www.python.org/
|
|
246
|
+
:alt: Python version
|
|
247
|
+
|
|
248
|
+
.. |PyPi| image:: https://badge.fury.io/py/TypedUnits.svg
|
|
249
|
+
:target: https://pypi.org/project/TypedUnits/
|
|
250
|
+
:alt: PyPi
|
|
251
|
+
|
|
252
|
+
.. |PyPi_download| image:: https://img.shields.io/pypi/dm/typedunits.svg
|
|
253
|
+
:target: https://pypistats.org/packages/typedunits
|
|
254
|
+
:alt: PyPi download statistics
|
|
255
|
+
|
|
256
|
+
.. |docs| image:: https://github.com/martinpdes/typedunits/actions/workflows/deploy_documentation.yml/badge.svg
|
|
257
|
+
:target: https://martinpdes.github.io/TypedUnits/
|
|
258
|
+
:alt: Documentation Status
|
|
259
|
+
|
|
260
|
+
.. |coverage| image:: https://raw.githubusercontent.com/MartinPdeS/TypedUnits/python-coverage-comment-action-data/badge.svg
|
|
261
|
+
:target: https://htmlpreview.github.io/?https://github.com/MartinPdeS/TypedUnits/blob/python-coverage-comment-action-data/htmlcov/index.html
|
|
262
|
+
:alt: Unittest coverage
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
TypedUnits
|
|
2
|
+
===========
|
|
3
|
+
|
|
4
|
+
|python| |docs| |coverage| |PyPi| |PyPi_download|
|
|
5
|
+
|
|
6
|
+
**Type-safe physical units for Python**
|
|
7
|
+
|
|
8
|
+
TypedUnits is a Python library that extends Pint with type safety and validation capabilities. It allows you to create unit-aware types that work seamlessly with Python's type system and provide runtime validation for physical quantities.
|
|
9
|
+
|
|
10
|
+
**Key Features:**
|
|
11
|
+
|
|
12
|
+
- **Type Safety**: Use `isinstance()` checks with unit types like `Energy`, `Time`, `Power`
|
|
13
|
+
- **Automatic Validation**: Function decorators for unit validation based on type annotations
|
|
14
|
+
- **Pydantic Integration**: Full compatibility with Pydantic dataclasses and models
|
|
15
|
+
- **Normal Pint Usage**: Keep your existing Pint code - just add type safety on top
|
|
16
|
+
|
|
17
|
+
----
|
|
18
|
+
|
|
19
|
+
**Quick Example**
|
|
20
|
+
|
|
21
|
+
.. code-block:: python
|
|
22
|
+
|
|
23
|
+
from TypedUnits import Energy, Time, Power, validate, ureg
|
|
24
|
+
|
|
25
|
+
# Normal Pint instantiation
|
|
26
|
+
energy = 100 * ureg.joule
|
|
27
|
+
time = 10 * ureg.second
|
|
28
|
+
|
|
29
|
+
# But now isinstance works!
|
|
30
|
+
assert isinstance(energy, Energy) # True
|
|
31
|
+
assert isinstance(time, Time) # True
|
|
32
|
+
|
|
33
|
+
@validate_units
|
|
34
|
+
def calculate_power(energy: Energy, time: Time) -> Power:
|
|
35
|
+
"""Function with automatic unit validation."""
|
|
36
|
+
return (energy / time).to(ureg.watt)
|
|
37
|
+
|
|
38
|
+
# This works
|
|
39
|
+
power = calculate_power(energy, time)
|
|
40
|
+
print(f"Power: {power}")
|
|
41
|
+
|
|
42
|
+
# This fails with clear error message
|
|
43
|
+
length = 5 * ureg.meter
|
|
44
|
+
# calculate_power(length, time) # TypeError: Expected Energy, got Length
|
|
45
|
+
|
|
46
|
+
----
|
|
47
|
+
|
|
48
|
+
**Installation**
|
|
49
|
+
|
|
50
|
+
.. code-block:: bash
|
|
51
|
+
|
|
52
|
+
pip install TypedUnits
|
|
53
|
+
|
|
54
|
+
----
|
|
55
|
+
|
|
56
|
+
**Basic Usage**
|
|
57
|
+
|
|
58
|
+
**Unit Type Checking**
|
|
59
|
+
|
|
60
|
+
.. code-block:: python
|
|
61
|
+
|
|
62
|
+
from TypedUnits import Energy, Length, Mass, ureg
|
|
63
|
+
|
|
64
|
+
# Create quantities the normal way
|
|
65
|
+
kinetic_energy = 0.5 * 10 * ureg.kilogram * (5 * ureg.meter / ureg.second)**2
|
|
66
|
+
potential_energy = 10 * ureg.kilogram * 9.81 * ureg.meter / ureg.second**2 * 2 * ureg.meter
|
|
67
|
+
distance = 100 * ureg.meter
|
|
68
|
+
mass = 5 * ureg.kilogram
|
|
69
|
+
|
|
70
|
+
# Type checking works
|
|
71
|
+
assert isinstance(kinetic_energy, Energy)
|
|
72
|
+
assert isinstance(potential_energy, Energy)
|
|
73
|
+
assert isinstance(distance, Length)
|
|
74
|
+
assert isinstance(mass, Mass)
|
|
75
|
+
|
|
76
|
+
**Function Validation**
|
|
77
|
+
|
|
78
|
+
.. code-block:: python
|
|
79
|
+
|
|
80
|
+
from TypedUnits import validate_units, Energy, Time, Power
|
|
81
|
+
|
|
82
|
+
@validate_units
|
|
83
|
+
def calculate_work(power: Power, time: Time) -> Energy:
|
|
84
|
+
"""Calculate work from power and time."""
|
|
85
|
+
return (power * time).to(ureg.joule)
|
|
86
|
+
|
|
87
|
+
power = 1500 * ureg.watt
|
|
88
|
+
time = 3600 * ureg.second # 1 hour
|
|
89
|
+
work = calculate_work(power, time)
|
|
90
|
+
print(f"Work done: {work.to(ureg.kilowatt_hour)}")
|
|
91
|
+
|
|
92
|
+
**Pydantic Integration**
|
|
93
|
+
|
|
94
|
+
.. code-block:: python
|
|
95
|
+
|
|
96
|
+
from TypedUnits import Energy, Length, Mass
|
|
97
|
+
from pydantic.dataclasses import dataclass
|
|
98
|
+
|
|
99
|
+
@dataclass
|
|
100
|
+
class Projectile:
|
|
101
|
+
kinetic_energy: Energy
|
|
102
|
+
height: Length
|
|
103
|
+
mass: Mass
|
|
104
|
+
|
|
105
|
+
# Automatic validation on creation
|
|
106
|
+
projectile = Projectile(
|
|
107
|
+
kinetic_energy=500 * ureg.joule,
|
|
108
|
+
height=10 * ureg.meter,
|
|
109
|
+
mass=2 * ureg.kilogram
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
----
|
|
113
|
+
|
|
114
|
+
**Available Unit Types**
|
|
115
|
+
|
|
116
|
+
TypedUnits includes common physical unit types:
|
|
117
|
+
|
|
118
|
+
- **Mechanical**: `Energy`, `Power`, `Force`, `Pressure`
|
|
119
|
+
- **Spatial**: `Length`, `Area`, `Volume`, `Angle`
|
|
120
|
+
- **Temporal**: `Time`, `Frequency`
|
|
121
|
+
- **Thermal**: `Temperature`, `ThermalConductivity`
|
|
122
|
+
- **Electrical**: `Current`, `Voltage`, `Resistance`, `Capacitance`
|
|
123
|
+
- **Optical**: `RefractiveIndex`, `Wavelength`, `ElectricField`
|
|
124
|
+
- **Mass**: `Mass`, `Density`, `MolarMass`
|
|
125
|
+
|
|
126
|
+
----
|
|
127
|
+
|
|
128
|
+
**Advanced Features**
|
|
129
|
+
|
|
130
|
+
**Custom Unit Types**
|
|
131
|
+
|
|
132
|
+
.. code-block:: python
|
|
133
|
+
|
|
134
|
+
from TypedUnits import create_unit_type
|
|
135
|
+
|
|
136
|
+
# Create your own unit types
|
|
137
|
+
MagneticField = create_unit_type('MagneticField', '[magnetic_flux_density]')
|
|
138
|
+
|
|
139
|
+
field = 1.5 * ureg.tesla
|
|
140
|
+
assert isinstance(field, MagneticField)
|
|
141
|
+
|
|
142
|
+
**Flexible Validation**
|
|
143
|
+
|
|
144
|
+
.. code-block:: python
|
|
145
|
+
|
|
146
|
+
from TypedUnits import validate_enhanced
|
|
147
|
+
|
|
148
|
+
@validate_enhanced(strict_mode=False, convert_types=True)
|
|
149
|
+
def flexible_function(energy: Energy, count: int) -> str:
|
|
150
|
+
"""Function with flexible validation options."""
|
|
151
|
+
return f"Energy per item: {energy / count}"
|
|
152
|
+
|
|
153
|
+
----
|
|
154
|
+
|
|
155
|
+
**Requirements**
|
|
156
|
+
|
|
157
|
+
- Python ≥ 3.8
|
|
158
|
+
- Pint ≥ 0.20
|
|
159
|
+
- Pydantic ≥ 2.0 (optional, for dataclass integration)
|
|
160
|
+
|
|
161
|
+
----
|
|
162
|
+
|
|
163
|
+
**Testing**
|
|
164
|
+
|
|
165
|
+
.. code-block:: bash
|
|
166
|
+
|
|
167
|
+
git clone https://github.com/MartinPdeS/TypedUnits.git
|
|
168
|
+
cd TypedUnits
|
|
169
|
+
pip install -e ".[testing]"
|
|
170
|
+
pytest
|
|
171
|
+
|
|
172
|
+
----
|
|
173
|
+
|
|
174
|
+
**Contributing**
|
|
175
|
+
|
|
176
|
+
TypedUnits is open source and contributions are welcome! Whether you're fixing bugs, adding new unit types, or improving documentation, your help is appreciated.
|
|
177
|
+
|
|
178
|
+
**Author:** `Martin Poinsinet de Sivry-Houle <https://github.com/MartinPdeS>`_
|
|
179
|
+
|
|
180
|
+
**Email:** `martin.poinsinet-de-sivry@polymtl.ca <mailto:martin.poinsinet-de-sivry@polymtl.ca?subject=TypedUnits>`_
|
|
181
|
+
|
|
182
|
+
----
|
|
183
|
+
|
|
184
|
+
.. |python| image:: https://img.shields.io/pypi/pyversions/typedunits.svg
|
|
185
|
+
:target: https://www.python.org/
|
|
186
|
+
:alt: Python version
|
|
187
|
+
|
|
188
|
+
.. |PyPi| image:: https://badge.fury.io/py/TypedUnits.svg
|
|
189
|
+
:target: https://pypi.org/project/TypedUnits/
|
|
190
|
+
:alt: PyPi
|
|
191
|
+
|
|
192
|
+
.. |PyPi_download| image:: https://img.shields.io/pypi/dm/typedunits.svg
|
|
193
|
+
:target: https://pypistats.org/packages/typedunits
|
|
194
|
+
:alt: PyPi download statistics
|
|
195
|
+
|
|
196
|
+
.. |docs| image:: https://github.com/martinpdes/typedunits/actions/workflows/deploy_documentation.yml/badge.svg
|
|
197
|
+
:target: https://martinpdes.github.io/TypedUnits/
|
|
198
|
+
:alt: Documentation Status
|
|
199
|
+
|
|
200
|
+
.. |coverage| image:: https://raw.githubusercontent.com/MartinPdeS/TypedUnits/python-coverage-comment-action-data/badge.svg
|
|
201
|
+
:target: https://htmlpreview.github.io/?https://github.com/MartinPdeS/TypedUnits/blob/python-coverage-comment-action-data/htmlcov/index.html
|
|
202
|
+
:alt: Unittest coverage
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# file generated by setuptools-scm
|
|
2
|
+
# don't change, don't track in version control
|
|
3
|
+
|
|
4
|
+
__all__ = [
|
|
5
|
+
"__version__",
|
|
6
|
+
"__version_tuple__",
|
|
7
|
+
"version",
|
|
8
|
+
"version_tuple",
|
|
9
|
+
"__commit_id__",
|
|
10
|
+
"commit_id",
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
TYPE_CHECKING = False
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from typing import Tuple
|
|
16
|
+
from typing import Union
|
|
17
|
+
|
|
18
|
+
VERSION_TUPLE = Tuple[Union[int, str], ...]
|
|
19
|
+
COMMIT_ID = Union[str, None]
|
|
20
|
+
else:
|
|
21
|
+
VERSION_TUPLE = object
|
|
22
|
+
COMMIT_ID = object
|
|
23
|
+
|
|
24
|
+
version: str
|
|
25
|
+
__version__: str
|
|
26
|
+
__version_tuple__: VERSION_TUPLE
|
|
27
|
+
version_tuple: VERSION_TUPLE
|
|
28
|
+
commit_id: COMMIT_ID
|
|
29
|
+
__commit_id__: COMMIT_ID
|
|
30
|
+
|
|
31
|
+
__version__ = version = '0.0.3'
|
|
32
|
+
__version_tuple__ = version_tuple = (0, 0, 3)
|
|
33
|
+
|
|
34
|
+
__commit_id__ = commit_id = 'g60e49e059'
|