digital literacy for everyone


[lit]

python with braces

[lit] if you dont like mandatory indentation and want to code python with braces, simply code like this: for p in range(10): { if p > 5: { print("\{") print( p ) } } then pipe the code through the following python script: #!/usr/bin/env python2 # coding: utf-8 #### license: creative commons cc0 1.0 (public domain) #### http://creativecommons.org/publicdomain/zero/1.0/ proginf = "python with braces, feb 2019 mn" from sys import stdin, stdout from os import name as osname nl = chr(10) if osname == "nt": nl = chr(13) + chr(10) indent = 0 igwhite = 0 buf = "" esc = 0 doublenl = 0 for p in stdin: stdout.write(chr(32) * 4 * indent) for each in p.lstrip(): # prevent spaces between braces from messing with indents if igwhite: if each in (chr(32), chr(9)): each = "" else: igwhite = 0 # you must escape all braces that are not syntactic if buf == chr(92): if esc: if not each in "{}": stdout.write(chr(92)) esc = 0 else: if each == "{": indent += 1 each = nl + (chr(32) * 4 * indent) igwhite = 1 elif each == "}": indent -= 1 each = nl igwhite = 1 # these escapes arent brace related, so emit else: if esc: stdout.write(chr(92)) esc = 0 if each != chr(92): stdout.write(each) stdout.flush() else: esc = 1 buf = each # dont let indents concatenate if not p.lstrip(): stdout.write(nl) if esc: stdout.write(chr(92)) esc = 0
home: [lit]https://codeinfig.neocities.org[lit]