1010from enum import Enum
1111
1212class CurrencyFormat (Enum ):
13+ """An Enum representing different formats of Currency in coda.
14+
15+ Constants:
16+ WHOLE - represents whole coda (1 whole coda == 10^9 nanocodas)
17+ NANO - represents the atomic unit of coda
18+ """
1319 WHOLE = 1
1420 NANO = 2
1521
1622class CurrencyUnderflow (Exception ):
1723 pass
1824
1925class Currency ():
26+ """A convenience wrapper around interacting with coda currency values.
27+
28+ This class supports performing math on Currency values of differing formats.
29+ Currency instances can be added or subtracted. Currency instances can also be
30+ scaled through multiplication (either against another Currency instance or a
31+ int scalar).
32+ """
33+
2034 @classmethod
2135 def __nanocodas_from_int (_cls , n ):
2236 return n * 1000000000
@@ -35,6 +49,15 @@ def __nanocodas_from_string(_cls, s):
3549
3650 @classmethod
3751 def random (_cls , lower_bound , upper_bound ):
52+ """Generates a random Currency instance between a provided lower_bound and upper_bound
53+
54+ Arguments:
55+ lower_bound {Currency} -- A Currency instance representing the lower bound for the randomly generated value
56+ upper_bound {Currency} -- A Currency instance representing the upper bound for the randomly generated value
57+
58+ Returns:
59+ Currency - A randomly generated Currency instance between the lower_bound and upper_bound
60+ """
3861 if not (isinstance (lower_bound , Currency ) and isinstance (upper_bound , Currency )):
3962 raise Exception ('invalid call to Currency.random: lower and upper bound must be instances of Currency' )
4063 if not upper_bound .nanocodas () >= lower_bound .nanocodas ():
@@ -46,6 +69,18 @@ def random(_cls, lower_bound, upper_bound):
4669 return lower_bound + Currency (delta , format = CurrencyFormat .NANO )
4770
4871 def __init__ (self , value , format = CurrencyFormat .WHOLE ):
72+ """Constructs a new Currency instance. Values of different CurrencyFormats may be passed in to construct the instance.
73+
74+ Arguments:
75+ value {int|float|string} - The value to construct the Currency instance from
76+ format {CurrencyFormat} - The representation format of the value
77+
78+ Return:
79+ Currency - The newly constructed Currency instance
80+
81+ In the case of format=CurrencyFormat.WHOLE, then it is interpreted as value * 10^9 nanocodas.
82+ In the case of format=CurrencyFormat.NANO, value is only allowed to be an int, as there can be no decimal point for nanocodas.
83+ """
4984 if format == CurrencyFormat .WHOLE :
5085 if isinstance (value , int ):
5186 self .__nanocodas = Currency .__nanocodas_from_int (value )
@@ -64,13 +99,23 @@ def __init__(self, value, format=CurrencyFormat.WHOLE):
6499 raise Exception ('invalid Currency format %s' % format )
65100
66101 def decimal_format (self ):
102+ """Computes the string decimal format representation of a Currency instance.
103+
104+ Return:
105+ str - The decimal format representation of the Currency instance
106+ """
67107 s = str (self .__nanocodas )
68108 if len (s ) > 9 :
69109 return s [:- 9 ] + '.' + s [- 9 :]
70110 else :
71111 return '0.' + ('0' * (9 - len (s ))) + s
72112
73113 def nanocodas (self ):
114+ """Accesses the raw nanocodas representation of a Currency instance.
115+
116+ Return:
117+ int - The nanocodas of the Currency instance represented as an integer
118+ """
74119 return self .__nanocodas
75120
76121 def __str__ (self ):
0 commit comments