Re: [Tutor] Problem with print

2010-12-20 Thread Steven D'Aprano

David Hutto wrote:

On Sun, Dec 19, 2010 at 4:33 PM, Hugo Arts  wrote:

On Sun, Dec 19, 2010 at 10:11 PM, Sander Sweers  wrote:

On 19 December 2010 21:54, jtl999  wrote:

 File "GettingStarted.py", line 91
   print ("Lesson Two")
   ^
SyntaxError: invalid syntax


[...]


Apparently so, but I'd like to see the full code to know what caused
the error to point to print.


It is very common for an error in parentheses or brackets to lead to a 
syntax error on the following line.


>>> compile("""
... x = 1
... y = (x+1/2
... z = 3
... """, "", "exec")
Traceback (most recent call last):
  File "", line 5, in 
  File "", line 4
z = 3
^
SyntaxError: invalid syntax


--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] calling setters of superclasses

2010-12-20 Thread Steven D'Aprano

Alan Gauld wrote:


"Gregory, Matthew"  wrote

class PositiveX(object):
   def __init__(self):
   @property
   def x(self):
   @x.setter
   def x(self, val):


I don't use properties in Python very often (hardly ever in fact) and 
I've never used @setter so there may be naming requirements I'm not 
aware of. But in general I'd avoid having two methods with the same name.


That's generally good advice, since one will over-write the other, but 
in this specific case, the following is completely bad:


Try renaming the setter to setX() or somesuch and see if you get the 
same error.


When using the x.setter and x.deleter decorators of a property, you 
*must* use the same name. The example given by help(property) for 
Python2.6 says this:


 |  Decorators make defining new properties or modifying existing ones 
easy:

 |  class C(object):
 |  @property
 |  def x(self): return self._x
 |  @x.setter
 |  def x(self, value): self._x = value
 |  @x.deleter
 |  def x(self): del self._x

and the docs are even more explicit:

http://docs.python.org/library/functions.html#property

If you don't use the same name, chaos reigns:


>>> class Broken(object):
... def __init__(self):
... self._x = 42
... @property
... def x(self):
... return self._x
... @x.setter
... def set_x(self, value):
... self._x = value + 1
...
>>>
>>> obj = Broken()
>>> obj.x
42
>>> obj.x = 20
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: can't set attribute


--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] doing maths on lists

2010-12-20 Thread Chris Begert
Bonjour

I have three lists with 65 float items and would like to do the following sum:

L0 = ([sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(0,64,1))])

So it just should do a sum across all the items in the list:

L0A[0]*cos(L0B[0]+L0C[0]*JME)+ L0A[1]*cos(L0B[1]+L0C[1]*JME)+...
+ L0A[64]*cos(L0B[64]+L0C[64]*JME)= some float number


However, I always get this error:

TypeError: can't multiply sequence by non-int of type 'float'   


I looked it up and there seems to be some solution using either the "for-in" 
command or the "map(int,...)" command but I just can't get it to work


Any help will be very much appreciated :)

Greetings from Sydney
Chris
-- 
GRATIS! Movie-FLAT mit über 300 Videos. 
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] doing maths on lists

2010-12-20 Thread Nitin Pawar
you may want to do type casting whenever you have a float answer cause
integer to float causes data loss

On Mon, Dec 20, 2010 at 4:28 PM, Chris Begert  wrote:

> Bonjour
>
> I have three lists with 65 float items and would like to do the following
> sum:
>
> L0 = ([sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(0,64,1))])
>
> So it just should do a sum across all the items in the list:
>
> L0A[0]*cos(L0B[0]+L0C[0]*JME)+ L0A[1]*cos(L0B[1]+L0C[1]*JME)+...
> + L0A[64]*cos(L0B[64]+L0C[64]*JME)= some float number
>
>
> However, I always get this error:
>
> TypeError: can't multiply sequence by non-int of type 'float'
>
> I looked it up and there seems to be some solution using either the
> "for-in" command or the "map(int,...)" command but I just can't get it to
> work
>
>
> Any help will be very much appreciated :)
>
> Greetings from Sydney
> Chris
> --
> GRATIS! Movie-FLAT mit über 300 Videos.
> Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Nitin Pawar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] doing maths on lists

2010-12-20 Thread Chris Begert
Thanks

I tried to put a float around my lists as well as around the sum but it didn't 
work.

Do I have to do some type casting around all items of the list?

Cheers

 Original-Nachricht 
> Datum: Mon, 20 Dec 2010 16:33:39 +0530
> Von: Nitin Pawar 
> An: Chris Begert 
> CC: tutor@python.org
> Betreff: Re: [Tutor] doing maths on lists

> you may want to do type casting whenever you have a float answer cause
> integer to float causes data loss
> 
> On Mon, Dec 20, 2010 at 4:28 PM, Chris Begert  wrote:
> 
> > Bonjour
> >
> > I have three lists with 65 float items and would like to do the
> following
> > sum:
> >
> > L0 = ([sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(0,64,1))])
> >
> > So it just should do a sum across all the items in the list:
> >
> > L0A[0]*cos(L0B[0]+L0C[0]*JME)+ L0A[1]*cos(L0B[1]+L0C[1]*JME)+...
> > + L0A[64]*cos(L0B[64]+L0C[64]*JME)= some float number
> >
> >
> > However, I always get this error:
> >
> > TypeError: can't multiply sequence by non-int of type 'float'
> >
> > I looked it up and there seems to be some solution using either the
> > "for-in" command or the "map(int,...)" command but I just can't get it
> to
> > work
> >
> >
> > Any help will be very much appreciated :)
> >
> > Greetings from Sydney
> > Chris
> > --
> > GRATIS! Movie-FLAT mit über 300 Videos.
> > Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome
> > ___
> > Tutor maillist  -  Tutor@python.org
> > To unsubscribe or change subscription options:
> > http://mail.python.org/mailman/listinfo/tutor
> >
> 
> 
> 
> -- 
> Nitin Pawar

-- 
GRATIS! Movie-FLAT mit über 300 Videos. 
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] doing maths on lists

2010-12-20 Thread Christian Witts

On 20/12/2010 12:58, Chris Begert wrote:

Bonjour

I have three lists with 65 float items and would like to do the following sum:

L0 = ([sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(0,64,1))])

So it just should do a sum across all the items in the list:

L0A[0]*cos(L0B[0]+L0C[0]*JME)+ L0A[1]*cos(L0B[1]+L0C[1]*JME)+...
+ L0A[64]*cos(L0B[64]+L0C[64]*JME)= some float number


However, I always get this error:

TypeError: can't multiply sequence by non-int of type 'float'   


I looked it up and there seems to be some solution using either the "for-in" command or 
the "map(int,...)" command but I just can't get it to work


Any help will be very much appreciated :)

Greetings from Sydney
Chris
   


Surely it should be
L0 = sum([(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(0, 64)])
as you want to generate the answers and then sum it up.

--
Kind Regards,
Christian Witts


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] doing maths on lists

2010-12-20 Thread Steven D'Aprano

Chris Begert wrote:

Bonjour

I have three lists with 65 float items and would like to do the following sum:

L0 = ([sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(0,64,1))])

[...]

However, I always get this error:

TypeError: can't multiply sequence by non-int of type 'float'   


Works for me:


>>> from math import cos
>>> L0A = [i for i in range(64)]  # make up some data
>>> L0B = [2*i+3 for i in range(64)]
>>> L0C = [3*i-2 for i in range(64)]
>>> JME = 2
>>> L0 = ([sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(0,64,1))])
>>> L0
[39.022477151098606]


Please compare what you typed in your email with the code you are trying 
to run. I think you will find that you have something like this by mistake:


([sum(L0A*cos(L0B[i]+L0C[i]*JME) for i in range(0,64,1))])

Notice that it has L0A*cos(...) instead of L0A[i]*cos(...).


Some other comments:

Why is the sum stored in a one-item list? I don't think you need either 
of the two outer-most brackets, ([...]). That is:


L0 = sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(0,64,1))


You can simplify the call to range: range(0, 64, 1) is just a long way 
of writing range(64):


L0 = sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(64))


Rather than explicitly specify the indexes used, a better solution is to 
use zip() like this:


L0 = sum(a*cos(b+c*JME) for a,b,c in zip(L0A, L0B, L0C))



--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] doing maths on lists

2010-12-20 Thread Dave Angel

On 01/-10/-28163 02:59 PM, Chris Begert wrote:

Bonjour

I have three lists with 65 float items and would like to do the following sum:

L0 = ([sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(0,64,1))])

So it just should do a sum across all the items in the list:

L0A[0]*cos(L0B[0]+L0C[0]*JME)+ L0A[1]*cos(L0B[1]+L0C[1]*JME)+...
+ L0A[64]*cos(L0B[64]+L0C[64]*JME)= some float number


However, I always get this error:

TypeError: can't multiply sequence by non-int of type 'float'   


I looked it up and there seems to be some solution using either the "for-in" command or 
the "map(int,...)" command but I just can't get it to work


Any help will be very much appreciated :)

Greetings from Sydney
Chris


Look up range(), and notice that the first and 3rd arguments default to 
0 and 1 respectively.  So you're just doing range(64), which gives you 
the first 64 items in the list.  But you said there were 65, so why 
didn't you use 65 ?


Where did you get that python expression?  If you just want a sum, 
there's no need for the outer pair of square braces or parentheses.  As 
it is, it'll correctly build a list of 1 item, which is the sum of all 
but one term of the desired expression.


Anyway, the expression doesn't give an error, if you've really got those 
three arrays of floats.  My guess is that you either copied the 
expression wrong (use copy/paste), or you've got something else in those 
lists.  Make a simple test case, and show the whole thing, including 
imports:



from math import cos

JME = 0.4
L0A = [2.3, 4.65]
L0B = [1.8, 2.2]
L0C = [12.1, 4]
limit = len(L0A)

L0 = sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(limit))


print L0

runs fine with Python 2.6 on Linux, producing output of:

-1.52286725666


DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] doing maths on lists

2010-12-20 Thread Chris Begert
Dave and Christian

Thanks a lot for the help, it looks like it works!

Chris


 Original-Nachricht 
> Datum: Mon, 20 Dec 2010 07:32:45 -0500
> Von: Dave Angel 
> An: Chris Begert 
> CC: tutor@python.org
> Betreff: Re: [Tutor] doing maths on lists

