An object-oriented Python interface to udunits
Project description
gimli.units
An object-oriented Python interface to udunits2 built with cython.
Requirements
gimli.units requires Python 3 and udunits2.
Apart from Python, gimli has a number of other requirements, the main one being udunits2. 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.txt file.
If you are a developer of gimli you will also want to install additional dependencies for running gimli’s tests to make sure that things are working as they should. These dependencies are listed in requirements-testing.txt.
Installation
To install gimli, first create a new environment in which gimli will be installed. This, although not necessary, will isolate the installation so that there won’t be conflicts with your base Python installation. This can be done with conda as:
$ conda create -n gimli python=3 $ conda activate gimli
Stable Release
gimli, and its dependencies, can be installed either with pip or conda. Using pip:
$ pip install gimli.units
Using conda:
$ conda install gimli.units -c conda-forge
From Source
After downloading the gimli source code, run the following from gimli’s top-level folder (the one that contains setup.py) to install gimli into the current environment:
$ pip install -e .
Usage
Primarily, gimli.units is a Python library 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 --data=1.0
5280.000000
Values to convert are passed through the –data option as a comma-separated list of numbers. You can also provide values through files and 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
Credits
Development Lead
Eric Hutton (@mcflugen)
Contributors
None yet. Why not be the first?
Changelog for gimli
0.3.0 (2021-11-16)
Added support for a wider range of array dtypes (#13)
Fixed AttributeError when deallocating UnitSystem after exiting Python (#12)
Fixed –version option for the gimli command (#11)
0.2.1 (2021-03-02)
Fixed a bug caused by passing a single value to the cli (#10)
Added content to the README, mainly installation and usage instructions (#9)
0.2.0b1 (2021-02-22)
Added release and prerelease actions to publish on PyPI and TestPyPI (#8)
Change name of package to gimli.units (#7)
0.2.0b0 (2021-02-20)
Added gimli command-line interface (#6)
Better handling of multi-dimensional and non-contiguous arrays (#5)
Fixed a issue that caused a segmentation fault when using dimensionless units (#4)
0.1.1 (2021-02-17)
Created the gimli package (#1)
Set continuous integration using GitHub actions (#2)
Deploy to TestPyPI on a version tag (#3)
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
Built Distributions
Hashes for gimli.units-0.3.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf88046d2c6ae7e5194b86a49d5c849fbcad504e39c1e3909c2f32fad3ef7aa6 |
|
MD5 | 0af8472a1cfc9e910e16c85c0033dd61 |
|
BLAKE2b-256 | b065dfd2c5f1c796a130637ca5a87d8c3ed523e63a412d9ec4f22bc6a9989b1c |
Hashes for gimli.units-0.3.0-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5dcb2306f08a6295059813b1aa8b3ee1d929955272129cca5e28ad8ef349588d |
|
MD5 | 6968a23df7edda061b0053215f923c60 |
|
BLAKE2b-256 | af5e5912ae17d144056bd190343a640101c9ea1b9472d385a01585dc24e37c6b |
Hashes for gimli.units-0.3.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff812d9bfa9ef24d3a192fa447b1c27f2e500bd12a42365f11ca33502ac23795 |
|
MD5 | 10da2e6ec92bc99b1759277ca1d96873 |
|
BLAKE2b-256 | 4652560e6b0ff150aadba998ef4b0a5b58c97db85ef25b8494b5e387d9a75ca1 |
Hashes for gimli.units-0.3.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a3dde3ae8e941701270b26ccf9825c0745105b5ccd4be6ed52e8cc6d79c18f9 |
|
MD5 | 5902b18c5e4b024b78e83cd415e74c7c |
|
BLAKE2b-256 | a99bfd41e45d416b9a20acd1da82eba4acf5c8c25c10eb914f4cc869191c9f10 |
Hashes for gimli.units-0.3.0-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5f2474c93f14289bc1e3c5b8b23f6ca50c79f665602c9b46949bcba10c9cbe5 |
|
MD5 | f351905f56bdc86a3a6f2e798f8ebc03 |
|
BLAKE2b-256 | 73403d08ae89dbb774d021f3ca78d3ae1bfa119caf3b44d99e74609632652bf3 |
Hashes for gimli.units-0.3.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b672c59aff0f8e5c430a9aa315a42a0bbe0380ab8b9affb4f8f02480a4a5eaa8 |
|
MD5 | 30f7f4a6e6782e54cfb5369857d73d29 |
|
BLAKE2b-256 | 73d30a747296ebd29a10ddf330bbf8077a67e73a610ea714f78e509c64874f1e |
Hashes for gimli.units-0.3.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c7b44f28c0a089d1b57f4fbde21fa42ac839c304b0065e0fdd55e97b960f940e |
|
MD5 | f403b4dde23ff2d2b9cc5ab22ad735aa |
|
BLAKE2b-256 | 2dda25dbbc7df10c9d6fa58b2c9b022a4bb13a72e146378b2a6abdc3c30ecf67 |
Hashes for gimli.units-0.3.0-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | db993dc23f3a59a859e3d7965791c67e88511a3807ff1cffb235bd4b2a7572f3 |
|
MD5 | 2837951b4be55cadae94ccaea20d49b2 |
|
BLAKE2b-256 | 593330ab72d89787bd97d4ee9a6d43b63be8910df5c20278c101a9300b609382 |
Hashes for gimli.units-0.3.0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d77adf219a6e9845282e92fe6f204e6395262232b1d98c8db9ef92343a20d95 |
|
MD5 | 9cdb2f1b1ae9dfc9e4ff98f2306666e4 |
|
BLAKE2b-256 | cbaf95f31380a8bb8f95a022b1b15fcfdab7b369fe088f9354efa6012597b4d0 |