Skip to content

Instantly share code, notes, and snippets.

@alex-luxonis
Created August 13, 2021 02:14
Show Gist options
  • Save alex-luxonis/dd5e418d62cde4b666c03849c1893609 to your computer and use it in GitHub Desktop.
Save alex-luxonis/dd5e418d62cde4b666c03849c1893609 to your computer and use it in GitHub Desktop.
Run and prettify output of scripts that print raw strings
'''
Pretty-prints the raw-strings output of scripts.
Usage:
Place this script in the same directory as your main script,
say that's named `file.py`, then provide the script name
as parameter, but omitting the `.py` extension:
python3 pretty.py file
'''
import io
import sys
import importlib
import codecs
from contextlib import redirect_stdout
f = io.StringIO()
with redirect_stdout(f):
try:
if len(sys.argv) != 2:
raise Exception("Please provide input file as parameter, without `.py`")
importlib.import_module(sys.argv[1])
except Exception as e:
print("FAILED. An exception occurred:", e)
out = f.getvalue()
decoded = codecs.decode(out, 'unicode_escape')
print(decoded)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment