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条回答
Summer. ? 凉城
2楼-- · 2019-01-30 04:02

I appreciate your problem, but think you are specifying the implementation instead of the problem you need solved. Instead of converting to braces, how about working on a way for your screen reader to tell you the indentation level?

For example, some people have worked on vim syntax coloring to represent python indentation levels. Perhaps a modified syntax coloring could produce something your screen reader would read?

查看更多
Ridiculous、
3楼-- · 2019-01-30 04:07

You should be able to configure your editor to speak the tabs and spaces -- I know it's possible to display whitespace in most editors, so there must be an accessibility option somewhere to speak them.

Failing that, there is pybraces, which was written as a practical joke but might actually be useful to you with a bit of work.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-30 04:11

Although I am not blind, I have heard good things about Emacspeak. They've had a Python mode since their 8.0 release in 1998 (they seem to be up to release 28.0!). Definitely worth checking out.

查看更多
beautiful°
5楼-- · 2019-01-30 04:12

Searching an accessible Python IDE, found this and decided to answer. Under Windows with JAWS:

  1. 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.
  2. In the search field, type "indent"
  3. There will be only one result: "Say indent characters". Turn this on.
  4. Enjoy!

The only thing that is frustrating for us is that we can't enjoy code examples on websites (since indent speaking in browsers is not too comfortable — it generates superfluous speech).

Happy coding from another Python beginner).

查看更多
SAY GOODBYE
6楼-- · 2019-01-30 04:14

All of these "no you can't" types of answers are really annoying. Of course you can.

It's a hack, but you can do it.

http://timhatch.com/projects/pybraces/

uses a custom encoding to convert braces to indented blocks before handing it off to the interpreter.


As an aside, and as someone new to python - I don't accept the reasoning behind not even allowing braces/generic block delimiters ... apart from that being the preference of the python devs. Braces at least won't get eaten accidentally if you're doing some automatic processing of your code or working in an editor that doesn't understand that white space is important. If you're generating code automatically, it's handy to not have to keep track of indent levels. If you want to use python to do a perl-esque one-liner, you're automatically crippled. If nothing else, just as a safeguard. What if your 1000 line python program gets all of its tabs eaten? You're going to go line-by-line and figure out where the indenting should be?

Asking about it will invariably get a tongue-in-cheek response like "just do 'from __ future __ import braces'", "configure your IDE correctly", "it's better anyway so get used to it" ...

I see their point, but hey, if i wanted to, i could put a semicolon after every single line. So I don't understand why everyone is so adamant about the braces thing. If you need your language to force you to indent properly, you're not doing it right in the first place.

Just my 2c - I'm going to use braces anyway.

查看更多
对你真心纯属浪费
7楼-- · 2019-01-30 04:14

Edsger Dijkstra used if ~ fi and do ~ od in his "Guarded Command Language", these appear to originate from the Algol68. There were also some example 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 mangled python code can be unambiguously unmangled and reindented if one uses guarded blocks if/fi, do/od & try/yrt together with semicolons ";" to separate statements. Excellent for unambiguous magazine listings or cut/pasting from web pages.

It should be easy enough to write a short python program to insert/remove the guard blocks and semicolons.

查看更多
登录 后发表回答