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.

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

the printGrid2 method can't be found or something.

Indentation is very important in Python. Your printGrid2 method is defined within printGrid, so it isn't a member of the class Grid

This should fix it:

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

It appears that you're using a mix of tabs and spaces, which is a no-no. Save yourself the trouble and convert all your tabs to spaces (4)

Sorry, you are right :)

the problem was the fact that the interpreter I use at home automatically worked with tabs and the vizard interpreter somehow had some stuff coded in spaces. In the vizard interpreter this wasnt visible yet untill I found out how to make it show.

So yes the problem was with combinging tabs and spaces but I got it solved. Stupidly enough I didnt see it in the code i posted here. Thanks for the help though.

Many editors have an option to set the tab key to use 4 or 8 spaces instead of the '\t' character. Also, there is usually a batch edit command such as "Replace leading tabs with spaces", or "Replace all tabs", etc. It's usually a good idea to check what the tab preferences are in any editor the first time you use it.

I use Notepad++ and always have the 'View' -> 'Show whitespace' option on. It adds a barely visible 'dot' in place of spaces and a ----> In space of tabs which helps to instantly identify which is being used. This is esecially helpful when using someone else's code (it's the first thing I noticed when I copy-pasted your code)

This question has already been answered. Start a new discussion instead.