Newby Needs Help with Python code

Peter Otten __peter__ at web.de
Tue Sep 7 10:36:56 CEST 2010


Nally Kaunda-Bukenya wrote:

> I am very sorry to bother you, but I need help understanding the code that
>  you assisted me with. It does exactly what I needed done, I can't thank
>  you enough for that. I am just learning Python, and would appreciate all
>  the help.  please see my comments below and help me understand the code.

Please don't email me directly, send questions to the list instead to give 
more people a chance to answer.

> Code below:
> 
> #WHAT IS A DICT? SAME AS LIST?

This is a very basic question which is dealt with in every tutorial. Look 
here for a starting point:

http://wiki.python.org/moin/BeginnersGuide/NonProgrammers
 
> #iterate over the Outfalls_ND table twice, the first time to calculate the
> #sums per OUTFALL_ID and put them into a dict. With the second pass the
> #Tot_Outf_Area column is updated
> import arcgisscripting

> def rows(cur):
>     while True:
>         row = cur.Next()
>         if row is None:
>             break #??
>         yield row #??

break terminates a loop, yield gives back a result without terminating the 
function which is then called "generator". For now I recommend that you 
treat this generator as a black box.

> gp = arcgisscripting.create()
> gp.Workspace = "C:\\NPDES\\NPDES_PYTHON.mdb"
> TABLE = "Outfalls_ND"
> GROUP = "OUTFALL_ID" #does upper case mean anything?? (group below is 
lower
> case?)

Python is case-sensitive, so GROUP and group (and Group or grouP) are 
different names. I am following a convention here that uppercase names are 
used for constants, i. e. the value of GROUP never changes after the first 
assignment.

> SUM = "AREA_ACRES"
> TOTAL = "Tot_Outf_Area"
> 
> aggregate = {}
> cur = gp.UpdateCursor(TABLE)
> for row in rows(cur): #is this diff from for rows in cur?
>     group = row.GetValue(GROUP) #loop thru rows and search for outfall_ID
>     amount = row.GetValue(SUM)  #loop thru rows and search for area_acres
>     aggregate[group] = aggregate.get(group, 0.0) + amount #need help
> w/explanation on aggregate fn
> #aggregate[group] is what is it? is this a list of unique outfall_ID's?
> #aggregate.get (group, 0.0) how do you get 0.0?
> #I tried this code on a diff field and got this error: TypeError:
>  unsupported operand #type(s) for +: 'float' and 'NoneType'
> 
> cur = gp.UpdateCursor(TABLE)
> for row in rows(cur):
>     group = row.GetValue(GROUP)
>     row.SetValue(TOTAL, aggregate[group]) #??
>     cur.UpdateRow(row)

Sorry, I don't think answering any of the above questions worthwhile until 
you've worked through an introductory text. Once you /have/ a basic 
understanding you can often find out the answers by asking python itself.
Just fire up the interactive interpreter and type

>>> help(dict.get)

If the explanation given there is not sufficient you can use the online 
documentation as a fallback:

http://docs.python.org/library/stdtypes.html#mapping-types-dict

Good look with your efforts!

Peter





More information about the Python-list mailing list