Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Extend file type

Reply
Thread Tools

Extend file type

 
 
abcd
Guest
Posts: n/a
 
      09-26-2006
I have a class which extends 'file' ....

class MyFile(file):
def __init__(self, fname, mode='r'):
file.__init__(self, fname, mode)

def write(self, str):
print "writing a string"
file.write(self, str)

def writelines(self, lines):
print "writing lines"
file.writelines(self, lines)

I use this with subprocess....

f = MyFile('out.txt', 'w')
p = subprocess.Popen(cmd, stdout=f)

.....and I can see that stuff goes into out.txt.....however, I never see
"writing a string" or "writing lines" from the class MyFile.

Any ideas what methods the stdout (and I guess stderr) of Popen objects
from subprocess call?

Any suggestions on how to find out? I did try adding to MyFile....

def __call__(self, *args):
print "calling:", args
return file.__call__(self, *args)


but I never see that either.

thanks

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      09-26-2006
abcd wrote:

> Any ideas what methods the stdout (and I guess stderr) of Popen objects
> from subprocess call?


the external process only sees OS-level file handles (the number you get
from the fileno() method on your file objects), not Python objects. no
matter how you override things in your process, you cannot make your OS
pretend that the other process is written in Python...

</F>

 
Reply With Quote
 
 
 
 
Sybren Stuvel
Guest
Posts: n/a
 
      09-26-2006
abcd enlightened us with:
> Any suggestions on how to find out? I did try adding to MyFile....
>
> def __call__(self, *args):
> print "calling:", args
> return file.__call__(self, *args)
>
> but I never see that either.


I don't know the answer to your problem, but I can explain why this
doesn't work. __call__ is used when an instance if MyFile is called as
if it were a function:

mf = MyFile("blabla")
mf()

Sybren
--
Sybren Stüvel
Stüvel IT - http://www.stuvel.eu/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it possible (and wise) to extend the None-type ? Stef Mientki Python 3 11-27-2008 10:17 AM
Re: Is it possible (and wise) to extend the None-type ? Terry Reedy Python 3 11-27-2008 12:47 AM
Re: Is it possible (and wise) to extend the None-type ? Chris Rebert Python 0 11-26-2008 06:05 PM
Can't extend function type Paul Rubin Python 5 10-07-2005 02:25 PM
Advice on ways to extend a type using XSD Charles Fineman XML 2 02-25-2004 02:28 PM



Advertisments