Skip to content

Commit

Permalink
Handle more poorly named products
Browse files Browse the repository at this point in the history
  • Loading branch information
Javex committed Nov 10, 2023
1 parent 6b788ce commit a8c2f35
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
33 changes: 19 additions & 14 deletions hotprices_au/sites/coles.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,26 @@ def get_quantity_and_unit(item):
def parse_str_unit(size):
# Try coles-special methods before going to the generic function
size = size.lower()
matched = re.match(r'^.* (?P<quantity>[0-9]+)(?P<unit>[a-z]+):(pack(?P<count>[0-9]+)|(?P<each>ea))', size)
if matched:
quantity = float(matched.group('quantity'))
unit = matched.group('unit')
count_match = matched.group('count')
if count_match:
count = float(count_match)
else:
each_str = matched.group('each')
if each_str:
count = 1
matches = [
re.match(r'^.* (?P<quantity>[0-9]+)(?P<unit>[a-z]+):(pack(?P<count>[0-9]+)|(?P<each>ea))', size),
re.match(r'^.* (?P<count>[0-9]+)pk can (?P<quantity>[0-9]+)(?P<unit>[a-z]+)', size),
re.match(r'^.* (?P<quantity>[0-9]+)(?P<unit>[a-z]+) \((?P<count>[0-9]+)pk\)', size),
]
for matched in matches:
if matched:
quantity = float(matched.group('quantity'))
unit = matched.group('unit')
count_match = matched.group('count')
if count_match:
count = float(count_match)
else:
raise RuntimeError("Didn't get a count, expected 'each'/'ea'")
quantity *= count
return quantity, unit
each_str = matched.group('each')
if each_str:
count = 1
else:
continue
quantity *= count
return quantity, unit
else:
return units.parse_str_unit(size)

Expand Down
12 changes: 12 additions & 0 deletions tests/stores/test_coles.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ def test_get_canonical():
assert can_item['quantity'] == 2250
assert not can_item['isWeighted']

item = get_item(description='CARLTON DRY 8PK CAN 375ML:CTN24', quantity=0, isWeighted=False, size='')
can_item = coles.get_canonical(item, today)
assert can_item['unit'] == 'ml'
assert can_item['quantity'] == 3000
assert not can_item['isWeighted']

item = get_item(description='MOON DOG FIZZER SELTZER 6% CAN 330ML (10PK):CTN30', quantity=0, isWeighted=False, size='')
can_item = coles.get_canonical(item, today)
assert can_item['unit'] == 'ml'
assert can_item['quantity'] == 3300
assert not can_item['isWeighted']

item = get_item(description='SMIRNOFF RED 37.5% VODKA 375ML:EA', quantity=0, isWeighted=False, size='')
can_item = coles.get_canonical(item, today)
assert can_item['unit'] == 'ml'
Expand Down

0 comments on commit a8c2f35

Please sign in to comment.