Skip to main content

Currency classes to work with monetary values

Project description

Monepy

tests Codecov

License: MIT

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

Install

From github using pip:

pip install git+https://github.com/vsbits/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.1.1.tar.gz (10.3 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.1.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: monepy-0.1.1.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for monepy-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9f689d306b6d0eaab5b82b68acb1d492913fae7704df86e8382854dea3964c78
MD5 0a5eb82118882a09dceefca6c4ea9684
BLAKE2b-256 a0a7817a8b3d0500ea7b3d35779fb340ee1976cb9f4c1e6979e5ee9924d43d31

See more details on using hashes here.

File details

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

File metadata

  • Download URL: monepy-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for monepy-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 67e08b51c1b6c812aceecd4b79b3c50ae9328b1c4b5d12e6021ad4e45b8a2969
MD5 66e9eebf85814abd68a7e598269dc85e
BLAKE2b-256 c7d7864b9916c9d89976ef9bd8b2d6b36d97d94010f92bc928fd92931bd608f9

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