From b525d0531a9d70844917914a72be450e12574eae Mon Sep 17 00:00:00 2001 From: turulomio Date: Sat, 16 Dec 2023 08:11:30 +0100 Subject: [PATCH] Improved Percentage logic. Raised test coverage to 75% (#38) * Improving percentage logic. Coverage is now 73% * Improving percentage logic. Coverage is now 75% --- pydicts/percentage.py | 79 +++++++++++++++-------------- pydicts/tests/test_percentage.py | 86 +++++++++++++++++++++++++++++++- 2 files changed, 125 insertions(+), 40 deletions(-) diff --git a/pydicts/percentage.py b/pydicts/percentage.py index 7924e34..92b289c 100644 --- a/pydicts/percentage.py +++ b/pydicts/percentage.py @@ -1,72 +1,62 @@ -from logging import warning from decimal import Decimal ## Class to manage percentages in spreadsheets +## Converts values to decimal (Value is a decimal) 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 + + self.setValue(numerator, denominator) def __repr__(self): return self.string() def __neg__(self): - if self.value==None: - return self + if self.value is None: + return Percentage(None) 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