Re: for x,y in word1, word2 ?

2008-08-12 Thread Raja Baz
On Sun, 10 Aug 2008 21:40:55 -0700, Mensanator wrote:

 On Aug 10, 11:18�pm, ssecorp [EMAIL PROTECTED] wrote:
 Is there a syntax for looping through 2 iterables at the same time?

 for x in y:
 � � for a in b:

 is not what I want.

 I want:
 for x in y and for a in b:
 
 Something like this?
 
 a = ['a','b','c']
 b = [1,2,3]
 zip(a,b)
 [('a', 1), ('b', 2), ('c', 3)]

zip and the nested loops don't do the same thing
zipping then comparing would compare items that are at the same position
the nested loops would compare each item to all the items in the other
sequence, big difference
AFAIK there's no better way to accomplish what the nested loops do with 
a simpler syntax
--
http://mail.python.org/mailman/listinfo/python-list

Re: raw_input on several lines

2008-08-03 Thread Raja Baz
On Sat, 02 Aug 2008 21:58:09 +0200, TP wrote:

 Hi everybody,
 
 When using raw_input(), the input of the user ends when he types Return
 on his keyboard.
 How can I change this behavior, so that another action is needed to stop
 the input? For example, CTRL-G. It would allow the user to input several
 lines.
 
 Thanks
 
 Julien

Well I don't know about using CTRL-G. Now I'm pretty sure you can't 
change the behavior of raw_input() like this *but* what you do is the 
following:

 from sys import stdin
 user_input = stdin.readlines()
this
is 
a multiline
test
 user_input
['this\n', 'is\n', 'a multiline\n', 'test\n']
 

The end of the input is marked by the End of Transmission or End of 
File character(which can be obtained via ctrl+D, at least on linux, I 
have no idea about win32)
This would have the additional bonus of working with something being 
piped into it as an input
--
http://mail.python.org/mailman/listinfo/python-list


Re: very large dictionary

2008-08-01 Thread Raja Baz
On Fri, 01 Aug 2008 14:47:17 +0100, Sion Arrowsmith wrote:

 Simon Strobl  [EMAIL PROTECTED] wrote:
I tried to load a 6.8G large dictionary on a server that has 128G of
memory. I got a memory error. I used Python 2.5.2. How can I load my
data?
 
 Let's just eliminate one thing here: this server is running a 64-bit OS,
 isn't it? Because if it's a 32-bit OS, the blunt answer is You can't,
 no matter how much physical memory you have and you're going to have to
 go down the database route (or some approach which stores the mapping on
 disk and only loads items into memory on demand).

I very highly doubt he has 128GB of main memory and is running a 32bit OS.
--
http://mail.python.org/mailman/listinfo/python-list


Re: very large dictionary

2008-08-01 Thread Raja Baz
On Fri, 01 Aug 2008 14:47:17 +0100, Sion Arrowsmith wrote:

 Simon Strobl  [EMAIL PROTECTED] wrote:
I tried to load a 6.8G large dictionary on a server that has 128G of
memory. I got a memory error. I used Python 2.5.2. How can I load my
data?

 Let's just eliminate one thing here: this server is running a 64-bit OS,
 isn't it? Because if it's a 32-bit OS, [etc...]

I very highly doubt he has 128GB of main memory and is running a 32bit OS.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie having issues with threads

2008-08-01 Thread Raja Baz
On Thu, 31 Jul 2008 14:09:12 -0700, James Calivar wrote:

 I'm a newbie trying to write a script that uses threads.  I'm right now
 a little bit stuck in understanding why the code snippet I wrote doesn't
 seem to be entering the function defined in the start_new_thread() call.
 
 If I run it as is (the threaded version), the output is:
 
 UA_1 configuring...
 UA_1 halting..
 
 But if I comment out the line w/ the thread and just call the function
 directly, everything seems to work OK:
 
 UA_1 configuring...
 UA_1 executing...
 UA_1 halting...
 
 Can anyone tell me why the thread doesn't seem to invoke the function
 execute()?  I'm running Python 2.4.3.
 
 Here is my code:
 
 ===
 import thread
 
 class Test(object):
 def __init__(self, instanceID):
 self.instanceID = instanceID
 def configure(self):
 print self.instanceID +  configuring...
 def execute(self):
 print self.instanceID +  executing...
 def halt(self):
 print self.instanceID +  halting...
 
 if __name__ == __main__:
 usage: sipp_auto [options]
 
 ua1 = Test(UA_1)
 
 ua1.configure()
 
 #ua1.execute()
 thread.start_new_thread(ua1.execute, ())
 
 ua1.halt()
 
 ===
 Thanks, James

I've run into this problem before. The main problem is that the thread 
started via start_new_thread exits when the program does(instead of 
finishing its work first). So what happens here is that sometimes it may 
have the time to execute, and sometimes it won't

changing:
thread.start_new_thread(ua1.execute, ())

ua1.halt()

to:
thread.start_new_thread(ua1.execute, ())
from time import sleep
sleep(0.01) # this is barely even noticeable

ua1.halt()

fixes the problem in this case and the output is correct.

However, you seem to have taken up using the threading module which is 
probably better anyway, just wanted to explain why this was happening.
--
http://mail.python.org/mailman/listinfo/python-list