[Tutor] program idea

Sean 'Shaleh' Perry shalehperry@attbi.com
Thu May 15 21:23:08 2003


On Wednesday 14 May 2003 21:09, Kirk Bailey wrote:
> I have abn idea for a tool program.
> it would read in a script, and write it out again with a line number in a
> comment  at the end of each line, as
>
> f1=open(filename,mode)	#121
> owner=string.strip(f1.readline())	#122
> passwd=string.strip(f1.readline())	#123
> f1.close()	#123
> ##124
>
> etc.
> Line numbering gets VERY confusing if you slip up even a little when doing
> it by hand.
>

as others have discussed, the initial writing is easy however ....

My main issue is this is likely to spill code over the 80 chars One True 
Length.  Not a huge deal, but something to consider.

> Another would remove such and redo it when creating a new version with new
> lines in teh script.
>

this is harder.  You have to handle cases like this:

i = j + (k * 2) # add joules to twice the constant # 237

which I would expect to be common in script code.

I guess a regex like r'#\s\d+$' should cover it.

So, here is my version (which looks good in a fixed width font and horrible 
otherwise):
#! /usr/bin/python                                                      #  1
                                                                        #  2
import sys                                                              #  3
                                                                        #  4
def compute_padding(num):                                               #  5
    pad = 0                                                             #  6
    while num > 0:                                                      #  7
        num /= 10                                                       #  8
        pad += 1                                                        #  9
    return pad                                                          # 10
                                                                        # 11
def add_lineno(lines, fp = sys.stdout):                                 # 12
    MAX_LEN = 80                                                        # 13
                                                                        # 14
    trailer_gen = "# %%%ds" % compute_padding(len(lines))               # 15
                                                                        # 16
    count = 0                                                           # 17
                                                                        # 18
    for line in lines:                                                  # 19
        line = line.rstrip()                                            # 20
        count += 1                                                      # 21
        trailer = trailer_gen % count                                   # 22
        output_gen = "%%s%%%ds\n" % (80 - len(trailer) - len(line))     # 23
        fp.write(output_gen % (line, trailer))                          # 24
                                                                        # 25
def sub_lineno(lines, fp = sys.stdout):                                 # 26
    import re                                                           # 27
    stripper = re.compile(r'#\s+\d+$')                                  # 28
    for line in lines:                                                  # 29
        fp.write(stripper.sub('', line))                                # 30
                                                                        # 31
if __name__ == '__main__':                                              # 32
    if len(sys.argv) > 1 and sys.argv[1] == 'sub':                      # 33
        lines = sys.stdin.readlines()                                   # 34
        sub_lineno(lines)                                               # 35
    else:                                                               # 36
        lines = sys.stdin.readlines()                                   # 37
        add_lineno(lines)                                               # 38