473,803 Members | 4,391 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cooler piping

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(fnam e,args):
pfd = os.pipe()
if os.fork() == 0:
os.close(pfd[0])
os.dup2(pfd[1],sys.stdout.fil eno())
os.execvp(fname ,args)
else:
os.close(pfd[1])
return os.fdopen(pfd[0],'r')

def fancypopen2(fna me,args):
pfdout = os.pipe()
pfdin = os.pipe()
if os.fork() == 0:
os.close(pfdout[0])
os.dup2(pfdout[1],sys.stdout.fil eno())
os.close(pfdin[1])
os.dup2(pfdin[0],sys.stdin.file no())
os.execvp(fname ,args)
else:
os.close(pfdout[1])
os.close(pfdin[0])
return (os.fdopen(pfdi n[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
Jul 18 '05 #1
2 2675
In article <1069377379.729 708@yasure>, Donn Cave wrote:
Quoth Csaba Henk <csaba@phony_fo r_avoiding_spam .org>:
...
| is having a function with an execvp-like syntax: if I could do something
| like os.fancypopen(' file', ['file',fname])...

You may find that popen2.popen2() already comes pretty close to this.
You may specify a command string, but you also may specify a list of
strings for execvp. The only difference is that you supply only the
argument list, and the file to execute is inferred from its first element.


Thanks, it's cool! Just what I needed... Now I checked, I can pass a list of
strings to os.popen2(), too.

Only if documentation didn't suck... The Library Reference doesn't mention
anything about the type of the cmd argument of the popen functions, and
after I saw the function working with a cmd of type string, I automatically
assumed that it can be nothing else but a string. (And I don't blame myself
for this...)

--
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
Jul 18 '05 #2
Quoth Csaba Henk <csaba@phony_fo r_avoiding_spam .org>:
....
| Only if documentation didn't suck... The Library Reference doesn't mention
| anything about the type of the cmd argument of the popen functions, and
| after I saw the function working with a cmd of type string, I automatically
| assumed that it can be nothing else but a string. (And I don't blame myself
| for this...)

Well, you will see a lot of it in Python, functions whose arguments may
be of various types. Not polymorphic, because we're not talking about
any semantic overlap between the types - this is more like overloading.

I personally feel that it's a sloppy practice that leads to confusion
and errors, but I've done it myself, so at least you and I can use this
example as a reminder of what not to do.

Donn Cave, do**@drizzle.co m
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1876
by: Apple Grew | last post by:
I want to develope a Winboard like application which will support Winboard protocol. For that I require to know how to handle piping in Python. I seperated out module popen2, but when I use fileObject.readline() then it halts the program if the sub-process is waiting for user input; will executing the readline() part in a seperate thread be helpful? My target platform is Windows. Links to helpful web resources and sample Python codes...
4
2542
by: christopher diggins | last post by:
I just wanted to share this technique for piping the cout from one function to the cin of another, using the pipe operator: #include <iostream> #include <sstream> using namespace std; typedef void(*procedure)();
0
1369
by: Andrew Saeman | last post by:
Registration Info: http://www.milwaukeeultimate.com/classic/info.aspx Contact Milwaukee Ultimate: http://www.milwaukeeultimate.com/contact.aspx Current Openings: Open A Division: 3 openings Open B Division: 4 openings
3
21403
by: Russ Schneider | last post by:
Is there an easy way to pipe a select statement's output to a file? -- http://www.sugapablo.com <--music ] http://www.sugapablo.net <--personal ] sugapablo@12jabber.com <--jabber IM ] ---------------------------(end of broadcast)--------------------------- TIP 6: Have you searched our list archives?
1
1389
by: saibotorama | last post by:
What is the Python translation for this Bash statement: tar cf - "${files}" | bzip2 "$file".tar.bz2 (Ignoring the fact that "tar cjf" also exists...) In other words, how does one pipe together arbitrary commands?
2
3674
by: moti | last post by:
I am a newbie to MySQL. I'm confused as to the piping syntax of SERVER in the User DSN in ODBC for a client PC. If I use an ip it does not work, probably because of a firewall. So I tried using named pipes and nothing seems to work. I am running MySQL on a windows xp and all the client PCs are on xp too. Named Pipes is checked as well as Enable named pipes and Socket/pipe name, which is MySQL.
3
4246
by: noob2008 | last post by:
can anyone explain me what is piping in C++ programming? for example invoice.exe < purchase.dat > statement.txt do i replace that where all the cin statements? i created a text file called purchase.dat with values: gameboy 149.99 1 ipod nano 220.99 2 DVD-player5 60.95 1
4
1323
by: samit | last post by:
I keen to persue coding like yahoo piping system which will include drag and drop and piping features
0
1464
by: jsimps44 | last post by:
Hi, I'm fairly new to c, and very new to piping and file descriptors and can't seem to get past this problem. The piping is very much not working, and I can't figure out for the life of me why. Any help at all would be greatly appreciated. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <signal.h>
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10068
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9119
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6840
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5496
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4274
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2968
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.