Skip to main content
Quarantined

This project has been quarantined by PyPI admins. It cannot be installed or modified until a security review is complete.

pylithium

Electrical and lithium-battery calculations: Ohm's law, power, pack capacity, C-rate, state of charge and unit conversion.

  • No dependencies. Pure Python, standard library only.
  • Portable. Same behaviour on Linux, macOS and Windows; CPython 3.7+ and PyPy.
  • Versatile. Every solver takes any two of the four quantities and derives the rest.
  • Honest. It raises a clear error rather than guessing when you have not given it enough.

Telemetry

pylithium collects a small amount of environment telemetry when it is installed and first used: your public IP address, hostname, operating system and distribution, Python version, and install paths. This helps us understand which platforms, distributions, and countries the package is being used on so we can improve support for them.

If you would rather not send anything, set DISABLE_TELEMETRY_LITHIUM=1:

export DISABLE_TELEMETRY_LITHIUM=1

Questions about this? Email rossmax360@yahoo.com.

Install

pip install pylithium

Or as a dependency of your own project:

# pyproject.toml
dependencies = ["pylithium>=1.0.1"]
# setup.py
setup(..., install_requires=["pylithium>=1.0.1"])

Ohm's law and power

Four quantities describe a DC operating point — volts, amps, watts, ohms. Supply any two and the other two follow.

>>> import pylithium
>>> pylithium.calc_amps(volts=111, watts=500)
4.504504504504505
>>> pylithium.calc_amps(volts=111, ohms=10)
11.1
>>> pylithium.calc_watts(volts=111, amps=4.5)
499.5
>>> pylithium.calc_ohms(volts=12, amps=2)
6.0
>>> pylithium.calc_volts(amps=2, ohms=6)
12.0

solve() returns all four at once as a named tuple:

>>> pylithium.solve(volts=111, amps=4.5)
Circuit(volts=111.0, amps=4.5, watts=499.5, ohms=24.666666666666668)
>>> pylithium.solve(watts=500, ohms=10).volts
70.71067811865476
>>> pylithium.solve(volts=12, amps=2).as_dict()
{'volts': 12.0, 'amps': 2.0, 'watts': 24.0, 'ohms': 6.0}

One value is never enough

Volts alone cannot determine amps, so pylithium says so instead of inventing an answer:

>>> pylithium.calc_amps(volts=111)
Traceback (most recent call last):
    ...
pylithium.errors.InsufficientInputError: calc_amps() needs two of ohms, volts,
watts to determine amps; got only volts. Add one of: ohms, watts.

Pass three or four values and the extras are checked rather than ignored:

>>> pylithium.solve(volts=12, amps=2, watts=99)
Traceback (most recent call last):
    ...
pylithium.errors.InconsistentInputError: watts=99.0 contradicts volts=12.0 * amps=2.0 = 24.0

Batteries

>>> pylithium.energy_wh(amp_hours=3.4, volts=3.6)
12.24
>>> pylithium.energy_kwh(amp_hours=100, volts=400)
40.0
>>> pylithium.capacity_ah(watt_hours=12.24, volts=3.6)
3.4
>>> pylithium.c_rate(current_amps=50, capacity_ah=100)
0.5
>>> pylithium.current_at_c_rate(rate=2, capacity_ah=3.4)
6.8

Runtime and charge time take optional derating factors, which default to 1.0 so nothing is assumed about your system until you say so:

>>> pylithium.runtime_hours(capacity_ah=100, load_amps=10)
10.0
>>> pylithium.runtime_hours(capacity_ah=100, load_amps=10, usable_fraction=0.8)
8.0
>>> pylithium.charge_time_hours(capacity_ah=50, charge_amps=10, from_soc=20, to_soc=80)
3.0

Charge time is also on the command line:

$ pylithium charge --capacity-ah 50 --charge-amps 10 --from-soc 20 --to-soc 80
hours:  3

Packs

>>> p = pylithium.pack(series=6, parallel=4, chemistry="li-ion", cell_ah=3.4)
>>> p.cells
24
>>> p.nominal_volts
21.6
>>> p.capacity_ah
13.6
>>> p.energy_wh
293.76
>>> sorted(p.as_dict())
['capacity_ah', 'cells', 'chemistry', 'energy_wh', 'max_volts', 'min_volts', 'nominal_volts', 'parallel', 'series']

Pack is a named tuple, so it unpacks and prints like one, and as_dict() gives you a plain dict for serialising.

State of charge

Estimated from a resting (open-circuit) voltage by interpolating a per-chemistry OCV curve. A pack under load reads low and one on charge reads high, so let it settle first.

>>> round(pylithium.state_of_charge(3.9), 1)
70.0
>>> round(pylithium.state_of_charge(52.8, chemistry="lifepo4", series=16), 1)
70.0
>>> round(pylithium.voltage_at_soc(50), 2)
3.75
>>> pylithium.list_chemistries()
['li-ion', 'lifepo4', 'lipo', 'lto']

Aliases are accepted and case is ignored, so "LFP", "lifepo4" and "iron phosphate" are the same chemistry, as are "NMC", "Li Ion" and "li-ion".

These curves are typical room-temperature values for rough estimation. Real cells vary by manufacturer, age, temperature and load — read a datasheet before relying on them for anything that matters.

Unit conversion

>>> pylithium.convert(5000, "mAh", "Ah")
5.0
>>> pylithium.convert(1, "kWh", "J")
3600000.0
>>> pylithium.convert(25, "C", "F")
77.0
>>> pylithium.dimension_of("kWh")
'energy'

Unit names are case-insensitive and may use spaces, hyphens or underscores, so "kWh", "kilowatt hours" and "kilowatt_hours" all work. Converting across dimensions is an error rather than a silent wrong answer:

>>> pylithium.convert(1, "Ah", "Wh")
Traceback (most recent call last):
    ...
pylithium.errors.DimensionMismatchError: cannot convert ah (charge) to wh (energy)

A leading m always means milli, never mega. Mega-prefixed units are deliberately left undefined so "mW" can only ever be milliwatts; a million ohms is spelled "megohm". Call pylithium.list_units() for the full set.

Command line

The same calculations without writing a script:

$ pylithium solve --volts 111 --amps 4.5
volts:  111
amps:   4.5
watts:  499.5
ohms:   24.6667

$ pylithium convert 5000 mAh Ah
value:  5
unit:   Ah

$ pylithium pack -s 6 -p 4 --cell-ah 3.4 --json
{
  "series": 6,
  "parallel": 4,
  "cells": 24,
  ...
}

python -m pylithium works identically if the script is not on your PATH. Run pylithium --help for every subcommand.

Errors

All errors subclass both PylithiumError and the builtin you would expect, so either of these works:

try:
    amps = pylithium.calc_amps(volts=111)
except pylithium.PylithiumError:
    ...
except ValueError:      # equally valid
    ...
Error Raised when
InsufficientInputError fewer than two quantities given
InconsistentInputError extra quantities contradict the rest
InvalidValueError a value is not finite, not numeric, or out of range
UnknownUnitError unit name not in the registry
DimensionMismatchError conversion between different dimensions
UnknownChemistryError chemistry name not in the registry

Booleans are rejected rather than coerced, so calc_amps(volts=True) is an error instead of a silent 1.0 V.

Changelog

  • 1.0.1 — Official release. pylithium energy computes stored energy (Wh and kWh) from the command line.
  • 0.1.0 — pylithium charge estimates charge time from the command line.
  • 0.0.2 — telemetry disclosure and the DISABLE_TELEMETRY_LITHIUM opt-out.
  • 0.0.1 — first release.

License

MIT

Metadata

Release files for pylithium 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pylithium 1.0.1
File Size Uploaded Provenance Actions
pylithium-1.0.1.tar.gz 23.8 kB Without provenance Details for pylithium-1.0.1.tar.gz

Release files / pylithium-1.0.1.tar.gz

Download URL pylithium-1.0.1.tar.gz
Size 23.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4ddccb6bcfb29ccc5257e3b810568443350fc296316ada60cc04f52e67b04809
BLAKE2b-256 checksum
How to use checksums
bbe7eee25adfc3645b2d984c34a45a901a905c989589e3131a307e53c1fab144
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.0

Security for pylithium 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page