Skip to main content

Easy engineering notation

Project description

Travis CI Build Status

Purpose

To easily work with human-readable engineering notation. I wrote this as a quick tool for my own use. I found that I was writing the same functionality into multiple packages and would like a quick pip-installable package to take care of this manipulation for me. The package should be easily extended for other use cases. The package is unit-less, so only operates on numeric values. Unit detection may be added in future versions.

More information may be found at for(embed).

Installation

Install using pip: pip install engineering_notation.

Status and Contributions

This project currently has 100% test coverage. Have a look in test.py for examples of how to use this library. To execute the tests, run pytest from the main directory or, in some environments, it may be necessary to run python3 -m pytest.

Any contributions must pass 100% of current tests and pass flake8. To execute flake8, navigate to the project directory and python3 setup.py flake8.

Your pull request will automatically be run through testing and flake8 checks and any pull requests that do not pass these will be put on hold pending passing.

Use

There are multiple ways of initializing a number to a particular value, but a string is the preferred method:

>>> from engineering_notation import EngNumber
>>> EngNumber('10k')
10k
>>> EngNumber('10000')
10k
>>> EngNumber(10000)
10k
>>> EngNumber(10000.0)
10k
>>> EngNumber(1e4)
10k

Where decimals are involved, we use a default precision of 2 digits:

>>> EngNumber('4.99k')
4.99k
>>> EngNumber('4.9k')
4.90k

This behavior can truncate your results in some cases, and cause your number to round. To specify more or less digits, simply specify the precision in the declaration:

>>> EngNumber('4.999k')
5k
>>> EngNumber('4.999k', precision=3)
4.999k

Most operations that you would perform on numeric values are valid, although all operations are not implemented:

>>> EngNumber('2.2k') * 2
4.40k
>>> 2 * EngNumber('2.2k')
4.40k
>>> EngNumber(1.2) > EngNumber('3.3k') 
False
>>> EngNumber(1.2) <= EngNumber('3.3k')
True
>>> EngNumber('3.3k') == EngNumber(3300)
True

All of the above operations are also possible on the EngUnit() class as well. The only difference is that units must match for addition/subtraction/comparison operations. Although multiplication and division operations will work numerically, they may not always be strictly correct. This is because EngUnit is not intended to replace a computer algebra system!

>>> EngUnit('2s') / EngUnit('4rotations')
0.5s/rotations

Additionally, since there are 'reserved' letters for sizing the number, you must be careful with your units!

>>> EngUnit('2mm')
2mm        # <<< this value equivalent to "0.002m"
>>> EngUnit('2meter')
2meter     # <<< this value is equivalent to "0.002eter", the "m" was used to scale the unit!

Contributions

Contributions are welcome. Feel free to make feature requests in the issues.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

engineering_notation-0.5.2-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file engineering_notation-0.5.2-py3-none-any.whl.

File metadata

File hashes

Hashes for engineering_notation-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cb458a31c54016411c62399ece761a22f50064d6deff96181094c28f3ed392c9
MD5 13d8903f1deb9efe2e9f8bcfee236052
BLAKE2b-256 211b5d21849ef984b4c8458c946c4539a469d4307a3881111e87ee9174c15cc0

See more details on using hashes here.

Provenance

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