Is there a way to convert indentation in Python co

2019-01-30 03:24发布

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?

12条回答
男人必须洒脱
2楼-- · 2019-01-30 04:19

I use eclipse with the pydev extensions since it's an IDE I have a lot of experience with. I also appreciate the smart indentation it offers for coding if statements, loops, etc. I have configured the pindent.py script as an external tool that I can run on the currently focused python module which makes my life easier so I can see what is closed where with out having to constantly check indentation.

查看更多
▲ chillily
3楼-- · 2019-01-30 04:22

There are various answers explaining how to do this. But I would recommend not taking this route. While you could use a script to do the conversion, it would make it hard to work on a team project.

My recommendation would be to configure your screen reader to announce the tabs. This isn't as annoying as it sounds, since it would only say "indent 5" rather than "tab tab tab tab tab". Furthermore, the indentation would only be read whenever it changed, so you could go through an entire block of code without hearing the indentation level. In this way hearing the indentation is no more verbose than hearing the braces.

As I don't know which operating system or screen reader you use I unfortunately can't give the exact steps for achieving this.

查看更多
神经病院院长
4楼-- · 2019-01-30 04:24

Python supports braces for defining code blocks, and it also supports using 'begin' and 'end' tags.

Please see these code examples:

class MyClass(object): #{
    def myfunction(self, arg1, arg2): #{
        for i in range(arg1): #{ 
            print i
        #}
    #}
#}

And an example with bash style:

fi = endclass = enddef = endclass = done = None
class MyClass(object):
    def myfunction(self, arg1, arg2):
        for i in range(arg1): #do
            if i > 5: #then
                print i
            fi
        done
    enddef
endclass

The best thing about this is is that you can forget to put a close bracket in, and it's still valid python!

class MyClass(object): #{
    def myfunction(self, arg1, arg2): #{
        for i in range(arg1): #{ 
            print i
        # whoops, forgot to close that bracket!
    #}
#}

original gag

My real advice is to get a Braille display if you can afford one/source one - blind python programmers of my acquaintance really found a Braille display indispensable for writing python programs, it makes the indentation thing much less painful. A 40 cell display is well worth it.

查看更多
Rolldiameter
5楼-- · 2019-01-30 04:25

If you're on Windows, I strongly recommend you take a look at EdSharp from: http://empowermentzone.com/EdSharp.htm It supports all of the leading Windows screenreaders, it can be configured to speak the indentation levels of code, or it has a built in utility called PyBrace that can convert to and from braces syntax if you want to do that instead, and it supports all kinds of other features programmers have come to expect in our text editors. I've been using it for years, for everything from PHP to JavaScript to HTML to Python, and I love it.

查看更多
我想做一个坏孩纸
6楼-- · 2019-01-30 04:27

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

查看更多
Fickle 薄情
7楼-- · 2019-01-30 04:28

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 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 -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 -d 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.

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.

查看更多
登录 后发表回答