Skip to main content

An object-oriented Python interface to udunits

Project description

Build Status PyPI Anaconda

gimli.units

An object-oriented Python interface to udunits2 built with cython.

NOTE: gimli.units includes a vendored version of udunits2 so that you do not have to install the udunits2 library separately.

Installation

pip install gimli.units

or

conda install gimli.units -c conda-forge

From Source

pip install git+https://github.com/mcflugen/gimli.git

Note: gimli.units requires the libudunits library. You will need to have this installed before building gimli.units. You can do this using conda,

conda install udunits2 -c conda-forge

or by building udunits2 from source (we use a vendored version of udunits2, which can be found in the extern folder of this repository).

Usage

Primarily, gimli.units is a Python module with an API that reflects that of the udunits2 library. gimli, however, also comes with a command-line interface.

API

You primarily will access gimli through gimli.units,

>>> from gimli.units import units

units is an instance of the default UnitSystem class, which contains all of the units contained in a given unit system. If you like, you can create your own unit system but, typically, the default should be fine.

units is a dict-like object whose keys are strings representing units and values are instances of those units. For example,

>>> units["m"]
Unit('meter')
>>> units["m/s"]
Unit('meter-second^-1')
>>> units["kg m-3"]
Unit('meter^-3-kilogram')
>>> units["N m"]
Unit('joule')

Every Unit instance has a to method, which returns a unit converter for converting values from one unit to another,

>>> lbs = units["lb"]
>>> kgs = units["kg"]
>>> kgs_to_lbs = kgs.to(lbs)
>>> kgs_to_lbs(1.0)
2.2046226218487757

You can also construct units that are a combination of other units.

>>> ft_per_s = units["ft / s"]
>>> m_per_s = units["m s-1"]
>>> ft_per_s.to(m_per_s)([1.0, 2.0])
array([0.3048, 0.6096])

Command-line interface

From the command line you can use gimli to convert values from one unit to another.

gimli --from=miles --to=ft
5280.000000

Values to convert can be passed through the files (use a dash for stdin).

echo "1.0" | gimli --from=cal --to=joule -
4.186800

When reading from a file, gimli tries to preserve the format of the input file,

cat values.csv
1.0, 2.0, 3.0
4.0, 5.0, 6.0
gimli --from=knot --to=m/s values.txt
0.514444, 1.028889, 1.543333
2.057778, 2.572222, 3.086667

Notice of Vendored Libraries

gimli.units includes two third-party libraries, expat and udunits, which are "vendored" as part of our codebase. This means that these libraries are embedded directly within gimli.units, rather than being external dependencies.

Reasons for Vendoring

  • udunits: A library for units of physical quantities, vendored to provide robust unit conversion and management, ensuring compatibility and consistency in unit operations.
  • expat: An XML parser library written in C, vendored to ensure consistent and reliable XML parsing across various platforms and environments without requiring separate installation of the library. expat is a dependency of udunits.

Implications for Users

  • No Additional Installations: Users do not need to install these libraries separately; they are fully integrated into our package.
  • Package Size: The inclusion of these libraries increases the size of our package. We have taken care to ensure that this does not significantly impact the installation and usage experience.
  • Compatibility: Vendoring these libraries helps us manage compatibility and reliability issues, as we use specific versions tested to work with our package.

Licensing

Updates and Security

  • We actively monitor and incorporate updates, including security patches, for these vendored libraries. Should there be any significant updates or security concerns, we aim to address these in a timely manner.

Further Information

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

gimli.units-0.3.2.tar.gz (31.8 kB view hashes)

Uploaded Source

Built Distributions

gimli.units-0.3.2-cp312-cp312-win_amd64.whl (219.1 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

gimli.units-0.3.2-cp312-cp312-musllinux_1_1_x86_64.whl (633.8 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

gimli.units-0.3.2-cp312-cp312-musllinux_1_1_aarch64.whl (632.0 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ ARM64

gimli.units-0.3.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (588.0 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

gimli.units-0.3.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (585.4 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

gimli.units-0.3.2-cp312-cp312-macosx_11_0_arm64.whl (98.4 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

gimli.units-0.3.2-cp312-cp312-macosx_10_9_x86_64.whl (162.0 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

gimli.units-0.3.2-cp311-cp311-win_amd64.whl (221.3 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

gimli.units-0.3.2-cp311-cp311-musllinux_1_1_x86_64.whl (639.5 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

gimli.units-0.3.2-cp311-cp311-musllinux_1_1_aarch64.whl (640.8 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ ARM64

gimli.units-0.3.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (595.2 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

gimli.units-0.3.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (593.9 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

gimli.units-0.3.2-cp311-cp311-macosx_11_0_arm64.whl (99.8 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

gimli.units-0.3.2-cp311-cp311-macosx_10_9_x86_64.whl (165.0 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

gimli.units-0.3.2-cp310-cp310-win_amd64.whl (221.3 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

gimli.units-0.3.2-cp310-cp310-musllinux_1_1_x86_64.whl (609.9 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

gimli.units-0.3.2-cp310-cp310-musllinux_1_1_aarch64.whl (610.5 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ ARM64

gimli.units-0.3.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (567.9 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

gimli.units-0.3.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (563.6 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

gimli.units-0.3.2-cp310-cp310-macosx_11_0_arm64.whl (99.6 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

gimli.units-0.3.2-cp310-cp310-macosx_10_9_x86_64.whl (164.7 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

Supported by

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