-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Moved currency and percentage modules from unogenerator * Added basic tests * Added basic tests * pydicts-0.10.0
- Loading branch information
Showing
11 changed files
with
282 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from datetime import datetime | ||
__version__="0.9.0" | ||
__versiondatetime__= datetime(2023, 12, 4, 20, 0) | ||
__version__="0.10.0" | ||
__versiondatetime__= datetime(2023, 12, 8, 9, 37) | ||
__versiondate__=__versiondatetime__.date() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
from logging import error | ||
from sys import exit | ||
from decimal import Decimal | ||
|
||
## Class to manage currencies in officegenerator | ||
## | ||
## The symbol is defined by code with self.symbol() | ||
class Currency: | ||
def __init__(self, amount=None, currency=None) : | ||
if amount==None: | ||
self.amount=Decimal(0) | ||
else: | ||
self.amount=Decimal(str(amount)) | ||
if currency==None: | ||
self.currency='EUR' | ||
else: | ||
self.currency=currency | ||
|
||
|
||
def __add__(self, money): | ||
"""Si las divisas son distintas, queda el resultado con la divisa del primero""" | ||
if self.currency==money.currency: | ||
return self.__class__(self.amount+money.amount, self.currency) | ||
else: | ||
error("Before adding, please convert to the same currency") | ||
raise "OdfMoneyOperationException" | ||
|
||
def __sub__(self, money): | ||
"""Si las divisas son distintas, queda el resultado con la divisa del primero""" | ||
if self.currency==money.currency: | ||
return self.__class__(self.amount-money.amount, self.currency) | ||
else: | ||
error("Before substracting, please convert to the same currency") | ||
raise "CurrencyOperationException" | ||
|
||
def __lt__(self, money): | ||
if self.currency==money.currency: | ||
if self.amount < money.amount: | ||
return True | ||
return False | ||
else: | ||
error("Before lt ordering, please convert to the same currency") | ||
exit(1) | ||
|
||
## Si las divisas son distintas, queda el resultado con la divisa del primero | ||
## | ||
## En caso de querer multiplicar por un numero debe ser despues. For example: money*4 | ||
def __mul__(self, money): | ||
if money.__class__.__name__ in ("int", "float", "Decimal"): | ||
return self.__class__(self.amount*money, self.currency) | ||
if self.currency==money.currency: | ||
return self.__class__(self.amount*money.amount, self.currency) | ||
else: | ||
error("Before multiplying, please convert to the same currency") | ||
exit(1) | ||
|
||
def __truediv__(self, money): | ||
"""Si las divisas son distintas, queda el resultado con la divisa del primero""" | ||
if self.currency==money.currency: | ||
return self.__class__(self.amount/money.amount, self.currency) | ||
else: | ||
error("Before true dividing, please convert to the same currency") | ||
exit(1) | ||
|
||
def __repr__(self): | ||
return self.string(2) | ||
|
||
## Returs a typical currency string | ||
## @param digits int that defines the number of decimals. 2 by default | ||
## @return string | ||
def string(self, digits=2): | ||
return "{} {}".format(round(self.amount, digits), currency_symbol(self.currency)) | ||
|
||
def isZero(self): | ||
if self.amount==Decimal(0): | ||
return True | ||
else: | ||
return False | ||
|
||
def isGETZero(self): | ||
if self.amount>=Decimal(0): | ||
return True | ||
else: | ||
return False | ||
|
||
def isGTZero(self): | ||
if self.amount>Decimal(0): | ||
return True | ||
else: | ||
return False | ||
|
||
def isLTZero(self): | ||
if self.amount<Decimal(0): | ||
return True | ||
else: | ||
return False | ||
|
||
def isLETZero(self): | ||
if self.amount<=Decimal(0): | ||
return True | ||
else: | ||
return False | ||
|
||
def __neg__(self): | ||
"""Devuelve otro money con el amount con signo cambiado""" | ||
return self.__class__(-self.amount, self.currency) | ||
|
||
def round(self, digits=2): | ||
return round(self.amount, digits) | ||
|
||
|
||
## Returns the symbol of the currency | ||
def currency_symbol(currency): | ||
if currency=="EUR": | ||
return "€" | ||
elif currency=="USD": | ||
return "$" | ||
elif currency in ["CNY", "JPY"]: | ||
return "¥" | ||
elif currency=="GBP": | ||
return "£" | ||
elif currency=="u": | ||
return "u"## Returns the symbol of the currency | ||
|
||
def currency_name(name): | ||
if name=="EUR": | ||
return "Euro" | ||
elif name=="USD": | ||
return "American Dolar" | ||
elif name=="CNY": | ||
return "Chinese Yoan" | ||
elif name=="JPY": | ||
return "Japanes Yen" | ||
elif name=="GBP": | ||
return "Pound" | ||
elif name=="u": | ||
return "Unit" | ||
|
||
|
||
def MostCommonCurrencyTypes(): | ||
return ['CNY', 'EUR', 'GBP', 'JPY', 'USD', 'u'] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
from logging import warning | ||
from decimal import Decimal | ||
|
||
## Class to manage percentages in spreadsheets | ||
class Percentage: | ||
def __init__(self, numerator=None, denominator=None): | ||
self.setValue(self.toDecimal(numerator),self.toDecimal(denominator)) | ||
|
||
def toDecimal(self, o): | ||
if o==None: | ||
return o | ||
if o.__class__.__name__ in ["Currency", "Money"]: | ||
return o.amount | ||
elif o.__class__.__name__=="Decimal": | ||
return o | ||
elif o.__class__.__name__ in ["int", "float"]: | ||
return Decimal(o) | ||
elif o.__class__.__name__=="Percentage": | ||
return o.value | ||
else: | ||
warning (o.__class__.__name__) | ||
return None | ||
|
||
def __repr__(self): | ||
return self.string() | ||
|
||
def __neg__(self): | ||
if self.value==None: | ||
return self | ||
return Percentage(-self.value, 1) | ||
|
||
def __lt__(self, other): | ||
if self.value==None: | ||
value1=Decimal('-Infinity') | ||
else: | ||
value1=self.value | ||
if other.value==None: | ||
value2=Decimal('-Infinity') | ||
else: | ||
value2=other.value | ||
if value1<value2: | ||
return True | ||
return False | ||
|
||
|
||
|
||
def __add__(self,p): | ||
return self.__class__(self.value+p.value,1) | ||
|
||
def __sub__(self, p): | ||
return self.__class__(self.value-p.value,1) | ||
|
||
def __mul__(self, value): | ||
if self.value==None or value==None: | ||
r=None | ||
else: | ||
r=self.value*self.toDecimal(value) | ||
return Percentage(r, 1) | ||
|
||
def __truediv__(self, value): | ||
try: | ||
r=self.value/self.toDecimal(value) | ||
except: | ||
r=None | ||
return Percentage(r, 1) | ||
|
||
def setValue(self, numerator, denominator): | ||
try: | ||
self.value=Decimal(numerator/denominator) | ||
except: | ||
self.value=None | ||
|
||
def value_100(self): | ||
if self.value==None: | ||
return None | ||
else: | ||
return self.value*Decimal(100) | ||
|
||
## @return percentage float value | ||
def float_100(self): | ||
return float(self.value_100()) | ||
|
||
def string(self, rnd=2): | ||
if self.value==None: | ||
return "None %" | ||
return "{} %".format(round(self.value_100(), rnd)) | ||
|
||
## Returns if the percentage is valid. I mean it's value different of None | ||
def isValid(self): | ||
if self.value!=None: | ||
return True | ||
return False | ||
|
||
def isGETZero(self): | ||
if self.isValid() and self.value>=0: | ||
return True | ||
return False | ||
|
||
def isGTZero(self): | ||
if self.isValid() and self.value>0: | ||
return True | ||
return False | ||
|
||
def isLTZero(self): | ||
if self.isValid() and self.value<0: | ||
return True | ||
return False | ||
|
||
## Calculates porcentage to pass from a to b | ||
## @param a. Can be an object divisible and that can be substracted | ||
def percentage_between(a,b): | ||
try: | ||
return Percentage(b-a,a) | ||
except: | ||
return Percentage() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from pydicts.currency import Currency | ||
def test_currency(): | ||
assert Currency(None).amount== 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from pydicts.percentage import Percentage | ||
def test_percentage(): | ||
assert Percentage(None).value== None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters