Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to convert indentation in Python code to braces?

I am a totally blind programmer who would like to learn Python. Unfortunately the fact that code blocks are represented with different levels of indentation is a major stumbling block. I was wondering if there were any tools available that would allow me to write code using braces or some other code block delimiter and then convert that format into a properly indented representation that the Python interpreter could use?

like image 418
Jared Avatar asked Sep 23 '08 01:09

Jared


People also ask

Why does Python use indentation instead of braces?

For a human to be able to read the code indentation is a much better way of providing the visual cues about block structure. As indentation also contains all the information for the compiler, to use both would be redundant. As indentation is better for humans, it makes sense to use that for the compiler too.

Can I use braces in Python?

In fact, Python supports curly braces, BEGIN/END, and almost any other language's block schemes: see python.org/doc/humor/…!

Does Python follow the indentation instead of brackets?

As its name implies, Python with Braces doesn't care about indentation: you're free to make you code extremely ugly, or write your code properly in K&R style. Each line is terminated in a semicolon, and blocks of code with only one statement don't require curly braces, just like C and Java.


2 Answers

There's a solution to your problem that is distributed with python itself. pindent.py, it's located in the Tools\Scripts directory in a windows install (my path to it is C:\Python25\Tools\Scripts), it looks like you'd have to grab it from svn.python.org if you are running on Linux or OSX.

It adds comments when blocks are closed, or can properly indent code if comments are put in. Here's an example of the code outputted by pindent with the command:

pindent.py -c myfile.py

def foobar(a, b):    if a == b:        a = a+1    elif a < b:        b = b-1        if b > a: a = a-1        # end if    else:        print 'oops!'    # end if # end def foobar 

Where the original myfile.py was:

def foobar(a, b):    if a == b:        a = a+1    elif a < b:        b = b-1        if b > a: a = a-1    else:        print 'oops!' 

You can also use pindent.py -r to insert the correct indentation based on comments (read the header of pindent.py for details), this should allow you to code in python without worrying about indentation.

For example, running pindent.py -r myfile.py will convert the following code in myfile.py into the same properly indented (and also commented) code as produced by the pindent.py -c example above:

def foobar(a, b): if a == b: a = a+1 elif a < b: b = b-1 if b > a: a = a-1 # end if else: print 'oops!' # end if # end def foobar 

I'd be interested to learn what solution you end up using, if you require any further assistance, please comment on this post and I'll try to help.

like image 152
Ryan Avatar answered Sep 19 '22 16:09

Ryan


I personally doubt that there currently is at the moment, as a lot of the Python afficionados love the fact that Python is this way, whitespace delimited.

I've never actually thought about that as an accessibility issue however. Maybe it's something to put forward as a bug report to Python?

I'd assume that you use a screen reader here however for the output? So the tabs would seem "invisible" to you? With a Braille output, it might be easier to read, but I can understand exactly how confusing this could be.

In fact, this is very interesting to me. I wish that I knew enough to be able to write an app that will do this for you.

I think it's definately something that I'll put in a bug report for, unless you've already done so yourself, or want to.

Edit: Also, as noted by John Millikin There is also PyBraces Which might be a viable solution to you, and may be possible to be hacked together dependant on your coding skills to be exactly what you need (and I hope that if that's the case, you release it out for others like yourself to use)

Edit 2: I've just reported this to the python bug tracker

like image 24
Mez Avatar answered Sep 22 '22 16:09

Mez