Re: [Tutor] A newbie question about running python scripts

2005-06-15 Thread ZIYAD A. M. AL-BATLY
On Wed, 2005-06-15 at 20:19 -0700, typetext wrote: 
> I am trying to run my first python scripts from a windows XP command
> line, and using Ivan Langhan's book Teach yourself python in 24 hours.
> I have installed Active Python 2.4 and have had no trouble running the
> scripts in the IDE . Following the instructions for running from a
> command line, I type, for instance, Python, which brings up the
> correct info and the ">> line". Then I type, for example,
> helloworld.py., the name of the first script. Here is input, followed
> by what happens:
> 
> >>>helloworld.py
> Traceback (most recent call last)
> File "", line 1 in ?
> NameError:name 'helloworld' is not defined
> >>>
> 
> I know this must be a simple problem, but none of my saved scripts
> work . They all work in the IDE mode. Would appreciate any help.
> 
> Michael Riggs
> Seattle

Within the directory where “helloworld.py” is: 
 1. Either from a Windows command prompt type: “python
helloworld.py” (this is the recommended/right way to run Python
applications/scripts), or 
 2. From the Python interactive shell after the “>>> ” prompt, type:
“import helloworld” (very useful in some situations).

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


Re: [Tutor] A newbie question about running python scripts

2005-06-15 Thread ZIYAD A. M. AL-BATLY
On Wed, 2005-06-15 at 21:23 -0600, Chuck Allison wrote:
> Dear Tutors,
> 
> Here is a query from a student from a corporate class I'm teaching. Do 
> you know of any such modules of the top of your head? Thanks!
> 
> ===
> Quick question since the next class isn't until Monday.  Do you know of 
> any modules in Python that can convert a bitmap from one format to 
> another, specifically .BMPs to .JPGs?
> 
> We have a need to do this conversion.  Our developers have grown fond of 
> screen shots as they give much information that may not be written up in 
> a bug.  However, our most common tool for getting a screen shot is a 
> screen capture in VMWare which only does .BMP format files.  They are 
> about 2.3 Mb apiece.   Where as a .JPG of the same thing is only about 
> 34 - 175 Kb.  The space savings is significant.   So, we need a way of 
> efficiently convert these screenshots.  This class presents me an 
> opportunity to create a utility to do this for us.  Hence, the question.
> ===
> 
> -- Chuck Allison
PIL¹ (Portable Image Library) will do what you want.  However, I have
some humble advice to give.  It appears to me that you're using those
screen shots to read (as in Humans reading English) some info from the
application.  Using JPEG format in these situations is not recommended
as JPEG is a lossy format (in other words, it introduce some artifacts
to the image) which make it very hard for reading (especially, small
fonts).  I recommend using either PNG or BMP after compressing it with
zip, gzip, or bz2.

Links:
 1. http://www.pythonware.com/products/pil/

Just my humble answer.
Ziyad.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A newbie question about running python scripts

2005-06-15 Thread Chuck Allison
Dear Tutors,

Here is a query from a student from a corporate class I'm teaching. Do 
you know of any such modules of the top of your head? Thanks!

===
Quick question since the next class isn't until Monday.  Do you know of 
any modules in Python that can convert a bitmap from one format to 
another, specifically .BMPs to .JPGs?

We have a need to do this conversion.  Our developers have grown fond of 
screen shots as they give much information that may not be written up in 
a bug.  However, our most common tool for getting a screen shot is a 
screen capture in VMWare which only does .BMP format files.  They are 
about 2.3 Mb apiece.   Where as a .JPG of the same thing is only about 
34 - 175 Kb.  The space savings is significant.   So, we need a way of 
efficiently convert these screenshots.  This class presents me an 
opportunity to create a utility to do this for us.  Hence, the question.
===

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


[Tutor] A newbie question about running python scripts

2005-06-15 Thread typetext
I am trying to run my first python scripts from a windows XP command
line, and using Ivan Langhan's book Teach yourself python in 24 hours.
I have installed Active Python 2.4 and have had no trouble running the
scripts in the IDE . Following the instructions for running from a
command line, I type, for instance, Python, which brings up the
correct info and the ">> line". Then I type, for example,
helloworld.py., the name of the first script. Here is input, followed
by what happens:

>>>helloworld.py
Traceback (most recent call last)
File "", line 1 in ?
NameError:name 'helloworld' is not defined
>>>

I know this must be a simple problem, but none of my saved scripts
work . They all work in the IDE mode. Would appreciate any help.

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


Re: [Tutor] Popen4 and Paths Containing Spaces

2005-06-15 Thread Chuck Allison
Hello Hugo,

Wednesday, June 15, 2005, 12:20:37 PM, you wrote:

HGM> This problem is explained there. Also, why are you using those forward 
HGM> slashes in your Windows paths Meaning:  d:/program 
HGM> files/winzip/wzzip.exe

Windows doesn't care. The only place you can't use forward slashes in
path names is in a command prompt. It's been that way since DOS 2.0 in
the 80s. I prefer using the forward slashes in file name strings since
they're fairly portable.

-- 
Best regards,
 Chuck

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


Re: [Tutor] Process problem

2005-06-15 Thread Alberto Troiano
Hey

Let me make you understand

I need that levantamuertos.py run cotascamon.py (every script with it 
differents arguments that are passed) and then die letting the cotascamon.py 
scripts running independently

Now, I don't know if my code is right, and thinking now you're right, 
levantamuertos.py waits until cotascamon.py finish.
What can I do since cotascamon will never die (and it doesn't have to die)?

Maybe a different approach will be the solution but I can't find a way to do 
it

Best Regards

Alberto

>From: Hugo González Monteverde <[EMAIL PROTECTED]>
>To: Alberto Troiano <[EMAIL PROTECTED]>
>CC: tutor@python.org
>Subject: Re: [Tutor] Process problem
>Date: Wed, 15 Jun 2005 13:30:30 -0500
>
>Hi Alberto,
>
>Mmm I'm not sure if I get this all right. Why do you want to keep
>cotascamon running after levantamuertos dies?? If you're using system(),
>levantamuertos is waiting for cotascamon to finish completion before it
>continues running, there should be no point where levantamuertos *runs*
>while cotascamon also does (levantamuertos is sleeping)
>
>One advice though: it is easier and more elegant if you, instead of
>parsing the output of "ps" for a single process, you send it signal 0.
>This will do nothing to the process itself, but will complain with a 
>traceback (OSError) if the pid does not exist. Like this:
>
>from os import kill
>from signals import SIG_DFL
>
>pid = 16
>try:
>   kill(pid, SIG_DFL)
>except OSError:
>   print "Process does not exist..."
>
>Please note this is untested as I'm os a Windows machine right now =/
>
>
>Hope it helps,
>
>Hugo
>
>
>
>Alberto Troiano wrote:
>>Hey all
>>
>>I have a problem with a program
>>
>>You see, I have a python script made in Python 2.2 that runs every 15 
>>minutes on Linux Red Hat 9.0
>>This program (which code is attached below) named levantamuertos.py 
>>(spanish for death weakener) has to check in a database if the pids stored 
>>are running (note that it will be more than one pid) and if they are not 
>>it has to start again the process and it will store the new pid on the 
>>database
>>
>>The problem is that when I call it it runs and weaks all dead process but 
>>when it dies, he takes all his sons with him.
>>
>>How can I make that the cotascamon.py keep running although 
>>levantamuertos.py die?
>>
>>Thanks in advanced and here is the code
>>
>>Alberto
>>
>>##
>>### levantamuertos.py ###
>>##
>>import os
>>import sys
>>import MySQLdb
>>import time
>>
>>class conexion(object):
>> def __init__(self):
>> self.db = MySQLdb.connect(host="localhost", user="administrador", 
>>passwd="123456",db="seguridad")
>>
>> def consulta(self,query,args=None):
>> try:
>> self.cursor=self.db.cursor()
>> self.sql=self.cursor.execute(query,args)
>> except:
>> self.cerrar()
>> else:
>> self.cerrar()
>>
>> def cerrar(self):
>> self.db.close()
>>
>>query="SELECT PID,user,cam,ruta from proceso where user='DMALAKIAN'"
>>cur=conexion()
>>cur.consulta(query)
>>res=cur.cursor.fetchall()
>>if len(res)>0:
>> for elem in res:
>> pi=elem[0]
>> ps_command = 'ps -p %s' % pi
>> pidchk = os.popen(ps_command).readlines()
>> count = 0
>> for line in pidchk:
>> count +=1
>> if count > 1:
>> print "Encontre el proceso"
>> else:
>> query="update proceso set PID=%s where user='DMALAKIAN'"
>> p=os.getpid()
>> args=(p)
>> cur=conexion()
>> cur.consulta(query,args)
>> query="SELECT PID,user,cam,ruta from proceso where user <> 
>>'DMALAKIAN'"
>> cur=conexion()
>> cur.consulta(query)
>> res=cur.cursor.fetchall()
>> if len(res)>0:
>> for elem in res:
>> pid=elem[0]
>> user=elem[1]
>> cam=elem[2]
>> ruta=str(elem[3])
>> ps_command = 'ps -p %s' % pid
>> pidchk = os.popen(ps_command).readlines()
>> count = 0
>> for line in pidchk:
>> count +=1
>> if count > 1:
>> pass
>> else:
>> os.system("python2.2 /root/cotascamon.py 
>>"+str(ruta[19:len(ruta)])+" "+user+" "+str(cam))
>> else:
>> pass
>>else:
>> query="insert into proceso values('DMALAKIAN',0,%s,'NO HAY RUTA PARA 
>>ESTE USUARIO')"
>> p=os.getpid()
>> args=(p)
>> cur=conexion()
>> cur.consulta(query,args)
>> query="SELECT PID,user,cam,ruta from proceso where user <> 
>>'DMALAKIAN'"
>> cur=conexion()
>> cur.consulta(query)
>> res=cur.cursor.fetchall()
>> if len(res)>0:
>> for elem in res:
>> pid=elem[0]
>> user=elem[1]
>> cam=elem[2]
>> ruta=str(elem[3])
>> ps_command = 'ps -p %s' % pid
>> pidchk = os

Re: [Tutor] Buttons automatically executing

2005-06-15 Thread Danny Yoo


On Wed, 15 Jun 2005, Phillip Hart wrote:

> Thanks for taking the time to read this.
>
> Excuse me if this falls under the boundaries of a "newbie" question, as
> I am a self-taught programmer.

Hi Phillip,

It's a "newbie" question.  But that's perfectly ok.  *grin*


> While using Tkinter to create buttons, something unexpected happens when
> the button commands call functions. Namely, when I run the following, it
> immediately executes the print statements without the button ever being
> pressed. Additionally, pressing the button once the Tk window is up will
> not activate the button.

The issue is that there's a difference between calling a function, and
getting a function value.  Let's go through an example to get started:


For example, let's say that we do have that 'location' function defined:

###
>>> def location(x, y):
... print "I am in row", x, "and column", y
...
###



A function value is what we get when we don't use parentheses:

###
>>> location

###


And this thing can be handled just like any other value in Python: we can
pass it around and put it in lists:

###
>>> somelist = [location, location, location]
>>> somelist
[, , ]
###



One neat thing that you already know about functions is that they can be
called.  You're used to doing something like this:

###
>>> location(3, 4)
I am in row 3 and column 4
###


but we can also do something like this:

###
>>> somelist[0]

>>> somelist[0](2, 3)
I am in row 2 and column 3
###

That is, 'somelist[0]' is a function value, and when we use parens next to
it '(2, 3)', then we fire off that function value with those two
arguments.

Does this make sense so far?



When we're constructing a button:

b1=Button(can,bg="black",image=bimage,command=location(0,0))

we should be careful to pass a function value as the 'command': if we use
parens prematurely, what ends up happening is that command gets set to the
return value of calling 'location(0, 0)'.


It looks we want to set 'command' to a function value that, when called,
does a 'location(0,0)'.  We can do that:

###
def reset():
location(0, 0)
b1 = Button(can,bg="black",image=bimage,command=reset)
###

'reset' is a function that, when called, does location(0, 0).


Please feel free to ask more questions about this.  Good luck to you!

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


Re: [Tutor] Buttons automatically executing

2005-06-15 Thread Michael Lange
On Wed, 15 Jun 2005 14:31:55 -0500
Phillip Hart <[EMAIL PROTECTED]> wrote:


Hi Phillip,

> #!usr/bin/python
> 
> from Tkinter import *
> 
> def location(x,y):
> print "I am in row ",x," and column ",y
> 
> 
> root=Tk()
> root.geometry('300x300')
> can=Canvas(root,width=250, height=250)
> 
> bimage=PhotoImage(file='sq1.gif')
> 
> b1=Button(can,bg="black",image=bimage,command=location(0,0)).grid(row=0,column=0)

That's an error which frequently happens to Tkinter beginners.
When you assign the button's command like this the command is executed 
immediately
after button creation, the correct usage is:

b1 = Button(can, command=location)

without the parentheses; however if you need to pass arguments to the callback,
you can use a lambda expression like this:

b1 = Button(can, command = lambda x=0, y=0 : location(x, y))

Another problem in this line that doesn't matter here, but might cause 
confusion later on
is the use of grid() ; note that grid() returns None, so actually you set your 
variable
b1 to None which is probably not what you intended; if you need to keep a 
reference to
the Button object you need to split the above into two statements:

b1 = Button(can, )
b1.grid()

If you don't need a reference you can simply do:

Button(can,).grid(row=0,column=0)

without creating a (quite useless) variable.

I hope this helps

Michael

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


[Tutor] Buttons automatically executing

2005-06-15 Thread Phillip Hart
Thanks for taking the time to read this.

Excuse me if this falls under the boundaries of a "newbie" question, as I am a self-taught programmer.

While using Tkinter to create buttons, something unexpected happens
when the button commands call functions.  Namely, when I run the
following, it immediately executes the print statements without the
button ever being pressed.  Additionally, pressing the button once
the Tk window is up will not activate the button.

#!usr/bin/python

from Tkinter import *

def location(x,y):
    print "I am in row ",x," and column ",y

    
root=Tk()
root.geometry('300x300')
can=Canvas(root,width=250, height=250)

bimage=PhotoImage(file='sq1.gif')

b1=Button(can,bg="black",image=bimage,command=location(0,0)).grid(row=0,column=0)

can.pack()
root.mainloop()


Lastly, when I change the command to be anything other than a function,
such as a print statement, it works fine.  What am I doing wrong
here? 

thanks again for your time and help,

green python user (no pun intended),
-Phillip
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] check PID

2005-06-15 Thread Hugo González Monteverde
Hi Alberto,

Since then I've come across the very neat kill(pid, 0) trick, which I 
replied to you in another post.

Hugo

Alberto Troiano wrote:
> Hey all
> 
> I want to check if a PID number is running
> I have the pid number from other program and I want to check if it's alive
> Using Python 2.2 over Linux Red Hat 9
> 
> I tried to google but find how to know process of the app but not check for 
> other pids
> 
> Thanks ina dvanced
> 
> Alberto
> 
> 
> ___
> 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] Process problem

