Skip to content

Instantly share code, notes, and snippets.

@motebaya
Created August 23, 2025 14:10
Show Gist options
  • Select an option

  • Save motebaya/bf8a0ed98abfdf96134bcddaa1d9d4de to your computer and use it in GitHub Desktop.

Select an option

Save motebaya/bf8a0ed98abfdf96134bcddaa1d9d4de to your computer and use it in GitHub Desktop.
4 to 2 indentation
#!/usr/bin/python3
import sys
import re
def cv_indent(file: str) -> str:
x = open(file, "r").read().strip().splitlines()
print(f" *!reading: {len(x)} lines")
code = ""
for line in x:
space_ = re.search(r"^ *", line)
tabs_ = re.search(r"^\t*", line)
if space_:
line = " " * (len(space_.group()) // 2) + line.removeprefix(space_.group())
code += line + "\n"
else:
if tabs_:
line = ("\t" * len(tabs_.group())) + line.removeprefix("\t" * len(tabs_.group()))
code += line + "\n"
else:
if len(line) == 0:
code += "\n"
else:
code += line + "\n"
print(f" *!writing: {len(code)} lines")
open('out.py', "w", encoding="utf-8").write(code)
return code
if __name__ == "__main__":
sys.exit(cv_indent(sys.argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment