echoing input
Sean Dunn
sdunn at digitalanvil.com
Wed Sep 22 14:00:19 EDT 1999
Greg Wilson wrote:
> Hi. Is there a way to get Python to echo the contents of a script while it
> is
> being loaded? I have a script called x.py that contains:
# How about this: (you would call RunVerbose(and your file) which is listed
at the bottom)
# It basically finds statement blocks, inserts print commands around them,
with the correct
# whitespace.
#
# Oh.. and it hasn't even been tested.
#
def Unfinished(accum, first, last):
cCurly = 0
cSquare = 0
cParen = 0
bBackSlashLast = 0
lastCharacter = ''
lineCounter = first
while lineCounter < last:
for character in accum[lineCounter]:
if character == '(':
cParen = cParen + 1
elif character == ')':
cParen = cParen - 1
elif character == '[':
cSquare = cSquare + 1
elif character == ']':
cSquare = cSquare - 1
elif character == '{':
cCurly = cCurly + 1
elif character == '}':
cCurly = cCurly - 1
if character in " \t\n":
lastCharacter = character
#for
lineCounter = lineCounter + 1
#while
if (cParen > 0) || (cCurly > 0) || (cSquare > 0) || (lastCharacter ==
'\'):
# still more to read to make finished
#
return 1
elif (cParen < 0) || (cCurly < 0) || (cSquare < 0):
# We started negatively imbalanced!
#
raise RuntimeError, "Parenthetical Syntax Unbalanced"
else
# Cool..
#
return 0
#if
#def
def WhiteSpaceLevel(accum, first, last):
lineCounter = first
minLen = 32768 # good starting point?
minLenString = ''
while lineCounter < last:
tempString = ''
for character in accum[lineCounter]:
if character in ' \t':
tempString = tempString + character
else:
break
#if
#for
if len(tempString) < menLen:
minLen = len(tempString)
minLenString = tempString
#if
lineCounter = lineCounter + 1
#while
# We now have the smallest amount of whitespace in this chunk..
# Use it to space inserted commands
#
return minLenString
#def
def RunVerbose(file):
codeAccum = []
codeIndex = 0
fp = open(file)
codeLine = fp.readline()
unfinishedStart = 0
while(codeLine):
codeAccum[codeIndex] = codeLine
unfinishedStart = codeIndex
#make sure we insert the 'print' statement in the right place
#
while Unfinished(codeAccum, unfinishedStart, codeIndex):
codeLine = fp.readline()
codeAccum[codeIndex] = codeLine
codeIndex = codeIndex + 1
#while
#string to add before each line
#
whiteSpace = WhiteSpaceLevel(codeAccum, unfinishedStart, codeIndex)
codeInsertIndex = codeIndex+1 # where do we start
lineCount = 0 # line counter
maxLines = codeIndex - unfinishedStart # how many lines are we adding
# do the insertions
#
while(lineCount < maxLines):
codeAccum[codeInsertIndex + lineCount] = \
whiteSpace + "print ('" + codeAccum[unfinishedStart
+ lineCount] + "')"
lineCount = lineCount + 1
#while
codeIndex = codeInsertIndex + (codeIndex - unfinishedStart)
codeLine = fp.readline()
# at this point, codeIndex has been updated to the next place in the
array..
#
#while
if codeIndex > 0:
finalCodeString = ''
finalCounter = 0
codeIndex = codeIndex - 1 # back off
while finalCounter < codeIndex):
finalCodeString = finalCodeString + codeAccum[finalCounter]
finalCounter = finalCounter + 1
#while
# Without further ado.. cross your fingers
#
exec(finalCodeString)
#if
#def
More information about the Python-list
mailing list