Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ you can set it directly:
Price(amount=Decimal('1000'), currency='EUR')


Decimal separator
-----------------

If you know which symbol is used as a decimal separator in the input string,
pass that symbol in the ``decimal_separator`` argument to prevent price-parser
from guessing the wrong decimal separator symbol.

>>> Price.fromstring("Price: $140.600", decimal_separator=".")
Price(amount=Decimal('140.600'), currency='$')

>>> Price.fromstring("Price: $140.600", decimal_separator=",")
Price(amount=Decimal('140600'), currency='$')


Contributing
============

Expand Down
25 changes: 17 additions & 8 deletions price_parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def amount_float(self) -> Optional[float]:

@classmethod
def fromstring(cls, price: Optional[str],
currency_hint: Optional[str] = None) -> 'Price':
currency_hint: Optional[str] = None,
decimal_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 @@ -37,7 +38,10 @@ def fromstring(cls, price: Optional[str],
from ``currency_hint`` string.
"""
amount_text = extract_price_text(price) if price is not None else None
amount_num = parse_number(amount_text) if amount_text 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()
Expand Down Expand Up @@ -183,15 +187,15 @@ def extract_price_text(price: str) -> Optional[str]:
if price.count('€') == 1:
m = re.search(r"""
[\d\s.,]*?\d # number, probably with thousand separators
\s*?€\s*? # euro, probably separated by whitespace
\d\d
(?:$|[^\d]) # something which is not a digit
\s*?€(\s*?)? # euro, probably separated by whitespace
\d(?(1)\d|\d*?) # if separated by whitespace - search one digit, multiple digits otherwise
(?:$|[^\d]) # something which is not a digit
""", price, re.VERBOSE)
if m:
return m.group(0).replace(' ', '')
m = re.search(r"""
(\d[\d\s.,]*) # number, probably with thousand separators
\s*? # skip whitespace
\s*? # skip whitespace
(?:[^%\d]|$) # capture next symbol - it shouldn't be %
""", price, re.VERBOSE)

Expand Down Expand Up @@ -234,7 +238,8 @@ def get_decimal_separator(price: str) -> Optional[str]:
return m.group(1)


def parse_number(num: str) -> Optional[Decimal]:
def parse_number(num: str,
decimal_separator: Optional[str] = None) -> Optional[Decimal]:
""" Parse a string with a number to a Decimal, guessing its format:
decimal separator, thousand separator. Return None if parsing fails.

Expand Down Expand Up @@ -262,13 +267,17 @@ def parse_number(num: str) -> Optional[Decimal]:
Decimal('1235.99')
>>> parse_number("1.235€99")
Decimal('1235.99')
>>> parse_number("140.000", decimal_separator=",")
Decimal('140000')
>>> parse_number("140.000", decimal_separator=".")
Decimal('140.000')
>>> parse_number("")
>>> parse_number("foo")
"""
if not num:
return None
num = num.strip().replace(' ', '')
decimal_separator = get_decimal_separator(num)
decimal_separator = decimal_separator or get_decimal_separator(num)
# NOTE: Keep supported separators in sync with _search_decimal_sep
if decimal_separator is None:
num = num.replace('.', '').replace(',', '')
Expand Down
36 changes: 34 additions & 2 deletions tests/test_price_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ def __init__(self,
price_raw: Optional[str],
currency: Optional[str],
amount_text: Optional[str],
amount_float: Optional[Union[float, Decimal]]) -> None:
amount_float: Optional[Union[float, Decimal]],
decimal_separator: Optional[str] = None) -> None:
self.currency_raw = currency_raw
self.price_raw = price_raw
self.decimal_separator = decimal_separator
amount_decimal = None # type: Optional[Decimal]
if isinstance(amount_float, Decimal):
amount_decimal = amount_float
Expand Down Expand Up @@ -1960,6 +1962,16 @@ def __eq__(self, other):
]


PRICE_PARSING_DECIMAL_SEPARATOR_EXAMPLES = [
Example(None, '1250€ 600',
'€', '1250', 1250, "€"),
Example(None, '1250€ 60',
'€', '1250€60', 1250.60, "€"),
Example(None, '1250€600',
'€', '1250€600', 1250.600, "€"),
]


@pytest.mark.parametrize(
["example"],
[[e] for e in PRICE_PARSING_EXAMPLES_BUGS_CAUGHT] +
Expand All @@ -1969,11 +1981,12 @@ def __eq__(self, other):
[[e] for e in PRICE_PARSING_EXAMPLES_3] +
[[e] for e in PRICE_PARSING_EXAMPLES_NO_PRICE] +
[[e] for e in PRICE_PARSING_EXAMPLES_NO_CURRENCY] +
[[e] for e in PRICE_PARSING_DECIMAL_SEPARATOR_EXAMPLES] +
[pytest.param(e, marks=pytest.mark.xfail())
for e in PRICE_PARSING_EXAMPLES_XFAIL]
)
def test_parsing(example: Example):
parsed = Price.fromstring(example.price_raw, example.currency_raw)
parsed = Price.fromstring(example.price_raw, example.currency_raw, example.decimal_separator)
assert parsed == example, f"Failed scenario: price={example.price_raw}, currency_hint={example.currency_raw}"


Expand All @@ -1986,3 +1999,22 @@ def test_parsing(example: Example):
)
def test_price_amount_float(amount, amount_float):
assert Price(amount, None, None).amount_float == amount_float


@pytest.mark.parametrize(
"price_raw,decimal_separator,expected_result",
(
("140.000", None, Decimal("140000")),
("140.000", ",", Decimal("140000")),
("140.000", ".", Decimal("140.000")),
("140€33", "€", Decimal("140.33")),
("140,000€33", "€", Decimal("140000.33")),
("140.000€33", "€", Decimal("140000.33")),
)
)
def test_price_decimal_separator(price_raw, decimal_separator, expected_result):
parsed = Price.fromstring(
price_raw,
decimal_separator=decimal_separator
)
assert parsed.amount == expected_result