On Feb 19, 11:09 am, Jean-Paul Calderone <[EMAIL PROTECTED]> wrote: > On 19 Feb 2007 09:04:19 -0800, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > > >Hi, > > >I have the following code: > > >colorIndex = 0; > > >def test(): > > print colorIndex; > > >This won't work. > > Are you sure? > > [EMAIL PROTECTED]:~$ cat foo.py > colorIndex = 0 > > def test(): > print colorIndex > > test() > [EMAIL PROTECTED]:~$ python foo.py > 0 > [EMAIL PROTECTED]:~$ > > The global keyword lets you rebind a variable from the module scope. It > doesn't have much to do with accessing the current value of a variable. > > Jean-Paul
Thanks. Then I don't understand why I get this error in line 98: Traceback (most recent call last): File "./gensvg.py", line 109, in ? outdata, minX, minY, maxX, maxY = getText(data); File "./gensvg.py", line 98, in getText print colorIndex; UnboundLocalError: local variable 'colorIndex' referenced before assignment Here is my complete script: #!/usr/bin/python import re import sys import time import os import shutil colors = ["#FF0000", "#00FF00", "#0000FF", "#FFFF00" ,"#FFA500" ,"#DA70D6"] colorIndex = 0 def getText( intputstr): rc = "" maxX = 0; maxY = 0; minX = 10000000; minY = 10000000; for str in intputstr: print str; if str != "": pattern = "x:(\d+) y:(\d+) w:(\d+) h:(\d+) (.*)" match = re.findall(pattern, str) if match: x, y, width, height, area = match[0] colorIndex = colorIndex + 1 rc = rc + "<rect x=\"%(x)s\" y=\"%(y)s\" width=\"% (width)s\" height=\"%(height)s\" " % locals() rc = rc + "fill=\"%s\" stroke=\"#000000\" stroke-width= \"1px\" fill-opacity=\".5\" />\n" % colors[colorIndex % len(colors)] _x = int(x) _y = int(y) _width = int(width) _height = int(height) minX = min(minX, _x); minY = min(minY, _y); maxX = max(maxX, _x+ _width); maxY = max(maxY, _y+_height); else: pattern = "\((\d+),(\d+)\)\((\d+),(\d+)\)(.*)" match = re.findall(pattern, str) if match: x1, y1, x2, y2, ignore = match[0] rc = rc + "<line x1=\"%(x1)s\" y1=\"%(y1)s\" x2=\"% (x2)s\" y2=\"%(y2)s\" style=\"stroke:rgb(99,99,99);stroke-width:2\" / >" % locals() rc = rc + "\n" _x1 = int(x1) _y1 = int(y1) _x2 = int(x2) _y2 = int(y2) minX = min(_x1, _x2); minY = min(_y1, _y2); maxX = max(_x1, _x2); maxY = max(_y1, _y2); print colorIndex; print "match 2!" return rc, minX, minY, maxX, maxY; fileName = sys.argv[1] inputFile = open(fileName, 'r') data = inputFile.readlines(); outdata, minX, minY, maxX, maxY = getText(data); print minX, minY, maxX, maxY outputFile = open(fileName + '.svg', 'w') print >> outputFile, "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" id=\"body\">" outputFile.write(outdata); print >>outputFile, "</svg>" -- http://mail.python.org/mailman/listinfo/python-list