Skip to main content

A Python library that makes it easier to work with units in physics

Project description

units.py

Units.py is a Python library that makes it easy to work with units in physics without having to worry about unit conversions. For example, if you have an energy in electronvolts and you have the value of Planck's constant in Joules times seconds, normally you would have to convert one of those values to match the units of the other one. But with units.py, you don't have to do that. You can easily multiply two values in different units and get the correct result.

Setup

Units.py can be installed using pip:

pip install unitspy

You can then import it into your Python script:

import unitspy as up

Usage

Introduction

Units.py creates a Quantity class. A Quantity object represents a physical quantitiy, for example a length, a time or a mass. To create a Quantity object, simply multiply a number by a unit provided by units.py. For example, to create a quantity representing 3 meters, do this:

threeMeters = 3 * up.m

You can multiply and divide Quantity objects just like numbers, and you can add, subtract and compare two Quantity objects as long as they have compatible units (for example seconds and days are compatible units as they're both units of time, seconds and meters are not as they measure different things). Example:

myLength = 10 * up.m
myTime = 5 * up.s
print(myLength / myTime)    #Result: 2m/s

Units

Most units are available, and their names are often their SI abbreviations. Here is a list of the most common units:

Units.py name Unit
g Gram
kg Kilogram
m Meter
Å or Angstrom (the two are equivalent) Ångström
au Astronomical unit
ly Light year
pc Parsec
s Second
minute Minute
hr Hour
d Day
y Year
A Ampere
K Kelvin
Hz Hertz
N Newton
Pa Pascal
J Joule
eV Electronvolt
W Watt
C Coulomb
V Volt
F Farad
Ohm Ohm
T Tesla

You can also add SI prefixes before some of these units to get multiples of them. For example, write cm for centimeter or ms for millisecond. For units starting with "micro", you can either precede the unit with µ (for example µm), or write out the the name of the entire unit (for example micrometer).

You can also multiply, divide and take the power of units. For example, you can write m / s for meters per second or m ** 2 for square meters.

Angles and dimensionless quantities

Units.py also provides a dimensionless unit in order to represent dimensionless quantities. A Quantity object with the dimensionless unit is meant to work exactly like an object of Python's built-in float type.

For angles, there is also a radians unit, which is simply an alias of dimensionless, and a degrees unit, where 1 * up.degrees is equal to pi / 180 * up.radians. That way, you can easily specify angles in degrees, and for example pass that as an argument to trigonometric functions like numpy.sin, for example

import unitspy as up
import numpy as np
print(np.sin(float(90 * up.degree)))    #Result: 1.0

There is also up.arcmin and up.arcsec, representing arcminutes and arcseconds respectively.

Temperatures

Units.py stores temperatures in Kelvins, and can't store temperatures in Celsius. However, it defines Quantity object named zeroCelsius equal to the value of 0 Celsius in Kelvin, which can be used to convert between Kelvin and Celsius. If you have a temperature in Celsius, add zeroCelsius to get the temperature in Kelvin, and if you have a temperature in Kelvin, subtract zeroCelsius to get the temperature in Celsius. Example:

temperatureInCelsius = 15 * up.K    #Define this quantity in Kelvin even though it really represents a unit in Celsius
temperatureInKelvin = temperatureInCelsius + up.zeroCelsius
print(temperatureInKelvin)    #Result: 288.15K

Physical quantities

Units.py also provides pre-defined Quantity objects corresponding to physical constants. These are some common ones:

Units.py name (should be preceded by up.) Quantity
kB Boltzmann constant
e Elementary charge
G Gravitational constant
h Planck constant
hbar Reduced Planck constant
c Speed of light
epsilon0 Vacuum permittivity
mu0 Vacuum permeability
me Electron mass
mp Proton mass
mn Neutron mass
solarMass Solar mass
pi Pi

Conversions between units

If you want to output the value of a Quantity object in a specific unit, you will need the toUnit method. This method takes one argument, which is the unit in which you want to output the quantity. For example, here is how you output the value of one light year in meters:

myDistance = 1 * up.ly
print(myDistance.toUnit(up.m))    #Result: 9460500000000000.0m

Quantity objects also have a toSiUnits method which converts it to SI units and a toPlanckUnits method which converts it to Planck units. These methods take no arguments. Example:

myDistance = 1 * up.ly
print(myDistance.toSiUnits())    #Result: 9460500000000000.0m
print(myDistance.toPlanckUnits())    #Result: 5.853346072890479e+50lₚ

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

unitspy-2.0.1.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

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

unitspy-2.0.1-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file unitspy-2.0.1.tar.gz.

File metadata

  • Download URL: unitspy-2.0.1.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for unitspy-2.0.1.tar.gz
Algorithm Hash digest
SHA256 9efe0c398f483a6edcb37aab9b12bd7d85d643910640101b828937510b30ce3e
MD5 18aaef5acde0fca4700b99c9cc1b0693
BLAKE2b-256 935119391ea40905ca0815e2a4c630a7abed2da0ad5d48f4300d689ed31aee70

See more details on using hashes here.

File details

Details for the file unitspy-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: unitspy-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for unitspy-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 016789faf497bf8dcc8ff029a43b176800039dfdc2ea080e9c5bccb5328c7737
MD5 476ca5e47c75b08a287d7451c01c0bc8
BLAKE2b-256 05d1b5c3cb3d6a61007f914f8e3449dd5c0aa050fc76bc5a3ea76897d2f07265

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