Help understanding code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dhruva Hein

    #1

    Help understanding code

    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(s elf, "portal_catalog ")
    # call the catalog and loop through the records
    results = pc()
    <=== what is the difference between pc and pc()?
    numbers = {"total":len(re sults),"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.revi ew_state)
    num = numbers["bystate"].get(state, 0)
    num += 1
    numbers["bystate"][state] = num
    return numbers

  • elbertlev@hotmail.com

    #2
    Re: Help understanding code

    Reading the language tututorial would help you a lot :(

    === what is the difference between pc and pc()?

    pc = getToolByName(. ...) - returnes a refference to a method

    pc is a refference to a method, pc() is a method invocation.

    ============

    numbers = {"total":len(re sults),"bytype" :{},"bystate":{ }}
    <=== This is hard to understand
    num = numbers["bytype"].get(ctype, 0) <==== where does .get
    come from? and what is the string 'bytype' doing?
    numbers["bytype"][ctype] = num <====== is this some kind
    of an array?

    Read the tutorial especially portion about dictionaries!

    Comment

    • Bruno Desthuilliers

      #3
      Re: Help understanding code

      elbertlev@hotma il.com a écrit :[color=blue]
      > Reading the language tututorial would help you a lot :(
      >
      > === what is the difference between pc and pc()?
      >
      > pc = getToolByName(. ...) - returnes a refference to a method[/color]

      Nope.
      [color=blue]
      > pc is a refference to a method,
      >[/color]
      Nope.

      pc is not 'a reference to a method', it's a callable object (in this
      case a ZCatalog instance...)... .
      [color=blue]
      > pc() is a method invocation.[/color]

      In this case, it happens to be a call to a method of ZCatalog, but it
      could have been a call to a named or anonymous function as well...

      <op>
      In Python, functions and methods are objects too, so you can use them
      like any other object :

      def fun():
      print "hello world"

      machin = fun
      machin()[color=blue][color=green]
      >> hello world[/color][/color]

      But objects can be used like functions too, if they define a __call__
      method:

      class fakeFun(object) :
      def __init__(self, name):
      self.name = name
      def __call__(self):
      return "hello, I'm %s" % self.name

      f= fakeFun('foo')
      f()[color=blue][color=green]
      >> hello, I'm foo[/color][/color]
      </op>

      HTH
      Bruno

      Comment

      Working...