> On 01/-10/-28163 02:59 PM, Chris Begert wrote:
> > Bonjour
> >
> > I have three lists with 65 float items and would like to do the
> following sum:
> >
> > L0 = ([sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(0,64,1))])
> >
> > So it just should do a sum across all the items in the list:
> >
> > L0A[0]*cos(L0B[0]+L0C[0]*JME)+ L0A[1]*cos(L0B[1]+L0C[1]*JME)+...
> > + L0A[64]*cos(L0B[64]+L0C[64]*JME)= some float number
> >
> >
> > However, I always get this error:
> > 
> > TypeError: can't multiply sequence by non-int of type 'float'   
> > 
> >
> > I looked it up and there seems to be some solution using either the
> "for-in" command or the "map(int,...)" command but I just can't get it to
> work
> >
> >
> > Any help will be very much appreciated :)
> >
> > Greetings from Sydney
> > Chris
> 
> Look up range(), and notice that the first and 3rd arguments default to 
> 0 and 1 respectively.  So you're just doing range(64), which gives you 
> the first 64 items in the list.  But you said there were 65, so why 
> didn't you use 65 ?
> 
> Where did you get that python expression?  If you just want a sum, 
> there's no need for the outer pair of square braces or parentheses.  As 
> it is, it'll correctly build a list of 1 item, which is the sum of all 
> but one term of the desired expression.
> 
> Anyway, the expression doesn't give an error, if you've really got those 
> three arrays of floats.  My guess is that you either copied the 
> expression wrong (use copy/paste), or you've got something else in those 
> lists.  Make a simple test case, and show the whole thing, including 
> imports:
> 
> 
> from math import cos
> 
> JME = 0.4
> L0A = [2.3, 4.65]
> L0B = [1.8, 2.2]
> L0C = [12.1, 4]
> limit = len(L0A)
> 
> L0 = sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(limit))
> 
> 
> print L0
> 
> runs fine with Python 2.6 on Linux, producing output of:
> 
> -1.52286725666
> 
> 
> DaveA

-- 
Neu: GMX De-Mail - Einfach wie E-Mail, sicher wie ein Brief!  
Jetzt De-Mail-Adresse reservieren: http://portal.gmx.net/de/go/demail
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] why "ifconfig" is alway running?

2010-12-20 Thread lei yang
On Sun, Dec 19, 2010 at 9:05 PM, Sander Sweers  wrote:
> On 19 December 2010 13:43, lei yang  wrote:
>> Right, it gets stuck at the readline(), is there a function not get
>> stuck to instead of readline().
>
> readline() will keep reading stdout until it received a newline
> character. So if there is nothing to read it will wait forever. The
> solution is to wait with reading until there is actually something to
> read. And it is recommended [1] to use proc.communicate() instead of
> reading directly from stdout.
>
> Also you should use proc.terminate() or proc.kill() to stop the command.
>
> Below is a modified version of your function to show what I mean.
>
> [1] http://docs.python.org/library/subprocess.html#subprocess.Popen.stdin
>
> Greets
> Sander
>
> def host_run(cmd, secs=10):
>    print("running %s" % cmd)
>    timeout = datetime.timedelta(seconds=secs)
>    proc = subprocess.Popen(
>        cmd,
>        stdout=subprocess.PIPE,
>        stderr=subprocess.STDOUT,
>        shell=True)
>    start = datetime.datetime.now()
>    while ( proc.poll() is None and (datetime.datetime.now() - start)
> < timeout): #not timed out
>        print "hello,i'm here"
>        sleep(1)
>    if 0 == proc.poll():
>        stdout, stderr = proc.communicate() #get stdout and stderr
>        print("'%s' is program exited" %cmd)
>        print stdout
>        print stderr
>    else:
>        proc.terminate() #Can use proc.kill() as well

my python verison is 2.5, so has no kill or terminate attribute, so I still use
os.kill(proc.pid, signal.SIGINT) instead

>        stdout, stderr = proc.communicate() #get stdout and stderr


 and I find it will get stuck here

>        print 'Timeout: Process terminated'
>        print stdout
>        print stderr
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] calling setters of superclasses

2010-12-20 Thread Alan Gauld

"Steven D'Aprano"  wrote

I don't use properties in Python very often (hardly ever in fact) 
and I've never used @setter so there may be naming requirements I'm 
not aware of. But in general I'd avoid having two methods with the 
same name.


That's generally good advice, since one will over-write the other, 
but in this specific case, the following is completely bad:


Yep, that's what I suspected might be the case.


If you don't use the same name, chaos reigns:

>>> class Broken(object):
... def __init__(self):
... @property
... def x(self):
... @x.setter
... def set_x(self, value):
...
>>> obj = Broken()
>>> obj.x = 20
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: can't set attribute


All part of the reason I don't particularly like properties in Python.
It feels like it goes against the principle of explicit is better than
implicit... That and the fact that properties encourage a
data-centric view of objects.

Alan G.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] doing maths on lists

2010-12-20 Thread Peter Otten
Dave Angel wrote:

> from math import cos
> 
> JME = 0.4
> L0A = [2.3, 4.65]
> L0B = [1.8, 2.2]
> L0C = [12.1, 4]
> limit = len(L0A)
> 
> L0 = sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(limit))

OP, if you are planning to do this "maths on lists" a lot you should have a 
look at numpy. With numpy Dave's snippet can be written as

from numpy import array, cos, sum

j = 0.4
a = array([2.3, 4.65])
b = array([1.8, 2.2])
c = array([12.1, 4])

print sum(a*cos(b+c*j))

Peter

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Trying to parse a HUGE(1gb) xml file in python

2010-12-20 Thread ashish makani
Hi Python Tutor folks

This is a rather long post, but i wanted to include all the details &
everything i have tried so far myself, so please bear with me & read the
entire boringly long post.

Goal : I am trying to parse a ginormous ( ~ 1gb) xml file.

I am looking for a specific element..there are several 10s/100s occurrences
of that element in the 1gb file.

I need to detect them & then for each 1, i need to copy all the content b/w
the element's start & end tags & create a smaller xml


0. I am a python & xml n00b, s& have been relying on the excellent beginner
book DIP(Dive_Into_Python3 by MP(Mark Pilgrim) Mark , if u are readng
this, you are AWESOME & so is your witty & humorous writing style)

My hardware setup : I have a win7 pro box with 8gb of RAM & intel core2 quad
cpuq9400.
On this i am running sun virtualbox(3.2.12), with ubuntu 10.10(maverick) as
guest os, with 23gb disk space & 2gb(2048mb) ram, assigned to the guest
ubuntu os.

1. Almost all exmaples pf parsing xml in python, i have seen, start off with
these 4 lines of code.

import xml.etree.ElementTree as etree
tree = etree.parse('*path_to_ginormous_xml*')
root = tree.getroot()  #my huge xml has 1 root at the top level
print root

2. In the 2nd line of code above, as Mark explains in DIP, the parse
function builds & returns a tree object, in-memory(RAM), which represents
the entire document.
I tried this code, which works fine for a small ( ~ 1MB), but when i run
this simple 4 line py code in a terminal for my HUGE target file (1GB),
nothing happens.
In a separate terminal, i run the top command, & i can see a python process,
with memory (the VIRT column) increasing from 100MB , all the way upto
2100MB.

I am guessing, as this happens (over the course of 20-30 mins), the tree
representing is being slowly built in memory, but even after 30-40 mins,
nothing happens.
I dont get an error, seg fault or out_of_memory exception.

3. I also tried using lxml, but an lxml tree is much more expensive, as it
retains more info about a node's context, including references to it's
parent.

[http://www.ibm.com/developerworks/xml/library/x-hiperfparse/]

When i ran the same 4line code above, but with lxml's elementree ( using the
import below in line1of the code above)
import lxml.etree as lxml_etree

i can see the memory consumption of the python process(which is running the
code) shoot upto ~ 2700mb & then, python(or the os ?) kills the process as
it nears the total system memory(2gb)

