Help understanding code

D

Dhruva Hein

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
 
E

elbertlev

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(results),"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!
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
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
Nope.

pc is a refference to a method,
Nope.

pc is not 'a reference to a method', it's a callable object (in this
case a ZCatalog instance...)....
> pc() is a method invocation.

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()
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()</op>

HTH
Bruno
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top