Skip to content

Commit 9ab6112

Browse files
authored
Merge pull request #8 from scrapinghub/more-digits2
allow arbitrary precision
2 parents 06b38a7 + 6241b60 commit 9ab6112

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

price_parser/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def extract_price_text(price: str) -> Optional[str]:
207207
([.,€]) # decimal separator
208208
(?: # 1,2 or 4+ digits. 3 digits is likely to be a thousand separator.
209209
\d{1,2}|
210-
\d{4,8}
210+
\d{4}\d*
211211
)
212212
$
213213
""", re.VERBOSE).search

tests/test_price_parsing.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
we've found in a wild; PRICE_PARSING_EXAMPLES_NEW is a list of tests for
1212
new features. New tests should probably go these two lists.
1313
"""
14-
from typing import Optional
14+
from typing import Optional, Union
1515
from decimal import Decimal
1616

1717
import pytest
@@ -26,11 +26,13 @@ def __init__(self,
2626
price_raw: Optional[str],
2727
currency: Optional[str],
2828
amount_text: Optional[str],
29-
amount_float: Optional[float]) -> None:
29+
amount_float: Optional[Union[float, Decimal]]) -> None:
3030
self.currency_raw = currency_raw
3131
self.price_raw = price_raw
3232
amount_decimal = None # type: Optional[Decimal]
33-
if amount_float is not None:
33+
if isinstance(amount_float, Decimal):
34+
amount_decimal = amount_float
35+
elif amount_float is not None:
3436
# don't use Decimal(amount_float), as this is not what
3537
# one usually means, because of float precision
3638
amount_decimal = Decimal(str(amount_float))
@@ -57,6 +59,8 @@ def __eq__(self, other):
5759
'GBP', '34.992001', 34.992001),
5860
Example('GBP', '29.1583',
5961
'GBP', '29.1583', 29.1583),
62+
Example(None, '1.11000000000000009770',
63+
None, '1.11000000000000009770', Decimal('1.11000000000000009770')),
6064
]
6165

6266

0 commit comments

Comments
 (0)