# eecalpy Python module
[](https://badge.fury.io/py/eecalpy) [](https://travis-ci.org/wese3112/eecalpy)
The Electrical Engineering Calculations for Python module is a collection of classes for simple to complex electrical calculations, with a special focus on handling tolerances.
USE AT OWN RISK, I DO NOT GUARANTEE THE CORRECTNESS OF THE CALCULATIONS IN THIS PACKAGE
## Installation
The eecalpy package is available on the Python Package Index (PyPI). The package needs Python 3+, you can install it with:
$ pip install eecalpy
## Introduction
Check out the voltage divider below. For both resistors their tolerance and the temperature coefficient α are given (α in parts per million).

Let’s create two variables for them.
>>> r1 = R(resistance=1000, tolerance=0.05, alpha_ppm=250) >>> r2 = R(2e3, 0.01, 100) >>> r1; r2 1.0kΩ ± 5.0% (± 50.0Ω) [0.9500 .. 1.0500]kΩ @ 20°C α=250ppm 2.0kΩ ± 1.0% (± 20.0Ω) [1.9800 .. 2.0200]kΩ @ 20°C α=100ppm
The formula for the voltage divider factor is r1 / (r1 + r2). To calculate it use R.voltage_divider(other_resistor):
>>> r1.voltage_divider(r2) 0.33 ± 4.0% [0.3199 .. 0.3465]
You can also use a shorthand notation:
>>> r1 // r2 0.33 ± 4.0% [0.3199 .. 0.3465]
Attention: Do not use the statement r1 / (r1 + r2) here, because it would use the tolerance limits of r1 twice (addition and division) and therefore yield a false result.
The result above is an instance of the Factor class. Now only the voltage is missing. These are created using U(voltage, tolerance=0.0).
Let’s assume the input voltage is 24V with a 1% tolerance the output voltage of the voltage divider then is:
>>> vin = U(24, 0.01) >>> vout = r1 // r2 * vin >>> vout 8.0V ± 5.0% (± 400.0mV) [7.6000 .. 8.4000]V
Note: the statement vout = vin * r1 // r2 does not work. It’s evaluated from left to right, so python first tries vin * r1 which is not implemented (voltage times resistance), but you can always use parenthesis:
>>> vin * (r1 // r2) 8.0V ± 5.0% (± 400.0mV) [7.6000 .. 8.4000]V
For demonstration, let’s calculate some of the voltage divider parameters.
Current through R1 and R2 (to GND):
>>> vin / (r1 + r2) 8.01mA ± 3.33% (± 266.81µA) [7.7394 .. 8.2730]mA
Power dissipation of the resistors:
>>> vout**2 / r1 65.46mW ± 21.35% (± 13.97mW) [51.4842 .. 79.4301]mW >>> (vin - vout)**2 / r2 128.26mW ± 12.3% (± 15.78mW) [112.4776 .. 144.0351]mW
Let’s also see how vout changes when the ambient temperature is 200°C:
>>> r1.at_T(200) // r2.at_T(200) * vin 8.14V ± 4.97% (± 404.16mV) [7.7359 .. 8.5443]V
R.at_T(temperature) is the same as R.at_temperature(temperature). It returns a new resistor object at the given temperature (in °C).
You can of course also use perfect values, so without the tolerance and temperature coefficient:
>>> r1 = R(1e3) >>> r2 = R(2e3) >>> vin = U(24) >>> r1; r2; vin 1.0kΩ @ 20°C 2.0kΩ @ 20°C 24.0V >>> vout = r1 / (r1 + r2) * vin >>> vout 8.0V
By the way, you can get the series resistance using + and the parallel resistance using |:
>>> r1 + r2 3.0kΩ @ 20°C >>> r1 | r2 666.67Ω @ 20°C >>> r1 | (R(5e3) + R(3e3)) | r2 # complex statements allowed! 615.38Ω @ 20°C
## Classes
The available classes are:
Voltage U(voltage, tolerance=0.0)
Resistance R(resistance, tolerance=0.0, alpha_ppm=None)
Current I(current, tolerance=0.0)
Power P(power, tolerance=0.0)
Factor Factor(factor, tolerance) (unitless factor, example below)
squared Voltage (V²) Usq(voltage, tolerance=0.0)
squared Current (A²) Isq(voltage, tolerance=0.0)
All classes do have the following members (example when using a voltage):
>>> v1 = U(24, 0.04) >>> v1 24.0V ± 4.0% (± 960.0mV) [23.0400 .. 24.9600]V >>> v1.value 24 >>> v1.min 23.04 >>> v1.max 24.96 >>> v1.unit 'V'
A unit can also be created using the .from_min_max(min, max) classmethod when the lower and upper limit is known (min/max):
>>> P.from_min_max(3, 4) 3.5W ± 14.29% (± 500.0mW) [3.0000 .. 4.0000]W
All units feature the add, subtract, multiply and divide operators. The calculation only works if the result’s type is one of the classes above:
This works because the result type is one of the known classes:
>>> U(10) + U(20) 30.0V >>> I(2e-3) - I(10e-3) -8.0mA >>> U(10) * I(2e-3) 20.0mW >>> U(10) / I(2e-3) 5.0kΩ @ 20°C >>> U(10) * Factor(2) 20.0V >>> I(10e-3) * R(150) 1.5V >>> P(200) / U(5) 40.0A >>> U(3) * U(3) 9.0V² >>> U(3)**2 # U squared 9.0V² >>> U(3)**2 / R(1e3) 9.0mW
This does not work because voltage divided by power is not a known class:
>>> U / P Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for /: 'type' and 'type'
Metadata
Release files for eecalpy 0.9.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| eecalpy-0.9.4.tar.gz | 11.8 kB | Without provenance | Details for eecalpy-0.9.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| eecalpy-0.9.4-py3-none-any.whl | Python 3 | none | any | Details for eecalpy-0.9.4-py3-none-any.whl |
Total release size: 33.9 kB
Release files / eecalpy-0.9.4.tar.gz
| Download URL | eecalpy-0.9.4.tar.gz |
|---|---|
| Size | 11.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fac5cdd1570d45906cf796d422ad6bd669c6d8a194691e4d19ee2d5c1c7dc3ab
|
|
BLAKE2b-256 checksum How to use checksums |
db4aa67e6a9b347525a397ca1c7b822fea3d6ca188b6b67ffd3cd2a9b0adcaf7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.0
|
Release files / eecalpy-0.9.4-py3-none-any.whl
| Download URL | eecalpy-0.9.4-py3-none-any.whl |
|---|---|
| Size | 22.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0d1c4458a5366b4838bd7f78b459ce5f31a8bd18ff5818f3b5ebe27071a50cc3
|
|
BLAKE2b-256 checksum How to use checksums |
3e216e82ccea060ff81289d0039dd387afad04f7e41b8fbc898954de5ae51431
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.0
|
Security for eecalpy 0.9.4
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.