Skip to main content

A simple unit converter for chemical engineers

Project description

z-units

A simple unit-converter for chemical engineers

Features

  • Gauge pressure units (MPag, kPag, psig, ...) with configurable atmospheric pressure
  • Standard volume units (Sm³) with configurable STP conditions
  • Friendly to HYSYS users
  • Rich formatting options

Installation

pip install z-units

Quickstart

>>> from z_units import MolarFlow, Length, Temperature, Pressure
>>> f = MolarFlow(3)
>>> f
<MolarFlow(3, 'kmol/s')>
>>> f.value, f.unit
(3, <Unit('kmol/s')>)
>>> f.to('kmol/h')
<MolarFlow(10800.0, 'kmol/h')>
>>> Length(100, 'cm') == Length(1000, 'mm')
True
>>> Temperature('100C')
<Temperature(1, 'C')>
>>> Pressure(15, 'psi').to('MPag')
<Pressure(0.0020963594, 'MPag')>
>>> from z_units import convert
>>> convert(1, 'm', 'ft')
<Length(3.2808399, 'ft')>

Advanced Features

Gauge Pressure Handling

The atmospheric pressure reference (default: 101325 Pa) can be configured globally or passed as an argument locally.

Using argument locally:

>>> from z_units import Pressure
>>> # Define source environment
>>> Pressure(100, 'kPag', atm_pressure=50e3).to('kPa')
<Pressure(150.0, 'kPa')>
>>> # Define target environment
>>> Pressure(150, 'kPa').to('kPag', atm_pressure=50e3)
<Pressure(100.0, 'kPag')>
>>> # Convert between different local environments
>>> Pressure(1, 'MPag', atm_pressure=1e5).to('MPag', atm_pressure=2e5)
<Pressure(0.9, 'MPag')>

Using global configuration:

>>> from z_units.environment import get_env
>>> from z_units import Pressure
# Before
>>> Pressure(100, 'kPa').to('kPag')
<Pressure(-1.325, 'kPag')>
# Set to 100 kPa
# atmospheric_pressure in Pa
>>> env = get_env()
>>> env.atmospheric_pressure = 100e3
# After
>>> Pressure(100, 'kPa').to('kPag')
<Pressure(0.0, 'kPag')>

Standard Volume Units

Standard temperature and pressure (STP) conditions can be configured for standard volume conversions:

>>> from z_units.environment import get_env
>>> from z_units import Substance
# Default STP: 20°C, 101325 Pa
>>> Substance(100, 'Nm3').to('Sm3')
<Substance(107.321984, 'Sm3')>
# Change to 15°C, set value in K
>>> get_env().standard_temperature = 273.15 + 15
>>> Substance(100, 'Nm3').to('Sm3')
<Substance(105.491488, 'Sm3')>

Unit Formatting

Multiple formatting styles are supported:

>>> from z_units import MolarEntropy
>>> x = MolarEntropy(100)
# Only value
>>> f"{x}"
'100'
# Quick style (simplified)
>>> f"{x:q}"
'100 kJ/kmol-C'
# Python style (full)
>>> f"{x:p}"
'100 kJ/(kmol*C)'

Predefined Quantities

  • Length (m, km, cm, mm, ft, in, ...)
  • Area (m2, km2, ...)
  • Volume (m3, L, mL, ...)
  • Time (s, min, h, ...)
  • Mass (kg, g, lb, ...)
  • Force (N, kN, ...)
  • Substance (mol, kmol, ...)
  • Energy (J, kJ, kWh, ...)
  • Velocity (m/s, km/h, ...)
  • Temperature (K, C, F)
  • DeltaTemperature (K, C, F)
  • Pressure (Pa, kPa, kPag, MPa, MPag, bar, barg, psi, psig...)
  • VolumeFlow (m³/s, L/min, ...)
  • StandardGasFlow (Nm3/h, Sm3/h, ...)
  • MolarFlow (mol/s, kmol/h, ...)
  • MassFlow (kg/s, t/h, ...)
  • Density (kg/m3, g/cm3, ...)
  • MolarDensity (mol/m3, kmol/m3)
  • HeatCapacity (J/kg-K, kJ/kg-K)
  • MolarHeatCapacity (J/mol-K, kJ/kmol-K))
  • Entropy (J/kg-K), kJ/kg-K)
  • MolarEntropy (J/mol-K, kJ/kmol-K)
  • ThermalConductivity (W/m-K)
  • Viscosity (Pa-s, cP)
  • KinematicViscosity (m2/s, cSt)
  • SurfaceTension (N/m)
  • Dimensionless

Environment Configuration

Global environment parameters can be configured:

>>> from z_units.environment import get_env
# Set atmospheric pressure
>>> get_env().atmospheric_pressure = 100e3  # 100 kPa
# Set standard conditions
>>> get_env().standard_temperature = 273.15  # 0°C
>>> get_env().standard_pressure = 101325  # 1 atm
# Reset to defaults
>>> get_env().reset()

About

As a chemical engineer, the Gauge-Pressure units are very useful to me. Unfortunately those units are not supported in some popular modules, so I reinvent the wheel.

License

MIT License

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

z_units-0.4.1.tar.gz (30.1 kB view details)

Uploaded Source

Built Distribution

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

z_units-0.4.1-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file z_units-0.4.1.tar.gz.

File metadata

  • Download URL: z_units-0.4.1.tar.gz
  • Upload date:
  • Size: 30.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for z_units-0.4.1.tar.gz
Algorithm Hash digest
SHA256 c7108145425fe28db0c6f3bcdd9097845479d2afd5d395688fdc1fd609e198d2
MD5 f633fd1323a92e5e362a467d3f1293e2
BLAKE2b-256 c1662dc91860b9456da596a2e9149e11539809f8e1487b5b01801695f7c6ce03

See more details on using hashes here.

File details

Details for the file z_units-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: z_units-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 15.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for z_units-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eb731f030ce636c12dac71b28e50a131f400d0f4d718cde025cb02fc7860c13d
MD5 7042c9fd0c8c0f5b17f5ed13e772af0b
BLAKE2b-256 e72ed78c48eb6043c467eea694b2186bc10dab3d11d7c0c577a6feec66d3bc11

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