[Tutor] py.test on Windows

2009-04-23 Thread Hans DushanthaKumar
Hi,

Has anyone here got py.test running on Windows? How do I run a test
script?

I have previously installed and used it on Linux and it all worked
magically - all I needed to do was type in "py.test ".
However on windows, the installation doesn't seem to set the paths
appropriately.

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


[Tutor] Importing serial module

2005-08-21 Thread Hans Dushanthakumar
Hi Folks
   Another newbie here :) Heres a couple of questions:

1) I downloaded the python serial port module (pyserial-2.2.win32.exe)
from http://sourceforge.net/project/showfiles.php?group_id=46487
And installed it on my Win XP PC.
   However, when I try to import the module, I get the foll: error

>>> import serial
Traceback (most recent call last):
  File "", line 1, in ?
import serial
  File "C:\Python\Lib\site-packages\serial\__init__.py", line 13, in ?
from serialwin32 import *
  File "C:\Python\Lib\site-packages\serial\serialwin32.py", line 9, in ?
import win32file  # The base COM port and file IO functions.
ImportError: No module named win32file
>>> 

   Is there something else that I need to specify?

2) I want to send data (ascii text) to a serial port, but at the same
time want to keep monitoring incoming data in that same serial port.
Whats the best approach to do this? Multithreading? Or is there a more
straightforward/easier approach?

Cheers
Hans
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Split a string into characters

2005-08-22 Thread Hans Dushanthakumar
Hi,
   A quick one...
   How do I split a string like "Hans" into a list of characters
['H','a','n','s']?
Note that there are no spaces in the original string.
   Str.split() without any arguments looks for whitespace as splitting
character. So, this doesn't serve the purpose.
And str.split("") appears to be illegal.
Cheers
Hans
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Split a string into characters

2005-08-22 Thread Hans Dushanthakumar
Bingo :)

Thanks Rick

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Rick Pasotto
Sent: Tuesday, 23 August 2005 1:55 p.m.
To: tutor@python.org
Subject: Re: [Tutor] Split a string into characters

On Tue, Aug 23, 2005 at 01:18:43PM +1200, Hans Dushanthakumar wrote:
> Hi,
>A quick one...
>How do I split a string like "Hans" into a list of characters 
> ['H','a','n','s']?
> Note that there are no spaces in the original string.
>Str.split() without any arguments looks for whitespace as splitting

> character. So, this doesn't serve the purpose.
> And str.split("") appears to be illegal.

list('Hans')

--
"History will be kind to me, for I intend to write it."
-- Winston Churchill
Rick Pasotto[EMAIL PROTECTED]http://www.niof.net
___
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] Importing modules/classes

2005-08-24 Thread Hans Dushanthakumar
 
Hi
   Am trying to get my head around classes in python.

I have a file dummy_class.py with a class definition in it, as foloows

class dummy_class:
def __init__(self):
print "__init__"

def run(self):
print "run"


Now, I have another file test_dummy.py, which only has the foll 2 lines

import dummy_class
d=dummy_class()


When I run this file (via IDLE), I get the foll error:
Traceback (most recent call last):
  File "H:/Docs/PyScripts/test_dummy_class.py", line 3, in -toplevel-
d=dummy_class()
TypeError: 'module' object is not callable

However, if the class definition was part of the same file
(test_dummy.py), instead of importing it, it runs as expected. Why does
this happen?

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


Re: [Tutor] Importing modules/classes

2005-08-24 Thread Hans Dushanthakumar
Me again :)

Just to make sure that I understand it right, 
  1) the __init__ method in a class is invoked when a object is
instantiated from a class
  2) the run method is invoked when a class derived from
"threading.Thread" is "start"ed
Is that right?

The snippet below,

><--

import threading

class show_num(threading.Thread):

def __init__(self, num):
print "__init__: Num = ", num

def run(self):
print "run"

show_num_thread = show_num(742)
show_num_thread.start()
 
---><-

Throws an error

>>> 
__init__: Num =  742

Traceback (most recent call last):
  File "H:/Docs/PyScripts/test_thread_1.py", line 12, in -toplevel-
show_num_thread.start()
AssertionError: Thread.__init__() not called
>>> 

Which __init__ method is it referring to?


Thanks in advance for your help (and time). U guys are awesome :)


-Original Message-
From: Danny Yoo [mailto:[EMAIL PROTECTED] 
Sent: Thursday, 25 August 2005 12:17 p.m.
To: Hans Dushanthakumar
Cc: Tutor
Subject: Re: [Tutor] Importing modules/classes



> class dummy_class:
> def __init__(self):
> print "__init__"
>
> def run(self):
> print "run"
>
>
> Now, I have another file test_dummy.py, which only has the foll 2 
> lines
>
> import dummy_class
> d=dummy_class()


Hi Hans,

In Python, modules are containers.  They can contain possibly more than
one thing, so you need to make sure to fully qualify the class:

import dummy_class
d = dummy_class.dummy_class()

Does this make sense?

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


[Tutor] Differences in running a multithreaded script under IDLE and otherwise

2005-08-24 Thread Hans Dushanthakumar
Hi,
   While running the foll script by double-clicking it (under WinXP), it
runs as expected. However, when I run it via IDLE, it hangs after a few
secs (no runtime errors - just hangs). Why does this happen?

Cheers
Hans

import threading

class incr_num(threading.Thread):
num = ''

def __init__(self, local_num):
global num
threading.Thread.__init__(self)
num = local_num
print "__init__: ", num

def run(self):
global num
for k in range (20):
print "run: ", num
num = num + 1

incr_num_thread = incr_num(501)
incr_num_thread.start()

print "Wait for thread to finish"
incr_num_thread.join()
print "Thread finished"

raw_input("Press enter")
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Differences in running a multithreaded script under IDLE and otherwise

2005-08-25 Thread Hans Dushanthakumar
Kent,
   I'm using the same version (2.4.1) under Win XP. The program works as
expected (ie prints "run: " a few times, and then just goes dead - no
errors. Having said that, I did see one instance where it ran to
completion under IDLE. So looks like the behaviour is not consistent.
Cheers
Hans

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Kent Johnson
Sent: Thursday, 25 August 2005 11:23 p.m.
Cc: tutor@python.org
Subject: Re: [Tutor] Differences in running a multithreaded script under
IDLE and otherwise

Hans Dushanthakumar wrote:
> Hi,
>While running the foll script by double-clicking it (under WinXP), 
> it runs as expected. However, when I run it via IDLE, it hangs after a

> few secs (no runtime errors - just hangs). Why does this happen?

It works for me in IDLE with Python 2.4.1 on Win2k. What version of
Python do you have? Do you get any output at all?

Kent

> 
> Cheers
> Hans
> 
> import threading
> 
> class incr_num(threading.Thread):
> num = ''
> 
> def __init__(self, local_num):
> global num
> threading.Thread.__init__(self)
> num = local_num
> print "__init__: ", num
> 
> def run(self):
> global num
> for k in range (20):
> print "run: ", num
> num = num + 1
> 
> incr_num_thread = incr_num(501)
> incr_num_thread.start()
> 
> print "Wait for thread to finish"
> incr_num_thread.join()
> print "Thread finished"
> 
> raw_input("Press enter")
> ___
> 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 maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] "Lock"ing threads

2005-08-28 Thread Hans Dushanthakumar
Hi,
   In a multithreaded program, how do I ensure that a block of code in a
thread is always executed fully before passing control to another
thread. Does "Lock" serve this purpose?

The foll program is a dummy one, with 2 threads. One put a number onto a
queue (of max size 1) and the other thread reads from the que.

However, on running this program (Win XP, NOT via IDLE - it hangs when I
run it thru IDLE) the output that I see on screen indicates that the
block of code within the lock aquire and release was not run completely
before the other thread started running. Note that the print messages
from the 2 threads seem to be interspersed together:


import threading
import Queue

class put_num(threading.Thread):
stop_thread = 0

def __init__(self, num, que):
threading.Thread.__init__(self)
self.que = que
self.num = num
self.lock = threading.Lock()

def run(self):
global stop_thread
for k in range (20):
self.lock.acquire()
print "put_num: ", self.num
self.que.put(str(self.num))
print "put_num: Que size = ", self.que.qsize()
self.num = self.num + 1
self.lock.release()

class get_num(threading.Thread):
stop_thread = 0

def __init__(self, que):
threading.Thread.__init__(self)
self.que = que
self.lock = threading.Lock()

def run(self):
global stop_thread
for k in range (20):
self.lock.acquire()
mynum = self.que.get()
print "get_num: ", mynum
print "get_num: Que size = ", self.que.qsize()
self.lock.release()

my_que = Queue.Queue(1)

put_num_thread = put_num(742, my_que)
get_num_thread = get_num(my_que)

print "Starting threads"
put_num_thread.start()
get_num_thread.start()

print "Waiting for threads to finish"
put_num_thread.join()
get_num_thread.join()

print "Closing down"
raw_input("\n\nPress enter to Quit: ")




This is the out put of the above program:



Starting threads
put_num:  742
Waiting for threads to finish
put_num: Que size =  1
get_num:  742
get_num: Que size =  0
put_num:  743
put_num: Que size =  1
get_num:  743
get_num: Que size =  0
put_num:  744
put_num: Que size =  1
get_num:  744
get_num: Que size =  0
put_num:  745
put_num: Que size =  1
get_num:  745
get_num: Que size =  0
put_num:  746
put_num: Que size =  1
get_num:  746
get_num: Que size =  0
put_num:  747
put_num: Que size =  get_num:  747
get_num: Que size =  0
0
put_num:  748
put_num: Que size =  1
get_num:  748
get_num: Que size =  0
put_num:  749
put_num: Que size =  get_num:  749
get_num: Que size =  0
0
put_num:  750
put_num: Que size =  1
get_num:  750
get_num: Que size =  0
put_num:  751
put_num: Que size =  1
get_num:  751
get_num: Que size =  0
put_num:  752
put_num: Que size =  get_num:  752
get_num: Que size =  0
0
put_num:  753
put_num: Que size =  1
get_num:  753
get_num: Que size =  0
put_num:  754
put_num: Que size =  1
get_num:  754
get_num: Que size =  0
put_num:  755
put_num: Que size =  get_num:  755
get_num: Que size =  0
0
put_num:  756
put_num: Que size =  get_num:  756
get_num: Que size =  0
0
put_num:  757
put_num: Que size =  get_num:  757
get_num: Que size =  0
0
put_num:  758
put_num: Que size =  1
get_num:  758
get_num: Que size =  0
put_num:  759
put_num: Que size =  get_num:  759
get_num: Que size =  0
0
put_num:  760
put_num: Que size =  1
get_num:  760
get_num: Que size =  0
put_num:  761
put_num: Que size =  get_num:  761
get_num: Que size =  0
0
Closing down


Press enter to Quit:
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Killing a thread from main - was RE: "Lock"ing threads

2005-08-29 Thread Hans Dushanthakumar
Thanks Kent

How do I send a signal from the main thread to stop execution of a child
thread?
   
I tried the foll:, but got an error:
Other than by this method, is there any other mechanism to stop a
thread?

import threading
import time

class shownum(threading.Thread):

def __init__(self, start_num):
threading.Thread.__init__(self)
self.num = start_num
self.stop = 0

def run(self):
for i in range(12):
time.sleep(1)
print "shownum: ", self.num
self.num = self.num + 1
if self.stop == 1:
break

def stop(self):
self.stop = 1

def chng(self):
self.num = 1

incr_num_thread = shownum1(201)
incr_num_thread.start()

time.sleep(3)
incr_num_thread.chng()
time.sleep(3)
incr_num_thread.stop()


Output:

shownum:  201
shownum:  202
shownum:  1
shownum:  2
shownum:  3
Traceback (most recent call last):
  File "H:\Docs\PyScripts\test_threads.py", line 31, in ?
incr_num_thread.stop()
TypeError: 'int' object is not callable
shownum:  4
shownum:  5
shownum:  6

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


Re: [Tutor] Killing a thread from main - was RE: "Lock"ing threads

2005-08-29 Thread Hans Dushanthakumar
 
Oops - the error was probably due to using 'stop' as a method name.
:)


-Original Message-----
From: Hans Dushanthakumar 
Sent: Tuesday, 30 August 2005 2:18 p.m.
To: tutor@python.org
Subject: Killing a thread from main - was RE: [Tutor] "Lock"ing threads

Thanks Kent

How do I send a signal from the main thread to stop execution of a child
thread?
   
I tried the foll:, but got an error:
Other than by this method, is there any other mechanism to stop a
thread?

import threading
import time

class shownum(threading.Thread):

def __init__(self, start_num):
threading.Thread.__init__(self)
self.num = start_num
self.stop = 0

def run(self):
for i in range(12):
time.sleep(1)
print "shownum: ", self.num
self.num = self.num + 1
if self.stop == 1:
break

def stop(self):
self.stop = 1

def chng(self):
self.num = 1

incr_num_thread = shownum1(201)
incr_num_thread.start()

time.sleep(3)
incr_num_thread.chng()
time.sleep(3)
incr_num_thread.stop()


Output:

shownum:  201
shownum:  202
shownum:  1
shownum:  2
shownum:  3
Traceback (most recent call last):
  File "H:\Docs\PyScripts\test_threads.py", line 31, in ?
incr_num_thread.stop()
TypeError: 'int' object is not callable
shownum:  4
shownum:  5
shownum:  6

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


Re: [Tutor] How to write this to a file?

2005-10-06 Thread Hans Dushanthakumar
 print outputs the string to the std output (sys.stdout). You could
redirect the std output to a file instead of the screen as follows, but
it's a bit clumsy.

fptr = open("hans.log", "a+")
import sys
sys.stdout = fptr
print "Hi there"

This will output the string into the file hans.log. However, to get it
to print to the screen again, u will need to have stored the original
contents of sys.stdout somewhere and then redirected sys.stdout to that
again.

TCL provides a more handy way of doing this:
print $fptr "Hi there"

Cheers
Hans


-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Dick Moores
Sent: Thursday, 6 October 2005 12:18 p.m.
To: tutor@python.org
Subject: [Tutor] How to write this to a file?

I have a script that writes it's output to a file. I also print the time
with

print "Time was %.4g seconds" % (timeEnd - timeStart)

How could I also have the same output of the print expression, written
to the file?

Thanks,

Dick Moores

___
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] help with elif statements

2005-10-12 Thread Hans Dushanthakumar
U might wanna change the code to something along the lines of

 def getcredits(num):
if num < 7:
return 'Freshman'
elif num >= 7 and num <16
return 'Sophomore'
elif num >= 16 and num < 26:
return 'Junior'

Etc...

Or even 

 def getcredits(num):
if num < 7:
return 'Freshman'
elif num <16
return 'Sophomore'
elif num < 26:
return 'Junior' 
...



-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of [EMAIL PROTECTED]
Sent: Thursday, 13 October 2005 2:05 p.m.
To: tutor@python.org
Subject: [Tutor] help with elif statements

hello

below is my code and everytime I input a value of 16 or more it keeps
returning sophomore. could anyone help me figure out what to change so
that it won't return sophmore for things greater than or equal to 16?

def getcredits(num):
if num < 7:
return 'Freshman'
elif num >= 7:
return 'Sophomore'
elif num <16:
return 'Sophomore'
elif num >= 16:
return 'Junior'
elif num < 26:
return 'Junior'
else:
return 'Senior'

def main():
g = input('Enter number of credits:')
print 'Your standing is %s' % (getcredits(int(g)))

main()

___
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] question about serial coms

2005-11-14 Thread Hans Dushanthakumar
Ive worked on a similar application. I used one thread to read from the serial 
port and another one to handle the writes. 

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Hugo González 
Monteverde
Sent: Tuesday, 15 November 2005 7:36 a.m.
To: nephish
Cc: tutor
Subject: Re: [Tutor] question about serial coms

Hi Nephish,

Are you using pyserial or rolling your own? Normally you can write and read to 
the /dev/ttySXX file at the same time; since they're special files, not 
ordinary files, the driver handles that.

Handling both writing and reading in your program's flow control is a wholly 
different matter, though. You might  need to use select()  to avoid blocking.

Are you using two completely different scripts for reding and writing?

There is some valuable info, if not about python, in the Serial Programming 
howto, at:

http://www.tldp.org/HOWTO/Serial-Programming-HOWTO/


Hugo

nephish wrote:
> Hey there,
>   i am developing on a linux computer with the serial module. Now, i 
> already am able to recieve info from a serial RS232 device and process 
> everything ok. What i need to do now is write to the serial device, 
> but i also need to be able to not interrupt the script that is reading 
> from it.
>   I guess my question is, do i have to interrupt the reading script to 
> write to the same RS232 device ?
>   and if so, how do i do that?
> 
>   thanks,
>   shawn
> 
> ___
> 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 maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about serial coms

2005-11-14 Thread Hans Dushanthakumar
I believe that the drivers take care of that, however, I did use locks to make 
sure that there were no conflicts.

In the listener thread I had something along the lines of:

 Acquire lock
 readline() from the ser port
 Release lock 

And in the sender thread,

 Acquire lock
 send msg over ser port
 Release lock 

Cheers
Hans

-Original Message-
From: nephish [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, 15 November 2005 10:47 a.m.
To: Hans Dushanthakumar
Cc: Hugo González Monteverde; tutor
Subject: RE: [Tutor] question about serial coms

well thats encouraging, did you have to do anything special to prevent an error 
when trying to read or write at the same time ?

thanks
sk


On Tue, 2005-11-15 at 09:29 +1300, Hans Dushanthakumar wrote:
> Ive worked on a similar application. I used one thread to read from the 
> serial port and another one to handle the writes. 
> 
> -Original Message-
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On 
> Behalf Of Hugo González Monteverde
> Sent: Tuesday, 15 November 2005 7:36 a.m.
> To: nephish
> Cc: tutor
> Subject: Re: [Tutor] question about serial coms
> 
> Hi Nephish,
> 
> Are you using pyserial or rolling your own? Normally you can write and read 
> to the /dev/ttySXX file at the same time; since they're special files, not 
> ordinary files, the driver handles that.
> 
> Handling both writing and reading in your program's flow control is a wholly 
> different matter, though. You might  need to use select()  to avoid blocking.
> 
> Are you using two completely different scripts for reding and writing?
> 
> There is some valuable info, if not about python, in the Serial Programming 
> howto, at:
> 
> http://www.tldp.org/HOWTO/Serial-Programming-HOWTO/
> 
> 
> Hugo
> 
> nephish wrote:
> > Hey there,
> > i am developing on a linux computer with the serial module. Now, i 
> > already am able to recieve info from a serial RS232 device and 
> > process everything ok. What i need to do now is write to the serial 
> > device, but i also need to be able to not interrupt the script that 
> > is reading from it.
> > I guess my question is, do i have to interrupt the reading script 
> > to write to the same RS232 device ?
> > and if so, how do i do that?
> > 
> > thanks,
> > shawn
> > 
> > ___
> > 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 maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about serial coms

2005-11-14 Thread Hans Dushanthakumar
Lock() is provided by the threading module.
see
http://docs.python.org/lib/module-threading.html 
&
http://docs.python.org/lib/lock-objects.html

Cheers
Hans


-Original Message-
From: nephish [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, 15 November 2005 11:23 a.m.
To: Hans Dushanthakumar
Cc: Hugo González Monteverde; tutor
Subject: RE: [Tutor] question about serial coms

ok, lock is something you wrote yourself ?
i can't find it in the docs. However, i think i can essentially build the same 
thing. 
the serial module i use is pyserial. pyserial.sourceforge.net.
the docs are a wee bit on the sparce side. But i think i can pull it off. 
Thanks for your help. 

shawn


On Tue, 2005-11-15 at 10:58 +1300, Hans Dushanthakumar wrote:
> I believe that the drivers take care of that, however, I did use locks to 
> make sure that there were no conflicts.
> 
> In the listener thread I had something along the lines of:
> 
>  Acquire lock
>  readline() from the ser port
>  Release lock
> 
> And in the sender thread,
> 
>  Acquire lock
>  send msg over ser port
>  Release lock
> 
> Cheers
> Hans
> 
> -Original Message-
> From: nephish [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, 15 November 2005 10:47 a.m.
> To: Hans Dushanthakumar
> Cc: Hugo González Monteverde; tutor
> Subject: RE: [Tutor] question about serial coms
> 
> well thats encouraging, did you have to do anything special to prevent an 
> error when trying to read or write at the same time ?
> 
> thanks
> sk
> 
> 
> On Tue, 2005-11-15 at 09:29 +1300, Hans Dushanthakumar wrote:
> > Ive worked on a similar application. I used one thread to read from the 
> > serial port and another one to handle the writes. 
> > 
> > -Original Message-
> > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On 
> > Behalf Of Hugo González Monteverde
> > Sent: Tuesday, 15 November 2005 7:36 a.m.
> > To: nephish
> > Cc: tutor
> > Subject: Re: [Tutor] question about serial coms
> > 
> > Hi Nephish,
> > 
> > Are you using pyserial or rolling your own? Normally you can write and read 
> > to the /dev/ttySXX file at the same time; since they're special files, not 
> > ordinary files, the driver handles that.
> > 
> > Handling both writing and reading in your program's flow control is a 
> > wholly different matter, though. You might  need to use select()  to avoid 
> > blocking.
> > 
> > Are you using two completely different scripts for reding and writing?
> > 
> > There is some valuable info, if not about python, in the Serial Programming 
> > howto, at:
> > 
> > http://www.tldp.org/HOWTO/Serial-Programming-HOWTO/
> > 
> > 
> > Hugo
> > 
> > nephish wrote:
> > > Hey there,
> > >   i am developing on a linux computer with the serial module. Now, 
> > > i already am able to recieve info from a serial RS232 device and 
> > > process everything ok. What i need to do now is write to the 
> > > serial device, but i also need to be able to not interrupt the 
> > > script that is reading from it.
> > >   I guess my question is, do i have to interrupt the reading script 
> > > to write to the same RS232 device ?
> > >   and if so, how do i do that?
> > > 
> > >   thanks,
> > >   shawn
> > > 
> > > ___
> > > 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 maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about serial coms

2005-11-14 Thread Hans Dushanthakumar
Just to make sure that I understood it right,

Does this snippet mimic the problem that you have? Ive hardcoded "line".

x=
import serial
import time
ser=serial.Serial(0,57600,timeout=0.1)
i=0
line = ["Hi","There","Hans"]
while i <= (len(line)-1):

ser.write(line[i] + "\r")
print "Sending: " + line[i]
time.sleep(1)
data_in = ser.read(100)
print "Response: " + data_in
time.sleep(.2)
i = i + 1

print "Closing Serial Port\n"
ser.close()
=x= 

Cheers
Hans

-Original Message-
From: nephish [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, 15 November 2005 2:10 p.m.
To: [EMAIL PROTECTED]
Cc: Hans Dushanthakumar; tutor
Subject: Re: [Tutor] question about serial coms

oh yeah, i will need this too!
sk


On Mon, 2005-11-14 at 17:04 -0800, Bennett, Joe wrote:
> I have been working with pyserial. One question I have is this. I have 
> a loop that writes to the serial port and then waits about 500ms and 
> then reads from the serial port. The first thing read from the serial 
> port is ALWAYS the data written to the serial port... I must be 
> missing something obvious, but I thuoght the two buffers were 
> separate...
> 
> Here is the code I'm using if that helps:
> 
> while i == 0:
>   line = parmfile.readline()
>   line = string.rstrip(line)
>   #print line
>   if line == "":
> i = 1
> break
>   
>   else:
> 
> ser.write(line + "\r")
> #ser.write("\r\n")
> print "Sending: " + line
> 
> 
>
> time.sleep(1)
> data_in = ser.read(100)
> print "Response: " + data_in
> time.sleep(.2)
> 
> 
> print "Closing Serial Port\n"
> ser.close()
> 
> 
> 
> -Joe
> 
> 
> --- nephish <[EMAIL PROTECTED]> wrote:
> 
> > ok, i think i got it. Thanks so much. 
> > let you know how it turns out.
> > shawn
> > 
> > 
> > On Tue, 2005-11-15 at 11:27 +1300, Hans Dushanthakumar wrote:
> > > Lock() is provided by the threading module.
> > > see
> > > http://docs.python.org/lib/module-threading.html
> > > &
> > > http://docs.python.org/lib/lock-objects.html
> > > 
> > > Cheers
> > > Hans
> > > 
> > > 
> > > -Original Message-
> > > From: nephish [mailto:[EMAIL PROTECTED]
> > > Sent: Tuesday, 15 November 2005 11:23 a.m.
> > > To: Hans Dushanthakumar
> > > Cc: Hugo González Monteverde; tutor
> > > Subject: RE: [Tutor] question about serial coms
> > > 
> > > ok, lock is something you wrote yourself ?
> > > i can't find it in the docs. However, i think i
> > can essentially build the same thing. 
> > > the serial module i use is pyserial.
> > pyserial.sourceforge.net.
> > > the docs are a wee bit on the sparce side. But i
> > think i can pull it off. Thanks for your help. 
> > > 
> > > shawn
> > > 
> > > 
> > > On Tue, 2005-11-15 at 10:58 +1300, Hans
> > Dushanthakumar wrote:
> > > > I believe that the drivers take care of that,
> > however, I did use locks to make sure that there were no conflicts.
> > > > 
> > > > In the listener thread I had something along the
> > lines of:
> > > > 
> > > >  Acquire lock
> > > >  readline() from the ser port
> > > >  Release lock
> > > > 
> > > > And in the sender thread,
> > > > 
> > > >  Acquire lock
> > > >  send msg over ser port
> > > >  Release lock
> > > > 
> > > > Cheers
> > > > Hans
> > > > 
> > > > -Original Message-
> > > > From: nephish [mailto:[EMAIL PROTECTED]
> > > > Sent: Tuesday, 15 November 2005 10:47 a.m.
> > > > To: Hans Dushanthakumar
> > > > Cc: Hugo González Monteverde; tutor
> > > > Subject: RE: [Tutor] question about serial coms
> > > > 
> > > > well thats encouraging, did you have to do
> > anything special to prevent an error when trying to read or write at 
> > the same time ?
> > > > 
> > > > thanks
> > > > sk
> > > > 
> > > > 
> > > > On Tue, 2005-11-15 at 09:29 +1300, Hans
> > Dushanthakumar wrote:
> > > > > Ive worked on a similar application. I used
> > one thread to read from the serial port and ano

Re: [Tutor] Newbie question

2005-11-22 Thread Hans Dushanthakumar



A short-cut if you dont want to use DOS to traverse to your 
directory or if you are feeling too lazy to type in the entire path & script 
name.
 
You can right-click on the script file in the Windows 
explorer and choose "Send to -> Command prompt". This opens a command prompt 
with the path already set to the one that you want, and the name of the script 
already typed in. Now, just cursor to the left untill you reach the ">" 
prompt and type in "python ". So now your command prompt reads "C:\whatever 
path>python yourfile.py". Press enter to run the 
script.


From: [EMAIL PROTECTED] 
[mailto:[EMAIL PROTECTED] On Behalf Of bobSent: 
Wednesday, 23 November 2005 10:44 a.m.To: Liam Clarke-Hutchinson; 
'Douglass, Erik'; 'tutor@python.org'Subject: Re: [Tutor] Newbie 
question
At 12:20 PM 11/22/2005, Liam Clarke-Hutchinson wrote:
Hi Eric, 
   Either - 
   add this line to the end of 
  your scripts -discard = raw_input("Press enter to 
  finish.") Or - 
   Click on Start > Run... 
  type cmd.exe and use DOS to move to the directory where your scripts are 
  stored and run them via Python there.This is preferred, 
since any exception traceback will remain visible.
 It's not trivial when you're starting. :-) Regards, Liam Clarke-Hutchinson-Original Message-From: [EMAIL PROTECTED] [ 
  mailto:[EMAIL PROTECTED]] On Behalf Of Douglass, 
  ErikSent: Wednesday, 23 November 2005 3:03 a.m.To: 
  tutor@python.orgSubject: [Tutor] Newbie question
  
I am trying to follow some online first timer tutorials, and I am 
writing the practice scripts in notepad (only w32 at work L)..   I save the script with a .py 
extension, and when I run it it opens for a brief moment in a command prompt 
then closes before I even have a chance to see what it says.  This may 
seem trivial, but Python also happens to be my first language so this is all 
new to me.    Using Python 2.4.2
 
Thanks for any help.
 
Erik A new 
  monthly electronic newsletter covering all aspects of MED's work is now 
  available.  Subscribers can choose to receive news from any or all of 
  seven categories, free of charge: Growth and Innovation, Strategic Directions, 
  Energy and Resources, Business News, ICT, Consumer Issues and Tourism.  
  See http://news.business.govt.nz 
  for more details. govt.nz - 
  connecting you to New Zealand central & local government 
  services
  
  Any opinions expressed in this message are not 
  necessarily those of the Ministry of Economic Development. This message and 
  any files transmitted with it are confidential and solely for the use of the 
  intended recipient. If you are not the intended recipient or the person 
  responsible for delivery to the intended recipient, be advised that you have 
  received this message in error and that any use is strictly prohibited. Please 
  contact the sender and delete the message and any attachment from your 
  computer. 
  
  ___Tutor maillist  
  -  Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Using py2exe

2005-11-22 Thread Hans Dushanthakumar
Hi,
   Im trying to convert a python program to a stand-alone executable
that I can distribute. How do I use py2exe to do this?
   The python program consists of a py script that uses (imports)
several other modules (py scripts) located in another directory.

Heres what Ive tried so far:

   1) Running "python setup.py py2exe" on the cmd line created the
required exe file. Hoever, on running the exe file, it reports an error:
  File "log_all_msgs_spartacus.py", line 8, in ?
ImportError: No module named listener

   2) Running the command followed by comma seperated module names (all
modules that are imported by the main script)

python setup.py py2exe -i ..\lib\listener.py, ..\lib\
sender.py, ..\lib\gpsmsg.py, ..\lib\envmsg.py, ..\lib\cmds.py,
..\lib\logger.py, ..\lib\nmea.py

Produced the foll error:
invalid command name '..\lib\sender.py,'

The contents of setup.py is as follows (got it off the py2exe website):

from distutils.core import setup
import py2exe

setup(
version = "0.0.1",
description = "Log_all_msgs_from_Spartacus",
name = "Spartacus_monitor",

# targets to build
console = ["log_all_msgs_spartacus.py"],
)

Once I've created the exe file, can it be run on a PC which does not
python setup in it? Note: - the program uses the standard pyserial
module as well.

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


[Tutor] How to Pass lists by value

2005-12-05 Thread Hans Dushanthakumar
 
Hi folks,
   How do I pass a list by value to a function.

The foll: snippet of code produces the output as shown:

Code:
-
def junk(x):
x.append("20")
return x

a = ["10"]
b = junk(a)

print b
print a


Output:
---
>>> 
b =  ['10', '20']
a =  ['10', '20']

This indicates that the variable "a" was passed by reference because the
function alters its value in the main script.

However, the following code produces a slightly diff output:

Code:
-

def junk(x):
x = "30"
return x

a = ["10"]
b = junk(a)

print "b = ", b
print "a = ", a

Output:
---
>>> 
b =  30
a =  ['10']

In this case, "a" seems to be passed by value, because the value is
unchanged even after the call to the function.


Question is, in the 1st scenario, how do I forcefully pass the variable
by value rather than reference. Why is there a difference between the
two scenarios?

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


Re: [Tutor] How to Pass lists by value

2005-12-05 Thread Hans Dushanthakumar



Thanks guys
 
Yes either of the foll solves the 
problem:
 
b = junk(copy.copy(a))
 OR
b = 
junk(a[:])
 
 
Why is there a difference between the way the 
two lines (x.append("20")  and x = "30") are handled within a 
function?
 


From: Adam [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, 6 December 2005 1:49 p.m.To: Hans 
DushanthakumarSubject: Re: [Tutor] How to Pass lists by 
value
On 06/12/05, Hans Dushanthakumar <[EMAIL PROTECTED]> 
wrote:

Hi 
  folks,   How do I pass a list by value to a function.The 
  foll: snippet of code produces the output as 
  shown:Code:-def 
  junk(x):x.append("20")return 
  xa = ["10"]b = junk(a)print bprint 
  aOutput:--->>>b =  ['10', 
  '20']a =  ['10', '20']This indicates that the variable 
  "a" was passed by reference because the function alters its value in the 
  main script.However, the following code produces a slightly diff 
  output:Code:-def junk(x):x 
  = "30"return xa = ["10"] b = 
  junk(a)print "b = ", bprint "a = ", 
  aOutput:--->>>b =  30a 
  =  ['10']In this case, "a" seems to be passed by value, 
  because the value is unchanged even after the call to the 
  function.Question is, in the 1st scenario, how do I forcefully 
  pass the variableby value rather than reference. Why is there a difference 
  between thetwo scenarios? 
  CheersHans___Tutor 
  maillist  -  Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor 
  
 def junk(x):   x.append("20")  
 return xa = ["10"]b = junk(a[:])That should give you 
what you're looking for a[:] will pass the values of 
a.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to Pass lists by value

2005-12-06 Thread Hans Dushanthakumar
 Thanks for your valuable feedback guys.

Cheers
Hans

-Original Message-
From: w chun [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, 6 December 2005 9:11 p.m.
To: Hans Dushanthakumar
Cc: tutor@python.org
Subject: Re: How to Pass lists by value

On 12/5/05, Kent Johnson <[EMAIL PROTECTED]> wrote:
>
> You have to change how you think about variables. In Python, a 
> variable is not a storage location into which values are put, it is a 
> reference to an object - a name given to an object. Assignment binds a

> name to a value.
>
> When you call a function, the names of the formal parameters are bound

> to the values passed in to the function. Parameters are *always* 
> passed by reference.


hans,

kent makes some good points here that you need to master.  to further
elaborate, here are three things to keep in mind:

1) in Python, everything is an object, and all names are just references
or aliases to those objects.

2) managing these objects is based on how many references exist that
point to an object -- this is called a "reference count."  objects are
"deallocated" when an object's reference count goes to zero.

3) there are both mutable (can be changed) and immutable (cannot be
changed without creating a new one) objects.  lists and dictionaries are
mutable while numbers, strings, and tuples are not.

in your example, you passed in a list by reference.  that list had a
reference count of 1 when you created it; when you passed it to the
function, a new reference, local to the called function, is created for
that object, incrementing its reference count by 1, and now it totals 2.
(a side note here: that only mutable objects have methods.) you then
called a method [[].append()] which alters that object, which it did.
in the function, you only had access to the 2nd alias to the list, but
it doesn't matter whether you used this one, or called the list's method
from the global code using the original reference as either would affect
the list the way it did.  note that when the function concluded, the
local variable went away, decrementing the list object's reference count
back down to 1.

in your second example, you almost did the exact same thing.  you
*did* pass the list in as an argument, creating another reference to the
list object, incrementing its reference count to 2.  but in this case,
you immediately "wiped" access to that object by reassigning that
variable name to point to something else, an integer.  all you did here
was to decrement the reference count to the list object by one (back to
1).  the outer print gave the expected output because you just passed
that integer object back to the calling function.

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

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


[Tutor] Writing/reading lists to a file

2005-12-27 Thread Hans Dushanthakumar

Hi,
   Is there any easy way of writing lists to a file and more
importantly, reading it back as a list of lists rather than as a list of
strings.

Eg:

>>> t = ["t1", "PASS", 31]
>>> f = open("pass.txt","a+")
>>> f.write(str(t) + "\n")
>>> f.write(str(t) + "\n")
>>> f.close()

At this stage, the file contains two lines.

Now, if I use the readlines() function to read it back, heres what I
get:
>>> f = open("pass.txt","r+")
>>> r = f.readlines()
>>> r
["['t1', 'PASS', 31]\n", "['t1', 'PASS', 31]\n", "['t1', 'PASS', 31]\n"]
>>> r[0]
"['t1', 'PASS', 31]\n"

So, r[0] is now a string. Is there ant direct way of extracting the list
from this string?

Or alternatively, can I read the file as a list of lists rather than
list of strings (which is what readlines() appears to do).

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


Re: [Tutor] new to linux and I cannot find some python things

2005-12-29 Thread Hans Dushanthakumar
Anothet linux noobie here :)
How do I uninstall python? I use the SimplyMepis flavor of linux.

The reason I want to uninstall python is that its an old version (2.3).
Not that I particularly want the new version, but the IDLE installation
that I downloaded reports all kinds of errors because apparently its
meant to work with python 2.4. 

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Brian van den Broek
Sent: Thursday, 29 December 2005 12:58 p.m.
To: Simon Gerber
Cc: Tutor
Subject: Re: [Tutor] new to linux and I cannot find some python things

Simon Gerber said unto the world upon 28/12/05 05:12 PM:
>>Hi all,
>>
>>I'm a week or so into having switched from WinXP to linux (ubuntu 
>>breezy). There is a lot to learn about the differences in the OS'es 
>>and that's just fine.
> 
> 
> Excellent! Another Ubuntu Breezy user here. If there's anything Ubuntu

> I can help you with, drop me an e-mail and I'll do what I can to help.

Hi Simon,

thanks for the reply and the offer :-)

>>But, a couple of things have been in my way with Python. Most notably,
>>  I don't know how one browses the documentation. On Windows, I just 
>>fired up the .chm (I think cmh--at any rate, the compiled help file.)
> 
> 
> Yeah, chm. Incidentally, there's a chm reader for Linux. Very 
> primative, but it works in a pinch. Look for 'xchm' in the Universe 
> repository.

Thanks. A bit part of the difficulty in the transition is suddenly I
don't know what program to use for what. Pointers help :-)

> 
>>I have installed the docs on the linux side and they can be found by
>>python:
>>
>> >>> help()
>>
>>help> NONE
> 
> 
> Nah, that's part of core Python. Nothing to do with the 'python-doc'
> package you installed.

I beg to differ :-)

