Confused compare function :)

MRAB python at mrabarnett.plus.com
Thu Dec 6 18:10:00 CET 2012


On 2012-12-06 14:22, Anatoli Hristov wrote:
> Guys I'm still confusing my script is working better, but not enough.
> I did a logfile to see which products are not found anymore in the CSV
> and I found some that are present but python says they are not ??
>
> Here is the product in the CSV:
> MONIIE2407HDS-B1;MON;II;E2407HDS-B1;E2407HDS-B1;IIYAMA LCD 24" Wide
> 1920x1080TN Speakers 2ms Black DVI HDMI;133;20;RECTD0.41;0,41;;;;;;;;;
>
> Here is what python reports:
> e2208hds-b2, 721 not found
> e2273hds-b1, 722 not found
> e2274hds-b2, 723 not found
> e2407hds-b1, 724 not found
>
> And here is my final code: ( I hope it look better now :) )
>
> def Change_price(): # Changes the price in the DB if the price in the CSV is changed
>      TotalUpdated = 0 # Counter for total updated
>      TotalSKUFound = 0 # Total SKU from the DB coresponds to the one in the CSV
>      TotalSKUinDB = 0 # Total SKU in the DB
>      for row in PRODUCTSDB:
>          TotalSKUinDB +=1
>          db_sku = row["sku"].lower()
>          db_price = float(row["price"])
>          found = False
>          try:
>              for x in pricelist:
>                  try:
>                      csv_price = x[6]
>                      csv_price = csv_price.replace(",",".")
>                      csv_price = float(csv_price)
>                      csv_new_price = csv_price*1.10
>                      csv_sku = x[4].lower()
>                      csv_stock = int(x[7]) # I used this as normally I used  stock in the condition
 >                      match = re.search(db_sku, csv_sku)

This line is equivalent to:

     match = db_sku in csv_sku

This means that it's looking for db_sku anywhere in csv_sku. For
example, all of these are true: "72" in "720"; "20" in "720"; "720" in
"720"; etc.

>                      if len(db_sku) != 0 and match:
>                          TotalSKUFound +=1

TotalSKUFound is incremented if it finds a match.

Can "db_sku in csv_sku" be true multiple times for a given value of
db_sku? (See above.) Is it possible that TotalSKUFound is incremented
multiple times for some values of csv_sku?

>                          if csv_new_price < db_price and csv_stock > 0:
>                              print db_sku, csv_price, db_price, csv_new_price
>                              Update_SQL(csv_new_price, db_sku)
>                              TotalUpdated += 1
>                              found = True

It sets found to True if it updated.
>
>                  except IndexError: # I have a lot of index error in the CSV (empty fields) and the loop gives "index error" I  don't care about them
>                      pass
>                  except ValueError:
>                      pass
>                  except TypeError:
>                      pass

Even after finding a match (and possibly updating), it continues
iterating though pricelist.

>          except IndexError:

There's no need to catch IndexError here because the only places it
could be raised are also within the inner try..except.

>              pass
>          if not found: WriteLog(db_sku, db_sku,)

Calling it 'found' is misleading, because it's True only if it updated.
If it found a match but didn't update, 'found' will still be False.

>      TotalNotFound = TotalSKUinDB - TotalSKUFound
>      print "Total SKU in the DB %s" % TotalSKUinDB
>      print "Total SKU coresponds to the DB and CSV %s" % TotalSKUFound
>      print "Total updated: %s" % TotalUpdated
>      print"Total not found with in the distributor: %s" % TotalNotFound
>
Using a loop within a loop like this could be the cause of your
problem. It's certainly not the most efficient way of doing it.




More information about the Python-list mailing list