diff --git a/price_parser/parser.py b/price_parser/parser.py index 94e9e9c..d65fcf0 100644 --- a/price_parser/parser.py +++ b/price_parser/parser.py @@ -26,7 +26,8 @@ def amount_float(self) -> Optional[float]: @classmethod def fromstring(cls, price: Optional[str], currency_hint: Optional[str] = None, - decimal_separator: Optional[str] = None) -> 'Price': + decimal_separator: Optional[str] = None, + digit_group_separator: Optional[str] = None) -> 'Price': """ Given price and currency text extracted from HTML elements, return ``Price`` instance, which provides a clean currency symbol and @@ -36,15 +37,29 @@ def fromstring(cls, price: Optional[str], which may contain currency, as a hint. If currency is present in ``price`` string, it could be **preferred** over a value extracted from ``currency_hint`` string. + + ``decimal_separator`` is optional; it is used to determine the + decimal separator in price. If ``decimal_separator`` is ``None``, + then it is guessed from ``price`` string. If ``decimal_separator`` + is ``"."``, then ``1.000`` is parsed as ``1``. If it is ``,```, + then ``1.000`` is parsed as ``1000``. + + ``digit_group_separator`` is optional; it is used to determine the + digit group separator in price. If ``digit_group_separator`` is + ``None``, then it is guessed from ``price`` string. If + ``digit_group_separator`` is ``"."``, then ``1.000`` is parsed as + ``1000``. If it is ``,``, then ``1.000`` is parsed as ``1``. """ + currency = extract_currency_symbol(price, currency_hint) + if currency is not None: + currency = currency.strip() + if digit_group_separator: + price = price.replace(digit_group_separator, '') amount_text = extract_price_text(price) if price is not None else None amount_num = ( parse_number(amount_text, decimal_separator) if amount_text is not None else None ) - currency = extract_currency_symbol(price, currency_hint) - if currency is not None: - currency = currency.strip() return Price( amount=amount_num, currency=currency, diff --git a/tests/test_price_parsing.py b/tests/test_price_parsing.py index 42829f7..6895ec9 100644 --- a/tests/test_price_parsing.py +++ b/tests/test_price_parsing.py @@ -27,7 +27,8 @@ def __init__(self, currency: Optional[str], amount_text: Optional[str], amount_float: Optional[Union[float, Decimal]], - decimal_separator: Optional[str] = None) -> None: + decimal_separator: Optional[str] = None, + digit_group_separator: Optional[str] = None) -> None: self.currency_raw = currency_raw self.price_raw = price_raw self.decimal_separator = decimal_separator @@ -68,6 +69,11 @@ def idfn(val): 'GBP', '29.1583', 29.1583), Example(None, '1.11000000000000009770', None, '1.11000000000000009770', Decimal('1.11000000000000009770')), + Example(None, ' 423.923 KD', + 'KD', '423.923', 423.923, decimal_separator='.'), + Example(None, ' 123,456.789 OMR', + 'OMR', '123,456.789', 123456.789, + decimal_separator='.', digit_group_separator=','), ]