Skip to content

Instantly share code, notes, and snippets.

@pyrustic
Last active March 21, 2022 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pyrustic/c05dc63b5e808c2695e775da5a6d0d7f to your computer and use it in GitHub Desktop.
Save pyrustic/c05dc63b5e808c2695e775da5a6d0d7f to your computer and use it in GitHub Desktop.
Subrun Demo

Subrun Demo

This is a demo for Subrun.

Subrun is an intuitive API to safely start and communicate with processes in Python.

To play with this demo, copy-paste the Python code below and then run it after installing Subrun.

To install Subrun:

$ pip install subrun

Source code

A pipeline of three commands is run. The output is the Zen of Python sorted alphabetically. The title and the next line are removed before the sort operation.

You can run the same pipeline in your shell:

$ python -m this | tail --lines=+3 | sort

Output of the demo

Although never is often better than *right* now.
Although practicality beats purity.
Although that way may not be obvious at first unless you're Dutch.
Beautiful is better than ugly.
Complex is better than complicated.
Errors should never pass silently.
Explicit is better than implicit.
Flat is better than nested.
If the implementation is easy to explain, it may be a good idea.
If the implementation is hard to explain, it's a bad idea.
In the face of ambiguity, refuse the temptation to guess.
Namespaces are one honking great idea -- let's do more of those!
Now is better than never.
Readability counts.
Simple is better than complex.
Sparse is better than dense.
Special cases aren't special enough to break the rules.
There should be one-- and preferably only one --obvious way to do it.
Unless explicitly silenced.

Original Zen of Python

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
from subrun import pipeline
# Commands
ZEN = "python -m this"
TAIL = "tail --lines=+3"
SORT = "sort"
# Create and run the pipeline made of ZEN, TAIL, and SORT
info = pipeline.run(ZEN, TAIL, SORT)
# Control return codes
names = ("Zen", "Tail", "Sort")
for i, return_code in enumerate(info.return_codes):
if return_code != 0:
msg = "{} returned with {}".format(names[i], return_code)
raise Exception(msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment