On 5/8/2016 1:50 AM, Jussi Piitulainen wrote:
DFS writes:
The lists I actually use are:
for j in range(len(nms)):
cSQL = "INSERT INTO ADDRESSES VALUES (?,?,?,?,?)"
vals = nms[j],street[j],city[j],state[j],zipcd[j]
The enumerated version would be:
ziplists = zip(nms,street,city,state,zipcd)
for nm,street,city,state,zipcd in ziplists:
cSQL = "INSERT INTO ADDRESSES VALUES (?,?,?,?,?)"
vals = nm,street,city,state,zipcd
I guess the enumeration() is a little nicer to look at. Why do you
think it's more maintainable?
The following variations avoid the naming of the result of zip at all,
and also save a line or two, depending on what you actually do in the
loop, without introducing overly long lines. Judge for yourself.
I tried:
for nm,street,city,state,zipcd in zip(nms,street,city,state,zipcd):
but felt it was too long and wordy.
You don't need to name the individual components, if you only actually
use vals:
for vals in zip(nms,street,city,state,zipcd):
cSQL = "INSERT INTO ADDRESSES VALUES (?,?,?,?,?)"
I like that one. But I do one more thing (get a category ID) than just
use the vals.
--------------------------------------------------------------------
ziplists = zip(categories,names,streets,cities,states,zipcodes)
for category,name,street,city,state,zipcode in ziplists:
dupeRow, pyodbcErr = False, False
catID = getDataID("catID","CATEGORIES","catDesc",category)
cSQL = "INSERT INTO ADDRESSES VALUES (?,?,?,?,?,?,?,?,?)"
vals =
datasrcID,searchID,catID,name,street,city,state,zipcode,str(loaddt)
try: db.execute(cSQL, vals)
except (pyodbc.Error) as programError:
if str(programError).find("UNIQUE constraint failed") > 0:
dupeRow = True
dupes +=1
print " * duplicate address found: "+name+", "+street
else:
pyodbcErr = True
print "ODBC error: %s " % programError
addrReturned += 1
if not dupeRow and not pyodbcErr:
addrSaved += 1
if addrWant != "all":
if addrSaved >= addrWant: break
conn.commit()
--------------------------------------------------------------------
That's the 'post to db' routine
Or you can opt to name the tuple and its components the other way
around:
for vals in zip(nms,street,city,state,zipcd):
nm,street,city,state,zipcd = vals
cSQL = "INSERT INTO ADDRESSES VALUES (?,?,?,?,?)"
I like the first one better. python is awesome, but too many options
for doing the same thing also makes it difficult. For me, anyway.
Thanks
--
https://mail.python.org/mailman/listinfo/python-list