From 4e3818723c3d78af4f7ed574218f71a30832ff8a Mon Sep 17 00:00:00 2001 From: Georg Sauthoff Date: Sat, 5 Dec 2020 13:44:35 +0100 Subject: [PATCH] fix price comparision and latest column reordering i.e. latest Deutsche Post update inserted some columns, thus using the headers is more robust. --- examples/update_products.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/examples/update_products.py b/examples/update_products.py index 7475249..0eeabdc 100755 --- a/examples/update_products.py +++ b/examples/update_products.py @@ -14,41 +14,46 @@ import sys import json import csv +import decimal + +def quantize(x): + x = decimal.Decimal(x).quantize(decimal.Decimal('.01')) def main(csv_filename, json_filename, filename): with open(csv_filename, newline='', encoding='latin-1') as f \ , open(json_filename) as g \ , open(filename, 'w') as h: - rows = csv.reader(f, delimiter=';') + rows = csv.DictReader(f, delimiter=';') products = json.load(g) seen_ids = set() for row in rows: - key, price = row[2], row[5].replace(',', '.') - inter, weight, name = row[3]=='1', row[21], row[4] - seen_ids.add(row[2]) + key, price = row['PROD_ID'], row['PROD_BRPREIS'].replace(',', '.') + inter, weight, name = (row['PROD_AUSR'] in ('1', 'I')), row['MAXG'], row['PROD_NAME'] + seen_ids.add(key) if key in products: p = products[key] oprice, ointer = p['cost_price'], p['international'] oweight, oname = p['max_weight'], p['name'] # row[5] vs. row[7] => total vs. base product price # row[4] vs. row[6] => complete vs. base product name + price, oprice = quantize(price), quantize(oprice) c = False if price != oprice: - print(f'Price of {name} ({key}) changed: {oprice} -> {price} (from {row[0]})') + print(f'Price of {name} ({key}) changed: {oprice} -> {price} (from {row["PROD_GUEAB"]})') c = True p['cost_price'] = price if inter != ointer: - print(f'Nationality of {name} ({key}) changed: {ointer} -> {inter} (from {row[0]})') + print(f'Nationality of {name} ({key}) changed: {ointer} -> {inter} (from {row["PROD_GUEAB"]})') c = True p['international'] = inter if weight != oweight: - print(f'Weight of {name} ({key}) changed: {oweight} -> {weight} (from {row[0]})') + print(f'Weight of {name} ({key}) changed: {oweight} -> {weight} (from {row["PROD_GUEAB"]})') c = True p['max_weight'] = weight # The names returned by the ProdWS differ from the ones included in the PPL ... # if name != oname: - # print(f'Name of {name} ({key}) changed: {oname} -> {name} (from {row[0]})') + # print(f'Name of {name} ({key}) changed: {oname} -> {name} (from {row["PROD_GUEAB"]})') # c = True if c: print('*'*75)