I ran the code from 1 terminal window (screenshot :
http://imgur.com/ozLkB.png)
& ran top from another terminal (http://imgur.com/HAoHA.png)

4. I then investigated some streaming libraries, but am confused - there is
SAX[http://en.wikipedia.org/wiki/Simple_API_for_XML] , the iterparse
interface[http://effbot.org/zone/element-iterparse.htm], & several otehr
options ( minidom)

Which one is the best for my situation ?

Should i instead just open the file, & use reg ex to look for the element i
need ?


Any & all code_snippets/wisdom/thoughts/ideas/suggestions/feedback/comments/
of the Python tutor community would be greatly appreciated.
Plz feel free to email me directly too.

thanks a ton

cheers
ashish

email :
ashish.makani
domain:gmail.com

p.s.
Other useful links on xml parsing in python
0. http://diveintopython3.org/xml.html
1.
http://stackoverflow.com/questions/1513592/python-is-there-an-xml-parser-implemented-as-a-generator
2. http://codespeak.net/lxml/tutorial.html
3.
https://groups.google.com/forum/?hl=en&lnk=gst&q=parsing+a+huge+xml#!topic/comp.lang.python/CMgToEnjZBk
4. http://www.ibm.com/developerworks/xml/library/x-hiperfparse/
5.http://effbot.org/zone/element-index.htm
http://effbot.org/zone/element-iterparse.htm
6. SAX : http://en.wikipedia.org/wiki/Simple_API_for_XML
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trying to parse a HUGE(1gb) xml file in python

2010-12-20 Thread Luke Paireepinart
If you can assume a well formatted file I would just parse it linearly, should 
be much faster. Read the file in as lines if the XML is already in human 
readable form, or just read in blocks and append to a list and do a join() when 
you have a whole match.

-
Sent from a mobile device with a bad e-mail client.
-

On Dec 20, 2010, at 2:08 PM, ashish makani  wrote:

> 
> Hi Python Tutor folks
> This is a rather long post, but i wanted to include all the details & 
> everything i have tried so far myself, so please bear with me & read the 
> entire boringly long post.
> 
> Goal : I am trying to parse a ginormous ( ~ 1gb) xml file.
> 
> I am looking for a specific element..there are several 10s/100s occurrences 
> of that element in the 1gb file.
> 
> I need to detect them & then for each 1, i need to copy all the content b/w 
> the element's start & end tags & create a smaller xml
> 
> 
> 0. I am a python & xml n00b, s& have been relying on the excellent beginner 
> book DIP(Dive_Into_Python3 by MP(Mark Pilgrim) Mark , if u are readng 
> this, you are AWESOME & so is your witty & humorous writing style)
> 
> My hardware setup : I have a win7 pro box with 8gb of RAM & intel core2 quad 
> cpuq9400.
> On this i am running sun virtualbox(3.2.12), with ubuntu 10.10(maverick) as 
> guest os, with 23gb disk space & 2gb(2048mb) ram, assigned to the guest 
> ubuntu os.
> 
> 
> 1. Almost all exmaples pf parsing xml in python, i have seen, start off with 
> these 4 lines of code.
> 
> import xml.etree.ElementTree as etree
> tree = etree.parse('*path_to_ginormous_xml*')
> root = tree.getroot()  #my huge xml has 1 root at the top level
> print root
> 
> 2. In the 2nd line of code above, as Mark explains in DIP, the parse function 
> builds & returns a tree object, in-memory(RAM), which represents the entire 
> document.
> I tried this code, which works fine for a small ( ~ 1MB), but when i run this 
> simple 4 line py code in a terminal for my HUGE target file (1GB), nothing 
> happens.
> In a separate terminal, i run the top command, & i can see a python process, 
> with memory (the VIRT column) increasing from 100MB , all the way upto 2100MB.
> 
> I am guessing, as this happens (over the course of 20-30 mins), the tree 
> representing is being slowly built in memory, but even after 30-40 mins, 
> nothing happens.
> I dont get an error, seg fault or out_of_memory exception.
> 
> 3. I also tried using lxml, but an lxml tree is much more expensive, as it 
> retains more info about a node's context, including references to it's parent.
> 
> [http://www.ibm.com/developerworks/xml/library/x-hiperfparse/]
> 
> When i ran the same 4line code above, but with lxml's elementree ( using the 
> import below in line1of the code above)
> import lxml.etree as lxml_etree
> 
> i can see the memory consumption of the python process(which is running the 
> code) shoot upto ~ 2700mb & then, python(or the os ?) kills the process as it 
> nears the total system memory(2gb)
> 
> I ran the code from 1 terminal window (screenshot :http://imgur.com/ozLkB.png)
> & ran top from another terminal (http://imgur.com/HAoHA.png)
> 
> 4. I then investigated some streaming libraries, but am confused - there is 
> SAX[http://en.wikipedia.org/wiki/Simple_API_for_XML] , the iterparse 
> interface[http://effbot.org/zone/element-iterparse.htm], & several otehr 
> options ( minidom)
> 
> Which one is the best for my situation ?
> 
> Should i instead just open the file, & use reg ex to look for the element i 
> need ?
> 
> 
> 
> Any & all code_snippets/wisdom/thoughts/ideas/suggestions/feedback/comments/ 
> of the Python tutor community would be greatly appreciated.
> Plz feel free to email me directly too.
> 
> thanks a ton
> 
> cheers
> ashish
> 
> email : 
> ashish.makani
> domain:gmail.com
> 
> p.s.
> Other useful links on xml parsing in python
> 0. http://diveintopython3.org/xml.html
> 1. 
> http://stackoverflow.com/questions/1513592/python-is-there-an-xml-parser-implemented-as-a-generator
> 2. http://codespeak.net/lxml/tutorial.html
> 3. 
> https://groups.google.com/forum/?hl=en&lnk=gst&q=parsing+a+huge+xml#!topic/comp.lang.python/CMgToEnjZBk
> 4. http://www.ibm.com/developerworks/xml/library/x-hiperfparse/
> 5.http://effbot.org/zone/element-index.htm
> http://effbot.org/zone/element-iterparse.htm
> 6. SAX : http://en.wikipedia.org/wiki/Simple_API_for_XML
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trying to parse a HUGE(1gb) xml file in python

2010-12-20 Thread Steven D'Aprano

ashish makani wrote:


Goal : I am trying to parse a ginormous ( ~ 1gb) xml file.


I sympathize with you. I wonder who thought that building a 1GB XML file 
was a good thing.


Forget about using any XML parser that reads the entire file into 
memory. By the time that 1GB of text is read and parsed, you will 
probably have something about 6-8GB (estimated) in size.



[...]

My hardware setup : I have a win7 pro box with 8gb of RAM & intel core2 quad
cpuq9400.


In order to access 8GB of RAM, you'll be running a 64-bit OS, correct? 
In this case, you should expect double the memory usage of the XML 
object to (estimated) 12-16GB.




I am guessing, as this happens (over the course of 20-30 mins), the tree
representing is being slowly built in memory, but even after 30-40 mins,
nothing happens.


It's probably not finished. Leave it another hour or so and you'll get 
an out of memory error.




4. I then investigated some streaming libraries, but am confused - there is
SAX[http://en.wikipedia.org/wiki/Simple_API_for_XML] , the iterparse
interface[http://effbot.org/zone/element-iterparse.htm], & several otehr
options ( minidom)

Which one is the best for my situation ?


You absolutely need to use a streaming library. element-iterparse still 
builds the tree, so that's no use to you. I believe you should use SAX 
or minidom, but that's about my limit of knowledge of streaming XML parsers.




Should i instead just open the file, & use reg ex to look for the element i
need ?


That's likely to need less memory than building a parse tree, but still 
a huge amount of memory. And you don't know how complex the XML is, in 
general you *can't* correctly parse arbitrary XML with regular 
expressions (although you can for simple examples). Stick with the right 
tool for the job, the streaming XML library.



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trying to parse a HUGE(1gb) xml file in python

2010-12-20 Thread Brett Ritter
On Mon, Dec 20, 2010 at 4:19 PM, Steven D'Aprano  wrote:
>> Goal : I am trying to parse a ginormous ( ~ 1gb) xml file.
>
> I sympathize with you. I wonder who thought that building a 1GB XML file was
> a good thing.

XML is like violence: if it isn't working, try more.

-- 
Brett Ritter / SwiftOne
swift...@swiftone.org
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trying to parse a HUGE(1gb) xml file in python

2010-12-20 Thread Sithembewena Lloyd Dube
[?] Brett, that was very mischievous.

I wish I could help - am watching this thread with great curiosity, I could
learn something from it myself.

On Mon, Dec 20, 2010 at 11:40 PM, Brett Ritter wrote:

> On Mon, Dec 20, 2010 at 4:19 PM, Steven D'Aprano 
> wrote:
> >> Goal : I am trying to parse a ginormous ( ~ 1gb) xml file.
> >
> > I sympathize with you. I wonder who thought that building a 1GB XML file
> was
> > a good thing.
>
> XML is like violence: if it isn't working, try more.
>
> --
> Brett Ritter / SwiftOne
> swift...@swiftone.org
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Regards,
Sithembewena Lloyd Dube
<<338.gif>>___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trying to parse a HUGE(1gb) xml file in python

2010-12-20 Thread Steven D'Aprano

Brett Ritter wrote:

On Mon, Dec 20, 2010 at 4:19 PM, Steven D'Aprano  wrote:

Goal : I am trying to parse a ginormous ( ~ 1gb) xml file.

I sympathize with you. I wonder who thought that building a 1GB XML file was
a good thing.


XML is like violence: if it isn't working, try more.



I love it -- may I quote you?


--
Steven


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trying to parse a HUGE(1gb) xml file in python

2010-12-20 Thread Brett Ritter
On Mon, Dec 20, 2010 at 5:32 PM, Steven D'Aprano  wrote:
>> XML is like violence: if it isn't working, try more.
>
> I love it -- may I quote you?

I can't claim credit for it, I saw originally saw it on some sigs on
Slashdot a few years ago.  It certainly matches the majority of XML
usage I've encountered.

As to the original post: Yes, as others have suggested you're going to
want an event-based parser along the lines of SAX.  Sadly (for you)
this means a mental shift in how you address your code, but it's not
terrible - just different.

-- 
Brett Ritter / SwiftOne
swift...@swiftone.org
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trying to parse a HUGE(1gb) xml file in python

2010-12-20 Thread Alan Gauld

"ashish makani"  wrote

I am looking for a specific element..there are several 10s/100s 
occurrences

of that element in the 1gb file.

I need to detect them & then for each 1, i need to copy all the 
content b/w

the element's start & end tags & create a smaller xml


This is exactly what sax and its kin are for. If you wanted to 
manipulate
the xml data and recreate the original file tree based is better but 
for this

kind of one shot processing SAX will be much much faster.

The concept is simple enough if you have ever used awk to process
text files. (or the Python HTMLParser) You define a function that gets
triggered when the parser detects a matching tag.

My hardware setup : I have a win7 pro box with 8gb of RAM & intel 
core2 quad

cpuq9400.
On this i am running sun virtualbox(3.2.12), with ubuntu 
10.10(maverick) as
guest os, with 23gb disk space & 2gb(2048mb) ram, assigned to the 
guest

ubuntu os.


Obviously running the code in the virtuial machjine is limiting your
ability to deal with the data but in this case you would be pushing
hard to build the entire tree in RAM anyway so it probably doesn't
matter.

4. I then investigated some streaming libraries, but am confused - 
there is

SAX[http://en.wikipedia.org/wiki/Simple_API_for_XML] ,



Which one is the best for my situation ?


I've only used sax - I tried minidom once but couldn't get it to work
as I wanted so went back to sax... There are lots of examples of
xml parsing using sax, both in Python and Java - just google.

Should i instead just open the file, & use reg ex to look for the 
element i

need ?


Unless the xml is very simple you would probably find yourself
creating a bigger problem. regex's are not good at handling the
kinds of recursive data structures as can be found in SGML
based languages.

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trying to parse a HUGE(1gb) xml file in python

2010-12-20 Thread ashish makani
Thanks Luke, Steve, Brett, Lloyd & Alan
for your prompt responses & sharing your wisdom.

I <3 the python community... You(We ?) folks are AWESOME

I cross-posted this query on comp.lang.python
I bet most of you hang @ c.l.p too, but just in case, here is the link to
the discussion at c.l.p
https://groups.google.com/d/topic/comp.lang.python/i816mDMSoXM/discussion

Thanks again for the amazing help & advice

cheers
ashish


On Mon, Dec 20, 2010 at 5:13 PM, Alan Gauld wrote:

> "ashish makani"  wrote
>
>  I am looking for a specific element..there are several 10s/100s
>> occurrences
>> of that element in the 1gb file.
>>
>> I need to detect them & then for each 1, i need to copy all the content
>> b/w
>> the element's start & end tags & create a smaller xml
>>
>
> This is exactly what sax and its kin are for. If you wanted to manipulate
> the xml data and recreate the original file tree based is better but for
> this
> kind of one shot processing SAX will be much much faster.
>
> The concept is simple enough if you have ever used awk to process
> text files. (or the Python HTMLParser) You define a function that gets
> triggered when the parser detects a matching tag.
>
>
>  My hardware setup : I have a win7 pro box with 8gb of RAM & intel core2
>> quad
>> cpuq9400.
>> On this i am running sun virtualbox(3.2.12), with ubuntu 10.10(maverick)
>> as
>> guest os, with 23gb disk space & 2gb(2048mb) ram, assigned to the guest
>> ubuntu os.
>>
>
> Obviously running the code in the virtuial machjine is limiting your
> ability to deal with the data but in this case you would be pushing
> hard to build the entire tree in RAM anyway so it probably doesn't
> matter.
>
>
>  4. I then investigated some streaming libraries, but am confused - there
>> is
>> SAX[http://en.wikipedia.org/wiki/Simple_API_for_XML] ,
>>
>
>  Which one is the best for my situation ?
>>
>
> I've only used sax - I tried minidom once but couldn't get it to work
> as I wanted so went back to sax... There are lots of examples of
> xml parsing using sax, both in Python and Java - just google.
>
>
>  Should i instead just open the file, & use reg ex to look for the element
>> i
>> need ?
>>
>
> Unless the xml is very simple you would probably find yourself
> creating a bigger problem. regex's are not good at handling the
> kinds of recursive data structures as can be found in SGML
> based languages.
>
> HTH,
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


*

"We act as though comfort and luxury were the chief requirements of life,
when all that we need to make us happy is something to be enthusiastic
about."
-- Albert Einstein*
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trying to parse a HUGE(1gb) xml file in python

2010-12-20 Thread Chris Fuller

This isn't XML, it's an abomination of XML.  Best to not treat it as XML.  
Good thing you're only after one class of tags.  Here's what I'd do.  I'll 
give a general solution, but there are two parameters / four cases that could 
make the code simpler, I'll just point them out at the end.

Iterate over the file descriptor, reading in line-by-line.  This will be slow 
on a huge file, but probably not so bad if you're only doing it once.  It makes 
the rest easier.  Knuth has some sage advice on this point (*) :)  Some 
feedback on progress to the user can be helpful here, if it is slow.

Keep track of your offset into the file.  There are two ways: use the tell() 
method of the file descriptor (but you will have to subtract the length of the 
current line), or just add up the line lengths as you process them.

Scan each line for the open tag.  Add the offset to the tag to the offset 
within 
the file of  the current line, and push that to a stack.  Scan for the end tag, 
when you find one, pop an address from the stack, and put the two (start/end) 
addresses a list for later.  Keep doing this until you run out of file.

Now, take that list, and pull off the address-pairs; seek() and read() them 
directly.  Lather, rinse, repeat.

Some off-the-cuff untested code:

stk = []
yummydataaddrs = []

fileoff = 0

fd = open('ginormous.xml', 'r')
for line in fd:
lineoff = line.index(start_tag)
if fileoff != -1:
stk.append(fileoff+lineoff)

lineoff = line.index(end_tag)
if lineoff != -1:
yummydataaddr.append( (stk.pop(-1), fileoff+lineoff) )

fileoff += len(line)

for start,end in yummydataaddrs:
fd.seek(start)
print "here's your stupid data:", fd.read(end-start+1)


You can simplify a bit if the tags are one a line by themselves, since you 
don't have to keep track of the offset with the line of the tag.  The other 
simplification is if they aren't nested.  You don't need to mess around with a 
stack in this case.


(*) "Premature optimization is the root of all evil."


Cheers
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trying to parse a HUGE(1gb) xml file in python

2010-12-20 Thread ashish makani
Chris

This block of code made my day - especially yummydataaddrs & "here's your
stupid data"

> for start,end in yummydataaddrs:
>fd.seek(start)
>print "here's your stupid data:", fd.read(end-start+1)


Nothing is more impressive than solid code, with a good sense of humor.

Thanks for the code & especially since i am in a time crunch, this approach,
might get me what i need more quickly.

Thanks also for Knuth's awesome quote & reminded me of my stanford friend
who told me that Prof. Knuth, still holds a christmas tree lecture every
year...unfortunately inspite of being in the bay area this year, i missed it
:(
http://stanford-online.stanford.edu/seminars/knuth/101206-knuth-500.asx

Thanks a ton

cheers
ashish

p.s. To everybody

OT(off_topic): I moved to the bay area recently & am passionate about
technology in general & linux, python, c, embedded, mobile, wireless
stuff,.
I was wondering if any of you guys, are part of some bay area python( or
other tech) meetup ( as in do you guys meetup, in person) for like a tech
talk / discussion / brainstorming/ hack nights ?
If yes, i would love to know more & be a part of it

On Mon, Dec 20, 2010 at 9:27 PM, Chris Fuller  wrote:

>
> This isn't XML, it's an abomination of XML.  Best to not treat it as XML.
> Good thing you're only after one class of tags.  Here's what I'd do.  I'll
> give a general solution, but there are two parameters / four cases that
> could
> make the code simpler, I'll just point them out at the end.
>
> Iterate over the file descriptor, reading in line-by-line.  This will be
> slow
> on a huge file, but probably not so bad if you're only doing it once.  It
> makes
> the rest easier.  Knuth has some sage advice on this point (*) :)  Some
> feedback on progress to the user can be helpful here, if it is slow.
>
> Keep track of your offset into the file.  There are two ways: use the
> tell()
> method of the file descriptor (but you will have to subtract the length of
> the
> current line), or just add up the line lengths as you process them.
>
> Scan each line for the open tag.  Add the offset to the tag to the offset
> within
> the file of  the current line, and push that to a stack.  Scan for the end
> tag,
> when you find one, pop an address from the stack, and put the two
> (start/end)
> addresses a list for later.  Keep doing this until you run out of file.
>
> Now, take that list, and pull off the address-pairs; seek() and read() them
> directly.  Lather, rinse, repeat.
>
> Some off-the-cuff untested code:
>
> stk = []
> yummydataaddrs = []
>
> fileoff = 0
>
> fd = open('ginormous.xml', 'r')
> for line in fd:
>lineoff = line.index(start_tag)
>if fileoff != -1:
>stk.append(fileoff+lineoff)
>
>lineoff = line.index(end_tag)
>if lineoff != -1:
>yummydataaddr.append( (stk.pop(-1), fileoff+lineoff) )
>
>fileoff += len(line)
>
> for start,end in yummydataaddrs:
>fd.seek(start)
>print "here's your stupid data:", fd.read(end-start+1)
>
>
> You can simplify a bit if the tags are one a line by themselves, since you
> don't have to keep track of the offset with the line of the tag.  The other
> simplification is if they aren't nested.  You don't need to mess around
> with a
> stack in this case.
>
>
> (*) "Premature optimization is the root of all evil."
>
>
> Cheers
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



*"We act as though comfort and luxury were the chief requirements of life,
when all that we need to make us happy is something to be enthusiastic
about."
-- Albert Einstein*
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] decimal input problem

2010-12-20 Thread jtl999
when I try to multiply with a decimal  number in python with the input
this is what i get

 MathCheats Times-Ed by jtl999
Not for decimal numbers due to a bug in the code.
Enter first number: 1.2
Traceback (most recent call last):
  File "Timesed.py", line 18, in 
numberx1 = (int)(raw_input('Enter first number: '))
ValueError: invalid literal for int() with base 10: '1.2'

here is the code
#!/usr/bin/python
# MathCheats Times-Ed by jtl999
# (C) 2010 jtl999
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see .
# The GPL is in the file COPYING
print ("MathCheats Times-Ed by jtl999")
print ("Not for decimal numbers due to a bug in the code.")
numberx1 = (int)(raw_input('Enter first number: '))
numberx2 = (int)(raw_input('Enter second number: '))
print ("The answer was")
print numberx1*numberx2


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with print

2010-12-20 Thread David Hutto
On Mon, Dec 20, 2010 at 5:15 AM, Steven D'Aprano  wrote:
> David Hutto wrote:
>>
>> On Sun, Dec 19, 2010 at 4:33 PM, Hugo Arts  wrote:
>>>
>>> On Sun, Dec 19, 2010 at 10:11 PM, Sander Sweers 
>>> wrote:

 On 19 December 2010 21:54, jtl999  wrote:
>
>  File "GettingStarted.py", line 91
>   print ("Lesson Two")
>       ^
> SyntaxError: invalid syntax
>
> [...]
>
>> Apparently so, but I'd like to see the full code to know what caused
>> the error to point to print.
>
> It is very common for an error in parentheses or brackets to lead to a
> syntax error on the following line.
>
 compile("""
> ... x = 1
> ... y = (x+1/2
> ... z = 3
> ... """, "", "exec")
> Traceback (most recent call last):
>  File "", line 5, in 
>  File "", line 4
>    z = 3
>    ^
> SyntaxError: invalid syntax

I was used to the tutorials from 3 showing the correct print(), and
seeing the errors for print 'whatever', when i accidentally used them
in 3, that I used them exclusively after seeing the 3 tutorials, and
that it could be used as both a function and a statement(correct me if
the term is wrong) in 2.

It's important to note that had the op provided some code to go along
with the error, that it might have been spotted more sooner by a less
experienced eye.

>
>
> --
> Steven
>
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
They're installing the breathalyzer on my email account next week.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with print

2010-12-20 Thread Hugo Arts
On Tue, Dec 21, 2010 at 5:35 AM, David Hutto  wrote:
>
> I was used to the tutorials from 3 showing the correct print(), and
> seeing the errors for print 'whatever', when i accidentally used them
> in 3, that I used them exclusively after seeing the 3 tutorials, and
> that it could be used as both a function and a statement(correct me if
> the term is wrong) in 2.
>

Note that print can actually only be used as a statement in python 2.
It just so happens that you can include parentheses in the statement.
Though that makes it look similar to a function call, it certainly is
not. You'll see that when you try to supply multiple arguments to the
"function." This makes python suddenly interpret the parentheses as a
tuple, making it obviously not a function call.

Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with print

2010-12-20 Thread David Hutto
>
> Note that print can actually only be used as a statement in python 2.
> It just so happens that you can include parentheses in the statement.
> Though that makes it look similar to a function call, it certainly is
> not. You'll see that when you try to supply multiple arguments to the
> "function." This makes python suddenly interpret the parentheses as a
> tuple, making it obviously not a function call.

However, it's declared as a NoneType in Python 3.1.2:

>>> x = print('test','test1')
test test1
>>> x
>>> type(x)

>>>

I haven't gone further than that to test it's 'tupleness', or that
might not have been what you meant by it being 'interpreted as a
tuple'.



>
> Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Was: Trying to parse a HUGE(1gb) xml file in python Now: OT, Hackerspaces

2010-12-20 Thread Tino Dai
Hi Ashish,

Check out Noisebridge (
https://www.*noisebridge*.net/) in
SF. I think you will find there are like minded tech people there. It also
has Mitch Altman (
http://en.wikipedia.org/wiki/*Mitch*_*Altman*
) is one of the founders/original members.

-Tino
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with print

2010-12-20 Thread Hugo Arts
On Tue, Dec 21, 2010 at 6:08 AM, David Hutto  wrote:
>>
>> Note that print can actually only be used as a statement in python 2.
>> It just so happens that you can include parentheses in the statement.
>> Though that makes it look similar to a function call, it certainly is
>> not. You'll see that when you try to supply multiple arguments to the
>> "function." This makes python suddenly interpret the parentheses as a
>> tuple, making it obviously not a function call.
>
> However, it's declared as a NoneType in Python 3.1.2:
>
 x = print('test','test1')
> test test1
 x
 type(x)
> 

>
> I haven't gone further than that to test it's 'tupleness', or that
> might not have been what you meant by it being 'interpreted as a
> tuple'.
>

Sorry, I haven't made myself clear. In python3, print is a function,
and it return None. I'm not talking about python3 now, though.
Everything below will refer to python 2.x

In python2, print is a statement. The print statement in python2 can
sometimes look like a function call, but it never is:

Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello")
hello

See? it looks like a function call, but it actually isn't. for proof,
try to assign its return value:

>>> a = print("hello")
  File "", line 1
a = print("hello")
^
SyntaxError: invalid syntax

The parentheses that make this thing look like a function call so much
are just regular old parentheses. This becomes apparent when we
compare the output from these two statements:

>>> print('a', 'b')
('a', 'b')
>>> print 'a', 'b'
a b
>>>

The parentheses are part of what's being printed now! That's because
('a', 'b') is interpreted as a tuple in this case. In python3, it
would have been interpreted as a call to the print function, with
arguments 'a' and 'b'. But in python2 you can't even have a function
named print, because print is a keyword used in the print statement.

Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor