piep (pronounced “pipe”) is a new command line tool for processing text streams with a slightly modified python syntax, inspired by the main characters of a typical unix shell (
tr, etc). To pluck a random example. here’s how you might rename all files (but not directories) in the current folder to have a “.bak” extension (because you have a very strange and manual backup scheme, apparently):
$ ls -1 | piep 'not os.path.isdir(p) | sh("mv", p, p + ".bak")'
In this simple example we can see filtering, piping (note that the pipes between expressions are internal to piep’s single argument, and thus not interpreted by the shell), and shelling out to perform useful work.
Here’s another, to print out the size of files in the current directory that are greater than 1024 bytes:
$ ls -l | piep 'pp[1:] | p.splitre(" +", 7) | size=int(p) | size > 1024 | p, "is", p, "bytes"'
Or, if hacking through the output of
ls -l isn’t your thing (it’s most likely a terrible idea), you can do things the pythonic way:
$ ls -1 | piep --import 'stat' 'size=os.stat(p).st_size | size > 1024 | p, "is", size, "bytes"'
For a proper introduction, you should read the online documentation. But I wanted to address one specific point here, about the origins of
Recently I came across
pyp, The Pied Piper. It seemed like a great idea, but after I played with it for a little while I uncovered some unfortunate shortcomings, some of which are deal breakers. My list included:
- stream-based operation: there’s a beta implementation with “turbo” (line-wise) mode, but it seems very limited. I believe it should be the norm, and wanted to see if I could do things in a way that was just as convenient, but with all the benefits of lazy stream-based processing.
- Command execution: commands are made up by string concatenation, requiring manual effort to escape metacharacters including the humble space 1. Also, errors are silently ignored.
- Purity of data: things are routinely
strip()ed and empty strings are frequently dropped from computations. Breaking up a line into a list of data would (sometimes?) see each list merged back into the input stream, rather than maintained as a list.
- stream confusion: second stream, file inputs, etc. Not really sure why there are so many special cases
- a not-very-extensible extension mechanism, which is fairly manual and appears to preclude sharing or combining extensions
- lots of unnecessary machinery that complicates the code: macros, history, –rerun, three file input types, etc. Some of this may be useful once you use the tool a lot, but it hindered my ability to add the features I wanted to
I initially tried my hand at modifying
pyp to fix some of the things I didn’t like about it, but the last point there really got in my way. History is baked in, and doesn’t really work in the same manner for stream-based operations. The entire
pp class had to be rewritten, which is actually what I started doing when I decided to turn it into a separate tool (since it then became difficult to integrate this new
pp class with the rest of the system. Anyway, I hope this isn’t taken as an offence by the developers of
pyp - I really like the ideas, so much so that I was compelled to write my ideal version of them.
I humbly submit that concatenating strings is the worst possible way to generate shell commands, leading to countless dumb bugs that only rear their heads in certain situations (and often in cascading failures). Observe
piep’s method on a filename containing spaces:
$ ls -1 | piep 'sh("wc", "-c", p)' 82685610 Getting the Most Out of Python Imports.mp4
Compared to that of
pyp(and countless other tools):
$ ls -1 | pyp 'shell("wc -c " + p)' wc: Getting: No such file or directory wc: the: No such file or directory wc: Most: No such file or directory wc: Out: No such file or directory wc: of: No such file or directory wc: Python: No such file or directory wc: Imports.mp4: No such file or directory [0 total] $ echo $? 0
It is unacceptable for a language with simple and convenient sequence types to instead rely on complex string escaping rules to prevent data from being misinterpreted. To be honest, this on its own may be reason enough to use
piepover alternatives. ↩