Skip to main content

Etherium unit conversation and arithmetic library

Project description

etherunit

Etherium unit conversation and arithmetic library

Install

pip install -u etherunit

Usage

>>> from etherunit import Ether, Gwei, Wei, E

Create a new quantity with units

You can create a new quatity like this

>>> Ether(".05")
0.05 ether
>>> Gwei(".05")
0.05 gwei
>>> Wei("5")
5 wei

Or you can use the helper function E() to create a new quantity

>>> E(".05 eth")
0.05 ether
>>> E(".05 gwei")
0.05 gwei
>>> E("5 wei")
5 wei

Arithmetic operations

Different quatities can be added together without any problems

>>> E(".05 eth") + E("2 gwei") == E("0.050000002 eth")
True

This also applies for subtraction

>>> E(".05 eth") - E("2 gwei") == E("0.049999998 eth")
True

You can also multiply quanities with other integers

>>> E(".05 eth") * 2
0.1 ether

... but not with other quatities

>>> E(".05 eth") * E("2 gwei")  # type: ignore
AssertionError: 2 gwei is not an integer

You also can't multiply quatities with other integers, like floats. Why? Because it can result with fractional wei, which is not allowed

>>> E(".05 eth") * 1.5
AssertionError: 1.5 is not an integer

You can divide quatities with other integers, the result is always a quatity

>>> E(".05 eth") / 2
0.025 ether

And you can divide quatities with other quatities, the result is always an integer

>>> E("10 eth") / E("3 eth")
3

You can find the remainder of a division with mod operator (%)

>>> E("10 eth") % E("3 eth")
1 ether

You can also use divmod() to get both the quotient and the remainder

>>> divmod(E("10 eth"), E("3 eth"))
(3, 1 ether)

Conversion

You can convert a quatity to another quatity, though it's not necessary for arithmetic operations

>>> E("10 eth").gwei
10000000000 gwei
>>> E("10 eth").wei
10000000000000000000 wei
>>> (E("10 eth") % E("3 eth")).wei
1000000000000000000 wei
>>> E("10 eth").wei.eth.eth.eth.eth.gwei.wei.wei.wei  # :D
10000000000000000000 wei

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

etherunit-0.0.6.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

etherunit-0.0.6-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file etherunit-0.0.6.tar.gz.

File metadata

  • Download URL: etherunit-0.0.6.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for etherunit-0.0.6.tar.gz
Algorithm Hash digest
SHA256 40963d67c3012d21b5880b9b28e1ba76e2a75a09628295126d0bc86d8a6a4f87
MD5 ca57c60b2e83622b9ae217bd5bbbe26e
BLAKE2b-256 bd3ef18e309bdbf263e154430ad9d296f4613379edad2c6ec348987321878cea

See more details on using hashes here.

File details

Details for the file etherunit-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: etherunit-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for etherunit-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 86bed0ef6f6f7782c0b3f3a19db375f4d7395efdcf147dcc268e1aca887cceda
MD5 356fd6444958014a137822b94b4a88d6
BLAKE2b-256 bac728c3f9c6f1e13a53cab29e21c3fa722284bcadbab535da91d976a051dc08

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