473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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(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
2 1325
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: What-a-Tool | last post by:
Dim MyMsg Set MyMsg = server.createObject("Scripting.Dictionary") MyMsg.Add "KeyVal1", "My Message1" MyMsg.Add "KeyVal2", "My Message2" MyMsg.Add "KeyVal3", "My Message3" for i = 1 To...
2
by: Mike | last post by:
Hey guys, need some help understanding some things that maybe someone can explain or clarify it a little better then a text book. Here is my understanding so far: Class - basically a shell for...
22
by: Jeff Louie | last post by:
Well I wonder if my old brain can handle threading. Dose this code look reasonable. Regards, Jeff using System; using System.Diagnostics; using System.IO; using System.Threading;
26
by: Bail | last post by:
I will have a exam on the oncoming friday, my professor told us that it will base upon this program. i am having troubles understanding this program, for example what if i want to add all the...
6
by: varkey.mathew | last post by:
Dear all, Bear with me, a poor newbie(atleast in AD).. I have to authenticate a user ID and password for a user as a valid Active Directory user or not. I have created the IsAuthenticated...
6
by: drec | last post by:
I am just learning Javascript and I would like to create a basic form that gives me two options. This will be using either checkbox or radio input type, however I would like the second option to...
5
Reika
by: Reika | last post by:
Hello, I need help understanding some things in programming. I'm taking a high school level programming course, however my teacher is less than helpful in explaining or even understanding the...
4
by: Andrew Taylor | last post by:
Hi, I've been using PHP for a long time, I have designed and developed a number of mid-range systems. I've always used a procedural approach. I fully understand the concept of OO, I know all the...
19
by: mohammaditraders | last post by:
a program which consists of a class named Student, the class should consists of three data members Name, Ob_marks, Total_marks and two member functions Cal_percentage() which calculate the...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.