2005-06-15 Thread Hugo González Monteverde
Hi Alberto,

Mmm I'm not sure if I get this all right. Why do you want to keep
cotascamon running after levantamuertos dies?? If you're using system(),
levantamuertos is waiting for cotascamon to finish completion before it
continues running, there should be no point where levantamuertos *runs*
while cotascamon also does (levantamuertos is sleeping)

One advice though: it is easier and more elegant if you, instead of
parsing the output of "ps" for a single process, you send it signal 0.
This will do nothing to the process itself, but will complain with a 
traceback (OSError) if the pid does not exist. Like this:

from os import kill
from signals import SIG_DFL

pid = 16
try:
kill(pid, SIG_DFL)
except OSError:
print "Process does not exist..."

Please note this is untested as I'm os a Windows machine right now =/


Hope it helps,

Hugo



Alberto Troiano wrote:
> Hey all
> 
> I have a problem with a program
> 
> You see, I have a python script made in Python 2.2 that runs every 15 
> minutes on Linux Red Hat 9.0
> This program (which code is attached below) named levantamuertos.py (spanish 
> for death weakener) has to check in a database if the pids stored are 
> running (note that it will be more than one pid) and if they are not it has 
> to start again the process and it will store the new pid on the database
> 
> The problem is that when I call it it runs and weaks all dead process but 
> when it dies, he takes all his sons with him.
> 
> How can I make that the cotascamon.py keep running although 
> levantamuertos.py die?
> 
> Thanks in advanced and here is the code
> 
> Alberto
> 
> ##
> ### levantamuertos.py ###
> ##
> import os
> import sys
> import MySQLdb
> import time
> 
> class conexion(object):
> def __init__(self):
> self.db = MySQLdb.connect(host="localhost", user="administrador", 
> passwd="123456",db="seguridad")
> 
> def consulta(self,query,args=None):
> try:
> self.cursor=self.db.cursor()
> self.sql=self.cursor.execute(query,args)
> except:
> self.cerrar()
> else:
> self.cerrar()
> 
> def cerrar(self):
> self.db.close()
> 
> query="SELECT PID,user,cam,ruta from proceso where user='DMALAKIAN'"
> cur=conexion()
> cur.consulta(query)
> res=cur.cursor.fetchall()
> if len(res)>0:
> for elem in res:
> pi=elem[0]
> ps_command = 'ps -p %s' % pi
> pidchk = os.popen(ps_command).readlines()
> count = 0
> for line in pidchk:
> count +=1
> if count > 1:
> print "Encontre el proceso"
> else:
> query="update proceso set PID=%s where user='DMALAKIAN'"
> p=os.getpid()
> args=(p)
> cur=conexion()
> cur.consulta(query,args)
> query="SELECT PID,user,cam,ruta from proceso where user <> 
> 'DMALAKIAN'"
> cur=conexion()
> cur.consulta(query)
> res=cur.cursor.fetchall()
> if len(res)>0:
> for elem in res:
> pid=elem[0]
> user=elem[1]
> cam=elem[2]
> ruta=str(elem[3])
> ps_command = 'ps -p %s' % pid
> pidchk = os.popen(ps_command).readlines()
> count = 0
> for line in pidchk:
> count +=1
> if count > 1:
> pass
> else:
> os.system("python2.2 /root/cotascamon.py 
> "+str(ruta[19:len(ruta)])+" "+user+" "+str(cam))
> else:
> pass
> else:
> query="insert into proceso values('DMALAKIAN',0,%s,'NO HAY RUTA PARA 
> ESTE USUARIO')"
> p=os.getpid()
> args=(p)
> cur=conexion()
> cur.consulta(query,args)
> query="SELECT PID,user,cam,ruta from proceso where user <> 'DMALAKIAN'"
> cur=conexion()
> cur.consulta(query)
> res=cur.cursor.fetchall()
> if len(res)>0:
> for elem in res:
> pid=elem[0]
> user=elem[1]
> cam=elem[2]
> ruta=str(elem[3])
> ps_command = 'ps -p %s' % pid
> pidchk = os.popen(ps_command).readlines()
> count = 0
> for line in pidchk:
> count +=1
> if count > 1:
> pass
> else:
> os.system("python2.2 /root/cotascamon.py 
> "+str(ruta[19:len(ruta)])+" "+user+" "+str(cam))
> else:
> pass
> 
> 
> ___
> 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] Popen4 and Paths Containing Spaces

2005-06-15 Thread Hugo González Monteverde
Hi John,

Read the Python gotchas page at:

http://www.ferg.org/projects/python_gotchas.html

This problem is explained there. Also, why are you using those forward 
slashes in your Windows paths Meaning:  d:/program 
files/winzip/wzzip.exe

Hugo

Gooch, John wrote:
> I am having problems with using the Winzip command line tools and Python.
> 
> Here is an example of the several dozen different ways I have tried to
> execute the Wzzip.exe utility with arguments using
> popen2 -
> 
> This works (just runs the utility with not arguments ):
>   cmd = "\"d:/program files/winzip/wzzip.exe\"" 
>   results = popen2.popen4( cmd )
> 
> However this fails miserably:
> testpath = "D:/Program Files/WebTrends/SmartSource Data
> Collector/weblog/*.log"
> cmd = "\"d:/program files/winzip/wzzip.exe\" -ma test.zip \"" + testpath
> + "\"" 
> results = popen2.popen4( cmd )
> 
> If I set the script to print out the value stored in the variable "cmd" it
> says this:
>   "d:/program files/winzip/wzzip.exe\" -ma test.zip "D:/Program
> Files/WebTrends/SmartSource Data Collector/weblog/*.log"
> 
> Which if I paste it onto the command line, does exactly what I intend it to
> do. But for some reason, popen2.popen4(cmd) gives this as output ( from
> stderr ):
>   'd:/program' is not a file or command
> 
> My only guess is that popen2.popen4 is parsing the string differently than
> the "print" command does, because print recognizes that the backslash is
> escaping the double quotes ( e.g. "\"" ) in the string, and popen does not.
> On the other hand, it appears to be recognizing it in my first example, so
> that is what is really confusing me. It isn't consistent at all. 
> 
> Any ideas?  
> 
> 
> 
> John A. Gooch
> "May the Python-force be with you...always."
> Systems Administrator
> EchoStar Satellite L.L.C.
> Desk: 720-514-5708 
> 
> 
> 
> -Original Message-
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
> Of Danny Yoo
> Sent: Tuesday, June 14, 2005 4:21 PM
> To: Pujo Aji
> Cc: Tutor
> Subject: Re: [Tutor] can't see emacs timer in action
> 
> 
> 
> 
> On Tue, 14 Jun 2005, Pujo Aji wrote:
> 
> 
>>I tried this code in emacs.
>>for i in range(3):
>>  time.sleep(1)
>>  print i
>>
>>It shows the result but total result not second per second.
> 
> 
> Hi Pujo,
> 
> 
> In Emacs, you may want to first start up a Python subprocess by using the
> keystroke:
> 
> C-c !
> 
> and then run Python buffers with:
> 
> C-c C-c
> 
> Best of wishes!
> 
> ___
> 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] Controlling Where My Program Ends

2005-06-15 Thread Don Parris
On Wed, 15 Jun 2005 07:46:40 -0600
Mike Hansen <[EMAIL PROTECTED]> wrote:

> > Subject:
> > Re: [Tutor] Controlling Where My Program Ends
> > From:
> > Don Parris <[EMAIL PROTECTED]>
> > Date:
> > Tue, 14 Jun 2005 23:03:59 -0400
> > To:
> > tutor@python.org
> > 
> > To:
> > tutor@python.org
> > 
> > 
> > On Wed, 15 Jun 2005 00:59:24 -
> > "DC Parris" <[EMAIL PROTECTED]> wrote:
> > 
> > 
> >>Never mind.  I found it - sys.exit()
> >>
> >>Sorry to have wasted the bandwidth/time.
> >>-- 
> > 
> > 
> > This was in reference to a post about exiting from a program.  I
> > couldn't figure out why my program wouldn't let me exit from within a
> > sub-menu of the console interface.  Since my webmail client goofed up
> > the "from" header, it never showed up, and I've cancelled it to avoid
> > wasting everyone's time further. I found sys.exit() in the library
> > reference, which allows me to do what I want.
> > 
> > Don
> > 
> 
> If you use the if __name__ == '__main__': idiom, then you can just use
> return instead of sys.exit()
> 
> def main():
>  lotsa interesting python code
>  if somethinorother:
>  # sys.exit()
>  return
>  more interesting python code
> 
> if __name__ == '__main__':
>  main()
> 
> ___

Thanks.  I'll try that out a little later (when I get back to this). 
Incidentally, my menu system started off quite klunky, but I've thrown my
menu items into dictionaries and iterate through them to display the menu.
Thus, instead of:
print 'menuitem' 
print 'menuitem'
etc., etc., etc.

I now have:
for i in menu.iteritems():
print repr(key), '', 'value'


Now if I can figure out how to iterate through the if --> elif statements,
I'll be able to shorten the code a bit further.  First things first though. 
Let me just understand these basic things, and then I'll see if I can get a
little smarter.  

Maybe I'll start a thread "trials of a Python newbie".  It'll be hilarious
for the old timers, I'm sure.  It might help other newbies as well.

Don
-- 
evangelinuxGNU Evangelist
http://matheteuo.org/   http://chaddb.sourceforge.net/
"Free software is like God's love - you can share it with anyone anytime
anywhere."
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Which Popen For Win32?

2005-06-15 Thread Kent Johnson
Hugo González Monteverde wrote:
> Hi, maybe you'll want to give a try to:
> 
> import commands
> stat, outp = commands.getstatusoutput ("%s %s %s %s"%(zipCommand,
> zipParameters, archive, fpath))
> 
> This will give you the exit code in stat and the whole text of the
> putput in outp, so you may take a look at it. It's way simpler than popen.

According to the docs commands is Unix-only.

Kent

> 
> Hugo
> 
> 
> Gooch, John wrote:
> 
>>I am using the Winzip 9.0 Command Line Add-on to archive very large files (
>>1GB and over ) that are too large to be handled by Zipfile( this bug has
>>been reported but not resolved ). I tried calling the external program using
>>os.system(), but something about the output crashes my IDE ( Eclipse ), so I
>>started looking for a way to run the external application without capturing
>>the output, as I only care about the exit code ( 0 is good, anything else is
>>bad ). Here is what I have to far:
>>
>>myCmd = os.popen( "%s %s %s %s" % ( zipCommand, zipParameters,
>>archive,fpath ), 'w' )
>>return myCmd.close()
>>
>>Currently, I have no idea what is being returned by the "close()" method, it
>>doesn't appear to be the exit code, so perhaps it is the output of the
>>command. If so, how can I capture and return the exit code?
>>
>>Thank You, 
>>
>>
>>John A. Gooch
>>Systems Administrator
>>IT - Tools
>>EchoStar Satellite L.L.C.
>>9601 S. Meridian Blvd.
>>Englewood, CO  80112
>>Desk: 720-514-5708 
>>
>>
>>
>>
>>-Original Message-
>>From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
>>Of Gary Taylor
>>Sent: Tuesday, June 14, 2005 8:15 AM
>>To: tutor@python.org
>>Subject: Re: [Tutor] Passing command line argument in function
>>
>>
>>
>>
ftp.storbinary(stor file_to_transfer, open(file_to_transfer,"r"))

>>>
>>>You need to create the command as a string: ftp.storbinary("stor " + 
>>>file_to_transfer,
>>>open(file_to_transfer,"r"))
>>>
>>>Kent
>>
>>
>>That worked perfectly, I was way off.
>>
>>Thank-you!
>>
>>Gary
>>
>>p.s. sorry for the out of thread message, my mail seems to
>>be having problems.
>>
> 
> 
> ___
> 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] Which Popen For Win32?

2005-06-15 Thread Hugo González Monteverde
Hi, maybe you'll want to give a try to:

import commands
stat, outp = commands.getstatusoutput ("%s %s %s %s"%(zipCommand,
zipParameters, archive, fpath))

This will give you the exit code in stat and the whole text of the
putput in outp, so you may take a look at it. It's way simpler than popen.

Hugo


Gooch, John wrote:
> I am using the Winzip 9.0 Command Line Add-on to archive very large files (
> 1GB and over ) that are too large to be handled by Zipfile( this bug has
> been reported but not resolved ). I tried calling the external program using
> os.system(), but something about the output crashes my IDE ( Eclipse ), so I
> started looking for a way to run the external application without capturing
> the output, as I only care about the exit code ( 0 is good, anything else is
> bad ). Here is what I have to far:
> 
> myCmd = os.popen( "%s %s %s %s" % ( zipCommand, zipParameters,
> archive,fpath ), 'w' )
> return myCmd.close()
> 
> Currently, I have no idea what is being returned by the "close()" method, it
> doesn't appear to be the exit code, so perhaps it is the output of the
> command. If so, how can I capture and return the exit code?
> 
> Thank You, 
> 
> 
> John A. Gooch
> Systems Administrator
> IT - Tools
> EchoStar Satellite L.L.C.
> 9601 S. Meridian Blvd.
> Englewood, CO  80112
> Desk: 720-514-5708 
> 
> 
> 
> 
> -Original Message-
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
> Of Gary Taylor
> Sent: Tuesday, June 14, 2005 8:15 AM
> To: tutor@python.org
> Subject: Re: [Tutor] Passing command line argument in function
> 
> 
> 
>>>
>>>ftp.storbinary(stor file_to_transfer, open(file_to_transfer,"r"))
>>>
>>
>>You need to create the command as a string: ftp.storbinary("stor " + 
>>file_to_transfer,
>>open(file_to_transfer,"r"))
>>
>>Kent
> 
> 
> That worked perfectly, I was way off.
> 
> Thank-you!
> 
> Gary
> 
> p.s. sorry for the out of thread message, my mail seems to
> be having problems.
> 

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


[Tutor] Process problem

2005-06-15 Thread Alberto Troiano
Hey all

I have a problem with a program

You see, I have a python script made in Python 2.2 that runs every 15 
minutes on Linux Red Hat 9.0
This program (which code is attached below) named levantamuertos.py (spanish 
for death weakener) has to check in a database if the pids stored are 
running (note that it will be more than one pid) and if they are not it has 
to start again the process and it will store the new pid on the database

The problem is that when I call it it runs and weaks all dead process but 
when it dies, he takes all his sons with him.

How can I make that the cotascamon.py keep running although 
levantamuertos.py die?

Thanks in advanced and here is the code

Alberto

##
### levantamuertos.py ###
##
import os
import sys
import MySQLdb
import time

class conexion(object):
def __init__(self):
self.db = MySQLdb.connect(host="localhost", user="administrador", 
passwd="123456",db="seguridad")

def consulta(self,query,args=None):
try:
self.cursor=self.db.cursor()
self.sql=self.cursor.execute(query,args)
except:
self.cerrar()
else:
self.cerrar()

def cerrar(self):
self.db.close()

query="SELECT PID,user,cam,ruta from proceso where user='DMALAKIAN'"
cur=conexion()
cur.consulta(query)
res=cur.cursor.fetchall()
if len(res)>0:
for elem in res:
pi=elem[0]
ps_command = 'ps -p %s' % pi
pidchk = os.popen(ps_command).readlines()
count = 0
for line in pidchk:
count +=1
if count > 1:
print "Encontre el proceso"
else:
query="update proceso set PID=%s where user='DMALAKIAN'"
p=os.getpid()
args=(p)
cur=conexion()
cur.consulta(query,args)
query="SELECT PID,user,cam,ruta from proceso where user <> 
'DMALAKIAN'"
cur=conexion()
cur.consulta(query)
res=cur.cursor.fetchall()
if len(res)>0:
for elem in res:
pid=elem[0]
user=elem[1]
cam=elem[2]
ruta=str(elem[3])
ps_command = 'ps -p %s' % pid
pidchk = os.popen(ps_command).readlines()
count = 0
for line in pidchk:
count +=1
if count > 1:
pass
else:
os.system("python2.2 /root/cotascamon.py 
"+str(ruta[19:len(ruta)])+" "+user+" "+str(cam))
else:
pass
else:
query="insert into proceso values('DMALAKIAN',0,%s,'NO HAY RUTA PARA 
ESTE USUARIO')"
p=os.getpid()
args=(p)
cur=conexion()
cur.consulta(query,args)
query="SELECT PID,user,cam,ruta from proceso where user <> 'DMALAKIAN'"
cur=conexion()
cur.consulta(query)
res=cur.cursor.fetchall()
if len(res)>0:
for elem in res:
pid=elem[0]
user=elem[1]
cam=elem[2]
ruta=str(elem[3])
ps_command = 'ps -p %s' % pid
pidchk = os.popen(ps_command).readlines()
count = 0
for line in pidchk:
count +=1
if count > 1:
pass
else:
os.system("python2.2 /root/cotascamon.py 
"+str(ruta[19:len(ruta)])+" "+user+" "+str(cam))
else:
pass


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


[Tutor] Popen4 and Paths Containing Spaces

2005-06-15 Thread Gooch, John
I am having problems with using the Winzip command line tools and Python.

Here is an example of the several dozen different ways I have tried to
execute the Wzzip.exe utility with arguments using
popen2 -

This works (just runs the utility with not arguments ):
cmd = "\"d:/program files/winzip/wzzip.exe\"" 
results = popen2.popen4( cmd )

However this fails miserably:
testpath = "D:/Program Files/WebTrends/SmartSource Data
Collector/weblog/*.log"
cmd = "\"d:/program files/winzip/wzzip.exe\" -ma test.zip \"" + testpath
+ "\"" 
results = popen2.popen4( cmd )

If I set the script to print out the value stored in the variable "cmd" it
says this:
"d:/program files/winzip/wzzip.exe\" -ma test.zip "D:/Program
Files/WebTrends/SmartSource Data Collector/weblog/*.log"

Which if I paste it onto the command line, does exactly what I intend it to
do. But for some reason, popen2.popen4(cmd) gives this as output ( from
stderr ):
'd:/program' is not a file or command

My only guess is that popen2.popen4 is parsing the string differently than
the "print" command does, because print recognizes that the backslash is
escaping the double quotes ( e.g. "\"" ) in the string, and popen does not.
On the other hand, it appears to be recognizing it in my first example, so
that is what is really confusing me. It isn't consistent at all. 

Any ideas?  



John A. Gooch
"May the Python-force be with you...always."
Systems Administrator
EchoStar Satellite L.L.C.
Desk: 720-514-5708 



-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Danny Yoo
Sent: Tuesday, June 14, 2005 4:21 PM
To: Pujo Aji
Cc: Tutor
Subject: Re: [Tutor] can't see emacs timer in action




On Tue, 14 Jun 2005, Pujo Aji wrote:

> I tried this code in emacs.
> for i in range(3):
>   time.sleep(1)
>   print i
>
> It shows the result but total result not second per second.

Hi Pujo,


In Emacs, you may want to first start up a Python subprocess by using the
keystroke:

C-c !

and then run Python buffers with:

C-c C-c

Best of wishes!

___
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] Controlling Where My Program Ends

2005-06-15 Thread Mike Hansen
> Subject:
> Re: [Tutor] Controlling Where My Program Ends
> From:
> Don Parris <[EMAIL PROTECTED]>
> Date:
> Tue, 14 Jun 2005 23:03:59 -0400
> To:
> tutor@python.org
> 
> To:
> tutor@python.org
> 
> 
> On Wed, 15 Jun 2005 00:59:24 -
> "DC Parris" <[EMAIL PROTECTED]> wrote:
> 
> 
>>Never mind.  I found it - sys.exit()
>>
>>Sorry to have wasted the bandwidth/time.
>>-- 
> 
> 
> This was in reference to a post about exiting from a program.  I couldn't
> figure out why my program wouldn't let me exit from within a sub-menu of the
> console interface.  Since my webmail client goofed up the "from" header, it
> never showed up, and I've cancelled it to avoid wasting everyone's time
> further. I found sys.exit() in the library reference, which allows me to do
> what I want.
> 
> Don
> 

If you use the if __name__ == '__main__': idiom, then you can just use return 
instead of sys.exit()

def main():
 lotsa interesting python code
 if somethinorother:
 # sys.exit()
 return
 more interesting python code

if __name__ == '__main__':
 main()

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


Re: [Tutor] can't see emacs timer in action

2005-06-15 Thread Pujo Aji
thanks Noel,
It is great idea.

pujo

On 6/15/05, Max Noel <[EMAIL PROTECTED]> wrote:
> 
> On Jun 14, 2005, at 23:17, Pujo Aji wrote:
> 
> > I just use Ctrl+C Ctrl+C to run the code.
> > The code wait for 3 second and show all i all together.
> >
> > I can't feel every second pass.
> >
> > pujo
> 
>  Try running your script from a terminal (outside of emacs, that
> is).
> 
> -- Max
> maxnoel_fr at yahoo dot fr -- ICQ #85274019
> "Look at you hacker... A pathetic creature of meat and bone, panting
> and sweating as you run through my corridors... How can you challenge
> a perfect, immortal machine?"
> 
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] can't see emacs timer in action

2005-06-15 Thread Pujo Aji
Hallo Danny,

If we always start interpreter everytime we want to run python in
emacs/xemacs, is it counter productive? I usually have to clean the
buffer/output pane  before running my program, so I always do this to
start my program:
C-c ! to start the interpreter
C-c o to change my windows
C-c C-c to run the program
C-x k ENTER to close it
C-c o to change my editor again
C-c ! to start the interpreter again

This is one cycle of running.

Is that normal ???

What is the difference between running C-c C-c directly and running it
after the interpreter?

I need to make shortcut for starting interpreter do you have any idea.

For this moment I use SciTE and I want to learn using xemacs.

Thanks in advance,

pujo

On 6/15/05, Danny Yoo <[EMAIL PROTECTED]> wrote:
> 
> 
> On Wed, 15 Jun 2005, Pujo Aji wrote:
> 
> > Thanks Danny,
> >
> > Btw, I use xemacs now does it has folding class and method capabilities?
> 
> Hi Pujo,
> 
> [Note: in replies, please make sure to put tutor@python.org in CC.]
> 
> 
> According to:
> 
>  
> http://groups.google.ca/group/comp.lang.python/msg/956f1c2d37f93995?q=emacs+folding+python&hl=en&lr=&ie=UTF-8&oe=UTF-8&rnum=2
> 
> Yes.  *grin*
> 
> 
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor