[Tutor] executing a string representing python code

2007-03-05 Thread Adam Pridgen
here's the magic you are looking for:

func_str = \
'''
def some_func(value):
# youwould check value instance here and do something to it
print "Hello World", value
return "Done"
'''
exec(func_str)
f = locals()["some_func"]
print f("wasn't that cool!")

When you exec the str, it will create a function object, and then you
can obtain the object by accessing the object by kwd in the locals()
dictionary.  Guess it's not really magic, but I think it is still
pretty cool ;)  There also several variations to this method, but this
is the most readable IMHO.

Cheers,

Adam


On 3/2/07, Cecilia Alm <[EMAIL PROTECTED]> wrote:
> I know that there are several ways to execute a string which represents a
> piece of python code.
>  Out of curiosity, is it only eval which returns a value? (as below, where
> the string corresponds to a defined function).
>
>  >>> def addone(val):
>  ... return val + 1
>  ...
>  >>> res = eval('addone(10)')
>
> Thanks!
>
>
>
> ___
> 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] using tarfile on strings or filelike objects

2007-03-05 Thread Barton David
Thanks Kent,

But.. I've actually found that that doesn't work (raises a ReadError),
and neither does..
>>>tf=tarfile.open(mode="r|*",fileobj=filelike)
..which raises "AttributeError: _Stream instance has no attribute
'dbuf'"

However if I explicitly state the compression type.. e.g.
>>>tf=tarfile.open(mode="r|bz2",fileobj=filelike)
..then I can indeed go on to..
>>>print tf.getnames()
>>>assert archive_member_name in tf.getnames()
..and it works ok. Having to explicitly state the compression type isn't
exactly ideal, but I guess it'll do me for now.

Unfortunately, I'm still having trouble actually reading the contents of
'archive_member'.
i.e. ..
>>>tf_filelike=tf.extractfile(archive_member_name)
>>>print tf_filelike.read()
..raises..
File "C:\Python24\lib\tarfile.py", line 551, in _readnormal
self.fileobj.seek(self.offset + self.pos)
File "C:\Python24\lib\tarfile.py", line 420, in seek
raise StreamError, "seeking backwards is not allowed"

And I get the same error if I instead try..
>>>tf_infoobject=tf.getmember(archive_member_name)
>>>tf_filelike=tf.extractfile(tf_infoobject)
>>>print tf_filelike.read()

In fact I'm getting this even if I open the archive by passing the path
name (rather than using fileobj) so I guess this isn't the problem I
initially thought it was. I just don't get it.

Regards,
Dave

-Original Message-
From: Kent Johnson [mailto:[EMAIL PROTECTED] 
Sent: 03 March 2007 22:26
To: Barton David
Cc: tutor@python.org
Subject: Re: [Tutor] using tarfile on strings or filelike objects

Barton David wrote:
> I like that I can access the contents of a zip archive that's stored 
> in memory (rather than on disk) by packing the archive contents into a

> StringIO or cStringIO object and feeding that to ZipFile...
>  
> i.e.
>  
> filelike=cStringIO.StringIO(archive_as_string)
> zf=zipfile.ZipFile(filelike)
> content=zf.read(archive_member_name)
> zf.close()
> filelike.close()
>  
> but I can't get the same thing to work with TarFile. Is there any way 
> to do this? (Other than first saving the archive data to disk and then

> passing the path to TarFile.open?) The tarfile module documentation 
> talks about an optional fileobj flag but this doesn't seem to work.

Just from reading the docs I would try
filelike = cStringIO...
tf = tarfile.open(mode='r|', fileobj=filelike)

This is based on the example in the docs of reading from stdin.

Kent


This message has been checked for viruses but the contents of an attachment
may still contain software viruses, which could damage your computer system:
you are advised to perform your own checks. Email communications with the
University of Nottingham may be monitored as permitted by UK legislation.

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


Re: [Tutor] using tarfile on strings or filelike objects

2007-03-05 Thread Kent Johnson
Barton David wrote:
> Thanks Kent,
> 
> But.. I've actually found that that doesn't work (raises a ReadError),
> and neither does..
 tf=tarfile.open(mode="r|*",fileobj=filelike)
> ..which raises "AttributeError: _Stream instance has no attribute
> 'dbuf'"
> 
> However if I explicitly state the compression type.. e.g.
 tf=tarfile.open(mode="r|bz2",fileobj=filelike)
> ..then I can indeed go on to..
 print tf.getnames()
 assert archive_member_name in tf.getnames()
> ..and it works ok. Having to explicitly state the compression type isn't
> exactly ideal, but I guess it'll do me for now.
> 
> Unfortunately, I'm still having trouble actually reading the contents of
> 'archive_member'.
> i.e. ..
 tf_filelike=tf.extractfile(archive_member_name)
 print tf_filelike.read()
> ..raises..
> File "C:\Python24\lib\tarfile.py", line 551, in _readnormal
> self.fileobj.seek(self.offset + self.pos)
> File "C:\Python24\lib\tarfile.py", line 420, in seek
> raise StreamError, "seeking backwards is not allowed"

The docs for the | options say that seeking won't work with them. Maybe 
try just 'r'? A hacky workaround might be to open the file once to get 
the names out and a second time to read the data. Since the file is in 
memory that should be pretty quick.
> 
> And I get the same error if I instead try..
 tf_infoobject=tf.getmember(archive_member_name)
 tf_filelike=tf.extractfile(tf_infoobject)
 print tf_filelike.read()
> 
> In fact I'm getting this even if I open the archive by passing the path
> name (rather than using fileobj) so I guess this isn't the problem I
> initially thought it was. I just don't get it.

What if you try exactly the code shown in the tarfile examples for 
reading from stdin, with your StringIO file substituted for stdin?

You might want to ask about this on comp.lang.python as no one here 
seems to know the answer. This thread is slightly related:
http://tinyurl.com/2hrffs

Kent

> 
> Regards,
> Dave
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help me to do my exercise

2007-03-05 Thread Rikard Bosnjakovic
On 3/5/07, Vasile Filat <[EMAIL PROTECTED]> wrote:

> I just started learning Python from a book translated intro Russian. It is my 
> first exercise from the book and I don't know why it is not working. Help me 
> please to correct my code which I will include below.

You need to tell us why it's not working. Do you get any error
messages? Unexpected output? Loops never exiting?


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


Re: [Tutor] using tarfile on strings or filelike objects

2007-03-05 Thread Barton David
Thanks Kent,

I think I've hacked my way around this but it's a little weird.
Simplest to demonstrate with code (assuming cStringIO and tarfile are
imported and my_tarfile_string & archive_member_name are specified):
__

#approach 1
filelike=cStringIO.StringIO(my_tarfile_string)
tar = tarfile.open(mode="r|bz2", fileobj=filelike)

for tarinfo in tar:
if tarinfo.name==archive_member_name:
tfl=tar.extractfile(tarinfo)
print tfl.read()
tfl.close()

tar.close()
filelike.close()

#This works. Hooray.
#However, if mode=="r" the following error occurs: "AttributeError:
'NoneType' object has no attribute 'rfind'"
#and if mode=="r|" the following error occurs: "ReadError: empty,
unreadable or compressed file"
#and if mode=="r|*" the following error occurs: "AttributeError: _Stream
instance has no attribute 'dbuf'"
#I only understand the second of those error messages, to be honest.

#approach 2
filelike=cStringIO.StringIO(my_tarfile_string)
tar = tarfile.open(mode="r|bz2", fileobj=filelike)

tfl=tar.extractfile(archive_member_name)
print tfl.read()
tfl.close()

tar.close()
filelike.close()

#approach 3
filelike=cStringIO.StringIO(my_tarfile_string)
tar = tarfile.open(mode="r|bz2", fileobj=filelike)

tarinfo=tar.getmember(archive_member_name)
tfl=tar.extractfile(tarinfo)
print tfl.read()
tfl.close()

tar.close()
filelike.close()

#These DON'T work, and produce the following error: "StreamError:
seeking backwards is not allowed"
#Maybe this is just some wacky bug?


__
At any rate, in the 'slightly related' thread you linked to, the
statement that..
>"bzip2 compressed files cannot be read from a "fake" (StringIO) file
object, only from real files"
..doesn't seem to be the case after all, so that's something. 

Regards
Dave

P.S. I'm very sorry but whenever I post replies to Tutor (I'm using
Outlook and hitting Reply All in response to a tutor's direct reply) it
doesn't seem to add my message to the bottom of the existing thread. If
somebody can tell me what I'm doing wrong, I'd appreciate it.



-Original Message-
From: Kent Johnson [mailto:[EMAIL PROTECTED] 
Sent: 05 March 2007 12:44
To: Barton David
Cc: tutor@python.org
Subject: Re: [Tutor] using tarfile on strings or filelike objects

Barton David wrote:
> Thanks Kent,
> 
> But.. I've actually found that that doesn't work (raises a ReadError),

> and neither does..
 tf=tarfile.open(mode="r|*",fileobj=filelike)
> ..which raises "AttributeError: _Stream instance has no attribute 
> 'dbuf'"
> 
> However if I explicitly state the compression type.. e.g.
 tf=tarfile.open(mode="r|bz2",fileobj=filelike)
> ..then I can indeed go on to..
 print tf.getnames()
 assert archive_member_name in tf.getnames()
> ..and it works ok. Having to explicitly state the compression type 
> isn't exactly ideal, but I guess it'll do me for now.
> 
> Unfortunately, I'm still having trouble actually reading the contents 
> of 'archive_member'.
> i.e. ..
 tf_filelike=tf.extractfile(archive_member_name)
 print tf_filelike.read()
> ..raises..
> File "C:\Python24\lib\tarfile.py", line 551, in _readnormal
> self.fileobj.seek(self.offset + self.pos) File 
> "C:\Python24\lib\tarfile.py", line 420, in seek
> raise StreamError, "seeking backwards is not allowed"

The docs for the | options say that seeking won't work with them. Maybe
try just 'r'? A hacky workaround might be to open the file once to get
the names out and a second time to read the data. Since the file is in
memory that should be pretty quick.
> 
> And I get the same error if I instead try..
 tf_infoobject=tf.getmember(archive_member_name)
 tf_filelike=tf.extractfile(tf_infoobject)
 print tf_filelike.read()
> 
> In fact I'm getting this even if I open the archive by passing the 
> path name (rather than using fileobj) so I guess this isn't the 
> problem I initially thought it was. I just don't get it.

What if you try exactly the code shown in the tarfile examples for
reading from stdin, with your StringIO file substituted for stdin?

You might want to ask about this on comp.lang.python as no one here
seems to know the answer. This thread is slightly related:
http://tinyurl.com/2hrffs

Kent

> 
> Regards,
> Dave

This message has been checked for viruses but the contents of an attachment
may still contain software viruses, which could damage your computer system:
you are advised to perform your own checks. Email communications with the
University of Nottingham may be monitored as permitted by UK legislation.

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


Re: [Tutor] help me to do my exercise

2007-03-05 Thread Christopher Arndt
Vasile Filat schrieb:
> Help me please to correct my code which I will include below.

It is customary to include code examples either inline in your message or
attach them as a plain text source code file. Some mailing list frown on
attachments in general (but this one doesn't, to my knowledge), but binary
attachments, like the image you sent, should be avoided. If you want to show us
longer code examples, use a pasting bin [1]

One reason why it's not a good idea to show us your code as a screenshot from
your editor is that we have to type it up again, if we want to refer to it in
our replies. Would you want to make this (unnecessary) effort?

Chris


[1] http://en.wikipedia.org/wiki/Pastebin
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: executing a string representing python code

2007-03-05 Thread Cecilia Alm

Thanks, Adam. I guess the exec would be exec("some_func").
The result seems pretty similar to eval(), allthough eval() seems more
straight-forward if the aim is to assign the returned value ("Done") to a
variable.


eval('some_func("wasn\'t that cool")')

Hello World wasn't that cool
'Done'

in other words


s = eval('some_func("wasn\'t that cool")')

Hello World wasn't that cool

s

'Done'


2007/3/5, Adam Pridgen <[EMAIL PROTECTED]>:


here's the magic you are looking for:

func_str = \
'''
def some_func(value):
# youwould check value instance here and do something to it
print "Hello World", value
return "Done"
'''
exec(func_str)
f = locals()["some_func"]
print f("wasn't that cool!")

When you exec the str, it will create a function object, and then you
can obtain the object by accessing the object by kwd in the locals()
dictionary.  Guess it's not really magic, but I think it is still
pretty cool ;)  There also several variations to this method, but this
is the most readable IMHO.

Cheers,

Adam


On 3/2/07, Cecilia Alm < [EMAIL PROTECTED]> wrote:
> I know that there are several ways to execute a string which represents
a
> piece of python code.
>  Out of curiosity, is it only eval which returns a value? (as below,
where
> the string corresponds to a defined function).
>
>  >>> def addone(val):
>  ... return val + 1
>  ...
>  >>> res = eval('addone(10)')
>
> Thanks!
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>





--
E. Cecilia Alm
Graduate student, Dept. of Linguistics, UIUC
Office: 2013 Beckman Institute

--
E. Cecilia Alm
Graduate student, Dept. of Linguistics, UIUC
Office: 2013 Beckman Institute
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Hiding/Killing a DOS window (from a previous os.system call)

2007-03-05 Thread learner404

Hello,

I'm launching an external app through an os.system() or os.popen() but I
don't want the resulting DOS window to hang-on there forever (my python
script is launched with pythonw):
I use a sys.exit() to kill the script who launched the external
app.Thepython script is killed, the app is launched, but the resulting
DOS window
is still there (I have the same result if I use the DOS tskill or taskkill
command to kill the script with his PID).

How could I hide/kill the resulting DOS window  from an os.sytem /
os.popenwith Python ?

Also is there a way to launch an external app. but without os.system and
os.popen to wait there but immediately continue to the next python line (to
avoid this I'm using a thread for now).

As an example I'm using this so far.
If I use pythonw to launch the script, notepad is launched, the script is
killed but the DOS window stay there.



import os,sys,time
from thread import start_new_thread

def launchApp():
   os.system("notepad.exe")

try:
   start_new_thread(launchApp,())
except:
   print "Something went wrong => do somenthing"

time.sleep(2)

print "And now going out"
sys.exit()



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


Re: [Tutor] help me to do my exercise

2007-03-05 Thread Eric Walstad
In addition to what the others have said, you have a syntax error on line 7 of 
your leap.py file.  The 'for' loop will want an iterable to loop over.  The 
syntax error relates to the list you are trying to use, a slice of sys.argv.  
Remember, lists are denoted by square brackets '[' and ']'.


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


Re: [Tutor] executing a string representing python code

2007-03-05 Thread Alan Gauld
"Cecilia Alm" <[EMAIL PROTECTED]> wrote

> Thanks, Adam. I guess the exec would be exec("some_func").
> The result seems pretty similar to eval(), allthough eval() seems 
> more
> straight-forward if the aim is to assign the returned value ("Done") 
> to a
> variable.

 s = eval('some_func("wasn\'t that cool")')
> Hello World wasn't that cool
 s
> 'Done'

You should be able to just do:

>>> s = some_func("wasn't that cool")

The whole point of the exec is that the function now exists in
your local namespace. You can execute it as any other function.

>> func_str = \
>> '''
>> def some_func(value):
>> # youwould check value instance here and do something to it
>> print "Hello World", value
>> return "Done"
>> '''
>> exec(func_str)

This creates the function

>> f = locals()["some_func"]
>> print f("wasn't that cool!")

There should be no need for this trickery.

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] help me to do my exercise

2007-03-05 Thread Alan Gauld
"Vasile Filat" <[EMAIL PROTECTED]> wrote

> I just started learning Python from a book translated intro
> Russian. It is my first exercise from the book and I don't
> know why it is not working.

OK, First please send the code as text rather than as an image.
It makes it much easier to comment when we can cut n paste
sections into email.

Secondly please send along any error messages.

OK, Now looking at you code. Thee are several syntax errors
which python should be complaining about. The first I see is:

>  for i in sys.argvj 1:]:

I suspect the j should be a [

Next you have no indentation after the except:

also you have a colon after the continue plus a block of code.
continue just makes the loop go round again...

Next your test for y*100 == 0 will only pass if y is zero!

You only print the Gregorian result if y % 4 is zero, but
not if y%400 is zero...


That should be enough to keep you busy.
Please post text next time.

HTH,

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




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


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


Re: [Tutor] Hiding/Killing a DOS window (from a previous os.system call)

2007-03-05 Thread Alan Gauld

"learner404" <[EMAIL PROTECTED]> wrote

> I'm launching an external app through an os.system() or os.popen() 
> but I
> don't want the resulting DOS window to hang-on there forever

Try using os.spawnl() instead:

>>> pid = os.spawnl(os.P_NOWAIT,r'C:\Windows\System32\notepad.exe')
>>> print pid
2834

The P_NOWAIT makes it return immediately, and no DOS box
is created.

Note that the spawn family of processes are deprecated and the
subprocess module provides equivalent functionality.

Using subprocess it looks like:

>>> pid = Popen([r'C:\Windows\System32\notepad.exe', ""]).pid

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] Hiding/Killing a DOS window (from a previous os.system call)

2007-03-05 Thread learner404

Thanks a lot Alan !  both solutions work like a charm :)


On 3/5/07, Alan Gauld <[EMAIL PROTECTED]> wrote:



"learner404" <[EMAIL PROTECTED]> wrote

> I'm launching an external app through an os.system() or os.popen()
> but I
> don't want the resulting DOS window to hang-on there forever

Try using os.spawnl() instead:

>>> pid = os.spawnl(os.P_NOWAIT,r'C:\Windows\System32\notepad.exe')
>>> print pid
2834

The P_NOWAIT makes it return immediately, and no DOS box
is created.

Note that the spawn family of processes are deprecated and the
subprocess module provides equivalent functionality.

Using subprocess it looks like:

>>> pid = Popen([r'C:\Windows\System32\notepad.exe', ""]).pid

HTH,

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


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

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


Re: [Tutor] executing a string representing python code

2007-03-05 Thread Cecilia Alm

That's neat. When just the function call is the string, eval() seems
appropriate. (For example, if reading what function to call from a file.)


def some_func(val):

   return val


s = eval('some_func("that\'s also pretty cool")')
s

"that's also pretty cool"

At any rate, thanks for the responses,
Cecilia




You should be able to just do:

>>> s = some_func("wasn't that cool")

The whole point of the exec is that the function now exists in
your local namespace. You can execute it as any other function.

>> func_str = \
>> '''
>> def some_func(value):
>> # youwould check value instance here and do something to it
>> print "Hello World", value
>> return "Done"
>> '''
>> exec(func_str)

