diff --git a/README.md b/README.md index a4b25ba..41e97af 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,10 @@ You can access documentation in [Github Pydicts Wiki](https://github.com/turulom ## CHANGELOG +### 0.10.0 (2023-12-08) +- Improving str2decimal conversions. Changed type parameter to decimal_separator. +- Added Percentage and Currency classes to manage this objects + ### 0.9.0 (2023-12-04) - Added gettext support - Improved spanish translation diff --git a/pydicts.epj b/pydicts.epj index b465feb..3ee5a81 100644 --- a/pydicts.epj +++ b/pydicts.epj @@ -2,7 +2,7 @@ "header": { "comment": "eric project file for project pydicts", "copyright": "Copyright (C) 2023 , ", - "saved": "2023-12-04, 19:59:27" + "saved": "2023-12-08, 09:36:20" }, "project": { "AUTHOR": "", @@ -76,20 +76,24 @@ "pydicts/__init__.py", "pydicts/casts.py", "pydicts/classes.py", - "pydicts/devscripts.py", + "pydicts/currency.py", "pydicts/exceptions.py", "pydicts/lod.py", "pydicts/lod_xyv.py", "pydicts/lod_ymv.py", "pydicts/lol.py", "pydicts/myjsonencoder.py", + "pydicts/percentage.py", + "pydicts/poethepoet.py", "pydicts/pylatex.py", "pydicts/tests/__init__.py", "pydicts/tests/test_casts.py", + "pydicts/tests/test_currency.py", "pydicts/tests/test_lod.py", "pydicts/tests/test_lod_ymv.py", "pydicts/tests/test_lol.py", "pydicts/tests/test_myjsonencoder.py", + "pydicts/tests/test_percentage.py", "pydicts/tests/test_pylatex.py" ], "SOURCESDIR": "", diff --git a/pydicts/__init__.py b/pydicts/__init__.py index 10f9814..abab977 100644 --- a/pydicts/__init__.py +++ b/pydicts/__init__.py @@ -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() diff --git a/pydicts/currency.py b/pydicts/currency.py new file mode 100644 index 0000000..7097743 --- /dev/null +++ b/pydicts/currency.py @@ -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\n" "Language-Team: Spanish \n" @@ -20,7 +20,7 @@ msgstr "" "X-Generator: Lokalize 19.12.3\n" #, python-brace-format -msgid "Method str2decimal couln't convert {0} ({1}) to a Decimal" +msgid "Method str2decimal couln't convert {0} ({1}), after changing it to {2} to a Decimal" msgstr "" #, python-brace-format @@ -58,7 +58,7 @@ msgid "I can't traspaso a None object" msgstr "" msgid "lol_print: This list of lists hasn't data to print" -msgstr "" +msgstr "lol_print: Esta lista de listas no tiene data para imprimir" msgid "lol_print: No data was printed due to you selected 0 rows" msgstr "" diff --git a/pydicts/locale/es/LC_MESSAGES/pydicts.mo b/pydicts/locale/es/LC_MESSAGES/pydicts.mo index 4f8dddc..d086462 100644 Binary files a/pydicts/locale/es/LC_MESSAGES/pydicts.mo and b/pydicts/locale/es/LC_MESSAGES/pydicts.mo differ diff --git a/pydicts/locale/pydicts.pot b/pydicts/locale/pydicts.pot index 974bf11..9e357b4 100644 --- a/pydicts/locale/pydicts.pot +++ b/pydicts/locale/pydicts.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2023-12-07 17:52+0100\n" +"POT-Creation-Date: 2023-12-08 09:40+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -18,7 +18,7 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" #, python-brace-format -msgid "Method str2decimal couln't convert {0} ({1}) to a Decimal" +msgid "Method str2decimal couln't convert {0} ({1}), after changing it to {2} to a Decimal" msgstr "" #, python-brace-format diff --git a/pydicts/percentage.py b/pydicts/percentage.py new file mode 100644 index 0000000..7924e34 --- /dev/null +++ b/pydicts/percentage.py @@ -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=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() diff --git a/pydicts/tests/test_currency.py b/pydicts/tests/test_currency.py new file mode 100644 index 0000000..91617f9 --- /dev/null +++ b/pydicts/tests/test_currency.py @@ -0,0 +1,3 @@ +from pydicts.currency import Currency +def test_currency(): + assert Currency(None).amount== 0 diff --git a/pydicts/tests/test_percentage.py b/pydicts/tests/test_percentage.py new file mode 100644 index 0000000..d9d5c8f --- /dev/null +++ b/pydicts/tests/test_percentage.py @@ -0,0 +1,3 @@ +from pydicts.percentage import Percentage +def test_percentage(): + assert Percentage(None).value== None diff --git a/pyproject.toml b/pyproject.toml index ad1296a..5cfa2cf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pydicts" -version = "0.9.0" +version = "0.10.0" description = "Module to use dictionaries in various situations" authors = ["turulomio "] license = "GPL-3.0"