Re: Python Indentation Obligation...

  • From: "Hamid Hamraz" <hhamraz@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Sun, 12 Dec 2010 22:42:07 +0330

It is in Python. I tried the header comment to be descriptive enough. each of 
the begin/end markers must be in a separate line followed by a new line 
character. Each line including the begin/end markers will be deleted from the 
output script. You can also use it like this for example:

def myFunc()
#{
function_body
#}

use it from the command line like this:
braceRemover.py your_script_name

The output script is output.py

Note that the # sign is not necessary.
It is also easy to modify the new line character(s) to get adapted to your 
OS/Editor, from within the script.
One more thing you have to be cautious about, is the end of your file to be 
ended with a new line.
I haven't tested it so much. Up to now, it has worked fine for me. Feel free to 
change it as you want, and do let me know of any potential problems/bugs.

Cheers!
Hamid
  ----- Original Message ----- 
  From: QuentinC 
  To: programmingblind@xxxxxxxxxxxxx 
  Sent: Sunday, December 12, 2010 8:19 PM
  Subject: Re: Python Indentation Obligation...


  > I concur that the Python indentation is hard to deal, but at least for 
writing the Python scripts, I developed a simple script for removing the braces 
or whatever start/end markers is required to use for better readability, with 
the corresponding indent level. It is very short and easy for my initial Python 
scripting practises. If you would like, I will be glad to send it along.

  IN which language is your script ? I'm interested.
  I found a python modification named pybrace. In fact, that's the type of 
thing I'm looking for, because a script so modified would have a syntax 
relatively close to other scripting languages (js, lua, ruby, etc.). The 
problem with the braces if not to confuse with dictionnaries...
  I could perhaps adapt something to use with my 6pad editor. Thank you if you 
can send it to me.

  > Moreover, I also found Python a very easy to pick, and very strong 
scripting language.
  I don't doubt about its capabilities, its popularity is a kind of proof. The 
only thing which stops me in going further is that indentation problem.
# Takes a Python script as the command line input,
# and reformats it to a properly indented python script,
# based on the begin and the end markers, i.e. '{' and '}' respectively.
# Note that each of the begin/end markers must be in a seperate line, followed 
with a newline character, i.e. do press Enter after typing either of the 
markers. Be careful of the end of your script; do add a newline character 
(press Enter) if there is an end marker there!

import sys

SPACE = ' '
TAB = 4 * SPACE
beginMarker = '{\n'
endMarker = '}\n'

inFile = sys.argv[1] # the input script
outFile = 'output.py' # the output script

def adjustIndent(line, level):
        '''Takes an string, replaces any space/tab from the beginning, with the 
designated indents.'''
        i = 0
        l = len(line)
        while i < l:
                if line[i] != ' ' and line[i] != '\t':
                        break
                i += 1
        return level * TAB + line[i:]


fin = open(inFile)
fout = open(outFile, 'w')

indentLevel = 0

for eachLine in fin:
        line = eachLine
        if beginMarker in line:
                indentLevel += 1
        elif endMarker in line:
                indentLevel -= 1
        else:
                line = adjustIndent(line, indentLevel)
                fout.write(line)

fin.close()
fout.close()

Other related posts: