Re: [Tutor] More type() puzzlement

2007-10-28 Thread Alan Gauld

"Dick Moores" <[EMAIL PROTECTED]> wrote

> So you're saying that it's to be expected that the analogy, "int is
> to long as int is to float" will hold. But why should it be expected
> to hold? float and long are completely different animals, no?

No, they are all types of numbers.
The general rule is that Python (and most other languages)
will convert a simpler type to a more "powerful" type but never
the other way around - you may need to keep the power...

Alan G 


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


Re: [Tutor] system call

2007-10-28 Thread Alan Gauld

"Eric Walker" <[EMAIL PROTECTED]> wrote

> The problem is that I have another tool that has an initialization 
> period when you start it up. I want to see how long it takes 

Have you considered just using the Unix time command?

> So, to time the entire init process, I want to use python. 

As an exercise its fair enough, but the OS time command
is probably just as effective for this kind of task.

Reimplementing standard OS tools can be a great learning tool
so if that is your aim, carry on with it. The os.system route 
(or subprocess.call) should be fine for what you need.

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


Re: [Tutor] how to run function only for a pre-defined time

2007-10-28 Thread Vinu Vikram
Hi alan
Thanks.
But if I write a code like the followiing, how I can stop the thread

#!/usr/bin/env python
import threading
import time, os
class Infi(threading.Thread):
  def __init__(self, a):
self.a = a
threading.Thread.__init__(self)
  def run(self):
i = 1
while i==1:
  print self.a
  time.sleep(.5)
  self.a += 1
if __name__ == '__main__':
  t = Infi(1)
  t.start()
  time.sleep(2)
  if t.isAlive():
print 'Here I want to stop the thread (the while loop) and continue with
the remaining part of the script'
  obj_file = open(clus_cata,'r')
  try:
pnames = obj_file.readline().split()
  except:
pass
..
..
..
and so on

Thanks in Advance
Vinu Vikram

On 10/27/07, Alan Gauld <[EMAIL PROTECTED]> wrote:
>
>
> "Vinu Vikram" <[EMAIL PROTECTED]> wrote
>
> > Could anybody try to tell me whether it is possible to run a
> > function only
> > for a particular time period. In my python script I call some third
> > party
> > function which some time won't give results. I would like to write
> > the
> > script in such a way that, it will wait for the result from this
> > function
> > for some time, say 10 seconds, and if it fails to get any results
> > within
> > that time (10 seconds), the script should skip that function and
> > stop the
> > program.
> >
>
> I think you will need to use threading to launch your function in a
> separate thread. If the thread dopesn't end in time you can proceed
> in the main thread to kill the thread and exit the program.
>
> Read about Python threading here:
>
> http://heather.cs.ucdavis.edu/~matloff/Python/PyThreads.pdf
>
> --
> 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
>



-- 
VINU VIKRAM
http://iucaa.ernet.in/~vvinuv/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] system call

2007-10-28 Thread John
Here is my actual code (a section of a long script):

if os.path.isdir(ad) and aRun.split('_')[0]==STN_id[i] and in (aRun.split
('_')[1]):
   make_pages=os.path.join(BASEDIR,STN_id[i],SUBDIR[1],'make_pages')
   sedstr1="""cat %s | sed -e 's/ASYSmin=[0-9][0-9]*/ASYSmin=%s/g' > jnk"""
% (make_webpages,nf+1)
   sedstr2="""cat jnk | sed -e 's/ASYSmax=[0-9][0-9]*/ASYSmax=%s/g' > %s"""
% (nf+1,make_pages)
   cmd="""%s > %s """ % (make_pages,STN_id[i]+'.mw.out')
   print """ Now running make_pages for: %s """ % (files[nf])
   os.system(sedstr1)
   os.system(sedstr2)
   fail= os.system(cmd)
   print(cmd)
   if fail: print """For %s there was an error""" % (files[i])
   os.wait()
   print """Finished for %s """ % (files[i])
   raw_input('Continue?')

make_pages is  a shell script which usually is run with nohup and takes
anywhere from 1-24 hours to run, depending on ASYSmin, ASYSmax. If there the
same (which they are here) then it should take around one hour.

Perphaps I need to change the os.wait somehow? Or structure the
os.systemcall for cmd a little differently???

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


Re: [Tutor] 'source' in python or preloading variables

2007-10-28 Thread John
But won't if fail since the variabls in the file are not quoted?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] More type() puzzlement

2007-10-28 Thread Dick Moores
At 01:53 AM 10/28/2007, you wrote:

>"Dick Moores" <[EMAIL PROTECTED]> wrote
>
> > So you're saying that it's to be expected that the analogy, "int is
> > to long as int is to float" will hold. But why should it be expected
> > to hold? float and long are completely different animals, no?
>
>No, they are all types of numbers.
>The general rule is that Python (and most other languages)
>will convert a simpler type to a more "powerful" type but never
>the other way around - you may need to keep the power...

Ah, got it. Finally.

Thanks very much, Alan.

Dick


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


[Tutor] killing bash, sshd... dangerous?

2007-10-28 Thread John
Hello, I've written a little script with the intention of killing all of my
bash, sshd sessions... is this dangerous? How could I make it work so that
it didn't kill the session it was run from (as it is I suppose I have to run
it with nohup):


#!/usr/bin/env python
import os

cmd="ps -u myuser | grep 'bash' > .ps"
os.system(cmd)

pid=[]
fid=open('.ps','r')
for line in fid:
pid.append(line.split(' ')[0])

for i in pid:
cmd="""kill -9 %s""" % (i)
os.system(cmd)


cmd="ps -u myuser | grep 'sshd' > .ps"
os.system(cmd)

pid=[]
fid=open('.ps','r')
for line in fid:
pid.append(line.split(' ')[0])

for i in pid:
cmd="""kill -9 %s""" % (i)
os.system(cmd)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 'source' in python or preloading variables

2007-10-28 Thread Alan Gauld

"John" <[EMAIL PROTECTED]> wrote 

> But won't if fail since the variabls in the file are not quoted?

No the quoting is only necessary if you use eval/exec.
The quotes prevent the interpreter from treating them as 
variable names. 

If you read the values from the file they are already strings, 
no need for quoting.

Alan G

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


Re: [Tutor] killing bash, sshd... dangerous?

2007-10-28 Thread Tom Tucker
Instead of reading in an outputfile (.ps), try reading from command
output.  The correct terminology escapes me (for file in
os.popen(cmd).readlines():).

Are you looking for a auto logout method? For example if no activity
after X minutes kill the shell.  Bash and a few other shells have this
functionality already.





On 10/28/07, John <[EMAIL PROTECTED]> wrote:
> Hello, I've written a little script with the intention of killing all of my
> bash, sshd sessions... is this dangerous? How could I make it work so that
> it didn't kill the session it was run from (as it is I suppose I have to run
> it with nohup):
>
>
>
> #!/usr/bin/env python
> import os
>
> cmd="ps -u myuser | grep 'bash' > .ps"
> os.system(cmd)
>
> pid=[]
> fid=open('.ps','r')
> for line in fid:
> pid.append(line.split(' ')[0])
>
> for i in pid:
> cmd="""kill -9 %s""" % (i)
> os.system(cmd)
>
>
> cmd="ps -u myuser | grep 'sshd' > .ps"
> os.system(cmd)
>
> pid=[]
> fid=open('.ps','r')
> for line in fid:
> pid.append(line.split(' ')[0])
>
> for i in pid:
> cmd="""kill -9 %s""" % (i)
> os.system(cmd)
>
> ___
> 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] os.sys

2007-10-28 Thread Lawrence Shafer
Why doesn't this fill otp with the output of ls?? (I know python has 
it's own file tools, I'm just playing around ;)

import os
cmd="""ls"""
otp=os.system(cmd)
print otp
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] os.sys

2007-10-28 Thread James
Try using the commands, instead.


import commands
cmd = 'ls'
opt = commands.getoutput( cmd )
print opt


More information found here:
http://docs.python.org/lib/module-commands.html

:)
.james


On Oct 28, 2007, at 12:44 PM, Lawrence Shafer wrote:

> Why doesn't this fill otp with the output of ls?? (I know python has
> it's own file tools, I'm just playing around ;)
>
> import os
> cmd="""ls"""
> otp=os.system(cmd)
> print otp
> ___
> 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] os.sys

2007-10-28 Thread Andrew Nelsen
On 10/28/07, Lawrence Shafer <[EMAIL PROTECTED]> wrote:
>
> Why doesn't this fill otp with the output of ls?? (I know python has
> it's own file tools, I'm just playing around ;)
>
> import os
> cmd="""ls"""
> otp=os.system(cmd)
> print otp
> ___
> Tutor maillist  -  Tutor@python.org



 I actually had the same question a while back. I'm still a newbie, but the
advice I was given was  to use popen(). ls = os.popen("ls").read() worked
for me. Also, for Unix style OSes, you can import commands, and then it has
getoutput().
Just my two cents.

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


Re: [Tutor] os.sys

2007-10-28 Thread Alan Gauld

"Lawrence Shafer" <[EMAIL PROTECTED]> wrote

> Why doesn't this fill otp with the output of ls?? (I know python has
> it's own file tools, I'm just playing around ;)
>
> otp=os.system(cmd)

Because os.system returnsd the exit status of the command.
zero means the command executed OK anything else is an
error code.

To get the output you need to read stdout or stderr.

To do that, the current preferred mechanism is via the
subprocess module which replaces os.system/os.popen/commands
os.spawn etc etc.

The documentation has examples.

Also my web

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld tutor at the end of the OS 
topic has examples
of subprocess.Popen as well as the older methods.

HTH,


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


Re: [Tutor] killing bash, sshd... dangerous?

2007-10-28 Thread John
No, mostly just playing around with scripting and I have a problem
sometimes with cygwin where logging out from bash session, the xterm hangs,
so this seems to fix it... but it's not really the best solution... needs
some mods, I'm sure.


On 10/28/07, Tom Tucker <[EMAIL PROTECTED]> wrote:
>
> Instead of reading in an outputfile (.ps), try reading from command
> output.  The correct terminology escapes me (for file in
> os.popen(cmd).readlines():).
>
> Are you looking for a auto logout method? For example if no activity
> after X minutes kill the shell.  Bash and a few other shells have this
> functionality already.
>
>
>
>
>
> On 10/28/07, John <[EMAIL PROTECTED]> wrote:
> > Hello, I've written a little script with the intention of killing all of
> my
> > bash, sshd sessions... is this dangerous? How could I make it work so
> that
> > it didn't kill the session it was run from (as it is I suppose I have to
> run
> > it with nohup):
> >
> >
> >
> > #!/usr/bin/env python
> > import os
> >
> > cmd="ps -u myuser | grep 'bash' > .ps"
> > os.system(cmd)
> >
> > pid=[]
> > fid=open('.ps','r')
> > for line in fid:
> > pid.append(line.split(' ')[0])
> >
> > for i in pid:
> > cmd="""kill -9 %s""" % (i)
> > os.system(cmd)
> >
> >
> > cmd="ps -u myuser | grep 'sshd' > .ps"
> > os.system(cmd)
> >
> > pid=[]
> > fid=open('.ps','r')
> > for line in fid:
> > pid.append(line.split(' ')[0])
> >
> > for i in pid:
> > cmd="""kill -9 %s""" % (i)
> > os.system(cmd)
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
>



-- 
Configuration
``
Plone 2.5.3-final,
CMF-1.6.4,
Zope (Zope 2.9.7-final, python 2.4.4, linux2),
Five 1.4.1,
Python 2.4.4 (#1, Jul 3 2007, 22:58:17) [GCC 4.1.1 20070105 (Red Hat
4.1.1-51)],
PIL 1.1.6
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Offtopic: Re: killing bash, sshd... dangerous?

2007-10-28 Thread Eric Brunson
John wrote:
> No, mostly just playing around with scripting and I have a problem 
> sometimes with cygwin where logging out from bash session

Hi John,

This is a known bug.  The fix is to install a real operating system. 

For more info, please see one of the following: 

http://distrowatch.com
http://www.opensolaris.org
http://www.freebsd.org

;-)

Sincerely,
e.

> the xterm hangs, so this seems to fix it... but it's not really the 
> best solution... needs some mods, I'm sure.
>
>  
> On 10/28/07, *Tom Tucker* <[EMAIL PROTECTED] 
> > wrote:
>
> Instead of reading in an outputfile (.ps), try reading from command
> output.  The correct terminology escapes me (for file in
> os.popen(cmd).readlines():).
>
> Are you looking for a auto logout method? For example if no activity
> after X minutes kill the shell.  Bash and a few other shells have this
> functionality already.
>
>
>
>
>
> On 10/28/07, John <[EMAIL PROTECTED] >
> wrote:
> > Hello, I've written a little script with the intention of
> killing all of my
> > bash, sshd sessions... is this dangerous? How could I make it
> work so that
> > it didn't kill the session it was run from (as it is I suppose I
> have to run
> > it with nohup):
> >
> >
> >
> > #!/usr/bin/env python
> > import os
> >
> > cmd="ps -u myuser | grep 'bash' > .ps"
> > os.system(cmd)
> >
> > pid=[]
> > fid=open('.ps','r')
> > for line in fid:
> > pid.append(line.split(' ')[0])
> >
> > for i in pid:
> > cmd="""kill -9 %s""" % (i)
> > os.system(cmd)
> >
> >
> > cmd="ps -u myuser | grep 'sshd' > .ps"
> > os.system(cmd)
> >
> > pid=[]
> > fid=open('.ps','r')
> > for line in fid:
> > pid.append(line.split(' ')[0])
> >
> > for i in pid:
> > cmd="""kill -9 %s""" % (i)
> > os.system(cmd)
> >
> > ___
> > Tutor maillist  -  Tutor@python.org 
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
>
>
>
>
> -- 
> Configuration
> ``
> Plone 2.5.3-final,
> CMF-1.6.4,
> Zope (Zope 2.9.7-final, python 2.4.4, linux2),
> Five 1.4.1,
> Python 2.4.4 (#1, Jul 3 2007, 22:58:17) [GCC 4.1.1 20070105 (Red Hat 
> 4.1.1-51)],
> PIL 1.1.6
> 
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>   

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