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.

Requirements

udunits2 is the unit conversion C library that gimli wraps using cython. The easiest way to install udunits2 is through Anaconda (see the Install section), or yum (as udunits2-devel on ubuntu-based Linux). It can, however, also be compiled and installed from source. You can get the source code either as a .tar.gz or from GitHub.

All other requirements are available using either pip or conda. To see a full listing of the requirements, have a look at the project's requirements.in file.

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 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.

To get a specific unit from the system, do so by passing a unit string to the Units class. For example,

>>> units.Unit("m")
Unit('meter')
>>> units.Unit("m/s")
Unit('meter-second^-1')
>>> units.Unit("kg m-3")
Unit('meter^-3-kilogram')
>>> units.Unit("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.Unit("lb")
>>> kgs = units.Unit("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.Unit("ft / s")
>>> m_per_s = units.Unit("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.1.tar.gz (31.1 kB view hashes)

Uploaded Source

Built Distributions

gimli.units-0.3.1-cp312-cp312-win_amd64.whl (232.6 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

gimli.units-0.3.1-cp312-cp312-musllinux_1_1_x86_64.whl (706.1 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

gimli.units-0.3.1-cp312-cp312-musllinux_1_1_aarch64.whl (701.3 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ ARM64

gimli.units-0.3.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (663.7 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

gimli.units-0.3.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (658.1 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

gimli.units-0.3.1-cp312-cp312-macosx_11_0_arm64.whl (115.0 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

gimli.units-0.3.1-cp312-cp312-macosx_10_9_x86_64.whl (180.0 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

gimli.units-0.3.1-cp311-cp311-win_amd64.whl (236.3 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

gimli.units-0.3.1-cp311-cp311-musllinux_1_1_x86_64.whl (720.3 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

gimli.units-0.3.1-cp311-cp311-musllinux_1_1_aarch64.whl (716.8 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ ARM64

gimli.units-0.3.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (671.7 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

gimli.units-0.3.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (668.9 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

gimli.units-0.3.1-cp311-cp311-macosx_11_0_arm64.whl (117.9 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

gimli.units-0.3.1-cp311-cp311-macosx_10_9_x86_64.whl (185.8 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

gimli.units-0.3.1-cp310-cp310-win_amd64.whl (236.5 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

gimli.units-0.3.1-cp310-cp310-musllinux_1_1_x86_64.whl (686.3 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

gimli.units-0.3.1-cp310-cp310-musllinux_1_1_aarch64.whl (683.8 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ ARM64

gimli.units-0.3.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (641.9 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

gimli.units-0.3.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (635.4 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

gimli.units-0.3.1-cp310-cp310-macosx_11_0_arm64.whl (117.9 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

gimli.units-0.3.1-cp310-cp310-macosx_10_9_x86_64.whl (186.0 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