This creates the function

>> f = locals()["some_func"]
>> print f("wasn't that cool!")

There should be no need for this trickery.

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





--
E. Cecilia Alm
Graduate student, Dept. of Linguistics, UIUC
Office: 2013 Beckman Institute
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 37, Issue 15

2007-03-05 Thread Tony Cappellini
Rename your file from .py to .pyw.
This will prevent the DOS window from appearing

Message: 6
Date: Mon, 5 Mar 2007 16:51:42 +0100
From: learner404 <[EMAIL PROTECTED]>
Subject: [Tutor] Hiding/Killing a DOS window (from a previous
   os.system call)
To: "Tutor Python" 
Message-ID:
   <[EMAIL PROTECTED]>
Content-Type: text/plain; charset="iso-8859-1"

Hello,

I'm launching an external app through an os.system() or os.popen() but I
don't want the resulting DOS window to hang-on there forever (my python
script is launched with pythonw):
I use a sys.exit() to kill the script who launched the external
app.Thepython script is killed, the app is launched, but the resulting
DOS window
is still there (I have the same result if I use the DOS tskill or taskkill
command to kill the script with his PID).

How could I hide/kill the resulting DOS window  from an os.sytem /
os.popenwith Python ?

Also is there a way to launch an external app. but without os.system and
os.popen to wait there but immediately continue to the next python line (to
avoid this I'm using a thread for now).

As an example I'm using this so far.
If I use pythonw to launch the script, notepad is launched, the script is
killed but the DOS window stay there.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] executing a string representing python code

2007-03-05 Thread ALAN GAULD
> That's neat. When just the function call is the string, 
> eval() seems appropriate. (For example, if reading what 
> function to call from a file.)

Its conventient but incredibly dangerous.
Its much better in that case to create a dictionary of allowed
(ie safe!) functions that can vbe read and then look that up 
from the file input.

Otherwise anyone who can access the file (legitimately or 
otherwise) can start calling any of the standard Python functions, 
including os.unlink() to delete files, or even os.system(), to do 
just about anything - howsabout formatting your disk?

ok_funks = {
  'some_func' : some_func,
  'another_func': another_func,
  'some_fancy_name': sys.exit
}

func = raw_input('type a function name>')
try: ok_funks[func']()
except: print 'Thats not a valid function'

eval and exec are seductively powerful but they are immensely 
dangerous in a world of crackers and virus makers. They should 
only ever be used in strictly controlled scebnarios and even then 
as a last resort.

HTH,

Alan G.






___ 
What kind of emailer are you? Find out today - get a free analysis of your 
email personality. Take the quiz at the Yahoo! Mail Championship. 
http://uk.rd.yahoo.com/evt=44106/*http://mail.yahoo.net/uk ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 37, Issue 15

2007-03-05 Thread learner404

On 3/5/07, Tony Cappellini <[EMAIL PROTECTED]> wrote:


Rename your file from .py to .pyw.
This will prevent the DOS window from appearing



It's not the DOS window from the Python script I was talking about  (in my
mail I said "my python
script is launched with pythonw") but the one resulting from the os.systemor
os.popen commands.

The two solutions that Alan gave are perfect for these :)

Message: 6

Date: Mon, 5 Mar 2007 16:51:42 +0100
From: learner404 <[EMAIL PROTECTED]>
Subject: [Tutor] Hiding/Killing a DOS window (from a previous
   os.system call)
To: "Tutor Python" 
Message-ID:
   <[EMAIL PROTECTED]>
Content-Type: text/plain; charset="iso-8859-1"

Hello,

I'm launching an external app through an os.system() or os.popen() but I
don't want the resulting DOS window to hang-on there forever (my python
script is launched with pythonw):
I use a sys.exit() to kill the script who launched the external
app.Thepython script is killed, the app is launched, but the resulting
DOS window
is still there (I have the same result if I use the DOS tskill or taskkill
command to kill the script with his PID).

How could I hide/kill the resulting DOS window  from an os.sytem /
os.popenwith Python ?

Also is there a way to launch an external app. but without os.system and
os.popen to wait there but immediately continue to the next python line
(to
avoid this I'm using a thread for now).

As an example I'm using this so far.
If I use pythonw to launch the script, notepad is launched, the script is
killed but the DOS window stay there.

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


[Tutor] trouble with function-- trying to check differences btwn 2 strings

2007-03-05 Thread zannah marsh

I am very new to Python (I've been learning it for about 2 weeks, in a
class). I wrote this function ( as part of a larger program, for my
homework) which is supposed to step through two given strings and check for
differences between the strings. It returns a list of the indices at which
the characters are different. Here it is:

def getDiff(word1,word2):
   #this function takes two words and returns a list containing the index
of each point of difference in the words
   difList = [] #creates empty list to hold index (location) of differences
in words
   index = 0 #sets variable index to 0 to start
   for item in word1: #steps throug string
   if item == item in word2: #checks characters against each other
   print "there is a match at index ", index
   print word1[index]
   print word2[index]
   index += 1 #increments index

   else:
   print "they are not the same at index ", index
   print word1[index]
   print word2[index]
   difList.append(index) #adds index value to list
   index += 1 # increments index
   return difList

It works, but only sometimes-- in some cases, it evaluates letters that are
different as being the same. Seems to be happening after a run of a few
matching letters? It also works for "carpet" "carrot", but not for "carrot"
"carpet". I think it must be something wrong with the structure of the
loop-- or how i am using it-- but I can't see it. Does anyone have any
suggestions? Here is output from some sample runs:


getDiff("desk","foot") # works fine

they are not the same at index  0
d
f
they are not the same at index  1
e
o
they are not the same at index  2
s
o
they are not the same at index  3
k
t
[0, 1, 2, 3]

getDiff("beep","bees") # works fine

there is a match at index  0
b
b
there is a match at index  1
e
e
there is a match at index  2
e
e
they are not the same at index  3
p
s
[3]

getDiff("freeze","fresia")  # this one did not work!

there is a match at index  0
f
f
there is a match at index  1
r
r
there is a match at index  2
e
e
there is a match at index  3
e
s
they are not the same at index  4
z
i
there is a match at index  5
e
a
[4]

getDiff("carrot","carpet") # this one did not work!

there is a match at index  0
c
c
there is a match at index  1
a
a
there is a match at index  2
r
r
there is a match at index  3
r
p
they are not the same at index  4
o
e
there is a match at index  5
t
t
[4]

getDiff("carpet","carrot") #but this does work (inverted order from

above)
there is a match at index  0
c
c
there is a match at index  1
a
a
there is a match at index  2
r
r
they are not the same at index  3
p
r
they are not the same at index  4
e
o
there is a match at index  5
t
t
[3, 4]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] trouble with function-- trying to check differences btwn 2 strings

