Re: [Tutor] Variables don't change when altered within a loop?

2006-06-30 Thread Alan Gauld



> Hi, I just started picking up python 
yesterday, and have already come> across something that has me 
stumped.  I want some code that does> this:> > a = 
foo(a)> b = foo(b)> c = foo(c)> > So I try to do 
this with a for loop, like so:> > for i in [a, b, 
c]:>   i = foo(i)>   print 
i # make sure that it worked correctly> > 
So far, so good.  The problem is that outside the loop, the values> 
aren't changed.  For example,
You need to understand the difference 
between names 
and variables and values. A variable in 
Python is a reference 
to a value (or more correctly an object 
which has a value)
 
for i in [a,b,c]
 
makes a new variable(name) i which takes on 
the *values* 
of a,b,c in turn with each iteration of the 
loop.
 
i does not take on the name a,b or c, it 
takes on the value.
Thus in the first iteration the variable i 
will take on the 
value referenced by a, in the second 
iteration the value 
referenced by b and in the third iteration 
the value 
referenced by c. The original variables 
still refer 
to their original values.
 
i = foo(a)
 
now i will be rebound to the value returned 
by the function 
foo() when passed the value of a as an 
argument. The value 
refered to by a has not been changed in any 
way.
 
print i
 
prints the new value associated with i. It 
changes nothing.
 
in the second iteration the same happens 
but with i 
taking on the initial value of 
b.
 
Lets look at a more concrete 
example:
 
def foo(x): return x + 10
a,b,c = 1,2,3
 
code  
iter 1 iter 
2   iter 3

for i in 
[a,b,c]: i = 
1  i = 
2 i = 3   

   i = 
foo(i) i 
= 11 i = 
12i = 13
 
Note that a,b,c are not changed anywhere in 
this loop.
They retain the original values of 
1,2,3.
If you wanted to change them you would need 
to explicitly 
set their values
 
> What is going on?  
> Why aren't the values of my variables 
changing when> I change them inside a loop like this?
 
You are not changing the variables you are 
changing i 
which is an entirely different 
variable!
 
The normal way to do what I think you want 
is to 
put the variables in a list or dictionary, 
then 
you can loop over that and change the 
values. 
Like so:
 
vars = {'a':1, 'b':2,'c':3}
def foo(x): return x+10
for key,value in vars.items():
    vars[key] = 
foo(value)
 
print vars
 
HTH,
 
Alan GauldAuthor of the Learn to 
Program web sitehttp://www.freenetpages.co.uk/hp/alan.gauld
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Wiget

2006-06-30 Thread Alan Gauld
> The problem is at the end where i try and get the number of 
> Tries the user has tried it would just reset everytime the button 
> in clicked, so my question is how would i go about getting 
> the number of times the button is clicked and the anwser is wrong.

>def number_anwser(self):
>guess = self.guess_ent.get()
>guess = int(guess)
>response = ""
>tries = 1

You are resetting tries to one every time.
You need to store tries atthe object level by creating a self.tries 
in your init method. You can then increment self.tries in the code 
below.

>
>if (guess < the_number):
>response += "Higher"
>tries += 1
>elif (guess > the_number):
>response += "Lower"
>tries += 1
>else:
>tries = str(tries)

and this would become
 tries = str(self.tries)
that is, you don't want to convert self.tries to a string!

>response += "Correct! that was my number, \n"
>response += "You guessed it in just "
>response += tries
>response += " tries!"
>  
>self.response_txt.delete(0.0, END)
>self.response_txt.insert(0.0, response)
>

HTH

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

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


[Tutor] Python urllib (linux)

2006-06-30 Thread Patty
Hi all,
I'm doing some failure testing for a python script that uses urllib and  urllib2
to open a web page and post data. If the server's apache is down or if I change
the ip address the script is trying to contact to be a bogus address, the script
handles it prefectly. When I unplug the ethernet cable from my machine (client),
the script hangs. Is there a way I can implement a timeout to handle this 
problem?
I'd appreciate any suggestions because I'm clueless.
Thanks,
Patty

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


Re: [Tutor] Python urllib (linux)

2006-06-30 Thread Python
On Fri, 2006-06-30 at 13:58 +, Patty wrote:
> Hi all,
> I'm doing some failure testing for a python script that uses urllib and  
> urllib2
> to open a web page and post data. If the server's apache is down or if I 
> change
> the ip address the script is trying to contact to be a bogus address, the 
> script
> handles it prefectly. When I unplug the ethernet cable from my machine 
> (client),
> the script hangs. Is there a way I can implement a timeout to handle this 
> problem?
import socket
if not socket.getdefaulttimeout():
socket.setdefaulttimeout(25.0)

This sets a timout of 25 seconds.  You need to set the timeout BEFORE
using a socket.  The documentation is in the socket module, should you
need more detail.  This technique works for simple cases.

> I'd appreciate any suggestions because I'm clueless.
> Thanks,
> Patty
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp

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


Re: [Tutor] Python urllib (linux)

2006-06-30 Thread Terry Carroll
On Fri, 30 Jun 2006, Patty wrote:

> I'm doing some failure testing for a python script that uses urllib and
> urllib2 to open a web page and post data Is there a way I can
> implement a timeout to handle this problem?

I don't have any firsth-hand knowledge on this, but 
http://www.voidspace.org.uk/python/articles/urllib2.shtml says:


By default the socket module has no timeout and can hang. Currently, the 
socket timeout is not exposed at the httplib or urllib2 levels. However, 
you can set the default timeout globally for all sockets using :

import socket
import urllib2

# timeout in seconds
timeout = 10
socket.setdefaulttimeout(timeout)

# this call to urllib2.urlopen now uses the default timeout
# we have set in the socket module
req = urllib2.Request('http://www.voidspace.org.uk')
response = urllib2.urlopen(req)


Good luck!

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


Re: [Tutor] Python urllib (linux)

2006-06-30 Thread Patty
Hi again,


> > problem?
> import socket
> if not socket.getdefaulttimeout():
> socket.setdefaulttimeout(25.0)
> 

I'm sorry I forgot to mention that I'm using python 2.2.3, and this version
doesn't have those methods. 
Patty


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


Re: [Tutor] Unit testing

2006-06-30 Thread Christopher Arndt
Terry Carroll schrieb:
> You can just use a series of Queues, where each Queue represents the work 
> being passed from one thread to the other.

If you want, you can have a look at my threadpool module, which implements
exactly this pattern. It is basically nothing more than an elaborate example on
this technique:

http://chrisarndt.de/en/software/python/threadpool/

Chris
begin:vcard
fn:Christopher Arndt
n:Arndt;Christopher
adr:Fort Lorenzo;;Cuan Na Coille;Galway;Co. Galway;;Irland
email;internet:[EMAIL PROTECTED]
tel;work:+353 (0)91-745500
tel;cell:+353 (0)86-3378101
x-mozilla-html:TRUE
url:http://chrisarndt.de
version:2.1
end:vcard

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


[Tutor] saving output in a text file

2006-06-30 Thread Hafsa raza
Hi, my name is Hafsa
 
I am new to programming in Python. Basically i am working with regular _expression_ matching using Python. I have a problem with saving output from the regular _expression_ matches in a text file. Can you guide me how to save the output returned by a regular _expression_ group, into a text file? for instance for the following regular _expression_:
 
r1= re.compile("""(?PTYPE=\"PER\"\sSUBTYPE=\"Individual\")(?P.*?\<)
""",re.VERBOSE)
 
If the above regular _expression_ is searched for in a file, how can i save the output matches from the group "name" into a new text file?
 
Thanks & regards,
HafsaFREE pop-up blocking with the new MSN Toolbar MSN Toolbar Get it now!

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


Re: [Tutor] classes and functions

2006-06-30 Thread Mike Hansen
 

> -Original Message-
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of anil maran
> Sent: Thursday, June 29, 2006 12:51 PM
> To: Tutor
> Subject: [Tutor] classes and functions
> 
> how to use classes and functions in python
> thanks

What do you need to know? Have you read the tutorial?
http://pytut.infogami.com/

The tutorial wiki has some info about functions
http://pytut.infogami.com/node6.html

There's also some information on classes
http://pytut.infogami.com/node11-baseline.html

Post specific questions if these links aren't clear enough, and we can
probably help. I hope these links help you.

Mike
http://users.adelphia.net/~mahansen/programming/ 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] saving output in a text file

2006-06-30 Thread Danny Yoo

Hi Hafsa,

> Can you guide me how to save the output returned by a regular expression 
> group, into a text file? for instance for the following regular 
> expression:

[regex cut]

> If the above regular expression is searched for in a file, how can i 
> save the output matches from the group "name" into a new text file?


I'm not sure the problem you have has to do with regular expressions.  Do 
you mind if we change the question to something slightly different?

If you were given a function that takes a string and returns another 
string, could you write a program that uses it and writes the result to 
disk?  For example, let's say that we have a string like "hello world", 
process it through, well:

 def double(s):
 "double: string -> string
 doubles up the input string s."
 return s + s

Would you be able to write a program that takes "hello world", runs it 
through double(), and writes out "hello worldhelloworld" to disk?

What I'm trying to isolate the problem: does it have to do with 
input/output, or regular expressions, or something else?


Best of wishes!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] saving output in a text file

2006-06-30 Thread Danny Yoo
>def double(s):
>"double: string -> string
>doubles up the input string s."
>return s + s


Gaah.  Typos.  My apologies.  Here's a correction to double()

def double(s):
"""double: string -> string
Doubles up the input string s.  For example, double("abc")
should return "abcabc".
"""
return s + s


> Would you be able to write a program that takes "hello world", runs it 
> through double(), and writes out "hello worldhelloworld" to disk?

I meant to say that the expected content of the file should be:

 "hello worldhello world"

My apologies; I rushed that message too quickly.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor