By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,592 Members | 1,604 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,592 IT Pros & Developers. It's quick & easy.

Help understanding code

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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!

Jul 18 '05 #2

P: n/a
el*******@hotmail.com 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()
hello world
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() hello, I'm foo

</op>

HTH
Bruno
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.