[ACCEPTED]-Is there a way to convert indentation in Python code to braces?-blindness
There's a solution to your problem that 20 is distributed with python itself. pindent.py
, it's 19 located in the Tools\Scripts directory in 18 a windows install (my path to it is C:\Python25\Tools\Scripts), it 17 looks like you'd have to grab it from svn.python.org if you are running 16 on Linux or OSX.
It adds comments when blocks 15 are closed, or can properly indent code 14 if comments are put in. Here's an example 13 of the code outputted by pindent with the 12 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 11 also use pindent.py -r
to insert the correct indentation 10 based on comments (read the header of pindent.py 9 for details), this should allow you to code 8 in python without worrying about indentation.
For 7 example, running pindent.py -r myfile.py
will convert the following 6 code in myfile.py
into the same properly indented 5 (and also commented) code as produced by 4 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 3 learn what solution you end up using, if 2 you require any further assistance, please 1 comment on this post and I'll try to help.
I personally doubt that there currently 24 is at the moment, as a lot of the Python 23 afficionados love the fact that Python is 22 this way, whitespace delimited.
I've never 21 actually thought about that as an accessibility 20 issue however. Maybe it's something to put 19 forward as a bug report to Python?
I'd assume 18 that you use a screen reader here however 17 for the output? So the tabs would seem "invisible" to 16 you? With a Braille output, it might be 15 easier to read, but I can understand exactly 14 how confusing this could be.
In fact, this 13 is very interesting to me. I wish that I 12 knew enough to be able to write an app that 11 will do this for you.
I think it's definately 10 something that I'll put in a bug report 9 for, unless you've already done so yourself, or 8 want to.
Edit: Also, as noted by John Millikin There is also 7 PyBraces Which might be a viable solution to you, and 6 may be possible to be hacked together dependant 5 on your coding skills to be exactly what 4 you need (and I hope that if that's the 3 case, you release it out for others like 2 yourself to use)
Edit 2: I've just reported this to the 1 python bug tracker
Although I am not blind, I have heard good 4 things about Emacspeak. They've had a Python mode 3 since their 8.0 release in 1998 (they seem to be up 2 to release 28.0!). Definitely worth checking 1 out.
You should be able to configure your editor 7 to speak the tabs and spaces -- I know it's 6 possible to display whitespace in most editors, so 5 there must be an accessibility option somewhere 4 to speak them.
Failing that, there is pybraces, which 3 was written as a practical joke but might 2 actually be useful to you with a bit of 1 work.
If you're on Windows, I strongly recommend 11 you take a look at EdSharp from: http://empowermentzone.com/EdSharp.htm It supports 10 all of the leading Windows screenreaders, it 9 can be configured to speak the indentation 8 levels of code, or it has a built in utility 7 called PyBrace that can convert to and from 6 braces syntax if you want to do that instead, and 5 it supports all kinds of other features 4 programmers have come to expect in our text 3 editors. I've been using it for years, for 2 everything from PHP to JavaScript to HTML 1 to Python, and I love it.
All of these "no you can't" types 33 of answers are really annoying. Of course 32 you can.
It's a hack, but you can do it.
http://timhatch.com/projects/pybraces/
uses 31 a custom encoding to convert braces to indented 30 blocks before handing it off to the interpreter.
As 29 an aside, and as someone new to python - I 28 don't accept the reasoning behind not even 27 allowing braces/generic block delimiters ... apart 26 from that being the preference of the python 25 devs. Braces at least won't get eaten accidentally 24 if you're doing some automatic processing 23 of your code or working in an editor that 22 doesn't understand that white space is important. If 21 you're generating code automatically, it's 20 handy to not have to keep track of indent 19 levels. If you want to use python to do 18 a perl-esque one-liner, you're automatically 17 crippled. If nothing else, just as a safeguard. What 16 if your 1000 line python program gets all 15 of its tabs eaten? You're going to go line-by-line 14 and figure out where the indenting should 13 be?
Asking about it will invariably get a 12 tongue-in-cheek response like "just 11 do 'from __ future __ import braces'", "configure 10 your IDE correctly", "it's better 9 anyway so get used to it" ...
I see 8 their point, but hey, if i wanted to, i 7 could put a semicolon after every single 6 line. So I don't understand why everyone 5 is so adamant about the braces thing. If 4 you need your language to force you to indent 3 properly, you're not doing it right in the 2 first place.
Just my 2c - I'm going to use 1 braces anyway.
I appreciate your problem, but think you 9 are specifying the implementation instead 8 of the problem you need solved. Instead 7 of converting to braces, how about working 6 on a way for your screen reader to tell 5 you the indentation level?
For example, some people have 4 worked on vim syntax coloring to represent 3 python indentation levels. Perhaps a modified 2 syntax coloring could produce something 1 your screen reader would read?
Searching an accessible Python IDE, found 7 this and decided to answer. Under Windows 6 with JAWS:
- Go to Settings Center by pressing JawsKey+6 (on the number row above the letters) in your favorite text editor. If JAWS prompts to create a new configuration file, agree.
- In the search field, type "indent"
- There will be only one result: "Say indent characters". Turn this on.
- Enjoy!
The only thing that is frustrating 5 for us is that we can't enjoy code examples 4 on websites (since indent speaking in browsers 3 is not too comfortable — it generates superfluous 2 speech).
Happy coding from another Python 1 beginner).
I use eclipse with the pydev extensions 8 since it's an IDE I have a lot of experience 7 with. I also appreciate the smart indentation 6 it offers for coding if statements, loops, etc. I 5 have configured the pindent.py script as 4 an external tool that I can run on the currently 3 focused python module which makes my life 2 easier so I can see what is closed where 1 with out having to constantly check indentation.
There are various answers explaining how 16 to do this. But I would recommend not taking 15 this route. While you could use a script 14 to do the conversion, it would make it hard 13 to work on a team project.
My recommendation 12 would be to configure your screen reader 11 to announce the tabs. This isn't as annoying 10 as it sounds, since it would only say "indent 9 5" rather than "tab tab tab tab tab". Furthermore, the 8 indentation would only be read whenever 7 it changed, so you could go through an entire 6 block of code without hearing the indentation 5 level. In this way hearing the indentation 4 is no more verbose than hearing the braces.
As 3 I don't know which operating system or screen 2 reader you use I unfortunately can't give 1 the exact steps for achieving this.
Edsger Dijkstra used if ~ fi and do ~ od in his "Guarded Command 12 Language", these appear to originate 11 from the Algol68. There were also some example 10 python guarded blocks used in RosettaCode.org.
fi = od = yrt = end = lambda object: None;
class MyClass(object):
def myfunction(self, arg1, arg2):
for i in range(arg1) :# do
if i > 5 :# then
print i
fi
od # or end(i) #
end(myfunction)
end(MyClass)
Whitespace 9 mangled python code can be unambiguously 8 unmangled and reindented if one uses guarded 7 blocks if/fi, do/od & try/yrt together 6 with semicolons ";" to separate 5 statements. Excellent for unambiguous magazine 4 listings or cut/pasting from web pages.
It 3 should be easy enough to write a short python 2 program to insert/remove the guard blocks 1 and semicolons.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.