Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.134.4.91.MISMATCH!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; '%s"': 0.07; 'false.': 0.07; 'try:': 0.07; 'def': 0.10; 'index': 0.13; '(empty': 0.16; 'csv': 0.16; 'error"': 0.16; 'indexerror:': 0.16; 'row': 0.16; 'subject:compare': 0.16; 'true:': 0.16; 'valueerror,': 0.16; 'certainly': 0.17; 'changes': 0.20; 'received:209.85.214.174': 0.21; 'posted': 0.22; 'header:In-Reply- To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; 'normally': 0.30; 'error': 0.30; 'code': 0.31; 'problem.': 0.32; 'file': 0.32; 'could': 0.32; 'print': 0.32; 'to:addr:python-list': 0.33; 'changed': 0.34; 'received:google.com': 0.34; 'updated': 0.34; 'false': 0.35; 'doing': 0.35; 'received:209.85': 0.35; 'except': 0.36; 'skip:u 20': 0.36; 'but': 0.36; 'stock': 0.36; "didn't": 0.36; 'thank': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'little': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'most': 0.61; 'more': 0.63; 'within': 0.64; 'here': 0.65; 'total': 0.65; 'price': 0.66; 'products': 0.70 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=tevxWin7XZcdL3YpbB1s7EVroHe23tAhelh01M/0p8s=; b=nDr8eWP9TrDK2xuXPPXycsRQO2/E6K8N2KY4o0PTwZrg9jOgxCD+382RLGFrdUD5BH +5vXCHHZQARJhQQQtCY6hJ6AlaMUidff9UANxU4LjyyL0PfSsdmAvRTnTET7Bd5xb1XZ fTM9CMeUWfRoHtW6cr6VyPXXnip4lX5c4bNpiFib5Ice9L7tVkW2rEtLooZvnzQw2vLW H+AGj3823geLN713xegT574F1wnu1w1+iBqAl3aeEXRL+OGp/p16qURUV2TsQR+Clh50 aPrdj9LUObTqXuxsnDJDLggsRkACOoEdcXc4EOVoTVyCnviiXsKqDsKfN1/p26V1PvsH XOnA== MIME-Version: 1.0 In-Reply-To: References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50C0D168.2010101@mrabarnett.plus.com> Date: Fri, 7 Dec 2012 14:36:23 +0100 Subject: Re: Confused compare function :) From: Anatoli Hristov To: python-list@python.org Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354887386 news.xs4all.nl 6933 [2001:888:2000:d::a6]:56007 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34458 >> 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. >> 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. > > I will keep you posted THANK YOU And here is my final code -- I hope you will like it a little more :) def Change_price(): # Changes the price in the DB if the price in the CSV is changed TotalUpdated = 0 # Counter for total updated TotalSKUNotFound = 0 # Total SKU from the DB coresponds to the one in the CSV TotalSKUinDB = 0 found = None for row in PRODUCTSDB: TotalSKUinDB +=1 db_sku = row["sku"].lower() db_price = float(row["price"]) if CompareWithCSV(db_sku, db_price) == True: TotalUpdated +=1 else: TotalSKUNotFound +=1 Update_SQL_stock(db_sku) WriteLog(db_sku, row["product_id"]) print "Total updated: %s" % TotalUpdated print"Total not found with in the distributor: %s" % TotalSKUNotFound print "Total products in the DB %s" % TotalSKUinDB def CompareWithCSV(db_sku, db_price): 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 if len(db_sku) != 0 and db_sku == csv_sku and csv_stock > 0: print db_sku, csv_price, db_price, csv_new_price Update_SQL(csv_new_price, db_sku) return True except (IndexError, ValueError, TypeError, db_sku): # I have a lot of index error in the CSV (empty fields) and the loop gives "index error" I don't care about them WriteErrorLog("Error with CSV file loop: ", db_sku) except IndexError: WriteErrorLog("Error with CSV file loop: ", db_sku) return False