Advanced Python 3.10 Dataclass for handling monetary values, keeping amount and currency together
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file t_money-0.1.1.tar.gz.
File metadata
- Download URL: t_money-0.1.1.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.14 CPython/3.10.7 Linux/5.19.0-38-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
91e789c9627420f4f980be95aa886d493eb7b7066a36644503ebc8a89f77fcd0
|
|
| MD5 |
1c8a3820fb9749ebfbbd16c7484be74d
|
|
| BLAKE2b-256 |
22c3f90b7d1f846042bbf1ea4d75cb5ac3a4f96d6e72db2766a3050e677ff34a
|
File details
Details for the file t_money-0.1.1-py3-none-any.whl.
File metadata
- Download URL: t_money-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.14 CPython/3.10.7 Linux/5.19.0-38-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
67c3ff2200331e0d00fbfe77a4f310f4c82c95dabb82efd5a788c47782815391
|
|
| MD5 |
7f298463147b3d6623ffbc1c4ef18c16
|
|
| BLAKE2b-256 |
845cc50eb36426ed30cc4718437ebe0fe4776cffc38164c27b96ef075e237526
|