Skip to main content

collection of classes for simple to complex electrical calculations

Project description

# eecalpy Python module

[![pypi package](https://badge.fury.io/py/eecalpy.svg)](https://badge.fury.io/py/eecalpy) [![Build Status](https://travis-ci.org/wese3112/eecalpy.svg?branch=master)](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).

![Simple voltage divider](img/vdiv.png?raw=true “voltage divider”)

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'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

eecalpy-0.9.4.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

eecalpy-0.9.4-py3-none-any.whl (22.1 kB view details)

Uploaded Python 3

File details

Details for the file eecalpy-0.9.4.tar.gz.

File metadata

  • Download URL: eecalpy-0.9.4.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for eecalpy-0.9.4.tar.gz
Algorithm Hash digest
SHA256 fac5cdd1570d45906cf796d422ad6bd669c6d8a194691e4d19ee2d5c1c7dc3ab
MD5 8cc5985a67c72fd1ced1ebd74a0c0401
BLAKE2b-256 db4aa67e6a9b347525a397ca1c7b822fea3d6ca188b6b67ffd3cd2a9b0adcaf7

See more details on using hashes here.

File details

Details for the file eecalpy-0.9.4-py3-none-any.whl.

File metadata

  • Download URL: eecalpy-0.9.4-py3-none-any.whl
  • Upload date:
  • Size: 22.1 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for eecalpy-0.9.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0d1c4458a5366b4838bd7f78b459ce5f31a8bd18ff5818f3b5ebe27071a50cc3
MD5 7a93dbe291f716236bfe3044b3ef8655
BLAKE2b-256 3e216e82ccea060ff81289d0039dd387afad04f7e41b8fbc898954de5ae51431

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page