mpu.units

Module contents

Handle units - currently only currencies.

class mpu.units.Currency(name, code, numeric_code, symbol, exponent, entities, withdrawal_date, subunits)[source]

Bases: object

Currency base class which contains information similar to ISO 4217.

for_json()

Return a JSON-serializable object.

classmethod from_json(json)[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()

Return a JSON-serializable object.

classmethod from_json(json)[source]

Create a Money object from a JSON dump.

mpu.units.get_currency(currency_str)[source]

Convert an identifier for a currency into a currency object.

Parameters

currency_str (str) –

Returns

currency

Return type

Currency

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