Tim Hatch

Weblog | Photos | Projects | Panoramas | About

pybraces

I used to work (back in undergrad) with a guy who never got used to Python's whitespace, and wished he could use curly braces. While at Euro Pycon (+8 hours from home, so nobody was online to chat to, and I was bored), I coded this up in a couple of hours.

Inspiration comes from coderanger and Matt Good a year prior where we wondered if you could write sourcecode in rot13. Instead, I invented my own encoding (installed systemwide) which mangles the source even more (by adding and removing characters to construct the indentation).

If you're considering writing your own encoding, I'll advise against it -- the API isn't well documented, and most errors get suppressed. Also make sure you generate .pyc/.pyo because if they can't be generated (in a root-writable-only directory) things may fail.

Downloads

There is no stable version You can try it out anyway, download braces.py and put it in your Python encodings directory (making sure that you python -O braces.py to create the pyc), then set the encoding of a source file to braces.

The code is up on github: https://github.com/thatch/pybraces

Simple Example

# -*- encoding: braces -*-

if(True) {
    print("Hello World");
}
#

Example Usage

# -*- encoding: braces -*-

i = 5;
   if(i>4) {
 print "Hello World";
      }

my_name = "Bob";

print """

digraph G {
a -> b;
c -> c;
}
"""


while(True) {
print "Hi!";
print "This is a test", i;
i++;
if(i>10) {
break;
}

}

Notes

If you attempt to use your own encoding, a BOM error means that your encoding had a parse error itself, not the source file. If you get errors about types, make sure that decode/encode are returning tuples of str/unicode (respectively) and length of characters consumed. If your fake language is line based, you'll need to leave the last piece of a line as unparsed, which caused my "eats the last line" bug.

Contributing

I welcome patches, suggestions, and bugreports. Send me an email to code@timhatch.com or in a pull request.