Skip to content

Instantly share code, notes, and snippets.

@ali-bahjati
Created June 30, 2021 11:15
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 ali-bahjati/9ac9f02d7ee46df55a54f91a448ea68a to your computer and use it in GitHub Desktop.
Save ali-bahjati/9ac9f02d7ee46df55a54f91a448ea68a to your computer and use it in GitHub Desktop.
Nested Function Call Formatter (useful for graphite queries)
#!/usr/bin/env python3
# Useful to format and see how nested function calls specially graphite expressions
# unquote is used to replace url-encoded expressions (useful when you inspect graphite query)
from urllib.parse import unquote
import sys
BASE_INDENT = 4
print("Please enter (paste) the query then press Ctrl-d or redirect from file:")
text = sys.stdin.read()
text = unquote(text).replace(' ', '').replace('\t', '').replace('\n', '')
cur_indent = 0
def new_line():
return '\n' + cur_indent * ' '
result = ''
for ch in text:
if ch == '(':
cur_indent += BASE_INDENT
result += ch + new_line()
elif ch == ',':
result += ch + new_line()
elif ch == ')':
cur_indent -= BASE_INDENT
result += new_line() + ch
else:
result += ch
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment