[Tutor] decorators, __call__ (able) objects

2009-07-15 Thread Todd Matsumoto
Hi,

Can some one give, or point to some good examples of how @decorators work, and 
__call__ (able) objects? I'm having trouble getting my head around these 
techniques (concepts).

Cheers,

T
-- 
Neu: GMX Doppel-FLAT mit Internet-Flatrate + Telefon-Flatrate
für nur 19,99 Euro/mtl.!* http://portal.gmx.net/de/go/dsl02
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] decorators, __call__ (able) objects

2009-07-15 Thread Kent Johnson
On Wed, Jul 15, 2009 at 7:41 AM, Todd Matsumoto wrote:
> Hi,
>
> Can some one give, or point to some good examples of how @decorators work, 
> and __call__ (able) objects?

Decorators:
http://personalpages.tds.net/~kent37/kk/1.html

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to pass command line variables to this python code...

2009-07-15 Thread vince spicer
good catch, my mistake

args[1] == 'yankees'
True


On Wed, Jul 15, 2009 at 12:25 AM, Christian Witts wrote:

> vince spicer wrote:
>
>> First off, selenium is a great tool and the python driver is very powerful
>>
>> there are numerous ways to access cli variables,
>>
>> the quickest
>>
>> import sys
>> print sys.srgv
>>
>> sys.argv will it output a array of all command line args
>>
>> ./selenium-google-test.py yankees
>> will out put:
>>
>> ['selenium-google-test.py', 'yankees']
>>
>> so
>>
>> args = sys.argv
>>
>> args[0] == 'yankees'
>> True
>>
> That would be false, the first argument (list index zero) is the script
> name.  You would need to do
> args = sys.argv[1:]
> if you want to dump the filename from the list.
>
> --
> Kind Regards,
> Christian Witts
>
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] decorators, __call__ (able) objects

2009-07-15 Thread vince spicer
simple example of calling a class


class myKlass(object):



On Wed, Jul 15, 2009 at 6:33 AM, Kent Johnson  wrote:

> On Wed, Jul 15, 2009 at 7:41 AM, Todd Matsumoto wrote:
> > Hi,
> >
> > Can some one give, or point to some good examples of how @decorators
> work, and __call__ (able) objects?
>
> Decorators:
> http://personalpages.tds.net/~kent37/kk/1.html
>
> Kent
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] decorators, __call__ (able) objects

2009-07-15 Thread vince spicer
simple example of calling a class

class myKlass(object):

def __call__(self, *args, **kws):
 print "i was called"


>> test = myKlass()

>> test()

>> i was called


>
>
>
> On Wed, Jul 15, 2009 at 6:33 AM, Kent Johnson  wrote:
>
>> On Wed, Jul 15, 2009 at 7:41 AM, Todd Matsumoto
>> wrote:
>> > Hi,
>> >
>> > Can some one give, or point to some good examples of how @decorators
>> work, and __call__ (able) objects?
>>
>> Decorators:
>> http://personalpages.tds.net/~kent37/kk/1.html
>>
>> Kent
>> ___
>> Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] just one question

2009-07-15 Thread amrita
Hi,

i want to ask one thing that suppose i have a .txt file having content
like:---


 47 8   ALA   H H  7.85 0.02 1
 48 8   ALA   HAH  2.98 0.02 1
 49 8   ALA   HBH  1.05 0.02 1
 50 8   ALA   C C179.39  0.3 1
 51 8   ALA   CAC 54.67  0.3 1
 52 8   ALA   CBC 18.85  0.3 1
 53 8   ALA   N N123.95  0.3 1
10715   ALA   H H  8.05 0.02 1
10815   ALA   HAH  4.52 0.02 1
10915   ALA   HBH  1.29 0.02 1
11015   ALA   C C177.18  0.3 1
11115   ALA   CAC 52.18  0.3 1
11215   ALA   CBC 20.64  0.3 1
11315   ALA   N N119.31  0.3 1
15421   ALA   H H  7.66 0.02 1
15521   ALA   HAH  4.05 0.02 1
15621   ALA   HBH  1.39 0.02 1
15721   ALA   C C179.35  0.3 1
15821   ALA   CAC 54.33  0.3 1

now what i want that i will make another .txt file in which first it will
write the position of ALA lets say 8, 15, 21 then its name ALA and then
the fifth column value for only three atoms C,CA and CB.

Means it will be someting like:

8  ALA  C = 179.39  CA = 54.67  CB = 18.85
15 ALA  C = 177.18  CA = 52.18  CB = 20.64
21 ALA  C = 179.35  CA = 54.33  CB =

if some value is not there then it will leave that as blank.I am new in
python but this is what we want, so how can i do it using python script.





Amrita Kumari
Research Fellow
IISER Mohali
Chandigarh
INDIA

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] just one question

2009-07-15 Thread vince spicer
one way is:

import re

infile = open("test.txt", "r") #: open read mode
outfile = open("out.tx", "w") #: open write mode

for line in infile:
values = re.split("\s+", line) # split values on spaces EX: ['47', '8',
'ALA', 'H', 'H', '7.85', '0.02', '1']
outfile.write("%s  %s C =  %s  CA =  %s CB = %s" % (values[1],
values[2], values[5], values[6], values[7]))

infile.close()
outfile.close()

not tested but should work

Vince

On Wed, Jul 15, 2009 at 6:24 AM,  wrote:

> Hi,
>
> i want to ask one thing that suppose i have a .txt file having content
> like:---
>
>
>  47 8   ALA   H H  7.85 0.02 1
>  48 8   ALA   HAH  2.98 0.02 1
>  49 8   ALA   HBH  1.05 0.02 1
>  50 8   ALA   C C179.39  0.3 1
>  51 8   ALA   CAC 54.67  0.3 1
>  52 8   ALA   CBC 18.85  0.3 1
>  53 8   ALA   N N123.95  0.3 1
> 10715   ALA   H H  8.05 0.02 1
> 10815   ALA   HAH  4.52 0.02 1
> 10915   ALA   HBH  1.29 0.02 1
> 11015   ALA   C C177.18  0.3 1
> 11115   ALA   CAC 52.18  0.3 1
> 11215   ALA   CBC 20.64  0.3 1
> 11315   ALA   N N119.31  0.3 1
> 15421   ALA   H H  7.66 0.02 1
> 15521   ALA   HAH  4.05 0.02 1
> 15621   ALA   HBH  1.39 0.02 1
> 15721   ALA   C C179.35  0.3 1
> 15821   ALA   CAC 54.33  0.3 1
>
> now what i want that i will make another .txt file in which first it will
> write the position of ALA lets say 8, 15, 21 then its name ALA and then
> the fifth column value for only three atoms C,CA and CB.
>
> Means it will be someting like:
>
> 8  ALA  C = 179.39  CA = 54.67  CB = 18.85
> 15 ALA  C = 177.18  CA = 52.18  CB = 20.64
> 21 ALA  C = 179.35  CA = 54.33  CB =
>
> if some value is not there then it will leave that as blank.I am new in
> python but this is what we want, so how can i do it using python script.
>
>
>
>
>
> Amrita Kumari
> Research Fellow
> IISER Mohali
> Chandigarh
> INDIA
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] objects becoming pointers

2009-07-15 Thread chris Hynes

I guess I have to start somewhere to ask

I want the user to input a name, say "Chris". I know I can use the code:

name=raw_input()

I now want:

"Chris"=zeros((3,3))

so that when I type:

print Chris

the return will be an array of zero's 3x3

So that I can understand this deeper, I know that "name" is just a pointer to 
the object "Chris". I don't want to just change the pointer to something else, 
like "zeros((3,3))" but I want to make "Chris" become the pointer or the name 
of the pointer. At least that's what I think I want.

_
Windows Live™ SkyDrive™: Get 25 GB of free online storage.
http://windowslive.com/online/skydrive?ocid=TXT_TAGLM_WL_SD_25GB_062009___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] just one question

2009-07-15 Thread Rich Lovely
2009/7/15 vince spicer :
> one way is:
>
> import re
>
> infile = open("test.txt", "r") #: open read mode
> outfile = open("out.tx", "w") #: open write mode
>
> for line in infile:
>     values = re.split("\s+", line) # split values on spaces EX: ['47', '8',
> 'ALA', 'H', 'H', '7.85', '0.02', '1']
>     outfile.write("%s  %s C =  %s  CA =  %s CB = %s" % (values[1],
> values[2], values[5], values[6], values[7]))
>
> infile.close()
> outfile.close()
>
> not tested but should work
>
> Vince
>

That isn't what they're after at all.

Something more like

from __future__ import with_statement #only works on version 2.5 and later
from collections import defaultdict
from decimal import Decimal

atoms = defaultdict(dict)

with open("INPUT_FILENAME_HERE") as f:
for line in f:
n, pos, ala, at, symb, weight, rad, count = line.split()
atoms[int(pos)][at] = Decimal(weight)


#modify these lines to fit your needs:
positionsNeeded = (8, 15, 21)
atomsNeeded = ("C", "CA", "CB")


for position in positionsNeeded:
print position, "ALA C = %s CA = %s CB = %s" %
tuple(atoms[position].get(a,"") for a in atomsNeeded)

You would then call it with something like

$python myscipt.py > output.txt

-- 
Rich "Roadie Rich" Lovely
There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] objects becoming pointers

2009-07-15 Thread Rich Lovely
2009/7/15 chris Hynes :
> I guess I have to start somewhere to ask
>
> I want the user to input a name, say "Chris". I know I can use the code:
>
> name=raw_input()
>
> I now want:
>
> "Chris"=zeros((3,3))
>
> so that when I type:
>
> print Chris
>
> the return will be an array of zero's 3x3
>
> So that I can understand this deeper, I know that "name" is just a pointer
> to the object "Chris". I don't want to just change the pointer to something
> else, like "zeros((3,3))" but I want to make "Chris" become the pointer or
> the name of the pointer. At least that's what I think I want.
>
> 
> Windows Live™ SkyDrive™: Get 25 GB of free online storage. Get it on your
> BlackBerry or iPhone.
> ___
> Tutor maillist  -  tu...@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>

Sorry if I've sent this twice...

Why would you want to do that?

The closest you can get to that is using exec, but exec is usually
considered a code smell.  I'd say you're trying to do the wrong thing.

-- 
Rich "Roadie Rich" Lovely
There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to pass command line variables to this python code...

2009-07-15 Thread J Cook
Yeah, I figured that. I got it to work thanks, but I still don't 
understand how exactly. Coming from Perl I am used to a more procedural 
type of programming. BTW - Selenium is a great tool for web testing, and 
the way it will translate your web clickstream into your choice of 
languages rocks.


Justin

vince spicer wrote:

good catch, my mistake

args[1] == 'yankees'
True


On Wed, Jul 15, 2009 at 12:25 AM, Christian Witts 
mailto:cwi...@compuscan.co.za>> wrote:


vince spicer wrote:

First off, selenium is a great tool and the python driver is
very powerful

there are numerous ways to access cli variables,

the quickest

import sys
print sys.srgv

sys.argv will it output a array of all command line args

./selenium-google-test.py yankees
will out put:

['selenium-google-test.py', 'yankees']

so

args = sys.argv

args[0] == 'yankees'
True

That would be false, the first argument (list index zero) is the
script name.  You would need to do
args = sys.argv[1:]
if you want to dump the filename from the list.

-- 
Kind Regards,

Christian Witts




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] objects becoming pointers

2009-07-15 Thread bob gailer

chris Hynes wrote:

I guess I have to start somewhere to ask

I want the user to input a name, say "Chris". I know I can use the code:

name=raw_input()

I now want:

"Chris"=zeros((3,3))


This is a FAQ. In Python one is discouraged from dynamically creating 
variable names. Preferred is a dictionary.


names = {} # empty dictionary
name=raw_input()
names[name]=zeroes((3,3))
print names["Chris"]

--
Bob Gailer
Chapel Hill NC
919-636-4239
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] objects becoming pointers

2009-07-15 Thread vince spicer
not sure exactly why you would want to that, but you could assign attributes
to a class

EX:

class storage:
pass

>> store = Storage()
>> name=raw_input()

>> setattr(store, name, zeros(3,3))

>> print store.Chris


On Wed, Jul 15, 2009 at 9:19 AM, chris Hynes  wrote:

>  I guess I have to start somewhere to ask
>
> I want the user to input a name, say "Chris". I know I can use the code:
>
> name=raw_input()
>
> I now want:
>
> "Chris"=zeros((3,3))
>
> so that when I type:
>
> print Chris
>
> the return will be an array of zero's 3x3
>
> So that I can understand this deeper, I know that "name" is just a pointer
> to the object "Chris". I don't want to just change the pointer to something
> else, like "zeros((3,3))" but I want to make "Chris" become the pointer or
> the name of the pointer. At least that's what I think I want.
>
> --
> Windows Live™ SkyDrive™: Get 25 GB of free online storage. Get it on your
> BlackBerry or 
> iPhone.
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] objects becoming pointers

2009-07-15 Thread Kent Johnson
On Wed, Jul 15, 2009 at 11:19 AM, chris Hynes wrote:
> I guess I have to start somewhere to ask
>
> I want the user to input a name, say "Chris". I know I can use the code:
>
> name=raw_input()
>
> I now want:
>
> "Chris"=zeros((3,3))
>
> so that when I type:
>
> print Chris
>
> the return will be an array of zero's 3x3

The usual way to solve this is to use a dictionary:
name = raw_input()
values = dict()
values[name] = zeros((3, 3))
print values[name]

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] just one question

2009-07-15 Thread wesley chun
On Wed, Jul 15, 2009 at 8:29 AM, Rich Lovely wrote:
> 2009/7/15 vince spicer :
>>:
>> import re
>>:
>>     values = re.split("\s+", line) # split values on spaces EX: ['47', '8',
>
> That isn't what they're after at all.
> Something more like
> :
>        n, pos, ala, at, symb, weight, rad, count = line.split()
> :


on a side note, i also discourage the use of regular expressions for
this problem when a simpler solution like line.split() will suffice.
bringing up an old jwz quote: 'Some people, when confronted with a
problem, think “I know, I'll use regular expressions.” Now they have
two problems.'

best regards,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] decorators, __call__ (able) objects

2009-07-15 Thread wesley chun
>>> > Can some one give, or point to some good examples of how @decorators
>>> > work, and __call__ (able) objects?
>
> simple example of calling a class
>
> class myKlass(object):
>
> def __call__(self, *args, **kws):
>  print "i was called"
>
> >>> test = myKlass()
> >>> test()
> i was called


close. the example was right, but the description wasn't accurate...
you meant, "calling an instance." i'm going to plagarize and rip this
right out of section 14.1.4 from "Core Python Programming:"

"Python provides the __call__() special method for classes, which allows a
programmer to create objects (instances) that are callable. By default, the
__call__() method is not implemented, meaning that most instances are
not callable. However, if this method is overridden in a class definition,
instances of such a class are made callable. Calling such instance objects is
equivalent to invoking the __call__() method. Naturally, any arguments
given in the instance call are passed as arguments to __call__()."

as far as decorators go, kent's tutorial is great place to start. here
are 2 more articles plus PEP 318, where they were defined:

http://www.ibm.com/developerworks/linux/library/l-cpdecor.html
http://www.artima.com/weblogs/viewpost.jsp?thread=240808
http://www.python.org/dev/peps/pep-0318

in addition, i devoted section 11.3.6 of Core Python to decorators.

finally, it should be mentioned that starting in 2.6, you can now
decorate *classes*, as seen here in PEP 3129:

http://www.python.org/dev/peps/pep-3129/

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] decorators, __call__ (able) objects

2009-07-15 Thread vince spicer
agreed much better description, thanks

On Wed, Jul 15, 2009 at 1:02 PM, wesley chun  wrote:

> >>> > Can some one give, or point to some good examples of how @decorators
> >>> > work, and __call__ (able) objects?
> >
> > simple example of calling a class
> >
> > class myKlass(object):
> >
> > def __call__(self, *args, **kws):
> >  print "i was called"
> >
> > >>> test = myKlass()
> > >>> test()
> > i was called
>
>
> close. the example was right, but the description wasn't accurate...
> you meant, "calling an instance." i'm going to plagarize and rip this
> right out of section 14.1.4 from "Core Python Programming:"
>
> "Python provides the __call__() special method for classes, which allows a
> programmer to create objects (instances) that are callable. By default, the
> __call__() method is not implemented, meaning that most instances are
> not callable. However, if this method is overridden in a class definition,
> instances of such a class are made callable. Calling such instance objects
> is
> equivalent to invoking the __call__() method. Naturally, any arguments
> given in the instance call are passed as arguments to __call__()."
>
> as far as decorators go, kent's tutorial is great place to start. here
> are 2 more articles plus PEP 318, where they were defined:
>
> http://www.ibm.com/developerworks/linux/library/l-cpdecor.html
> http://www.artima.com/weblogs/viewpost.jsp?thread=240808
> http://www.python.org/dev/peps/pep-0318
>
> in addition, i devoted section 11.3.6 of Core Python to decorators.
>
> finally, it should be mentioned that starting in 2.6, you can now
> decorate *classes*, as seen here in PEP 3129:
>
> http://www.python.org/dev/peps/pep-3129/
>
> hope this helps!
> -- wesley
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> "Core Python Programming", Prentice Hall, (c)2007,2001
> "Python Fundamentals", Prentice Hall, (c)2009
>http://corepython.com
>
> wesley.j.chun :: wescpy-at-gmail.com
> python training and technical consulting
> cyberweb.consulting : silicon valley, ca
> http://cyberwebconsulting.com
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] decorators, __call__ (able) objects

2009-07-15 Thread Todd Matsumoto
hi kent,

thanks, i read through the link but still haven't got my head around this 
concept.

will read on.

cheers,

t
 Original-Nachricht 
> Datum: Wed, 15 Jul 2009 08:33:33 -0400
> Von: Kent Johnson 
> An: Todd Matsumoto 
> CC: tutor@python.org
> Betreff: Re: [Tutor] decorators, __call__ (able) objects

> On Wed, Jul 15, 2009 at 7:41 AM, Todd Matsumoto wrote:
> > Hi,
> >
> > Can some one give, or point to some good examples of how @decorators
> work, and __call__ (able) objects?
> 
> Decorators:
> http://personalpages.tds.net/~kent37/kk/1.html
> 
> Kent

-- 
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] reading complex data types from text file

2009-07-15 Thread Chris Castillo
I'm having some trouble reading multiple data types from a single text file.

say I had a file with names and numbers:

bob
100
sue
250
jim
300

I have a few problems. I know how to convert the lines into an integer but I
don't know how to iterate through all the lines and just get the integers
and store them or iterate through the lines and just get the names and store
them.

please help.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] help

2009-07-15 Thread jonathan wallis
i have a duel loop that looks like thiswhile y > 0 and x > 0:
i cant figure out if there is a way to make so if one loop ends it says
something different than if the other loop ends.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reading complex data types from text file

2009-07-15 Thread Michiel Overtoom

Chris Castillo wrote:

I don't know how to iterate through all the lines and just get the 
integers and store them or iterate through the lines and just get the 
names and store them.


You could use the int() function to try to convert a line to an integer, 
and if that fails with a ValueError exception, assume that it is a name.


names=[]
numbers=[]
for line in open("mixed.txt"):
try:
nr=int(line)
# it's an integer
numbers.append(line)
except ValueError:
# it's not an integer
names.append(line)

f=open("numbers.txt","wt")
f.write("".join(numbers))
f.close()

f=open("names.txt","wt")
f.write("".join(names))
f.close()



--
"The ability of the OSS process to collect and harness
the collective IQ of thousands of individuals across
the Internet is simply amazing." - Vinod Valloppillil
http://www.catb.org/~esr/halloween/halloween4.html
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help

2009-07-15 Thread Eric Walker

>
>From: jonathan wallis 
>To: tutor@python.org
>Sent: Wednesday, July 15, 2009 12:54:16 PM
>Subject: [Tutor] help
>
>>i have a duel loop that looks like thiswhile y > 0 and x > 0:
>i cant figure out if there is a way to make so if one loop ends it says 
>something different than if the other loop ends.

this is only one loop. Are you saying you want to know if the y went to zero or 
the x went to zero?



  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help

2009-07-15 Thread Michiel Overtoom

jonathan wallis wrote:

i cant figure out if there is a way to make so if one loop ends it says 
something different than if the other loop ends.


Maybe you could use two separate tests and break out of the loop if x or 
y gets too low.
Because the tests are separated you could say something different for 
each case.

For example:

import random

x=y=9
while True:
print "x is %d, y is %d" % (x,y)
if x<=0:
print "Loop stopped because X was too low"
break
if y<=0:
print "Loop stopped because Y was too low"
break
# do something with x and y
if random.random()>0.5:
x-=1
else:
y-=1


--
"The ability of the OSS process to collect and harness
the collective IQ of thousands of individuals across
the Internet is simply amazing." - Vinod Valloppillil
http://www.catb.org/~esr/halloween/halloween4.html
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] objects becoming pointers

2009-07-15 Thread bob gailer

Please always reply-all so a copy goes to the list.

chris Hynes wrote:
Ah, there you go, that's what I want to do, dynamically create 
variable names. Then I could interactively create as many arrays as I 
want to, say Chris1, Chris2, Chris3 and each of these would be 
different array with different results.


But based on what you showed me I'm going to have to at least type:

print names["Chris"]

as opposed to just typing:

print Chris
True. As others have pointed out you may use exec to dynamically create 
variables.


Most Python applications pretty quickly evolve beyond simple command 
line interfaces. At that point dealing with dynamically created names 
becomes a pain.


With a little finessing you can create a class instance with attributes 
that are names such as Chris. Then you could

>>> print c.Chris

--
Bob Gailer
Chapel Hill NC
919-636-4239
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] objects becoming pointers

2009-07-15 Thread Kent Johnson
> chris Hynes wrote:
>>
>> Ah, there you go, that's what I want to do, dynamically create variable
>> names. Then I could interactively create as many arrays as I want to, say
>> Chris1, Chris2, Chris3 and each of these would be different array with
>> different results.
>>
>> But based on what you showed me I'm going to have to at least type:
>>
>> print names["Chris"]
>>
>> as opposed to just typing:
>>
>> print Chris

I'm not sure you have really thought this through. The user typed
"Chris", and that value is stored in 'name'. It might just as well
have been "Bob" or "Kent". So you can't really say
  print Chris
you need something like
  print the_thing_called(name)
which you might as well spell as
  print values[name]

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Can't transform a list of tokens into a text

2009-07-15 Thread Eduardo Vieira
Hello, I have a file that was a resulted from a POS-Tagging program,
after some transformations, I wanted to restore to it's normal form.
So, I used sed to remove the POS-Tags and have something like this:

--- Example begins
No
,
thank...@+  # this +...@+ I inserted to mark paragraphs, because the
POS-Tagger don't keep paragraph marks
He
said
,
"
No
,
thanks
.
"
OK
?
--- Example ends
--- And I want to transform into this:
No, thanks.
He said, "No, thanks." OK?
--- End example
I tried different approaches, and dealing with the quote marks is what
is defeating my attempts:

I had originally this code:
import cStringIO
##import string
myoutput = cStringIO.StringIO()
f = open(r'C:\mytools\supersed\tradunew.txt')
lista = [line.strip() for line in f]
punct = '!#$%)*,-.:;<=>?@/\\]^_`}~”…'
for i, item in enumerate(lista):

if item == '"' and lista[i + 1] not in punct:
myoutput.write(item)
spacer = True


elif '+...@+' in item:
donewline = item.replace('+...@+','\n ')
myoutput.write(donewline)
elif item not in punct and lista[i + 1] in punct:
myoutput.write(item)
elif item in punct and lista[i + 1] in punct:
myoutput.write(item)
elif item in punct and lista[i + 1] == '"' and spacer:
myoutput.write(item)
spacer = False
elif item not in punct and lista[i + 1] == '"' and spacer:
myoutput.write(item)
spacer = False
elif item in '([{“':
myoutput.write(item)

else:
myoutput.write(item + " ")

newlist = myoutput.getvalue().splitlines()



myoutput.close()

f = open(r'C:\mytools\supersed\traducerto-k.txt', 'w')

for line in newlist:
f.write(line.lstrip()+'\n')
f.close()

#===
I tried this version to post in this forum but that gives me an error.
I don't know why I don't get an error with the code above which is
essentially the same:
# -*- coding: cp1252 -*-

result = ''
lista = [
'No', ',', 'thank...@+',
'He', 'said', ',', '"', 'no', ',', 'thanks', '.', '"', 'OK', '?', 'Hi']

punct = '!#$%)*,-.:;<=>?@/\\]^_`}~”…'
for i, item in enumerate(lista):

if item == '"' and lista[i + 1] not in punct:
result +=item
spacer = True
elif '+...@+' in item:
donewline = item.replace('+...@+','\n ')
result += donewline
elif item not in punct and lista[i + 1] in punct:
result += item
elif item in punct and lista[i + 1] in punct:
result += item
elif item in punct and lista[i + 1] == '"' and spacer:
result += item
spacer = False
elif item not in punct and lista[i + 1] == '"' and spacer:
result += item
spacer = False
elif item in '([{“':
result += item
else:
result += (item + " ")

print result

#==
The error is this:
Traceback (most recent call last):
  File "", line 244, in run_nodebug
  File "C:\mytools\jointags-v4.py", line 17, in 
elif item not in punct and lista[i + 1] in punct:
IndexError: list index out of range

I'm using python 2.6.2 with PyScripter IDE
I have tried a so many variations that I'm not sure what I'm doing any more
I'm just trying to avoid some post-processing with sed again.

Thankful,

Eduardo
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: The why

2009-07-15 Thread Rich Lovely
-- Forwarded message --
From: chris Hynes 
Date: 2009/7/15
Subject: The why
To: roadier...@googlemail.com


Well, I'm trying to create an interactive program, let's say I'm
running the program, I ask the user to give the array a name, I then
do some computations and store the results in that array. While I'm
still running the program I might decide to create several more arrays
of the same type/dimension with names of my choosing same types of
computation. When I'm done, I might have several arrays created, all
with different names that I have given them. In the ipython mode I
could call up any of these arrays off and on again as I want to
visually compare them to each other.

I mean I guess what I could do is go back to my code, name the array
Chris, let a calculation use x=2, save, run the program, go back in to
the code, edit it so that the array is named Bob along with changing
x=3, save, run the program a second time. When I'm done I'll now have
two arrays, one named Chris the other Bob with different results
because I changed some other input paramenters.

I just now thought of maybe making one huge array, storing all the
results of x=2 and use a conditional satement like if x=2, then name
was Chris. But it would be so much easier if I could type print Chris
or print Bob and it woulds spit out what those arrays were.

> From: roadier...@googlemail.com
> Date: Wed, 15 Jul 2009 16:33:00 +0100
> Subject: Re: [Tutor] objects becoming pointers
> To: cjhyne...@hotmail.com
> CC: tutor@python.org
>
> 2009/7/15 chris Hynes :
> > I guess I have to start somewhere to ask
> >
> > I want the user to input a name, say "Chris". I know I can use the code:
> >
> > name=raw_input()
> >
> > I now want:
> >
> > "Chris"=zeros((3,3))
> >
> > so that when I type:
> >
> > print Chris
> >
> > the return will be an array of zero's 3x3
> >
> > So that I can understand this deeper, I know that "name" is just a pointer
> > to the object "Chris". I don't want to just change the pointer to something
> > else, like "zeros((3,3))" but I want to make "Chris" become the pointer or
> > the name of the pointer. At least that's what I think I want.
> >
> > 
> > Windows Live™ SkyDrive™: Get 25 GB of free online storage. Get it on your
> > BlackBerry or iPhone.
> > ___
> > Tutor maillist  -  tu...@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
>
> Sorry if I've sent this twice...
>
> Why would you want to do that?
>
> The closest you can get to that is using exec, but exec is usually
> considered a code smell. I'd say you're trying to do the wrong thing.
>
> --
> Rich "Roadie Rich" Lovely
> There are 10 types of people in the world: those who know binary,
> those who do not, and those who are off by one.


Windows Live™: Keep your life in sync. Check it out.


-- 
Rich "Roadie Rich" Lovely
There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Can't transform a list of tokens into a text

2009-07-15 Thread Muhammad Ali
Try changing all instances of list[i + 1] to list[i]

On Thu, Jul 16, 2009 at 8:33 AM, Eduardo Vieira wrote:

> #===
> I tried this version to post in this forum but that gives me an error.
> I don't know why I don't get an error with the code above which is
> essentially the same:
> # -*- coding: cp1252 -*-
>
> result = ''
> lista = [
> 'No', ',', 'thank...@+',
> 'He', 'said', ',', '"', 'no', ',', 'thanks', '.', '"', 'OK', '?', 'Hi']
>
> punct = '!#$%)*,-.:;<=>?@/\\]^_`}~”…'
> for i, item in enumerate(lista):
>
>if item == '"' and lista[i + 1] not in punct:
>result +=item
>spacer = True
>elif '+...@+' in item:
>donewline = item.replace('+...@+','\n ')
>result += donewline
>elif item not in punct and lista[i + 1] in punct:
>result += item
>elif item in punct and lista[i + 1] in punct:
>result += item
>elif item in punct and lista[i + 1] == '"' and spacer:
>result += item
>spacer = False
>elif item not in punct and lista[i + 1] == '"' and spacer:
>result += item
>spacer = False
>elif item in '([{“':
>result += item
>else:
>result += (item + " ")
>
> print result
>
> #==
> The error is this:
> Traceback (most recent call last):
>  File "", line 244, in run_nodebug
>  File "C:\mytools\jointags-v4.py", line 17, in 
>elif item not in punct and lista[i + 1] in punct:
> IndexError: list index out of range
>
> I'm using python 2.6.2 with PyScripter IDE
> I have tried a so many variations that I'm not sure what I'm doing any
> more
> I'm just trying to avoid some post-processing with sed again.
>
> Thankful,
>
> Eduardo
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
http://alitechtips.blogspot.com/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reading complex data types from text file

2009-07-15 Thread Christian Witts

Chris Castillo wrote:
I'm having some trouble reading multiple data types from a single text 
file.


say I had a file with names and numbers:

bob
100
sue
250
jim
300

I have a few problems. I know how to convert the lines into an integer 
but I don't know how to iterate through all the lines and just get the 
integers and store them or iterate through the lines and just get the 
names and store them.


please help.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
  

You could do it with a list comprehension

>>> names = []
>>> numbers = []
>>> [numbers.append(int(line.strip())) if line.strip().isdigit() else 
names.append(line.strip()) for line in open('test.txt','rb') if 
line.strip()]

[None, None, None, None, None, None]
>>> names, numbers
(['bob', 'sue', 'jim'], [100, 250, 300])

The list comprehension would unfold to

for line in open('test.txt', 'rb'):
   if line.strip():
   if line.strip().isdigit():
   numbers.append(line.strip())
   else:
   names.append(line.strip())

And from there you can do what you like with the lists.

--
Kind Regards,
Christian Witts


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] XML: changing value of elements

2009-07-15 Thread Johan Geldenhuys
I have another question about writing the xml tree to a file.

Now, I have parsed and changed my xml tree, but I want to create the same
tree multiple times with different values and write it to one file.


Let's use this code:

import xml.etree.ElementTree as ET
doc = ET.parse('signal1.xml')
signal_node = doc.getroot()[0]
signal_node.set('name', 'Test_Name')

I have this in a function and it returns the signal_node everytime I call
it, but with a different values. 

So, How do I take the returned xml and write that to one xml file?

Thanks

Johan

-Original Message-
From: tutor-bounces+johan=accesstel.com...@python.org
[mailto:tutor-bounces+johan=accesstel.com...@python.org] On Behalf Of Stefan
Behnel
Sent: Thursday, June 11, 2009 4:37 PM
To: tutor@python.org
Subject: Re: [Tutor] XML: changing value of elements

Hi,

it's funny how many times I see Python users go: "I have an XML problem, so
I'll use minidom." Because then they have two problems.


Johan Geldenhuys wrote:
> I have a rather complex XML file and I need to change some values inside
> this file.
> 
> So far I have been using minidom, but I can't find the thing I am looking
> for.
> 
> My code so far:
> """
> from xml.dom import minidom
> 
> xmlFile = 'signal1.xml'
> xmlDocument = minidom.parse(xmlFile)
> 
> SignalsNode = xmlDocument.firstChild
> signalNode = SignalsNode.childNodes[1]
> 
> signalNode.removeAttribute("name")
> signalNode.setAttribute("name", "Test_Name")
> signalNode.getAttribute("name")
> 
> descElem = signalNode.childNodes[1]

That is a lot of code just to say

import xml.etree.ElementTree as ET
doc = ET.parse('signal1.xml')
signal_node = doc.getroot()[0]
signal_node.set('name', 'Test_Name')


> I know how to manipulate the value of the attributes, but I can't seem to
> change the values of eg: "Description"

description = signal_node[0]
description.text = "New value"

> Snippet from my XML file:
> 
> """
> 
>  
> 
>   -  

What's that "" bit?


>   -  name="Model_X" type="Flyer">
> 
>   Some description 
> 
>Model_X  
> 
>
> 
>   - 
> 
>   normal 
> 
>   Model X 1 
> 
>   
> 
>   -  type="close">
> 
>   minor 
> 
>Model X 2 
> 
>   
> 
>   
> 
>   
> """

Stefan

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

No virus found in this incoming message.
Checked by AVG - http://www.avg.com 
Version: 8.0.138 / Virus Database: 270.12.93/2206 - Release Date: 6/27/2009
5:55 PM

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] decorators, __call__ (able) objects

2009-07-15 Thread Todd Matsumoto
Thanks guys,

In the example the __call__ method has *args and **kws as arguments. Is that 
required?

Also when, in what situation would you use callable objects?

Cheers,

T
 Original-Nachricht 
> Datum: Wed, 15 Jul 2009 12:02:05 -0700
> Von: wesley chun 
> An: vince spicer , tmatsum...@gmx.net
> CC: Kent Johnson , tutor@python.org
> Betreff: Re: [Tutor] decorators, __call__ (able) objects

> >>> > Can some one give, or point to some good examples of how @decorators
> >>> > work, and __call__ (able) objects?
> >
> > simple example of calling a class
> >
> > class myKlass(object):
> >
> > def __call__(self, *args, **kws):
> >  print "i was called"
> >
> > >>> test = myKlass()
> > >>> test()
> > i was called
> 
> 
> close. the example was right, but the description wasn't accurate...
> you meant, "calling an instance." i'm going to plagarize and rip this
> right out of section 14.1.4 from "Core Python Programming:"
> 
> "Python provides the __call__() special method for classes, which allows a
> programmer to create objects (instances) that are callable. By default,
> the
> __call__() method is not implemented, meaning that most instances are
> not callable. However, if this method is overridden in a class
> definition,
> instances of such a class are made callable. Calling such instance objects
> is
> equivalent to invoking the __call__() method. Naturally, any arguments
> given in the instance call are passed as arguments to __call__()."
> 
> as far as decorators go, kent's tutorial is great place to start. here
> are 2 more articles plus PEP 318, where they were defined:
> 
> http://www.ibm.com/developerworks/linux/library/l-cpdecor.html
> http://www.artima.com/weblogs/viewpost.jsp?thread=240808
> http://www.python.org/dev/peps/pep-0318
> 
> in addition, i devoted section 11.3.6 of Core Python to decorators.
> 
> finally, it should be mentioned that starting in 2.6, you can now
> decorate *classes*, as seen here in PEP 3129:
> 
> http://www.python.org/dev/peps/pep-3129/
> 
> hope this helps!
> -- wesley
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> "Core Python Programming", Prentice Hall, (c)2007,2001
> "Python Fundamentals", Prentice Hall, (c)2009
> http://corepython.com
> 
> wesley.j.chun :: wescpy-at-gmail.com
> python training and technical consulting
> cyberweb.consulting : silicon valley, ca
> http://cyberwebconsulting.com

-- 
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3 -
sicherer, schneller und einfacher! http://portal.gmx.net/de/go/atbrowser
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] decorators, __call__ (able) objects

2009-07-15 Thread Vince Spicer


no the __call__ function can is like any function

def __call__(self, passedin):

or simply 

def __call__(self)


*args and **kws explained >  http://www.saltycrane.com/blog/2008/01/how-to-
use-args-and-kwargs-in-python/

On Thursday 16 July 2009 12:09:52 am Todd Matsumoto wrote:
> Thanks guys,
>
> In the example the __call__ method has *args and **kws as arguments. Is
> that required?
>
> Also when, in what situation would you use callable objects?
>
> Cheers,
>
> T
>  Original-Nachricht 
>
> > Datum: Wed, 15 Jul 2009 12:02:05 -0700
> > Von: wesley chun 
> > An: vince spicer , tmatsum...@gmx.net
> > CC: Kent Johnson , tutor@python.org
> > Betreff: Re: [Tutor] decorators, __call__ (able) objects
> >
> > >>> > Can some one give, or point to some good examples of how
> > >>> > @decorators work, and __call__ (able) objects?
> > >
> > > simple example of calling a class
> > >
> > > class myKlass(object):
> > >
> > > def __call__(self, *args, **kws):
> > >  print "i was called"
> > >
> > > >>> test = myKlass()
> > > >>> test()
> > >
> > > i was called
> >
> > close. the example was right, but the description wasn't accurate...
> > you meant, "calling an instance." i'm going to plagarize and rip this
> > right out of section 14.1.4 from "Core Python Programming:"
> >
> > "Python provides the __call__() special method for classes, which allows
> > a programmer to create objects (instances) that are callable. By default,
> > the
> > __call__() method is not implemented, meaning that most instances are
> > not callable. However, if this method is overridden in a class
> > definition,
> > instances of such a class are made callable. Calling such instance
> > objects is
> > equivalent to invoking the __call__() method. Naturally, any arguments
> > given in the instance call are passed as arguments to __call__()."
> >
> > as far as decorators go, kent's tutorial is great place to start. here
> > are 2 more articles plus PEP 318, where they were defined:
> >
> > http://www.ibm.com/developerworks/linux/library/l-cpdecor.html
> > http://www.artima.com/weblogs/viewpost.jsp?thread=240808
> > http://www.python.org/dev/peps/pep-0318
> >
> > in addition, i devoted section 11.3.6 of Core Python to decorators.
> >
> > finally, it should be mentioned that starting in 2.6, you can now
> > decorate *classes*, as seen here in PEP 3129:
> >
> > http://www.python.org/dev/peps/pep-3129/
> >
> > hope this helps!
> > -- wesley
> > - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> > "Core Python Programming", Prentice Hall, (c)2007,2001
> > "Python Fundamentals", Prentice Hall, (c)2009
> > http://corepython.com
> >
> > wesley.j.chun :: wescpy-at-gmail.com
> > python training and technical consulting
> > cyberweb.consulting : silicon valley, ca
> > http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor