Skip to main content

Currency classes to work with monetary values

Project description

Monepy

tests Codecov

PyPI - Version

License: MIT

A python package that implements currency classes to work with monetary values.

Install

From PyPI using pip:

pip install monepy

Behavior

The currency classes behave like a numeric type, with a formatted() method that returns the value in the currency format:

>>> from monepy import USD
>>> x = USD(10)
>>> x
<USD 10.00>
>>> x.formatted()
'$10.00'
>>> x == 1
False
>>> x == 10
True
>>> x > 10
False
>>> x < 10.1
True

The class constructor does not round the values recieved. If a value with more significant digits than the currency supports is provided, a ValueError will be raised.

Operators

Only supported operators are +, -, * and /.

The + and -

Operations with + and - can only be done with currencies of the same type and will return an object of the same currency:

>>> x = USD(10)
>>> y = USD(0.1)
>>> x + y
<USD 10.10>
>>> x - y
<USD 9.90>

The *

The * is only possible with a numeric value:

>>> x = USD(1.99)
>>> x * 1.23
<USD 2.44>

The /

The / operator is possible with both numeric and a currency of the same type:

>>> x = USD(10)
>>> x / 2
<USD 5.00>
>>> y = USD(3)
>>> x / y
3.3333333333333335

The %

The % is only supports numeric values

>>> x = USD(100)
>>> x / 15
<USD 6.66>
>>> x % 15
<USD 0.10>

Pandas integration

Currency classes can be used with the pandas library:

>>> import pandas as pd
>>> from monepy import EUR
>>> df = pd.DataFrame({
... "product": ["x", "y", "z"],
... "price": [10, 0.99, 25],
... "quantity": [8, 50, 200]
... })
>>> df
  product  price  quantity
0       x  10.00         8
1       y   0.99        50
2       z  25.00       200
>>> df["price"] = df["price"].map(EUR, na_action="ignore")
>>> df
  product  price  quantity
0       x  10,00         8
1       y   0,99        50
2       z  25,00       200
>>> df["subtotal"] = df["price"] * df["quantity"]
>>> df
  product  price  quantity  subtotal
0       x  10,00         8     80,00
1       y   0,99        50     49,50
2       z  25,00       200  5 000,00
>>> df[df["price"] >= 10]
  product  price  quantity  subtotal
0       x  10,00         8     80,00
2       z  25,00       200  5 000,00
>>> total = df["subtotal"].sum()
>>> print(f"The total is {total.formatted()}.")
The total is 5 129,50 €.

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

monepy-0.2.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

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

monepy-0.2-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file monepy-0.2.tar.gz.

File metadata

  • Download URL: monepy-0.2.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for monepy-0.2.tar.gz
Algorithm Hash digest
SHA256 5367f4381a10494fcb879032babfdbce76d899a4430d7fd04e67f9e762171738
MD5 b7b29af127725e49c2dc4bd25ad6510c
BLAKE2b-256 eb5ecf4580e03ab76d9c71bbf1a30c4135a5600083f83c6da7f79dc9654b35e3

See more details on using hashes here.

Provenance

The following attestation bundles were made for monepy-0.2.tar.gz:

Publisher: publish.yml on vsbits/monepy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file monepy-0.2-py3-none-any.whl.

File metadata

  • Download URL: monepy-0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for monepy-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0661109be69fc8bac22f2e31022dc8cc5ee93453fff156de6ed4924d0d4cfb39
MD5 8c1ea65030186e20baacae53578f779b
BLAKE2b-256 7431e081c2ced35d7d3a2fdca9e8e73252e2a4c300b769dde0ef982aa29cda71

See more details on using hashes here.

Provenance

The following attestation bundles were made for monepy-0.2-py3-none-any.whl:

Publisher: publish.yml on vsbits/monepy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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