Hello,
I'm writing my code into the 'vizard python editor' and sometimes it feels a bit bugged or something, now I get an error message that my class has no attribute.
I feel it has to do something with editing in vizard because in other editors I don't think i get this message.
Here's my code:
import csv
class Grid(list):
def __init__(self, width, height):
self.grid = []
self.width = width
self.height = height
self.length = width * height
for x in range(self.height):
col = []
for y in range(self.width):
col.append(Cell(x, y, self.grid))
self.grid.append(col)
def __getitem__(self, (x, y)):
return self.grid[x][y]
def fillBlock(self, left, right, top, bottom, value):
for x in range(left, right):
for y in range(top, bottom):
self[x, y].value = value
def firstFreeCell(self):
for y in range(self.height):
for x in range(self.width):
if self.grid[x][y].clusterId == -1:
return self.grid[x][y]
return None
def floodFill(self, x, y, clusterId, landUse):
if (x < 0 or y < 0 or x >= self.width or y >= self.height):
return
cell = self.grid[x][y]
cluster = Cluster()
if (cell.clusterId != -1 or cell.value != landUse):
return
cell.setClusterId(clusterId)
cluster.add(cell)
self.floodFill(x-1, y, clusterId, landUse)
self.floodFill(x+1, y, clusterId, landUse)
self.floodFill(x, y-1, clusterId, landUse)
self.floodFill(x, y+1, clusterId, landUse)
def analyze(self):
freeCell = self.firstFreeCell()
clusterId = 0
while freeCell != None:
self.floodFill(freeCell.x, freeCell.y, clusterId, freeCell.value)
freeCell = self.firstFreeCell()
clusterId += 1
def printClusterId(self, clusterId):
for y in range(self.height):
for x in range(self.width):
if self.grid[x][y].clusterId == clusterId:
print 'ClusterId:', clusterId, '=>', 'Cell-coordinates:',
'(', self.grid[x][y].x, ',', self.grid[x][y].y, ')',
'with a landUse value of:', self.grid[x][y].value
print "No cells with such clusterId left or the clusterId is not defined yet..."
def load(cls, filename):
print "Loaded csv file"
loadGrid = []
reader = csv.reader(open(filename), delimiter=';')
for line in reader:
loadGrid.append(line)
width = len(loadGrid[0])
height = len(loadGrid)
grid = Grid(width, height)
for x in range(width):
for y in range(height):
grid[x, y].value = loadGrid[y][x]
return grid
load = classmethod(load)
def printGrid(self):
for y in range(self.height):
for x in range(self.width):
print self[x, y].value,
print
def printGrid2(self):
for y in range(self.height):
for x in range(self.width):
print self[x, y].value,
print
class Cell(object):
def __init__(self, x, y, grid):
self.x = x
self.y = y
self.grid = grid
self.value = 0
self.clusterId = -1
def setClusterId(self, clusterId):
self.clusterId = clusterId
def getClusterId(self):
return self.clusterId
class Cluster(object):
def __init__(self):
self.cells = []
def __getitem__(self):
return self.cells[x][y]
def add(self, i):
self.cells.append(i)
import inspect
my_grid = Grid(6, 6)
my_grid.printGrid()
my_grid.fillBlock(2, 4, 2, 4, 2)
my_grid.printGrid()
my_grid.printGrid()
#my_grid.printGrid2()
try:
print inspect.ismethod(my_grid.printGrid)
print inspect.ismethod(my_grid.printGrid2)
except AttributeError, error:
print error
"""
template1 = Grid.load('template1.csv')
template2 = Grid.load('template2.csv')
template3 = Grid.load('template3.csv')
template4 = Grid.load('template4.csv')
template5 = Grid.load('template5.csv')
template6 = Grid.load('template6.csv')
template7 = Grid.load('template7.csv')
template8 = Grid.load('template8.csv')
"""
the printGrid2 method can't be found or something. When I delete this method and copy/paste the printGrid method and change this copied one to printGrid2 it sometimes works and other times it doesn't. I feel it has to do something with a bug or it doesn't read the syntax right sometimes. Anyone has this problem or sees why it happens? I would be thankfull.