From: pointal at lure.u-psud.fr (Laurent POINTAL)
Date: Tue, 11 May 1999 12:37:45 GMT
Subject: My quarterly question on Design by Contract in Python...
References: <000701be9925$4690ffa0$bd9e2299@tim> <373439CB.BA8A94CE@webone.com.au> <19990511065256.B552436@vislab.epa.gov>
Message-ID: <37382397.969598328@news.u-psud.fr>
Content-Length: 2933
X-UID: 1905                                                 

On Tue, 11 May 1999 06:52:56 -0400, Randall Hopper
<aa8vb at vislab.epa.gov> wrote:

>: In this case, if -O is given, will PRE/POST functions be generated, and
>: called when invoked?  Is there a way to have PRE and POST invocations
>: effectively be no-ops?  Ala:
>: 
>:   #ifdef DEBUG
>:   #  define PRE(x) assert(x)
>:   #else
>:   #  define PRE(x)
>:   #endif
>: 

I wrote a small tool to automatically comment/uncomment blocs of code
between balises like #<BALISE> and #</BALISE>.
Not perfect (especially if the two balises are not set
correspondingly). Maybe can be optimized. But it does its job and it
took only on quarter of hour to write.

A+

Laurent.


Here my conditional.py file:
------------------------------------------------------------------
"""Change comments status on conditional code in Python.

Generally used to automatically comment/uncomment a serial of lines
between two marks, generally debugging lines.

To comment   : python conditional.py COND_SYMB fileName
To uncomment : python conditional.py /COND_SYMB fileName
"""

from sys import argv, exit
from string import find, whitespace

def CountSpaces (l) :
    cpt = 0
    for c in l :
        if (not c in whitespace) : return cpt
        cpt = cpt + 1

def Work (lines, symbol, bComment) :
    niv = 0 # Number of symbol counted.
    blancs = 0
    for i in range(len(lines)) :
        l = lines[i]

        # Search #</SYMBOL>
        if (find (l, "#</"+symbol+">") != -1) :
            niv = niv-1
            if (niv == 0) : 
                blancs = 0
                continue

        # Search #<SYMBOL>
        if (find (l, "#<"+symbol+">") != -1) :
            niv = niv+1
            if (niv == 1) : 
                blancs = CountSpaces (l)
                continue
                
        # Process lines between symbols.
        if (niv > 0) :
            if (bComment) :
                l = l[0:blancs] + "#" + l[blancs:]
            else :
                l = l[0:blancs] + l[blancs+1:]
            lines[i] = l        
    
    return lines

if __name__ == '__main__' :
    if (len (argv) != 3) :
        print "Usage: python conditional.py symbol fileName"
        print "   If symbol is in the form SYMBOL, conditional lines
are commented."
        print "   If symbol is in the form /SYMBOL, conditional lines
are uncommented."
        exit ()

    # Get symbol.
    symb = argv[1]
    if (symb[0]=='/') :
        symb = symb[1:]
        bComment = 0
    else :
        bComment = 1
        
    # Read file.
    lines = open (argv[2], "r").readlines ()
    
    # Work.
    lines = Work (lines, symb, bComment)
     
    # Write file.
    open (argv[2], "w").writelines (lines)
------------------------------------------------------------------
    
    
---
Laurent POINTAL - CNRS/LURE - Service Informatique Experiences
Tel/fax: 01 64 46 82 80 / 01 64 46 41 48
email  : pointal at lure.u-psud.fr  ou  lpointal at planete.net 




