mpu.units
Module contents
Handle units - currently only currencies.
- class mpu.units.Currency(name: str, code: str, numeric_code: str, symbol: str, exponent: Optional[int], entities: Optional[List], withdrawal_date: Optional[str], subunits: Optional[str])[source]
Bases:
object
Currency base class which contains information similar to ISO 4217.
- for_json() Dict[str, Any]
Return a JSON-serializable object.
- classmethod from_json(json: Dict) mpu.units.Currency [source]
Create a Currency object from a JSON dump.
- class mpu.units.Money(value: Union[str, fractions.Fraction, int, Tuple], currency: Union[str, mpu.units.Currency])[source]
Bases:
object
Unit of account.
- Parameters
value (Union[str, fractions.Fraction, int, Tuple]) –
currency (Currency or str) –
Examples
>>> rent = Money(500, 'USD') >>> '{:.2f,shortcode}'.format(rent) 'USD 500.00' >>> '{:.2f,postshortcode}'.format(rent) '500.00 USD' >>> '{:.2f,symbol}'.format(rent) '$500.00' >>> '{:.2f,postsymbol}'.format(rent) '500.00$' >>> '{:.2f}'.format(rent) '500.00 USD'
- for_json() Dict[str, Any]
Return a JSON-serializable object.
- classmethod from_json(json: Dict[str, Any]) mpu.units.Money [source]
Create a Money object from a JSON dump.
- mpu.units.get_currency(currency_str: str) mpu.units.Currency [source]
Convert an identifier for a currency into a currency object.
- Parameters
currency_str (str) –
- Returns
currency
- Return type
Allowed operations with Money
Here you can see which operations are allowed by two Money objects of currencies (A and B):
Money A |
Operator |
Money A |
Money B |
int, Fraction |
---|---|---|---|---|
+ , - |
Money A |
N/A |
N/A |
|
* |
N/A |
N/A |
Money A |
|
/ |
N/A |
N/A |
N/A |
|
// |
Fraction |
N/A |
Money A |
|
>, >=, <, <= |
Bool |
N/A |
N/A |
|
== |
Bool |
False |
False |