Before I installed I got this:

IDLE 1.1.2
 >>> help()

Welcome to Python 2.4!  This is the online help utility.



help> topics

Here is a list of available topics.  Enter any topic name to get more
help.

ASSERTION   DELETIONLOOPING SEQUENCES



help> ASSERTION

Sorry, topic and keyword documentation is not available because the
Python HTML documentation files could not be found.  If you have
installed them, please set the environment variable PYTHONDOCS to
indicate their location.

On Debian GNU/{Linux,Hurd} systems you have to install the corresponding
pythonX.Y-doc package, i.e. python2.3-doc.

help>


On windows, one has to download the html version of the documentation
and point the PYDOCS (or something close) env. variable at them. On
ubuntu, once I installed python2.4-doc, it worked as shown in my OP. 
(I did test by removing the python2.4-doc package to get the behaviour
shown in this post, then reinstalling to get the original behaviour.)

>>I assume there is some linux facility for documentation browsing that 
>>beats importing modules and accessing docstrings. I'd work it out 
>>eventually, but a timesaving pointer would be appreciated.
> 
> 
> Firefox!
> 
> file:///usr/share/doc/python2.4/html/index.html

But shouldn't it be harder than that? :-)

> The python-doc package is just an offline version of 
> http://www.python.org/doc/2.4.2/
> 
> You can also probably find a copy of the book 'Dive into Python' here:
> file:///usr/share/doc/diveintopython/html/index.html
> 
> I know Hoary installed it by default. Not sure about Breezy, since I 
> just did a dist-upgrade from Hoary.

Yep, it was installed by default. I'd wondered where it lived. But,
since I've the dead-tree version, I didn't get motivated enough to find
out. Still, thanks.

> As a rule, with Ubuntu (and most other Linux distros), the 
> documentation goes under /usr/share/doc/. But you can always

> check to see exactly what a package has put where. From the 
> command-line, just type 'dpkg -L python-doc'.
> 
> Hope that helps,

Thanks, it did. Especially that last bit. I've been learnign the various
shell commands almost as quickly as I've been finding I want to know
what command does foo.

Thanks muchly,

Brian vdB
___
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] Disabling a frame in Tkinter

2006-01-05 Thread Hans Dushanthakumar
Hi,
   Is there any way to disable an entire frame (and all its included
widgets) in Tkinter. It does not seem to support state=DISABLED.

Another Tkinter question: In a listbox, how do I intially set a
"selected" item? What I want to do here is have the 1st item in a
listbox selected (ie highlighted) when the appln is run.

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


Re: [Tutor] Disabling a frame in Tkinter

2006-01-05 Thread Hans Dushanthakumar
Thanks John,
   Yup the code you provided disables all child-widgets:

def setState(self, widget, state='disabled'):
print type(widget)
try:
widget.configure(state=state)
except Tkinter.TclError:
pass
for child in widget.winfo_children():
self.setState(child, state=state)

Cheers
Hans

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of John Fouhy
Sent: Friday, 6 January 2006 12:03 p.m.
To: tutor@python.org
Subject: Re: [Tutor] Disabling a frame in Tkinter

On 06/01/06, Hans Dushanthakumar <[EMAIL PROTECTED]> wrote:
> Hi,
>Is there any way to disable an entire frame (and all its included
> widgets) in Tkinter. It does not seem to support state=DISABLED.

Not that I'm aware of...  You could try maybe something like this
(untested):

def setState(widget, state=DISABLED):
  try:
widget.config(state=state)
  except TclError:   # not sure what exception will be raised
pass
  for child in widget.children:
setState(child, state=state)

> Another Tkinter question: In a listbox, how do I intially set a 
> "selected" item? What I want to do here is have the 1st item in a 
> listbox selected (ie highlighted) when the appln is run.

Have a look at Fredrik Lundh's Introduction to Tkinter.  You can use
.selection_clear() to clear the selection and .selection_set(index) to
select an item. So, .selection_set(0) will select the first item (just
make sure the listbox isn't empty).

--
John.
___
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] Returning multiple values from a script

2006-01-11 Thread Hans Dushanthakumar
Hi,
   Can a script return multiple values to the os?

What I have in mind is something like the following:


1) Test.py
---
import sys

r = 7
sys.exit(r)
# What I really want to do is something along the lines of sys.exit(r,
"Hans")



2) Script1.py (This script executes script test.py and prints out its
exit code): 
--
import os

t = "test.py"
res = os.system('"python test.py")
print res



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


Re: [Tutor] Returning multiple values from a script

2006-01-11 Thread Hans Dushanthakumar
Yes I agree that it'd be cleaner to import the second script and call
it.

The reason I'm keen to find a alternate method is that I have a whole
lot of scripts that were designed to run only as standalone scripts. ie
each of these scripts is not a "function" that I could just call from
another script. They are all of the format:

1) Test.py
---
import sys

sys.exit(5)


Now I'm trying to write a master script that'd run each one of these
scripts. I'm sure it would have been a lot easier if the scripts were of
the following format. Unfortunately they are not.:

Test.py
---
import sys

Def test():
return(5, "Junk")

if __name__ == __main__:
   test()


Well if there is no other way I think I'll have to alter all the scripts
to be of the above format. Just wondering if anyone has any suggestions
...

Cheers
Hans


-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Kent Johnson
Sent: Thursday, 12 January 2006 12:06 p.m.
Cc: Python Tutor
Subject: Re: [Tutor] Returning multiple values from a script

Hans Dushanthakumar wrote:
> Hi,
>Can a script return multiple values to the os?

Is there a reason why you have to call the second test.py using
os.system()? I would write it to be imported and called.

test.py
--

def findR():
   return 7, 'Hans'


script1.py
-

import test

res = test.findR()
print res

Kent

> 
> What I have in mind is something like the following:
> 
> 
> 1) Test.py
> ---
> import sys
> 
> r = 7
> sys.exit(r)
> # What I really want to do is something along the lines of sys.exit(r,
> "Hans")
> 
> 
> 
> 2) Script1.py (This script executes script test.py and prints out its 
> exit code):
> --
> import os
> 
> t = "test.py"
> res = os.system('"python test.py")
> print res

___
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] Returning multiple values from a script

2006-01-11 Thread Hans Dushanthakumar
Thanks for your reply Kent.

Is it possible to dynamically import a module?

The foll snippet of code throws an error "ImportError: No module named
testname"


t = ["test1.py", "test2.py"] #Actually this list is filled in by a
Tkinter Listbox selection.

for f in t:
testname = f[:-3]
import "%s"%(testname)
print testname.run_test()


Any other means of importing dynamically?




-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Kent Johnson
Sent: Thursday, 12 January 2006 2:02 p.m.
Cc: Python Tutor
Subject: Re: [Tutor] Returning multiple values from a script

Hans Dushanthakumar wrote:
> Yes I agree that it'd be cleaner to import the second script and call 
> it.
> 
> The reason I'm keen to find a alternate method is that I have a whole 
> lot of scripts that were designed to run only as standalone scripts. 
> ie each of these scripts is not a "function" that I could just call 
> from another script. They are all of the format:
> 
> 1) Test.py
> ---
> import sys
> 
> sys.exit(5)
> 
> 
> Now I'm trying to write a master script that'd run each one of these 
> scripts. I'm sure it would have been a lot easier if the scripts were 
> of the following format. Unfortunately they are not.:
> 
> Test.py
> ---
> import sys
> 
> Def test():
> return(5, "Junk")
> 
> if __name__ == __main__:
>test()
> 
> 
> Well if there is no other way I think I'll have to alter all the 
> scripts to be of the above format. Just wondering if anyone has any 
> suggestions

ISTM that you have to change all the scripts anyway if you want to
return two values...why not change them to call a different function
(other than sys.exit) that does what you want? Do the scripts still have
to run standalone?

For example you could make a module mysys.py:

returnedvalue = None

def exit(value):
   global returnedvalue
   returnedvalue = value

Then just edit the scripts to import mysys and call mysys.exit(), run
the script with import and get the returnedvalue from mysys.

If you want a base hack that I couldn't possibly recommend :-) I suppose
you could replace sys.exit() with a function of your own choosing. 
Something like this should work...

  >>> import sys
  >>> returnedvalue = None
  >>>
  >>> def mysysexit(value):
  ...   global returnedvalue
  ...   returnedvalue = value
  ...
  >>> original_exit = sys.exit # if you need to keep the old value...
  >>> sys.exit = mysysexit
  >>>
  >>> sys.exit(5) # here you can just import the module you want to run
  >>> returnedvalue
5

Isn't Python wonderful!
Kent

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


Re: [Tutor] Returning multiple values from a script

2006-01-11 Thread Hans Dushanthakumar
Oops answered my own question. Dynamic importing is done using the
__import__ function:

t = ["test1.py", "test2.py"] #Actually this list is filled in by a
Tkinter Listbox selection.

for f in t:
testname = f[:-3]
test = __import__("%s"%(testname))
res = test.run_test()

Cheers
Hans


-----Original Message-
From: Hans Dushanthakumar 
Sent: Thursday, 12 January 2006 2:28 p.m.
Cc: Python Tutor
Subject: RE: [Tutor] Returning multiple values from a script

Thanks for your reply Kent.

Is it possible to dynamically import a module?

The foll snippet of code throws an error "ImportError: No module named
testname"


t = ["test1.py", "test2.py"] #Actually this list is filled in by a
Tkinter Listbox selection.

for f in t:
testname = f[:-3]
import "%s"%(testname)
print testname.run_test()


Any other means of importing dynamically?




-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Kent Johnson
Sent: Thursday, 12 January 2006 2:02 p.m.
Cc: Python Tutor
Subject: Re: [Tutor] Returning multiple values from a script

Hans Dushanthakumar wrote:
> Yes I agree that it'd be cleaner to import the second script and call 
> it.
> 
> The reason I'm keen to find a alternate method is that I have a whole 
> lot of scripts that were designed to run only as standalone scripts.
> ie each of these scripts is not a "function" that I could just call 
> from another script. They are all of the format:
> 
> 1) Test.py
> ---
> import sys
> 
> sys.exit(5)
> 
> 
> Now I'm trying to write a master script that'd run each one of these 
> scripts. I'm sure it would have been a lot easier if the scripts were 
> of the following format. Unfortunately they are not.:
> 
> Test.py
> ---
> import sys
> 
> Def test():
> return(5, "Junk")
> 
> if __name__ == __main__:
>test()
> 
> 
> Well if there is no other way I think I'll have to alter all the 
> scripts to be of the above format. Just wondering if anyone has any 
> suggestions

