C
Csaba Henk
For me, one reason for using Python is that with the help of it
I can rid of several problems that I faced with during writing scripts
in Bourneish shells. Biggest of these problem was the quotation madness
stemming from the typelessness of the shell language.
When using Python as a shell replacement I often launch apps from Python and
use different piping constructs to pass data to/get data from these apps. I
expect from the language that I could perform these actions simply and
effectively.
However, its not the case: there is os.pipe() which is low-level and its
usage is complex, and there are several popen functions which are simple to
use, but they evaluate the given command by passing it to the shell,
throwing me back to the middle of the quotation hell I want to escape from.
A typical case is when one tries to find out some info about a file by
invoking the "file" command on it: the name of the file can be (almost)
anything, thus doing os.popen("file " + filename) gets sucked easily (here
filename is a variable storing the name of the file). (Don't tell me there
is a module in Python with the fucntionality of the file command [I don't
know wheter is], I could find out many similar examples.) What would be cool
is having a function with an execvp-like syntax: if I could do something
like os.fancypopen('file', ['file',fname])...
Considering me, I came over this problem by using the following module:
<code>
import os
import sys
def fancypopen(fname,args):
pfd = os.pipe()
if os.fork() == 0:
os.close(pfd[0])
os.dup2(pfd[1],sys.stdout.fileno())
os.execvp(fname,args)
else:
os.close(pfd[1])
return os.fdopen(pfd[0],'r')
def fancypopen2(fname,args):
pfdout = os.pipe()
pfdin = os.pipe()
if os.fork() == 0:
os.close(pfdout[0])
os.dup2(pfdout[1],sys.stdout.fileno())
os.close(pfdin[1])
os.dup2(pfdin[0],sys.stdin.fileno())
os.execvp(fname,args)
else:
os.close(pfdout[1])
os.close(pfdin[0])
return (os.fdopen(pfdin[1],'w'),os.fdopen(pfdout[0],'r'))
sfancypopen = lambda f,a: fancypopen(f, [f] + a)
sfancypopen2 = lambda f,a: fancypopen2(f, [f] + a)
</code>
Now my question is that shouldn't (doesn't) Python include a *standard*
module like this? That wouldn't be much hassle but would improve the
usability of the language...
--
Csaba
"There's more to life, than books, you know but not much more..."
[The Smiths]
***
If you want to send me a mail, see it in the mailto link at
http://www.renyi.hu/~ekho/egyelore.html
I can rid of several problems that I faced with during writing scripts
in Bourneish shells. Biggest of these problem was the quotation madness
stemming from the typelessness of the shell language.
When using Python as a shell replacement I often launch apps from Python and
use different piping constructs to pass data to/get data from these apps. I
expect from the language that I could perform these actions simply and
effectively.
However, its not the case: there is os.pipe() which is low-level and its
usage is complex, and there are several popen functions which are simple to
use, but they evaluate the given command by passing it to the shell,
throwing me back to the middle of the quotation hell I want to escape from.
A typical case is when one tries to find out some info about a file by
invoking the "file" command on it: the name of the file can be (almost)
anything, thus doing os.popen("file " + filename) gets sucked easily (here
filename is a variable storing the name of the file). (Don't tell me there
is a module in Python with the fucntionality of the file command [I don't
know wheter is], I could find out many similar examples.) What would be cool
is having a function with an execvp-like syntax: if I could do something
like os.fancypopen('file', ['file',fname])...
Considering me, I came over this problem by using the following module:
<code>
import os
import sys
def fancypopen(fname,args):
pfd = os.pipe()
if os.fork() == 0:
os.close(pfd[0])
os.dup2(pfd[1],sys.stdout.fileno())
os.execvp(fname,args)
else:
os.close(pfd[1])
return os.fdopen(pfd[0],'r')
def fancypopen2(fname,args):
pfdout = os.pipe()
pfdin = os.pipe()
if os.fork() == 0:
os.close(pfdout[0])
os.dup2(pfdout[1],sys.stdout.fileno())
os.close(pfdin[1])
os.dup2(pfdin[0],sys.stdin.fileno())
os.execvp(fname,args)
else:
os.close(pfdout[1])
os.close(pfdin[0])
return (os.fdopen(pfdin[1],'w'),os.fdopen(pfdout[0],'r'))
sfancypopen = lambda f,a: fancypopen(f, [f] + a)
sfancypopen2 = lambda f,a: fancypopen2(f, [f] + a)
</code>
Now my question is that shouldn't (doesn't) Python include a *standard*
module like this? That wouldn't be much hassle but would improve the
usability of the language...
--
Csaba
"There's more to life, than books, you know but not much more..."
[The Smiths]
***
If you want to send me a mail, see it in the mailto link at
http://www.renyi.hu/~ekho/egyelore.html