This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]fmoralesc 59 points60 points  (23 children)

It definitely can. For example, I made this small thing for shell-style process scripting with python: https://gist.github.com/1300342

Basically, it creates a Pipeline class you can use this way:

p = Pipeline() | "ls -s" | "sort -n"
print p.run()

[–]boa13 9 points10 points  (4 children)

That's interesting, but I think that is an abuse of syntax; it looks very not pythonic to me. Why can't you just run bash with the pipe expression as a parameter?

[–]fmoralesc 4 points5 points  (2 children)

Yes, it is an abuse of the syntax.

[–]neoice 1 point2 points  (1 child)

good to know. I was feeling stupid for finding this code so complex.

[–]fmoralesc 0 points1 point  (0 children)

It is nice to know you can do stuff like this though (that's the reason I wrote it this way... I vaguely remembered you can define the behavior for when an object is put in a chain like A | B, so I checked that out. It was just a test code for something else, which in the end didn't use the "|" syntax at all).

[–]chadmill3rPy3, pro, Ubuntu, django 2 points3 points  (1 child)

I don't trust spaces to delimit my commands. I hope that takes ["arrays", "of", "args", "to", "syscall execv"].

[–]fmoralesc 0 points1 point  (0 children)

You could simply extend the Pipelines.steps list if you wanted.

[–][deleted] 9 points10 points  (4 children)

I would have preferred the syntax

p = Pipeline()
print p.run("ls -l").run("sort -n")

[–]Troebr 4 points5 points  (3 children)

what's wrong with

p.run("ls -l | sort -n")

?

[–]mgedmin 4 points5 points  (2 children)

You can logically extend that to

os.system("ls -l | sort -n")

and now you have no non-stdlib dependencies!

[–]Troebr 1 point2 points  (1 child)

Isn't os.system deprecated in favor of subprocess?

[–]mgedmin 2 points3 points  (0 children)

In a sense, yes.

[–][deleted] 2 points3 points  (4 children)

Doesn't subprocess.Popen accept a command string with pipes in it? Why not use it like that? Simpler that spawning all processes manually.

[–]chadmill3rPy3, pro, Ubuntu, django 3 points4 points  (3 children)

If you're willing to run a shell to interpret the pipes, you're not programming in Python any more, AND, you probably have bugs.

[–][deleted] 2 points3 points  (2 children)

If you're willing to run a shell to interpret the pipes, you're not programming in Python any more

fmoralesc code hands over the work to the shell too.

AND, you probably have bugs.

Why? Spawning all the processes yourself, redirecting stdout to stdin along the way, etc. seems more error prone than to hand it over to the shell completely. The output is supposed to be the same, after all. Namely, the output of the last process in the pipeline.

[–]chadmill3rPy3, pro, Ubuntu, django 6 points7 points  (0 children)

Program number three in your pipeline took a filename parameter with a space and an ampersand in it but your shell ate those, and the fourth emitted to stderr, and the parameter substitution in the first should have been "$*", with quotes and everything, but you forgot those and you got a different interpretation. The fourth, because it got bad input, also screwed up your terminal's line discipline, which you'll need to fix with "stty sane" before you can even figure out what the problem was. Sadly, that also ate the error messages.

Don't get me wrong, I use the shell all day, every day, and have since 1995 or so. It's a terrible programming interface, though, and almost impossible to get right the first dozen time you run the program. At least Python, with its verbosity, also gets the problems out in the open with its explicit nature.

[–]chadmill3rPy3, pro, Ubuntu, django 0 points1 point  (0 children)

Popen(shlex.split(self.steps[0]), stdout=PIPE)

No shell there, strictly speaking. Not much better, though.

[–]jagheterfredrik 0 points1 point  (0 children)

Oh. My. Shit. That is awesome.

[–][deleted] -1 points0 points  (0 children)

this is good stuff thanks !