Re: Help understanding code

Discussion in 'Python' started by Fredrik Lundh, Apr 10, 2005.

  1. Dhruva Hein wrote:

    > 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()?


    pc refers to an object, pc() calls it. in this case, it looks like pc is
    the
    portal catalog, and calling the catalog returns the contents.

    > numbers = {"total":len(results),"bytype":{},"bystate":{}} <===
    > This is hard to understand


    that line creates a dictionary with three members. you have read the
    Python tutorial, right?

    the code above is equivalent to:

    numbers = {}
    numbers["total"] = len(results)
    numbers["bytype"] = {}
    numbers["bystate"] = {}

    where {} creates a new, empty dictionary.

    > 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?


    "bytype" is a dictionary key, so numbers["bytype"] fetches a
    dictionary from the numbers dictionary (see above).

    get is a dictionary method; it returns the value corresponding
    to the given key (ctype), or a default value (0) if the key does
    not exist. that last line is equivalent to

    temp = numbers["bytype"]
    if temp.has_key(ctype):
    num = temp[ctype]
    else:
    num = 0

    > num += 1
    > numbers["bytype"][ctype] = num <====== is this some kind
    > of an array?


    no, it's a dictionary that contains a dictionary. that line is equi-
    valent to

    temp = numbers["bytype"]
    temp[ctype] = num

    for more on dictionaries, see the Python tutorial.

    > # 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


    hope this helps!

    </F>
     
    Fredrik Lundh, Apr 10, 2005
    #1
    1. Advertising

  2. Fredrik Lundh

    Duncan Booth Guest

    Fredrik Lundh wrote:

    > Dhruva Hein wrote:
    >> results = pc()
    >> <=== what is the difference between pc and pc()?

    >
    > pc refers to an object, pc() calls it. in this case, it looks like pc is
    > the
    > portal catalog, and calling the catalog returns the contents.
    >

    Unfortunately there is quite a bit going on behind the scenes here so if
    you don't know Plone it will be confusing:

    Calling the catalog is equivalent to calling the catalog method
    searchResults. This performs a catalog search and if (as in this case) no
    other arguments are passed to it it will pick up the search parameters from
    the current REQUEST object.

    Also it will add some additional search parameters: there must be a match
    between the user's allowed roles or userid and the 'allowedRolesAndUsers'
    index, and if the user doesn't have permission to access expired content,
    it will add some additional parameters to constrain the search to objects
    after their effective date and before their expired date.


    <snip>
    > that line is equivalent to
    >
    > temp = numbers["bytype"]
    > temp[ctype] = num


    .... and since temp is a constant throughout the loop it can be extracted
    from the loop and given a better name.

    All of that code would have been clearer if the original author had simply
    kept a reference to the bytype and bystate dictionaries and not insisted on
    the extra level of indirection by accessing them through the numbers
    dictionary.
     
    Duncan Booth, Apr 11, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JM
    Replies:
    3
    Views:
    2,202
    Andy Dingley
    Oct 1, 2005
  2. Dhruva Hein

    Help understanding code

    Dhruva Hein, Apr 10, 2005, in forum: Python
    Replies:
    2
    Views:
    351
    Bruno Desthuilliers
    Apr 11, 2005
  3. Replies:
    2
    Views:
    429
    Phlip
    Mar 15, 2007
  4. Need help in understanding c code

    , Jun 1, 2007, in forum: C Programming
    Replies:
    2
    Views:
    330
    Barry Schwarz
    Jun 2, 2007
  5. Daniel
    Replies:
    8
    Views:
    449
    Daniel
    Sep 13, 2007
Loading...

Share This Page