Skip to content

Commit

Permalink
pydicts-0.10.0 (#30)
Browse files Browse the repository at this point in the history
* Moved currency and percentage modules  from unogenerator

* Added basic tests

* Added basic tests

* pydicts-0.10.0
  • Loading branch information
turulomio authored Dec 8, 2023
1 parent 2a0d026 commit 550b4ff
Show file tree
Hide file tree
Showing 11 changed files with 282 additions and 10 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions pydicts.epj
Original file line number Diff line number Diff line change
Expand Up @@ -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": "",
Expand Down Expand Up @@ -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": "",
Expand Down
4 changes: 2 additions & 2 deletions pydicts/__init__.py
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()
143 changes: 143 additions & 0 deletions pydicts/currency.py
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']


6 changes: 3 additions & 3 deletions pydicts/locale/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: TooManyFiles\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-12-04 20:05+0100\n"
"POT-Creation-Date: 2023-12-08 09:39+0100\n"
"PO-Revision-Date: 2020-04-03 08:54+0200\n"
"Last-Translator: trabajo <turulomio@yahoo.es>\n"
"Language-Team: Spanish <kde-i18n-doc@kde.org>\n"
Expand All @@ -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
Expand Down Expand Up @@ -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 ""
Expand Down
Binary file modified pydicts/locale/es/LC_MESSAGES/pydicts.mo
Binary file not shown.
4 changes: 2 additions & 2 deletions pydicts/locale/pydicts.pot
Original file line number Diff line number Diff line change
Expand Up @@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand All @@ -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
Expand Down
115 changes: 115 additions & 0 deletions pydicts/percentage.py
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()
3 changes: 3 additions & 0 deletions pydicts/tests/test_currency.py
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
3 changes: 3 additions & 0 deletions pydicts/tests/test_percentage.py
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <turulomio@yahoo.es>"]
license = "GPL-3.0"
Expand Down

0 comments on commit 550b4ff

Please sign in to comment.