Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #34371
| 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 | <tolidtm@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '%s"': 0.07; '*not*': 0.07; 'try:': 0.07; 'valueerror:': 0.07; 'python': 0.09; '754': 0.09; 'comment,': 0.09; 'hiding': 0.09; 'likewise': 0.09; 'logic': 0.09; 'must,': 0.09; 'typeerror:': 0.09; 'variables,': 0.09; 'wrong,': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'index': 0.13; 'dec': 0.15; 'file,': 0.15; '"%s': 0.16; '(empty': 0.16; '(something': 0.16; ':-p': 0.16; 'both.': 0.16; 'bugs.': 0.16; 'comma': 0.16; 'compare.': 0.16; 'csv': 0.16; 'error"': 0.16; 'found"': 0.16; 'indexerror:': 0.16; 'luck!': 0.16; 'notation,': 0.16; 'really?': 0.16; 'redundant.': 0.16; 'renamed': 0.16; 'row': 0.16; 'still,': 0.16; 'subject:compare': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'two,': 0.16; 'valueerror,': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'fix': 0.17; 'comparing': 0.17; 'found,': 0.17; "shouldn't": 0.17; 'thu,': 0.17; 'variables': 0.17; 'code,': 0.18; '(or': 0.18; 'changes': 0.20; 'written': 0.20; 'received:209.85.214.174': 0.21; 'file:': 0.22; 'int,': 0.22; 'help.': 0.22; 'cc:2**0': 0.23; 'errors': 0.23; 'this:': 0.23; 'seems': 0.23; 'cc:no real name:2**0': 0.24; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; '(which': 0.26; 'skip:" 20': 0.26; 'values': 0.26; 'am,': 0.27; 'bugs': 0.27; 'guess': 0.27; 'possibly': 0.27; 'wonder': 0.27; 'replace': 0.27; 'message-id:@mail.gmail.com': 0.27; 'all.': 0.28; '+0100,': 0.29; 'block,': 0.29; "d'aprano": 0.29; 'steven': 0.29; 'url:mailman': 0.29; 'this.': 0.29; "i'm": 0.29; 'normally': 0.30; 'function': 0.30; 'error': 0.30; 'code': 0.31; 'url:python': 0.32; 'file': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'goes': 0.33; "aren't": 0.33; 'int': 0.33; 'rid': 0.33; 'anyone': 0.33; 'code:': 0.33; 'equal': 0.33; 'another': 0.33; 'skip:d 20': 0.34; 'changed': 0.34; 'received:google.com': 0.34; 'updated': 0.34; 'wrong': 0.34; 'list': 0.35; 'clear': 0.35; 'whatever': 0.35; 'false': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'next': 0.35; 'really': 0.36; 'except': 0.36; 'skip:u 20': 0.36; 'but': 0.36; 'url:org': 0.36; 'compare': 0.36; 'stock': 0.36; "didn't": 0.36; 'anything': 0.36; 'should': 0.36; 'thank': 0.36; 'problems': 0.36; 'does': 0.37; 'two': 0.37; 'why': 0.37; 'times': 0.63; 'more': 0.63; 'here': 0.65; 'total': 0.65; 'price': 0.66; 'records': 0.68; 'believe': 0.69; 'discover': 0.72; 'day': 0.73; 'attention': 0.75; 'as:': 0.75; 'change?': 0.84; 'confusion.': 0.84; 'horrible': 0.84; 'prices,': 0.84; 'say:': 0.84; 'hungarian': 0.91; 'seriously,': 0.91; 'total,': 0.91 |
| 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 :cc:content-type; bh=4TV1ZvxXEoTynLZ3eOoAz7aFGkd6AN0/KlbWBpwlXTs=; b=QHIQma54XzZkS0s1AfhN80whHnDzQaGLmf6WhKwMnMmXCUeqy9kbYCRbadel+CZNSc n3xqr5v2N7HRpbaazEZ0I3DiYXpGSm3LveZVJYEzzjU2T4ckhhedHpH9usKiBgkrvz7y WPhjnAVK5/XTeTK0M0K3bT1CxUDuCwxNljjGLU09RG/x74TGF5dTz+seN5vsCyq3IqlK eoQuSCLjYz4no1VYt6tNlcvcYgIZ3BpIHE+PaAdEeMfw/vCP9fA06bDXlX0erc/g5oXb sUeiguZXaBFUxmHXmq81R/3+H/dcamJ9eNco1nbaGFugiI77zoL26c+VzuNPGu9wBHBw le3w== |
| MIME-Version | 1.0 |
| In-Reply-To | <50bfe774$0$29994$c3e8da3$5496439d@news.astraweb.com> |
| References | <mailman.534.1354747852.29569.python-list@python.org> <50bfe774$0$29994$c3e8da3$5496439d@news.astraweb.com> |
| Date | Thu, 6 Dec 2012 10:36:38 +0100 |
| Subject | Re: Confused compare function :) |
| From | Anatoli Hristov <tolidtm@gmail.com> |
| To | "Steven D'Aprano" <steve+comp.lang.python@pearwood.info> |
| Content-Type | text/plain; charset=UTF-8 |
| Cc | python-list@python.org |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.551.1354786601.29569.python-list@python.org> (permalink) |
| Lines | 257 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1354786601 news.xs4all.nl 6947 [2001:888:2000:d::a6]:33443 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:34371 |
Show key headers only | View raw
On Thu, Dec 6, 2012 at 1:31 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> On Wed, 05 Dec 2012 23:50:49 +0100, Anatoli Hristov wrote:
>
>
>> def Change_price():
>
> Misleading function name. What price does it change?
>
>
>> total = 0
>> tnf = 0
>
> "tnf"? Does that mean something?
>
>
>> for row in DB: # DB is mySQL DB, logically I get out
>> # 1 SKU and I compare it with next loop
>
> Use of global variables, yuck. What happens if some day you need two
> databases at the same time?
>
>
>> isku = row["sku"]
>> isku = isku.lower()
>
> Hungarian Notation? This is better written as:
>
> sku = row["sku"].lower()
>
>
>> iprice = row["price"]
>> iprice = int(iprice)
>
> And likewise price = int(row["price"]). Or better still, "oldprice", or
> "price_in_database", or something that actually describes what it is.
>
>
>> found = 0
>
> found = False
>
>
>> 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"? D-for-database price? But this is the price from the CSV file,
> not from the database. Another misleading name, leading to confusion.
>
>
>> 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()
>
> And again, what's "dsku" mean? Database-SKU? But it's the CSV SKU.
>
>
>> stock = int(x[7])
>
> I don't believe that this is used at all. Get rid of it.
>
>
>> 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
>
> I think your logic is wrong here. You aren't comparing the price in the
> CSV here at all. You compare two prices, neither of which is the price in
> the CSV file:
>
> newprice = round(price in CSV) * 1.10
> iprice = price from the database
>
> (which is already an int, no need to call int *again* -- if you're going
> to use Hungarian Notation, pay attention to it!)
>
> So you have THREE prices, not two, and it isn't clear which ones you are
> *supposed* to compare. Either the code is wrong, or the comment is wrong,
> or possibly both.
>
> iprice = price from the database
> dprice = price from the CSV
> newprice = calculated new price
>
> I'm going to GUESS that you actually want to compare the new price with
> the price in the database.
>
> if newprice > iprice: # horrible name! no wonder you are confused
> # Update the database with the new price
>
>
>> print dsku, x[6], dprice, newprice
>> Update_SQL(newprice, isku)
>> # goes to the SQL Update
>
> Really? Gosh, without the comment, how would anyone know that Update_SQL
> updates the SQL? :-P
>
> Seriously, the comment is redundant. Get rid of it.
>
>
>> print isku, newprice
>> if isku == dsku:
>> # Just a check to see if it works
>> print "Found %s" %dsku
>> found = 1
>> else:
>> found = 0
>
> found = True or False is better.
>
> But this code cannot do anything but print Found, since above you already
> tested that isku == dsku. So this check is pointless.
>
> The reason is, your code does this:
>
> if isku == dsku and (something else):
> # Inside this block, isku MUST equal dsku
> blah blah blah
> if isku == dsku:
> print "Found"
> found = 1
> else:
> # But this cannot possibly happen
> print "not found"
> found = 0
>
>
>> except IndexError:
>> pass
>> except ValueError:
>> pass
>> except TypeError:
>> pass
>
> Why are you hiding errors? You should not hide errors unnecessarily, that
> means there are bugs in either the CSV or your code, you should fix the
> bugs.
>
> However, if you really must, then you can replace all of those with:
>
> except (IndexError, ValueError, TypeError):
> pass
>
>
>> except IndexError:
>> pass
>
> And hiding more errors?
>
>
>> if found == 1:
>> print "%s This is match" % isku
>> if found == 0:
>> print "%s Not found" % isku
>> tnf = tnf +1
>> total = total +1
>
> Better to write this as:
>
> if found:
> print "%s This is match" % isku
> else:
> print "%s Not found" % isku
> tnf = tnf + 1 # What does this mean?
> total += 1
>
>
>> print "Total updated: %s" % total
>
> That's wrong. total is *not* the number updated. It is the total, updated
> or not updated. This should say:
>
> print "Total records inspected: %s" % total
>
>
>> print"Total not found with in the distributor: %s" % tnf
>
> Ah-ha! It means "Total Not Found"! I shouldn't have to read all the way
> to the end of the code to discover this. Instead of "tnf", you should
> count the total number of SKUs, and count how many times you call
> Update_SQL. Then you can report:
>
> Total records inspected: 754
> Total records updated: 392
>
> (or whatever the values are).
>
>
>
> Simplify and clean up your code, and then it will be easier to find and
> fix the problems in it. Good luck!
>
>
>
> --
> Steven
> --
> http://mail.python.org/mailman/listinfo/python-list
Thank you Steven for your help. Now I renamed all the variables and it
seems working I didn't found the mistake, but it seems working :) Here
is the new code:
def Change_price(): # Changes the price in the DB if the price in the
CSV is changed
TotalUpdated = 0 # Counter for total updated
TotalNotFound = 0 # Counter for total not found
for row in PRODUCTSDB:
db_sku = row["sku"].lower()
db_price = int(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 = round(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_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)
found = True
TotalUpdated += 1
else:
found = False
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
except IndexError:
pass
if found:
TotalNotFound += 1
print "%s This is match" % db_sku
else:
print "%s Not found" % db_sku
TotalNotFound += 1
print "Total updated: %s" % TotalUpdated
print"Total not found with in the distributor: %s" % TotalNotFound
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-05 23:50 +0100
Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-06 00:31 +0000
Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 10:36 +0100
csiph-web