ISTM that you have to change all the scripts anyway if you want to
return two values...why not change them to call a different function
(other than sys.exit) that does what you want? Do the scripts still have
to run standalone?

For example you could make a module mysys.py:

returnedvalue = None

def exit(value):
   global returnedvalue
   returnedvalue = value

Then just edit the scripts to import mysys and call mysys.exit(), run
the script with import and get the returnedvalue from mysys.

If you want a base hack that I couldn't possibly recommend :-) I suppose
you could replace sys.exit() with a function of your own choosing. 
Something like this should work...

  >>> import sys
  >>> returnedvalue = None
  >>>
  >>> def mysysexit(value):
  ...   global returnedvalue
  ...   returnedvalue = value
  ...
  >>> original_exit = sys.exit # if you need to keep the old value...
  >>> sys.exit = mysysexit
  >>>
  >>> sys.exit(5) # here you can just import the module you want to run
  >>> returnedvalue
5

Isn't Python wonderful!
Kent

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


Re: [Tutor] Strings backwards

2006-01-18 Thread Hans Dushanthakumar

Try this:

print word[::-1]

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On
Behalf Of ryan luna
Sent: Thursday, 19 January 2006 12:13 p.m.
To: tutor@python.org
Subject: [Tutor] Strings backwards

Hello, what i need to do is get user input and then print the string
backwards ^^ i have no idea how to do that, 

print "Enter a word and i well tell you how to say it backwards"

word = raw_input("Your word: ")

print word

all that is simple enough im sure printing it out backwards is to, just
dont know how ^^, thanks for any help.
___
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] Getting Running File's Name

2006-01-30 Thread Hans Dushanthakumar
Under WinXP, the variable
sys.argv[0] holds the script file name (including the path). Not sure,
but it may work the same under Linux as well.


-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Bryan Carbonnell
Sent: Tuesday, 31 January 2006 3:11 p.m.
To: tutor@python.org
Subject: [Tutor] Getting Running File's Name

Can Python return the name of the current file's name?

In other works, if I am running a Python file (MyPythonFile.py) is there
a function that will return 'MyPythonFile.py'?

This will be used in Linux if that matters.

Thanks

--
Bryan Carbonnell - [EMAIL PROTECTED]
Warning: dates on calendar are closer than they appear.


___
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] Printing the Carriage return character

2006-02-19 Thread Hans Dushanthakumar
Hi,
   Not sure if this is a python thing or a Operating system peculiarity,
but here goes:
Why does the line
print "FirstLine" + "\rSecondLine"
produce different output when run via IDLE and when run in the python
prompt (both under Windows XP)?

Output in IDLE (ver 1.1.1, python 2.4.1):
>>> print "FirstLine" + "\rSecondLine"
FirstLine
SecondLine
>>> 

Output at the python prompt (python 2.4.1):
C:\QVCS\Mobile Data\>python
Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print "FirstLine" + "\rSecondLine"
SecondLine
>>>

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


Re: [Tutor] Printing the Carriage return character

2006-02-20 Thread Hans Dushanthakumar
Thanks Alan for clearing that up...I was trying to see why my "\r\n"
does not print 2 empty lines when I stumbled across this 'gotcha'. 

-Original Message-
From: Alan Gauld [mailto:[EMAIL PROTECTED] 
Sent: Monday, 20 February 2006 9:22 p.m.
To: Hans Dushanthakumar; tutor@python.org
Subject: Re: [Tutor] Printing the Carriage return character

>Not sure if this is a python thing or a Operating system 
> peculiarity,

An IDLE thing specifically - or maybe even  a Tkinter thing...

> Why does the line
> print "FirstLine" + "\rSecondLine"
> produce different output when run via IDLE and when run in the python 
> prompt (both under Windows XP)?

\r is a carriage return which literally means that the printhead
carriage should return to the start of the line. You need a line feed
character if you want a new line too.

Unfortunately some OS use \r to do both, others need both.
The safe way is to use \n (new line) instead.

> Output in IDLE (ver 1.1.1, python 2.4.1):
> FirstLine
> SecondLine

> Output at the python prompt (python 2.4.1):
> SecondLine

So being pedantic XP is correct, IDLE is wrong but in fact because the
conventions are so mixed up right and wrong is a bit woolly.

Which response were you trying to get?

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


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


[Tutor] Python for basic web-testing

2006-08-06 Thread Hans Dushanthakumar



Hi,
   How do I use python for basic web-tasks 
like inputting data or clicking buttons on web-pages. For eg, how do I enter my 
username and password and click on the "OK" button on the yahoo mail 
page?
I had 
a look at the webbrowser module documentation (http://www.python.org/doc/current/lib/module-webbrowser.html), 
but it doesnt seem to support clicking buttons or sending 
data.
Thanks,
Hans
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python for basic web-testing

2006-08-06 Thread Hans Dushanthakumar
Have been doing some googling myself..The Selenium tool looks good. It
appears to be a record-and-play extension to the firefox
browser...Anyone have any experience with the Selnium Remote Control?
I'm having difficulty installing it.
Cheers
Hans

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Dustin J. Mitchell
Sent: Monday, 7 August 2006 11:45 a.m.
To: tutor@python.org
Subject: Re: [Tutor] Python for basic web-testing

Hans Dushanthakumar wrote:
> Hi,
>How do I use python for basic web-tasks like inputting data or 
> clicking buttons on web-pages. For eg, how do I enter my username and 
> password and click on the "OK" button on the yahoo mail page?
> I had a look at the webbrowser module documentation 
> (http://www.python.org/doc/current/lib/module-webbrowser.html), but it

> doesnt seem to support clicking buttons or sending data.

All the stuff you need is right here:

http://wwwsearch.sourceforge.net/

Specifically, the clicking buttons part is in ClientForm.

Dustin
___
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] command history in a console

2008-09-17 Thread Hans Dushanthakumar

G'day everyone.

I'm experimenting with a custom console application, and trying to add 
command history functionality to it. It seems to basically work ok 
except for the fact that when I press the Up arrow key to run previous 
commands, the right commands are not displayed. It displays a wierd 
"^[[A" for each Up arrow key that I press. Inspite of this, when I hit 
enter, it DOES seem to run the right command.


Heres a bit of code (stripped down to the bare essentials to demonstrate 
just this problem) that shows this behaviour:


--X Code starts here

import readline
import select
import sys
import os

historyPath = ".pyhistory.test"

if os.path.exists(historyPath):
   readline.read_history_file(historyPath)

inputs = [sys.stdin]   #More on this later.

while 1:
   in_ready, out_ready, ex_ready = select.select(inputs, [], [], 10)   
#More on this later.

   x= raw_input()
   print "You entered: ", x

X End of code

Heres what an example run of the program looks like, with my comments:

[EMAIL PROTECTED]:~/tools$ python test_history1.py
ls <--Me: I entered "ls" here. So, 
this is the 1st command.

ls
You entered:  ls
1 <---Me: 2nd command "1"
1
You entered:  1
2 <---Me: 3rd command "2"
2
You entered:  2
^[[A^[[A <--Me: This is the strange characters. 
I pressed the Up key twice here.

1
You entered:  1