Confused compare function :)

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Dec 6 01:42:00 CET 2012


On Thu, 06 Dec 2012 01:19:58 +0100, Bruno Dupuis wrote:

> 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.

Yes!

But basically, the code seems to run a pair of nested for-loops:

for SKU in database:
    for SKU in csv file:
        if the two SKUs match:
            compare their prices and update the database



> def ... for ... try ... for ... if ... if. 

You missed a second try.

> 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.

I think that "never" is too strong, but otherwise I agree with you.


> 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.


There's nothing wrong with comparing floats to ints.

However, possibly iprice is not intended to be an int. The code is rather 
confused and the names are at best obscure and at worst actively 
misleading. I assumed that prices must be ints, (iprice means "integer 
price"?) but that might not be the case, since later on another price is 
multiplied by 1.10.

Also I wonder if the code is meant to calculate the new price:

newprice = round(dprice)*1.10  # dprice is the price in the CSV file!

or perhaps it is meant to be:

newprice = int(round(dprice*1.10))


That seems likely.


-- 
Steven



More information about the Python-list mailing list