# 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 `Money` object with a specific amount and currency.
- Perform arithmetic operations between `Money` objects with the same currency.
- Compare and sort `Money` objects.
- Convert a `Money` object to another currency.
- Round a `Money` object to a specified number of decimal places.
- Serialize and deserialize `Money` objects to/from JSON.
## Usage
from t_money import Money
### Creating a Money object
```python
usd = Money(Decimal("10.50"), "USD")
```
### Create a Money object from string
```python
usd_from_str = Money.from_str("USD 5.25")
```
### Arithmetic operations
```python
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
```python
print(usd1 == usd2)
print(usd1 < usd2)
```
### Rounding
```python
rounded_usd = usd1.round_to(1)
```
### Currency conversion
```python
eur = usd.convert("EUR", Decimal("0.85"))
```
### Money is hashable
```python
money_set = {usd1, usd2}
```
### Serialization and deserialization
```python
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.
Raw data
{
"_id": null,
"home_page": "https://github.com/jordan-dimov/t_money",
"name": "t-money",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.10,<4.0",
"maintainer_email": "",
"keywords": "money,currency,finance,accounting",
"author": "Jordan Dimov",
"author_email": "jdimov@a115.co.uk",
"download_url": "https://files.pythonhosted.org/packages/22/c3/f90b7d1f846042bbf1ea4d75cb5ac3a4f96d6e72db2766a3050e677ff34a/t_money-0.1.1.tar.gz",
"platform": null,
"description": "# Money dataclass\nAdvanced Python 3.10 Dataclass for handling monetary values, keeping amount and currency together\n\nThis `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.\n\n## Features\n\n- Initialize a `Money` object with a specific amount and currency.\n- Perform arithmetic operations between `Money` objects with the same currency.\n- Compare and sort `Money` objects.\n- Convert a `Money` object to another currency.\n- Round a `Money` object to a specified number of decimal places.\n- Serialize and deserialize `Money` objects to/from JSON.\n\n## Usage\n\n from t_money import Money\n\n### Creating a Money object\n\n```python\nusd = Money(Decimal(\"10.50\"), \"USD\")\n```\n\n### Create a Money object from string\n\n```python\nusd_from_str = Money.from_str(\"USD 5.25\")\n```\n\n### Arithmetic operations\n\n```python\nusd1 = Money(Decimal(\"10.50\"), \"USD\")\nusd2 = Money(Decimal(\"5.50\"), \"USD\")\n\nusd_sum = usd1 + usd2\nusd_difference = usd1 - usd2\nusd_product = usd1 * Decimal(\"2\")\n```\n\n### Comparisons\n\n```python\nprint(usd1 == usd2)\nprint(usd1 < usd2)\n```\n\n### Rounding\n\n```python\nrounded_usd = usd1.round_to(1)\n```\n\n### Currency conversion\n\n```python\neur = usd.convert(\"EUR\", Decimal(\"0.85\"))\n```\n\n### Money is hashable\n\n```python\nmoney_set = {usd1, usd2}\n```\n\n### Serialization and deserialization\n\n```python\nimport json\n\n# Serialize data containing Money object\ndata = {'money': usd}\njson_str = json.dumps(data, default=money_serializer)\n\n# Deserialize JSON data to Money object\nloaded_data = json.loads(json_str, object_hook=money_deserializer)\n```\n\n## Error Handling\n\nWhen 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.\n\n\n",
"bugtrack_url": null,
"license": "Creative Commons",
"summary": "Advanced Python 3.10 Dataclass for handling monetary values, keeping amount and currency together",
"version": "0.1.1",
"split_keywords": [
"money",
"currency",
"finance",
"accounting"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "845cc50eb36426ed30cc4718437ebe0fe4776cffc38164c27b96ef075e237526",
"md5": "7f298463147b3d6623ffbc1c4ef18c16",
"sha256": "67c3ff2200331e0d00fbfe77a4f310f4c82c95dabb82efd5a788c47782815391"
},
"downloads": -1,
"filename": "t_money-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "7f298463147b3d6623ffbc1c4ef18c16",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10,<4.0",
"size": 5748,
"upload_time": "2023-04-09T20:55:38",
"upload_time_iso_8601": "2023-04-09T20:55:38.569474Z",
"url": "https://files.pythonhosted.org/packages/84/5c/c50eb36426ed30cc4718437ebe0fe4776cffc38164c27b96ef075e237526/t_money-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "22c3f90b7d1f846042bbf1ea4d75cb5ac3a4f96d6e72db2766a3050e677ff34a",
"md5": "1c8a3820fb9749ebfbbd16c7484be74d",
"sha256": "91e789c9627420f4f980be95aa886d493eb7b7066a36644503ebc8a89f77fcd0"
},
"downloads": -1,
"filename": "t_money-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "1c8a3820fb9749ebfbbd16c7484be74d",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10,<4.0",
"size": 5888,
"upload_time": "2023-04-09T20:55:40",
"upload_time_iso_8601": "2023-04-09T20:55:40.620731Z",
"url": "https://files.pythonhosted.org/packages/22/c3/f90b7d1f846042bbf1ea4d75cb5ac3a4f96d6e72db2766a3050e677ff34a/t_money-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-04-09 20:55:40",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "jordan-dimov",
"github_project": "t_money",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "t-money"
}