Money dataclass
Advanced Python 3.10 Dataclass for handling monetary values, keeping amount and currency together
This Money class provides a simple and efficient way to manage amounts of money and perform arithmetic operations and comparisons on them. It supports different currencies and ensures that the operations are only performed on matching currencies to avoid inconsistencies.
Features
- Initialize a
Moneyobject with a specific amount and currency. - Perform arithmetic operations between
Moneyobjects with the same currency. - Compare and sort
Moneyobjects. - Convert a
Moneyobject to another currency. - Round a
Moneyobject to a specified number of decimal places. - Serialize and deserialize
Moneyobjects to/from JSON.
Usage
from t_money import Money
Creating a Money object
usd = Money(Decimal("10.50"), "USD")
Create a Money object from string
usd_from_str = Money.from_str("USD 5.25")
Arithmetic operations
usd1 = Money(Decimal("10.50"), "USD")
usd2 = Money(Decimal("5.50"), "USD")
usd_sum = usd1 + usd2
usd_difference = usd1 - usd2
usd_product = usd1 * Decimal("2")
Comparisons
print(usd1 == usd2)
print(usd1 < usd2)
Rounding
rounded_usd = usd1.round_to(1)
Currency conversion
eur = usd.convert("EUR", Decimal("0.85"))
Money is hashable
money_set = {usd1, usd2}
Serialization and deserialization
import json
# Serialize data containing Money object
data = {'money': usd}
json_str = json.dumps(data, default=money_serializer)
# Deserialize JSON data to Money object
loaded_data = json.loads(json_str, object_hook=money_deserializer)
Error Handling
When performing operations on Money objects with different currencies, a DifferentCurrencyError will be raised. Make sure to handle this error if necessary when dealing with multiple currencies.
Release files for t-money 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| t_money-0.1.1.tar.gz | 5.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| t_money-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.6 kB
Release files / t_money-0.1.1.tar.gz
| Download URL | t_money-0.1.1.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
91e789c9627420f4f980be95aa886d493eb7b7066a36644503ebc8a89f77fcd0
|
|
BLAKE2b-256 checksum How to use checksums |
22c3f90b7d1f846042bbf1ea4d75cb5ac3a4f96d6e72db2766a3050e677ff34a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.14 CPython/3.10.7 Linux/5.19.0-38-generic
|
Release files / t_money-0.1.1-py3-none-any.whl
| Download URL | t_money-0.1.1-py3-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
67c3ff2200331e0d00fbfe77a4f310f4c82c95dabb82efd5a788c47782815391
|
|
BLAKE2b-256 checksum How to use checksums |
845cc50eb36426ed30cc4718437ebe0fe4776cffc38164c27b96ef075e237526
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.14 CPython/3.10.7 Linux/5.19.0-38-generic
|