2007-03-05 Thread Rikard Bosnjakovic
On 3/6/07, zannah marsh <[EMAIL PROTECTED]> wrote:

> if item == item in word2: #checks characters against each other

Here's the error.

Loop variable "item" contains the actual character in word1. The
syntax "item in word2" checks if this character is _anywhere_ in
word2. What you want to do is rewriting this loop so it checks
character per character. Since it's for homework, I don't want to ruin
your grades by giving you a solution. Try it yourself first.

To see what I mean, try running getDiff("abcd", "dcba").


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


Re: [Tutor] trouble with function-- trying to check differences btwn 2 strings

2007-03-05 Thread Rikard Bosnjakovic
On 3/6/07, zannah marsh <[EMAIL PROTECTED]> wrote:
> thanks Rikard, that makes sense.

No problems, but please post answers to the list and not to my mail adress.

This list is braindead that doesn't use a reply-to-tag for the posts,
but we've had that debate already so there's no need for another.


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


Re: [Tutor] trouble with function-- trying to check differences btwn 2 strings

2007-03-05 Thread Rikard Bosnjakovic
On 3/6/07, zannah marsh <[EMAIL PROTECTED]> wrote:

> step through them as you would in an array or list. so i had this:
>  for i in word1
>  if word1[i] == word2[i]...

For this construction to work in Python, you use the range()-function.

for index in range(len(word1)):


Say word1 is "parrot", len(word1) == 6, range(len(word1)) = 0,1,2,3,4,5.


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


[Tutor] Quicktime in python

2007-03-05 Thread Ashok Nayar
I need a little help. I'm trying to learn how to manipulate  
Quicktimes through python. I want to be able to set up a grid of   
quicktime movies and be able to resize them all to a consistent width  
and height so it's all neat and tidy. I have found very little  
documentation regarding quicktime modules for python. Can anyone help  
me out?

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


Re: [Tutor] Quicktime in python

2007-03-05 Thread Alan Gauld

"Ashok Nayar" <[EMAIL PROTECTED]> wrote 

>I need a little help. I'm trying to learn how to manipulate  
> Quicktimes through python. 

Are you on a Mac? If so MacPyhon includes bindings 
for manipulating QT media. They are pretty low level
and I've never used them so I can't tell you much more 
than that.

If you are not on a Mac then I don't know whats available...

Alan g.


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


[Tutor] installers and more

2007-03-05 Thread Kirk Bailey
1. Installers. Apps which are hard to install do not get installed. 
MiniWiki is now fit for public display and use, but it still takes the 
geek nature to install it. I need to become expert with one of the free 
installers to turn it into something a business executive would be 
willing to download and fire off as an isntaller.

2. It takes python being in the laptop so it can be used. As I do not 
own it, I hesitate to bundle it in. What's the legal aspect of providing 
the language the app needs so it will run?


-- 
Salute!
-Kirk Bailey
   Think
  +-+
  | BOX |
  +-+
   knihT

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