Skip to content

Instantly share code, notes, and snippets.

@stefco
Last active September 13, 2017 17:40
Show Gist options
  • Save stefco/09448298455df0a389746110eb5a0bf5 to your computer and use it in GitHub Desktop.
Save stefco/09448298455df0a389746110eb5a0bf5 to your computer and use it in GitHub Desktop.
Run indented python code in a shell function so that things look nice and readable.
# a template that does nothing. the actual python script is 100% indented.
foo() {
python -c 'import textwrap; exec(textwrap.dedent(r"""
import sys
# remove the first argument, since it will just be '-c'
sys.argv.pop(0)
# put your actual code below
"""+" "*4))' "$@"
}
# an example that does something
foo() {
python -c 'import textwrap; exec(textwrap.dedent(r"""
import sys, os
# remove the first argument, since it will just be '-c'
sys.argv.pop(0)
sys.stdout.write("this is a test of dedenting a script.")
for arg in sys.argv:
sys.stdout.write("\nargument passed: " + arg)
sys.stdout.write("\ncwd: " + os.getcwd())
"""+" "*4))' "$@"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment