Skip to content

Commit

Permalink
Add a digit_group_separator parameter (#59)
Browse files Browse the repository at this point in the history
Co-authored-by: Emmanuel Rondan <emarondan@pop-os.localdomain>
  • Loading branch information
emarondan and Emmanuel Rondan committed Oct 17, 2023
1 parent 18c5ed8 commit bfd1f14
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
23 changes: 19 additions & 4 deletions price_parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion tests/test_price_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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=','),
]


Expand Down

0 comments on commit bfd1f14

Please sign in to comment.