Confused compare function :)
Bruno Dupuis
python.ml.bruno.dupuis at lisael.org
Wed Dec 5 19:19:58 EST 2012
On Wed, Dec 05, 2012 at 11:50:49PM +0100, Anatoli Hristov wrote:
> I'm confused again with a compare update function. The problem is that
> my function does not work at all and I don't get it where it comes
> from.
>
> in my DB I have total of 754 products. when I run the function is says:
> Total updated: 754
> Total not found with in the distributor: 747
> I just don't get it, can you find my mistake ?
>
> Thanks in advance
>
> def Change_price():
> total = 0
> tnf = 0
> for row in DB: # DB is mySQL DB, logically I get out 1 SKU and I
> compare it with next loop
> isku = row["sku"]
> isku = isku.lower()
> iprice = row["price"]
> iprice = int(iprice)
> found = 0
> try:
> for x in PRICELIST:# here is my next loop in a CSV file
> which is allready in a list PRICELIST
> try:
> dprice = x[6]
> dprice = dprice.replace(",",".") # As in the
> PRICELIST the prices are with commas I replace the comma as python
> request it
> dprice = float(dprice)
> newprice = round(dprice)*1.10
> dsku = x[4]
> dsku = dsku.lower()
> stock = int(x[7])
> if isku == dsku and newprice < int(iprice):# If
> found the coresponded SKU and the price is higher than the one in the
> CSV I update the price
> print dsku, x[6], dprice, newprice
> Update_SQL(newprice, isku)# goes to the SQL Update
> print isku, newprice
> if isku == dsku:# Just a check to see if it works
> print "Found %s" %dsku
> found = 1
> else:
> found = 0
> except IndexError:
> pass
> except ValueError:
> pass
> except TypeError:
> pass
> except IndexError:
> pass
> if found == 1:
> print "%s This is match" % isku
> if found == 0:
> print "%s Not found" % isku
> tnf = tnf +1
> total = total +1
> print "Total updated: %s" % total
> print"Total not found with in the distributor: %s" % tnf
I tried, I swear I did try, I didn't understand the whole algorithm of the
function. However, in a first sight, I find it way to deeply nested.
def ... for ... try ... for ... if ... if. Can't you split it in several
function, or in methods of a callable class? Somtimes it's finally much
more clear and the errors become obvious. Another advice: never ever
except XXXError:
pass
at least log, or count, or warn, or anything, but don't pass. I bet your
missing products have disapeared into those black holes.
mmmh, now, i see that you set found to 1 only if newprice <int(iprice)...
new_price is a float (newprice = round(dprice)*1.10) that you compare with
an int? is that correct? seems strangee to me.
--
Bruno Dupuis
More information about the Python-list
mailing list