Created
June 30, 2021 11:15
-
-
Save ali-bahjati/9ac9f02d7ee46df55a54f91a448ea68a to your computer and use it in GitHub Desktop.
Nested Function Call Formatter (useful for graphite queries)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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