Help understanding code

Dhruva Hein northstar.eight at verizon.net
Sun Apr 10 08:41:06 EDT 2005


Hi. I am trying to understand a section of code written for Plone and I 
am having problems understanding the Python syntax in a few of the 
following lines.

I'd be grateful if someone could help me understand what's happening in 
the lines I've marked. I can't see how the dictionary is built or how 
the lists ['bytype'][cytype] make sense in python.

Thanks in advance.

class Stats:

   def getContentTypes(self):
       """ Returns the number of documents by type """
       pc = getToolByName(self, "portal_catalog")
       # call the catalog and loop through the records
       results = pc()                                                   
               <=== what is the difference between pc and pc()?
       numbers = {"total":len(results),"bytype":{},"bystate":{}} <=== 
This is hard to understand
       for result in results:
           # set the number for the type
           ctype = str(result.Type)
           num = numbers["bytype"].get(ctype, 0)  <==== where does .get 
come from? and what is the string 'bytype' doing?
           num += 1
           numbers["bytype"][ctype] = num    <====== is this some kind 
of an array?

           # set the number for the state
           state = str(result.review_state)
           num = numbers["bystate"].get(state, 0)
           num += 1
           numbers["bystate"][state] = num
       return numbers




More information about the Python-list mailing list