forked from stefan-jansen/zipline-reloaded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrency.py
75 lines (60 loc) · 1.76 KB
/
currency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from functools import total_ordering
from iso4217 import Currency as ISO4217Currency
_ALL_CURRENCIES = {}
@total_ordering
class Currency:
"""A currency identifier, as defined by ISO-4217.
Parameters
----------
code : str
ISO-4217 code for the currency.
Attributes
----------
code : str
ISO-4217 currency code for the currency, e.g., 'USD'.
name : str
Plain english name for the currency, e.g., 'US Dollar'.
"""
def __new__(cls, code):
try:
return _ALL_CURRENCIES[code]
except KeyError:
if code is None:
name = "NO CURRENCY"
else:
try:
name = ISO4217Currency(code).currency_name
except ValueError as exc:
raise ValueError(
"{!r} is not a valid currency code.".format(code)
) from exc
obj = _ALL_CURRENCIES[code] = super(Currency, cls).__new__(cls)
obj._code = code
obj._name = name
return obj
@property
def code(self):
"""ISO-4217 currency code for the currency.
Returns
-------
code : str
"""
return self._code
@property
def name(self):
"""Plain english name for the currency.
Returns
-------
name : str
"""
return self._name
def __eq__(self, other):
if type(self) != type(other):
return NotImplemented
return self.code == other.code
def __hash__(self):
return hash(self.code)
def __lt__(self, other):
return self.code < other.code
def __repr__(self):
return "{}({!r})".format(type(self).__name__, self.code)