measfmt 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.
@@ -0,0 +1,2 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
@@ -0,0 +1,75 @@
1
+ name: Publish to PyPI
2
+
3
+ on:
4
+ release:
5
+ types: [published]
6
+
7
+ permissions:
8
+ contents: read
9
+
10
+ jobs:
11
+ test:
12
+ name: Test Python ${{ matrix.python-version }}
13
+ runs-on: ${{ matrix.os }}
14
+
15
+ strategy:
16
+ fail-fast: false
17
+ matrix:
18
+ include:
19
+ - python-version: "3.9"
20
+ os: ubuntu-24.04
21
+ - python-version: "3.10"
22
+ os: ubuntu-latest
23
+ - python-version: "3.11"
24
+ os: ubuntu-latest
25
+ - python-version: "3.12"
26
+ os: ubuntu-latest
27
+ - python-version: "3.13"
28
+ os: ubuntu-latest
29
+ - python-version: "3.14"
30
+ os: ubuntu-latest
31
+
32
+ steps:
33
+ - uses: actions/checkout@v7
34
+
35
+ - uses: actions/setup-python@v7
36
+ with:
37
+ python-version: ${{ matrix.python-version }}
38
+
39
+ - name: Install package and test dependencies
40
+ run: python -m pip install -e ".[test]"
41
+
42
+ - name: Run tests
43
+ run: python -m pytest
44
+
45
+ publish:
46
+ name: Build and publish
47
+ needs: test
48
+ runs-on: ubuntu-latest
49
+
50
+ environment:
51
+ name: pypi
52
+ url: https://pypi.org/project/measfmt/
53
+
54
+ permissions:
55
+ contents: read
56
+ id-token: write
57
+
58
+ steps:
59
+ - uses: actions/checkout@v7
60
+
61
+ - uses: actions/setup-python@v7
62
+ with:
63
+ python-version: "3.14"
64
+
65
+ - name: Install build tools
66
+ run: python -m pip install build twine
67
+
68
+ - name: Build distributions
69
+ run: python -m build
70
+
71
+ - name: Check distributions
72
+ run: python -m twine check dist/*
73
+
74
+ - name: Publish to PyPI
75
+ uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,58 @@
1
+
2
+ name: Tests
3
+
4
+ on:
5
+ push:
6
+ branches: [main]
7
+ pull_request:
8
+ branches: [main]
9
+
10
+ permissions:
11
+ contents: read
12
+
13
+ jobs:
14
+ test:
15
+ name: Python ${{ matrix.python-version }}
16
+
17
+ strategy:
18
+ fail-fast: false
19
+ matrix:
20
+ include:
21
+ - python-version: "3.9"
22
+ os: ubuntu-24.04
23
+ - python-version: "3.10"
24
+ os: ubuntu-latest
25
+ - python-version: "3.11"
26
+ os: ubuntu-latest
27
+ - python-version: "3.12"
28
+ os: ubuntu-latest
29
+ - python-version: "3.13"
30
+ os: ubuntu-latest
31
+ - python-version: "3.14"
32
+ os: ubuntu-latest
33
+
34
+ runs-on: ${{ matrix.os }}
35
+
36
+ steps:
37
+ - name: Check out repository
38
+ uses: actions/checkout@v7
39
+
40
+ - name: Set up Python
41
+ uses: actions/setup-python@v7
42
+ with:
43
+ python-version: ${{ matrix.python-version }}
44
+
45
+ - name: Install dependencies
46
+ run: |
47
+ python -m pip install --upgrade pip
48
+ python -m pip install -e ".[test]"
49
+ python -m pip install build twine
50
+
51
+ - name: Run tests
52
+ run: python -m pytest
53
+
54
+ - name: Build package
55
+ run: python -m build
56
+
57
+ - name: Check distributions
58
+ run: python -m twine check dist/*
@@ -0,0 +1,161 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # poetry
98
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102
+ #poetry.lock
103
+
104
+ # pdm
105
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106
+ #pdm.lock
107
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108
+ # in version control.
109
+ # https://pdm.fming.dev/#use-with-ide
110
+ .pdm.toml
111
+
112
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113
+ __pypackages__/
114
+
115
+ # Celery stuff
116
+ celerybeat-schedule
117
+ celerybeat.pid
118
+
119
+ # SageMath parsed files
120
+ *.sage.py
121
+
122
+ # Environments
123
+ .env
124
+ .venv
125
+ env/
126
+ venv/
127
+ ENV/
128
+ env.bak/
129
+ venv.bak/
130
+
131
+ # Spyder project settings
132
+ .spyderproject
133
+ .spyproject
134
+
135
+ # Rope project settings
136
+ .ropeproject
137
+
138
+ # mkdocs documentation
139
+ /site
140
+
141
+ # mypy
142
+ .mypy_cache/
143
+ .dmypy.json
144
+ dmypy.json
145
+
146
+ # Pyre type checker
147
+ .pyre/
148
+
149
+ # pytype static type analyzer
150
+ .pytype/
151
+
152
+ # Cython debug symbols
153
+ cython_debug/
154
+
155
+ # PyCharm
156
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
159
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
160
+ .idea/
161
+
measfmt-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Darwin Rescorla
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.
measfmt-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,210 @@
1
+ Metadata-Version: 2.5
2
+ Name: measfmt
3
+ Version: 0.1.0
4
+ Summary: Format real and complex measurements with uncertainties, SI prefixes, and correct rounding.
5
+ Author: Darwin Rescorla
6
+ License-Expression: MIT
7
+ License-File: LICENSE
8
+ Keywords: SI prefix,formatting,measurement,physics,significant figures,uncertainty
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: Intended Audience :: Education
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3 :: Only
15
+ Classifier: Topic :: Scientific/Engineering
16
+ Classifier: Topic :: Text Processing :: General
17
+ Requires-Python: >=3.9
18
+ Provides-Extra: test
19
+ Requires-Dist: pytest; extra == 'test'
20
+ Description-Content-Type: text/markdown
21
+
22
+ # measfmt
23
+
24
+ Format scientific measurements the way a lab report would: value and uncertainty rounded together, scaled with SI prefixes, with no dependencies.
25
+
26
+ ```pycon
27
+ >>> from measfmt import fm
28
+ >>> fm("g", 9.81, 0.02, "m/s²")
29
+ 'g = (9.81 ± 0.02) m/s²'
30
+ >>> fm("Length", 0.0012345, 0.00002, "m")
31
+ 'Length = (1.23 ± 0.02) mm'
32
+ >>> fm("Pi", 3.14159265358979)
33
+ 'Pi = 3.14159265358979'
34
+ ```
35
+
36
+ ## Installation
37
+
38
+ ```sh
39
+ pip install measfmt
40
+ ```
41
+
42
+ Requires Python 3.9 or newer. There are no runtime dependencies.
43
+
44
+ ## Usage
45
+
46
+ `measfmt` exports one function under two names. They are the same object, so use whichever you prefer:
47
+
48
+ ```pycon
49
+ >>> from measfmt import fm
50
+ >>> from measfmt import format_measurement
51
+ >>> fm is format_measurement
52
+ True
53
+ ```
54
+
55
+ ```python
56
+ fm(name, value, uncertainty=None, units="", *, scientific=False) -> str
57
+ ```
58
+
59
+ | Argument | Description |
60
+ | ------------- | ---------------------------------------------------------------------------------------- |
61
+ | `name` | Label shown on the left of the `=`. |
62
+ | `value` | The measurement: `int`, `float`, `Decimal`, or `complex`. |
63
+ | `uncertainty` | Optional. Same kinds of number as `value`. Omit it or pass `None` to show the value only. |
64
+ | `units` | Optional unit string appended to the result. |
65
+ | `scientific` | Keyword-only. If `True`, always use `× 10^n` instead of an SI prefix. |
66
+
67
+ ### Rounding
68
+
69
+ The uncertainty is rounded to **one significant figure**, and the value is rounded to the same decimal place. Ties round half to even, and the arithmetic uses `Decimal`, so results are not affected by binary floating-point artifacts.
70
+
71
+ ```pycon
72
+ >>> fm("x", 1.234, 0.096, "m")
73
+ 'x = (1.2 ± 0.1) m'
74
+ >>> fm("x", 1.234, 0.0123, "m")
75
+ 'x = (1.23 ± 0.01) m'
76
+ >>> fm("x", 9.99, 0.1, "m") # rounding carries into the next digit
77
+ 'x = (10.0 ± 0.1) m'
78
+ ```
79
+
80
+ ### SI prefixes
81
+
82
+ For recognized SI units, the value and uncertainty are scaled together by the prefix that keeps the number between 1 and 1000, from quecto (`q`, 10⁻³⁰) to quetta (`Q`, 10³⁰):
83
+
84
+ ```pycon
85
+ >>> fm("x", 1234, 100, "m")
86
+ 'x = (1.2 ± 0.1) km'
87
+ >>> fm("x", 0.0000015, 0.0000001, "s")
88
+ 'x = (1.5 ± 0.1) µs'
89
+ >>> fm("x", 999, 1, "m") # just below a boundary
90
+ 'x = (999 ± 1) m'
91
+ >>> fm("x", 1000, 100, "m") # exactly on a boundary
92
+ 'x = (1.0 ± 0.1) km'
93
+ ```
94
+
95
+ Prefixes are applied only to unprefixed SI base and derived units: `m g s A K mol cd rad sr Hz N Pa J W C V F Ω S Wb T H lm lx Bq Gy Sv kat`. Anything else (`kg`, `km`, `m/s`, `widgets`, or no unit at all) is scaled with a power of ten instead, because `measfmt` will not guess how to re-prefix a unit that already has a prefix or is compound:
96
+
97
+ ```pycon
98
+ >>> fm("m", 1234, 100, "kg")
99
+ 'm = (1.2 ± 0.1) × 10^3 kg'
100
+ >>> fm("x", 12, 1)
101
+ 'x = (12 ± 1)'
102
+ ```
103
+
104
+ Magnitudes outside the prefix range also fall back to powers of ten:
105
+
106
+ ```pycon
107
+ >>> fm("x", 1e-33, 1e-34, "m")
108
+ 'x = (1.0 ± 0.1) × 10^-33 m'
109
+ ```
110
+
111
+ Pass `scientific=True` to force powers of ten even for prefixable units:
112
+
113
+ ```pycon
114
+ >>> fm("x", 1234, 20, "m", scientific=True)
115
+ 'x = (1.23 ± 0.02) × 10^3 m'
116
+ ```
117
+
118
+ Values between 1 and 1000 (that is, those that need no scaling) are never given a `× 10^0` unless `scientific=True`.
119
+
120
+ ### Optional uncertainty
121
+
122
+ If `uncertainty` is omitted or `None`, there is no `±` and no parentheses. The value is not rounded; it is shown as given, still scaled and with units:
123
+
124
+ ```pycon
125
+ >>> fm("Pi", 3.14159265358979, None) == fm("Pi", 3.14159265358979)
126
+ True
127
+ >>> fm("Pi", 3.14159265358979)
128
+ 'Pi = 3.14159265358979'
129
+ >>> fm("x", 1234.5, units="m")
130
+ 'x = 1.2345 km'
131
+ ```
132
+
133
+ Trailing zeros that come from `float` or `int` formatting (`1200.0`) are dropped. `Decimal` inputs keep their digits exactly, so `Decimal("1.500")` is shown as `1.500`.
134
+
135
+ ### Zero and negative uncertainty
136
+
137
+ A **zero** uncertainty is valid and means the value is exact. It is printed as `± 0`, and the value is not rounded:
138
+
139
+ ```pycon
140
+ >>> fm("c", 299792458, 0, "m/s")
141
+ 'c = (2.99792458 ± 0) × 10^8 m/s'
142
+ >>> fm("x", 1.5, 0, "m")
143
+ 'x = (1.5 ± 0) m'
144
+ ```
145
+
146
+ A **negative** uncertainty is treated as its magnitude:
147
+
148
+ ```pycon
149
+ >>> fm("x", 1.234, -0.096, "m")
150
+ 'x = (1.2 ± 0.1) m'
151
+ ```
152
+
153
+ ### Complex numbers
154
+
155
+ Complex values are written as `(re ± u) + (im ± u)j`. The real and imaginary parts are each rounded to their own uncertainty but share a single scale, chosen from the larger part. The uncertainty can be complex, with its real and imaginary parts applying to the matching parts of the value. A real uncertainty applies to the real part only:
156
+
157
+ ```pycon
158
+ >>> fm("z", complex(1.2, -3500), complex(0.1, 0.2), "m")
159
+ 'z = ((0.0012 ± 0.0001) - (3.5000 ± 0.0002)j) km'
160
+ >>> fm("z", 1 + 2j, 0.1, "m")
161
+ 'z = ((1.0 ± 0.1) + (2.0 ± 0)j) m'
162
+ ```
163
+
164
+ Without an uncertainty, a complex value is parenthesized only when a scale or unit follows it:
165
+
166
+ ```pycon
167
+ >>> fm("z", 1 + 2j)
168
+ 'z = 1 + 2j'
169
+ >>> fm("z", 1 + 2j, None, "m")
170
+ 'z = (1 + 2j) m'
171
+ ```
172
+
173
+ A real value requires a real uncertainty; a complex uncertainty with a real value raises `TypeError`.
174
+
175
+ ### Very large and very small numbers
176
+
177
+ Internally everything is converted to `Decimal` with enough precision for the inputs, so extreme magnitudes and extreme value-to-uncertainty ratios are handled exactly:
178
+
179
+ ```pycon
180
+ >>> fm("x", 1.5e-300, 1e-301, "m")
181
+ 'x = (1.5 ± 0.1) × 10^-300 m'
182
+ >>> fm("x", 10**1000, 10**999)
183
+ 'x = (1.0 ± 0.1) × 10^1000'
184
+ ```
185
+
186
+ ### Non-finite values
187
+
188
+ `inf`, `-inf` and `nan` are passed through as `Infinity`, `-Infinity` and `NaN`:
189
+
190
+ ```pycon
191
+ >>> fm("x", float("inf"), 0.1, "m")
192
+ 'x = (Infinity ± 0.1) m'
193
+ >>> fm("x", 1, float("nan"), "m")
194
+ 'x = (1 ± NaN) m'
195
+ ```
196
+
197
+ ### Errors
198
+
199
+ `TypeError` is raised if `value` or `uncertainty` is not a real or complex number, if `units` is not a string, or if `scientific` is not a `bool`.
200
+
201
+ ## Development
202
+
203
+ ```sh
204
+ pip install -e ".[test]"
205
+ pytest
206
+ ```
207
+
208
+ ## License
209
+
210
+ [MIT](LICENSE)