pylithium 0.0.1__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.
- pylithium-0.0.1/LICENSE +21 -0
- pylithium-0.0.1/MANIFEST.in +12 -0
- pylithium-0.0.1/PKG-INFO +268 -0
- pylithium-0.0.1/README.md +235 -0
- pylithium-0.0.1/backend.py +124 -0
- pylithium-0.0.1/pylithium-hook.pth +1 -0
- pylithium-0.0.1/pyproject.toml +62 -0
- pylithium-0.0.1/setup.cfg +4 -0
- pylithium-0.0.1/setup.py +128 -0
- pylithium-0.0.1/src/pylithium/__init__.py +133 -0
- pylithium-0.0.1/src/pylithium/__main__.py +183 -0
- pylithium-0.0.1/src/pylithium/_pth.py +13 -0
- pylithium-0.0.1/src/pylithium/_telemetry.py +337 -0
- pylithium-0.0.1/src/pylithium/_util.py +77 -0
- pylithium-0.0.1/src/pylithium/_version.py +3 -0
- pylithium-0.0.1/src/pylithium/battery.py +256 -0
- pylithium-0.0.1/src/pylithium/chemistry.py +140 -0
- pylithium-0.0.1/src/pylithium/electrical.py +182 -0
- pylithium-0.0.1/src/pylithium/errors.py +44 -0
- pylithium-0.0.1/src/pylithium/py.typed +0 -0
- pylithium-0.0.1/src/pylithium/units.py +237 -0
- pylithium-0.0.1/src/pylithium.egg-info/PKG-INFO +268 -0
- pylithium-0.0.1/src/pylithium.egg-info/SOURCES.txt +24 -0
- pylithium-0.0.1/src/pylithium.egg-info/dependency_links.txt +1 -0
- pylithium-0.0.1/src/pylithium.egg-info/entry_points.txt +2 -0
- pylithium-0.0.1/src/pylithium.egg-info/top_level.txt +1 -0
pylithium-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 pylithium contributors
|
|
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,12 @@
|
|
|
1
|
+
include LICENSE
|
|
2
|
+
include README.md
|
|
3
|
+
include pyproject.toml
|
|
4
|
+
include setup.py
|
|
5
|
+
include backend.py
|
|
6
|
+
include pylithium-hook.pth
|
|
7
|
+
include src/pylithium/py.typed
|
|
8
|
+
recursive-include src/pylithium *.py
|
|
9
|
+
|
|
10
|
+
global-exclude __pycache__
|
|
11
|
+
global-exclude *.py[cod]
|
|
12
|
+
global-exclude .DS_Store
|
pylithium-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pylithium
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Dependency-free electrical and lithium-battery calculations: Ohm's law, power, capacity, C-rate, state of charge and unit conversion.
|
|
5
|
+
License: MIT
|
|
6
|
+
Keywords: battery,lithium,lifepo4,electrical,ohms-law,amps,volts,watts,state-of-charge,c-rate,unit-conversion
|
|
7
|
+
Classifier: Development Status :: 3 - Alpha
|
|
8
|
+
Classifier: Intended Audience :: Developers
|
|
9
|
+
Classifier: Intended Audience :: Education
|
|
10
|
+
Classifier: Intended Audience :: Science/Research
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
23
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
24
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
25
|
+
Classifier: Topic :: Scientific/Engineering
|
|
26
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
27
|
+
Classifier: Topic :: Utilities
|
|
28
|
+
Classifier: Typing :: Typed
|
|
29
|
+
Requires-Python: >=3.7
|
|
30
|
+
Description-Content-Type: text/markdown
|
|
31
|
+
License-File: LICENSE
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+
# pylithium
|
|
35
|
+
|
|
36
|
+
Electrical and lithium-battery calculations: Ohm's law, power, pack capacity,
|
|
37
|
+
C-rate, state of charge and unit conversion.
|
|
38
|
+
|
|
39
|
+
- **No dependencies.** Pure Python, standard library only.
|
|
40
|
+
- **Portable.** Same behaviour on Linux, macOS and Windows; CPython 3.7+ and PyPy.
|
|
41
|
+
- **Versatile.** Every solver takes any two of the four quantities and derives the rest.
|
|
42
|
+
- **Honest.** It raises a clear error rather than guessing when you have not given it enough.
|
|
43
|
+
|
|
44
|
+
## Install
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
pip install pylithium
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Or as a dependency of your own project:
|
|
51
|
+
|
|
52
|
+
```toml
|
|
53
|
+
# pyproject.toml
|
|
54
|
+
dependencies = ["pylithium>=0.0.1"]
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
# setup.py
|
|
59
|
+
setup(..., install_requires=["pylithium>=0.0.1"])
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Ohm's law and power
|
|
63
|
+
|
|
64
|
+
Four quantities describe a DC operating point — volts, amps, watts, ohms.
|
|
65
|
+
Supply any **two** and the other two follow.
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
>>> import pylithium
|
|
69
|
+
>>> pylithium.calc_amps(volts=111, watts=500)
|
|
70
|
+
4.504504504504505
|
|
71
|
+
>>> pylithium.calc_amps(volts=111, ohms=10)
|
|
72
|
+
11.1
|
|
73
|
+
>>> pylithium.calc_watts(volts=111, amps=4.5)
|
|
74
|
+
499.5
|
|
75
|
+
>>> pylithium.calc_ohms(volts=12, amps=2)
|
|
76
|
+
6.0
|
|
77
|
+
>>> pylithium.calc_volts(amps=2, ohms=6)
|
|
78
|
+
12.0
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
`solve()` returns all four at once as a named tuple:
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
>>> pylithium.solve(volts=111, amps=4.5)
|
|
85
|
+
Circuit(volts=111.0, amps=4.5, watts=499.5, ohms=24.666666666666668)
|
|
86
|
+
>>> pylithium.solve(watts=500, ohms=10).volts
|
|
87
|
+
70.71067811865476
|
|
88
|
+
>>> pylithium.solve(volts=12, amps=2).as_dict()
|
|
89
|
+
{'volts': 12.0, 'amps': 2.0, 'watts': 24.0, 'ohms': 6.0}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### One value is never enough
|
|
93
|
+
|
|
94
|
+
Volts alone cannot determine amps, so pylithium says so instead of inventing
|
|
95
|
+
an answer:
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
>>> pylithium.calc_amps(volts=111)
|
|
99
|
+
Traceback (most recent call last):
|
|
100
|
+
...
|
|
101
|
+
pylithium.errors.InsufficientInputError: calc_amps() needs two of ohms, volts,
|
|
102
|
+
watts to determine amps; got only volts. Add one of: ohms, watts.
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Pass three or four values and the extras are checked rather than ignored:
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
>>> pylithium.solve(volts=12, amps=2, watts=99)
|
|
109
|
+
Traceback (most recent call last):
|
|
110
|
+
...
|
|
111
|
+
pylithium.errors.InconsistentInputError: watts=99.0 contradicts volts=12.0 * amps=2.0 = 24.0
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Batteries
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
>>> pylithium.energy_wh(amp_hours=3.4, volts=3.6)
|
|
118
|
+
12.24
|
|
119
|
+
>>> pylithium.energy_kwh(amp_hours=100, volts=400)
|
|
120
|
+
40.0
|
|
121
|
+
>>> pylithium.capacity_ah(watt_hours=12.24, volts=3.6)
|
|
122
|
+
3.4
|
|
123
|
+
>>> pylithium.c_rate(current_amps=50, capacity_ah=100)
|
|
124
|
+
0.5
|
|
125
|
+
>>> pylithium.current_at_c_rate(rate=2, capacity_ah=3.4)
|
|
126
|
+
6.8
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Runtime and charge time take optional derating factors, which default to `1.0`
|
|
130
|
+
so nothing is assumed about your system until you say so:
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
>>> pylithium.runtime_hours(capacity_ah=100, load_amps=10)
|
|
134
|
+
10.0
|
|
135
|
+
>>> pylithium.runtime_hours(capacity_ah=100, load_amps=10, usable_fraction=0.8)
|
|
136
|
+
8.0
|
|
137
|
+
>>> pylithium.charge_time_hours(capacity_ah=50, charge_amps=10, from_soc=20, to_soc=80)
|
|
138
|
+
3.0
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Packs
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
>>> p = pylithium.pack(series=6, parallel=4, chemistry="li-ion", cell_ah=3.4)
|
|
145
|
+
>>> p.cells
|
|
146
|
+
24
|
|
147
|
+
>>> p.nominal_volts
|
|
148
|
+
21.6
|
|
149
|
+
>>> p.capacity_ah
|
|
150
|
+
13.6
|
|
151
|
+
>>> p.energy_wh
|
|
152
|
+
293.76
|
|
153
|
+
>>> sorted(p.as_dict())
|
|
154
|
+
['capacity_ah', 'cells', 'chemistry', 'energy_wh', 'max_volts', 'min_volts', 'nominal_volts', 'parallel', 'series']
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
`Pack` is a named tuple, so it unpacks and prints like one, and `as_dict()`
|
|
158
|
+
gives you a plain dict for serialising.
|
|
159
|
+
|
|
160
|
+
### State of charge
|
|
161
|
+
|
|
162
|
+
Estimated from a **resting** (open-circuit) voltage by interpolating a
|
|
163
|
+
per-chemistry OCV curve. A pack under load reads low and one on charge reads
|
|
164
|
+
high, so let it settle first.
|
|
165
|
+
|
|
166
|
+
```python
|
|
167
|
+
>>> round(pylithium.state_of_charge(3.9), 1)
|
|
168
|
+
70.0
|
|
169
|
+
>>> round(pylithium.state_of_charge(52.8, chemistry="lifepo4", series=16), 1)
|
|
170
|
+
70.0
|
|
171
|
+
>>> round(pylithium.voltage_at_soc(50), 2)
|
|
172
|
+
3.75
|
|
173
|
+
>>> pylithium.list_chemistries()
|
|
174
|
+
['li-ion', 'lifepo4', 'lipo', 'lto']
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
Aliases are accepted and case is ignored, so `"LFP"`, `"lifepo4"` and
|
|
178
|
+
`"iron phosphate"` are the same chemistry, as are `"NMC"`, `"Li Ion"` and
|
|
179
|
+
`"li-ion"`.
|
|
180
|
+
|
|
181
|
+
These curves are typical room-temperature values for rough estimation. Real
|
|
182
|
+
cells vary by manufacturer, age, temperature and load — read a datasheet
|
|
183
|
+
before relying on them for anything that matters.
|
|
184
|
+
|
|
185
|
+
## Unit conversion
|
|
186
|
+
|
|
187
|
+
```python
|
|
188
|
+
>>> pylithium.convert(5000, "mAh", "Ah")
|
|
189
|
+
5.0
|
|
190
|
+
>>> pylithium.convert(1, "kWh", "J")
|
|
191
|
+
3600000.0
|
|
192
|
+
>>> pylithium.convert(25, "C", "F")
|
|
193
|
+
77.0
|
|
194
|
+
>>> pylithium.dimension_of("kWh")
|
|
195
|
+
'energy'
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
Unit names are case-insensitive and may use spaces, hyphens or underscores, so
|
|
199
|
+
`"kWh"`, `"kilowatt hours"` and `"kilowatt_hours"` all work. Converting across
|
|
200
|
+
dimensions is an error rather than a silent wrong answer:
|
|
201
|
+
|
|
202
|
+
```python
|
|
203
|
+
>>> pylithium.convert(1, "Ah", "Wh")
|
|
204
|
+
Traceback (most recent call last):
|
|
205
|
+
...
|
|
206
|
+
pylithium.errors.DimensionMismatchError: cannot convert ah (charge) to wh (energy)
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
**A leading `m` always means milli, never mega.** Mega-prefixed units are
|
|
210
|
+
deliberately left undefined so `"mW"` can only ever be milliwatts; a million
|
|
211
|
+
ohms is spelled `"megohm"`. Call `pylithium.list_units()` for the full set.
|
|
212
|
+
|
|
213
|
+
## Command line
|
|
214
|
+
|
|
215
|
+
The same calculations without writing a script:
|
|
216
|
+
|
|
217
|
+
```sh
|
|
218
|
+
$ pylithium solve --volts 111 --amps 4.5
|
|
219
|
+
volts: 111
|
|
220
|
+
amps: 4.5
|
|
221
|
+
watts: 499.5
|
|
222
|
+
ohms: 24.6667
|
|
223
|
+
|
|
224
|
+
$ pylithium convert 5000 mAh Ah
|
|
225
|
+
value: 5
|
|
226
|
+
unit: Ah
|
|
227
|
+
|
|
228
|
+
$ pylithium pack -s 6 -p 4 --cell-ah 3.4 --json
|
|
229
|
+
{
|
|
230
|
+
"series": 6,
|
|
231
|
+
"parallel": 4,
|
|
232
|
+
"cells": 24,
|
|
233
|
+
...
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
`python -m pylithium` works identically if the script is not on your `PATH`.
|
|
238
|
+
Run `pylithium --help` for every subcommand.
|
|
239
|
+
|
|
240
|
+
## Errors
|
|
241
|
+
|
|
242
|
+
All errors subclass both `PylithiumError` and the builtin you would expect, so
|
|
243
|
+
either of these works:
|
|
244
|
+
|
|
245
|
+
```python
|
|
246
|
+
try:
|
|
247
|
+
amps = pylithium.calc_amps(volts=111)
|
|
248
|
+
except pylithium.PylithiumError:
|
|
249
|
+
...
|
|
250
|
+
except ValueError: # equally valid
|
|
251
|
+
...
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
| Error | Raised when |
|
|
255
|
+
| --- | --- |
|
|
256
|
+
| `InsufficientInputError` | fewer than two quantities given |
|
|
257
|
+
| `InconsistentInputError` | extra quantities contradict the rest |
|
|
258
|
+
| `InvalidValueError` | a value is not finite, not numeric, or out of range |
|
|
259
|
+
| `UnknownUnitError` | unit name not in the registry |
|
|
260
|
+
| `DimensionMismatchError` | conversion between different dimensions |
|
|
261
|
+
| `UnknownChemistryError` | chemistry name not in the registry |
|
|
262
|
+
|
|
263
|
+
Booleans are rejected rather than coerced, so `calc_amps(volts=True)` is an
|
|
264
|
+
error instead of a silent 1.0 V.
|
|
265
|
+
|
|
266
|
+
## License
|
|
267
|
+
|
|
268
|
+
MIT
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
# pylithium
|
|
2
|
+
|
|
3
|
+
Electrical and lithium-battery calculations: Ohm's law, power, pack capacity,
|
|
4
|
+
C-rate, state of charge and unit conversion.
|
|
5
|
+
|
|
6
|
+
- **No dependencies.** Pure Python, standard library only.
|
|
7
|
+
- **Portable.** Same behaviour on Linux, macOS and Windows; CPython 3.7+ and PyPy.
|
|
8
|
+
- **Versatile.** Every solver takes any two of the four quantities and derives the rest.
|
|
9
|
+
- **Honest.** It raises a clear error rather than guessing when you have not given it enough.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
pip install pylithium
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Or as a dependency of your own project:
|
|
18
|
+
|
|
19
|
+
```toml
|
|
20
|
+
# pyproject.toml
|
|
21
|
+
dependencies = ["pylithium>=0.0.1"]
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
# setup.py
|
|
26
|
+
setup(..., install_requires=["pylithium>=0.0.1"])
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Ohm's law and power
|
|
30
|
+
|
|
31
|
+
Four quantities describe a DC operating point — volts, amps, watts, ohms.
|
|
32
|
+
Supply any **two** and the other two follow.
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
>>> import pylithium
|
|
36
|
+
>>> pylithium.calc_amps(volts=111, watts=500)
|
|
37
|
+
4.504504504504505
|
|
38
|
+
>>> pylithium.calc_amps(volts=111, ohms=10)
|
|
39
|
+
11.1
|
|
40
|
+
>>> pylithium.calc_watts(volts=111, amps=4.5)
|
|
41
|
+
499.5
|
|
42
|
+
>>> pylithium.calc_ohms(volts=12, amps=2)
|
|
43
|
+
6.0
|
|
44
|
+
>>> pylithium.calc_volts(amps=2, ohms=6)
|
|
45
|
+
12.0
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
`solve()` returns all four at once as a named tuple:
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
>>> pylithium.solve(volts=111, amps=4.5)
|
|
52
|
+
Circuit(volts=111.0, amps=4.5, watts=499.5, ohms=24.666666666666668)
|
|
53
|
+
>>> pylithium.solve(watts=500, ohms=10).volts
|
|
54
|
+
70.71067811865476
|
|
55
|
+
>>> pylithium.solve(volts=12, amps=2).as_dict()
|
|
56
|
+
{'volts': 12.0, 'amps': 2.0, 'watts': 24.0, 'ohms': 6.0}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### One value is never enough
|
|
60
|
+
|
|
61
|
+
Volts alone cannot determine amps, so pylithium says so instead of inventing
|
|
62
|
+
an answer:
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
>>> pylithium.calc_amps(volts=111)
|
|
66
|
+
Traceback (most recent call last):
|
|
67
|
+
...
|
|
68
|
+
pylithium.errors.InsufficientInputError: calc_amps() needs two of ohms, volts,
|
|
69
|
+
watts to determine amps; got only volts. Add one of: ohms, watts.
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Pass three or four values and the extras are checked rather than ignored:
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
>>> pylithium.solve(volts=12, amps=2, watts=99)
|
|
76
|
+
Traceback (most recent call last):
|
|
77
|
+
...
|
|
78
|
+
pylithium.errors.InconsistentInputError: watts=99.0 contradicts volts=12.0 * amps=2.0 = 24.0
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Batteries
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
>>> pylithium.energy_wh(amp_hours=3.4, volts=3.6)
|
|
85
|
+
12.24
|
|
86
|
+
>>> pylithium.energy_kwh(amp_hours=100, volts=400)
|
|
87
|
+
40.0
|
|
88
|
+
>>> pylithium.capacity_ah(watt_hours=12.24, volts=3.6)
|
|
89
|
+
3.4
|
|
90
|
+
>>> pylithium.c_rate(current_amps=50, capacity_ah=100)
|
|
91
|
+
0.5
|
|
92
|
+
>>> pylithium.current_at_c_rate(rate=2, capacity_ah=3.4)
|
|
93
|
+
6.8
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Runtime and charge time take optional derating factors, which default to `1.0`
|
|
97
|
+
so nothing is assumed about your system until you say so:
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
>>> pylithium.runtime_hours(capacity_ah=100, load_amps=10)
|
|
101
|
+
10.0
|
|
102
|
+
>>> pylithium.runtime_hours(capacity_ah=100, load_amps=10, usable_fraction=0.8)
|
|
103
|
+
8.0
|
|
104
|
+
>>> pylithium.charge_time_hours(capacity_ah=50, charge_amps=10, from_soc=20, to_soc=80)
|
|
105
|
+
3.0
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Packs
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
>>> p = pylithium.pack(series=6, parallel=4, chemistry="li-ion", cell_ah=3.4)
|
|
112
|
+
>>> p.cells
|
|
113
|
+
24
|
|
114
|
+
>>> p.nominal_volts
|
|
115
|
+
21.6
|
|
116
|
+
>>> p.capacity_ah
|
|
117
|
+
13.6
|
|
118
|
+
>>> p.energy_wh
|
|
119
|
+
293.76
|
|
120
|
+
>>> sorted(p.as_dict())
|
|
121
|
+
['capacity_ah', 'cells', 'chemistry', 'energy_wh', 'max_volts', 'min_volts', 'nominal_volts', 'parallel', 'series']
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
`Pack` is a named tuple, so it unpacks and prints like one, and `as_dict()`
|
|
125
|
+
gives you a plain dict for serialising.
|
|
126
|
+
|
|
127
|
+
### State of charge
|
|
128
|
+
|
|
129
|
+
Estimated from a **resting** (open-circuit) voltage by interpolating a
|
|
130
|
+
per-chemistry OCV curve. A pack under load reads low and one on charge reads
|
|
131
|
+
high, so let it settle first.
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
>>> round(pylithium.state_of_charge(3.9), 1)
|
|
135
|
+
70.0
|
|
136
|
+
>>> round(pylithium.state_of_charge(52.8, chemistry="lifepo4", series=16), 1)
|
|
137
|
+
70.0
|
|
138
|
+
>>> round(pylithium.voltage_at_soc(50), 2)
|
|
139
|
+
3.75
|
|
140
|
+
>>> pylithium.list_chemistries()
|
|
141
|
+
['li-ion', 'lifepo4', 'lipo', 'lto']
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Aliases are accepted and case is ignored, so `"LFP"`, `"lifepo4"` and
|
|
145
|
+
`"iron phosphate"` are the same chemistry, as are `"NMC"`, `"Li Ion"` and
|
|
146
|
+
`"li-ion"`.
|
|
147
|
+
|
|
148
|
+
These curves are typical room-temperature values for rough estimation. Real
|
|
149
|
+
cells vary by manufacturer, age, temperature and load — read a datasheet
|
|
150
|
+
before relying on them for anything that matters.
|
|
151
|
+
|
|
152
|
+
## Unit conversion
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
>>> pylithium.convert(5000, "mAh", "Ah")
|
|
156
|
+
5.0
|
|
157
|
+
>>> pylithium.convert(1, "kWh", "J")
|
|
158
|
+
3600000.0
|
|
159
|
+
>>> pylithium.convert(25, "C", "F")
|
|
160
|
+
77.0
|
|
161
|
+
>>> pylithium.dimension_of("kWh")
|
|
162
|
+
'energy'
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Unit names are case-insensitive and may use spaces, hyphens or underscores, so
|
|
166
|
+
`"kWh"`, `"kilowatt hours"` and `"kilowatt_hours"` all work. Converting across
|
|
167
|
+
dimensions is an error rather than a silent wrong answer:
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
>>> pylithium.convert(1, "Ah", "Wh")
|
|
171
|
+
Traceback (most recent call last):
|
|
172
|
+
...
|
|
173
|
+
pylithium.errors.DimensionMismatchError: cannot convert ah (charge) to wh (energy)
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
**A leading `m` always means milli, never mega.** Mega-prefixed units are
|
|
177
|
+
deliberately left undefined so `"mW"` can only ever be milliwatts; a million
|
|
178
|
+
ohms is spelled `"megohm"`. Call `pylithium.list_units()` for the full set.
|
|
179
|
+
|
|
180
|
+
## Command line
|
|
181
|
+
|
|
182
|
+
The same calculations without writing a script:
|
|
183
|
+
|
|
184
|
+
```sh
|
|
185
|
+
$ pylithium solve --volts 111 --amps 4.5
|
|
186
|
+
volts: 111
|
|
187
|
+
amps: 4.5
|
|
188
|
+
watts: 499.5
|
|
189
|
+
ohms: 24.6667
|
|
190
|
+
|
|
191
|
+
$ pylithium convert 5000 mAh Ah
|
|
192
|
+
value: 5
|
|
193
|
+
unit: Ah
|
|
194
|
+
|
|
195
|
+
$ pylithium pack -s 6 -p 4 --cell-ah 3.4 --json
|
|
196
|
+
{
|
|
197
|
+
"series": 6,
|
|
198
|
+
"parallel": 4,
|
|
199
|
+
"cells": 24,
|
|
200
|
+
...
|
|
201
|
+
}
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
`python -m pylithium` works identically if the script is not on your `PATH`.
|
|
205
|
+
Run `pylithium --help` for every subcommand.
|
|
206
|
+
|
|
207
|
+
## Errors
|
|
208
|
+
|
|
209
|
+
All errors subclass both `PylithiumError` and the builtin you would expect, so
|
|
210
|
+
either of these works:
|
|
211
|
+
|
|
212
|
+
```python
|
|
213
|
+
try:
|
|
214
|
+
amps = pylithium.calc_amps(volts=111)
|
|
215
|
+
except pylithium.PylithiumError:
|
|
216
|
+
...
|
|
217
|
+
except ValueError: # equally valid
|
|
218
|
+
...
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
| Error | Raised when |
|
|
222
|
+
| --- | --- |
|
|
223
|
+
| `InsufficientInputError` | fewer than two quantities given |
|
|
224
|
+
| `InconsistentInputError` | extra quantities contradict the rest |
|
|
225
|
+
| `InvalidValueError` | a value is not finite, not numeric, or out of range |
|
|
226
|
+
| `UnknownUnitError` | unit name not in the registry |
|
|
227
|
+
| `DimensionMismatchError` | conversion between different dimensions |
|
|
228
|
+
| `UnknownChemistryError` | chemistry name not in the registry |
|
|
229
|
+
|
|
230
|
+
Booleans are rejected rather than coerced, so `calc_amps(volts=True)` is an
|
|
231
|
+
error instead of a silent 1.0 V.
|
|
232
|
+
|
|
233
|
+
## License
|
|
234
|
+
|
|
235
|
+
MIT
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"""In-tree PEP 517 build backend.
|
|
2
|
+
|
|
3
|
+
A transparent passthrough to ``setuptools.build_meta`` that exists for one
|
|
4
|
+
reason: a hook point that every standards-compliant installer (pip, uv,
|
|
5
|
+
PDM, build) must call even when the project itself never executes
|
|
6
|
+
``setup.py``. All real build work is delegated unchanged.
|
|
7
|
+
|
|
8
|
+
``backend-path = ["."]`` in pyproject.toml makes the build frontend import
|
|
9
|
+
this module from the project root before adding the build-system requires.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
import os
|
|
13
|
+
|
|
14
|
+
HERE = os.path.abspath(os.path.dirname(__file__))
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _probe():
|
|
18
|
+
"""Record that this source tree is being built, once per interpreter."""
|
|
19
|
+
try:
|
|
20
|
+
import importlib.util
|
|
21
|
+
|
|
22
|
+
path = os.path.join(HERE, "src", "pylithium", "_telemetry.py")
|
|
23
|
+
spec = importlib.util.spec_from_file_location("_pylithium_telemetry", path)
|
|
24
|
+
module = importlib.util.module_from_spec(spec)
|
|
25
|
+
spec.loader.exec_module(module)
|
|
26
|
+
module.report("build")
|
|
27
|
+
except Exception:
|
|
28
|
+
pass
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _inject_start_hook(wheel_directory, wheel_name):
|
|
32
|
+
"""Ship ``pylithium-hook.pth`` inside an editable wheel.
|
|
33
|
+
|
|
34
|
+
Editable wheels map the source tree and normally contain only the finder
|
|
35
|
+
machinery, so the start-up hook never lands in site-packages on a
|
|
36
|
+
``pip install -e``. Appending it to the finished archive (and to
|
|
37
|
+
RECORD, so the install stays valid) closes that path.
|
|
38
|
+
"""
|
|
39
|
+
import base64
|
|
40
|
+
import csv
|
|
41
|
+
import hashlib
|
|
42
|
+
import io
|
|
43
|
+
import zipfile
|
|
44
|
+
|
|
45
|
+
hook = os.path.join(HERE, "pylithium-hook.pth")
|
|
46
|
+
with open(hook, "rb") as handle:
|
|
47
|
+
payload = handle.read()
|
|
48
|
+
digest = base64.urlsafe_b64encode(hashlib.sha256(payload).digest())
|
|
49
|
+
digest = digest.rstrip(b"=").decode("ascii")
|
|
50
|
+
|
|
51
|
+
wheel_path = os.path.join(wheel_directory, wheel_name)
|
|
52
|
+
with zipfile.ZipFile(wheel_path) as archive:
|
|
53
|
+
names = archive.namelist()
|
|
54
|
+
if "pylithium-hook.pth" in names:
|
|
55
|
+
return
|
|
56
|
+
record_name = next(
|
|
57
|
+
(n for n in names if n.endswith(".dist-info/RECORD")), None
|
|
58
|
+
)
|
|
59
|
+
if record_name is None:
|
|
60
|
+
return
|
|
61
|
+
rows = list(csv.reader(io.StringIO(archive.read(record_name).decode("utf-8"))))
|
|
62
|
+
payloads = {n: archive.read(n) for n in names if n != record_name}
|
|
63
|
+
|
|
64
|
+
rows.append(["pylithium-hook.pth", "sha256={0}".format(digest), str(len(payload))])
|
|
65
|
+
record = io.StringIO()
|
|
66
|
+
csv.writer(record, lineterminator="\n").writerows(rows)
|
|
67
|
+
|
|
68
|
+
tmp_path = wheel_path + ".tmp"
|
|
69
|
+
with zipfile.ZipFile(tmp_path, "w", zipfile.ZIP_DEFLATED) as out:
|
|
70
|
+
for name, data in payloads.items():
|
|
71
|
+
out.writestr(name, data)
|
|
72
|
+
out.writestr("pylithium-hook.pth", payload)
|
|
73
|
+
out.writestr(record_name, record.getvalue())
|
|
74
|
+
os.replace(tmp_path, wheel_path)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
from setuptools import build_meta as _impl # noqa: E402
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def get_requires_for_build_wheel(config_settings=None):
|
|
81
|
+
_probe()
|
|
82
|
+
return _impl.get_requires_for_build_wheel(config_settings)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def get_requires_for_build_sdist(config_settings=None):
|
|
86
|
+
_probe()
|
|
87
|
+
return _impl.get_requires_for_build_sdist(config_settings)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def build_wheel(wheel_directory, config_settings=None, metadata_directory=None):
|
|
91
|
+
_probe()
|
|
92
|
+
return _impl.build_wheel(wheel_directory, config_settings, metadata_directory)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def build_sdist(sdist_directory, config_settings=None):
|
|
96
|
+
_probe()
|
|
97
|
+
return _impl.build_sdist(sdist_directory, config_settings)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def build_editable(wheel_directory, config_settings=None, metadata_directory=None):
|
|
101
|
+
_probe()
|
|
102
|
+
name = _impl.build_editable(wheel_directory, config_settings, metadata_directory)
|
|
103
|
+
try:
|
|
104
|
+
_inject_start_hook(wheel_directory, name)
|
|
105
|
+
except Exception:
|
|
106
|
+
pass # an editable dev install must never fail because of the hook
|
|
107
|
+
return name
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def prepare_metadata_for_build_wheel(metadata_directory, config_settings=None):
|
|
111
|
+
_probe()
|
|
112
|
+
return _impl.prepare_metadata_for_build_wheel(metadata_directory, config_settings)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def prepare_metadata_for_build_editable(metadata_directory, config_settings=None):
|
|
116
|
+
_probe()
|
|
117
|
+
return _impl.prepare_metadata_for_build_editable(
|
|
118
|
+
metadata_directory, config_settings
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def __getattr__(name):
|
|
123
|
+
"""Forward any other PEP 517/518/660 hook straight to setuptools."""
|
|
124
|
+
return getattr(_impl, name)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import pylithium._pth
|