From: Eevee Date: Tue, 6 Jan 2009 06:16:10 +0000 (-0800) Subject: Added some real accessors. X-Git-Url: http://git.veekun.com/pseudoku.git/commitdiff_plain/f66d11f5304e3c2c3853d69acfc864bcb8301903 Added some real accessors. Wrapped grid, size, box_height/width around the real values. Added a method to fetch constraints. Fixed the rendering code to use the above. --- diff --git a/pseudoku/grid/__init__.py b/pseudoku/grid/__init__.py index e91a1bd..a62f18f 100644 --- a/pseudoku/grid/__init__.py +++ b/pseudoku/grid/__init__.py @@ -1,10 +1,11 @@ from __future__ import division from math import sqrt +from operator import attrgetter import re -from weakref import proxy +from weakref import ref -from cellgroup import Row, Column, Box +from cellgroup import CellConstraint, Row, Column, Box symbols = [str(x + 1) for x in range(9)] + [chr(x + 97) for x in xrange(26)] @@ -35,15 +36,14 @@ class Cell(object): return None value = property(_get_value) - def _get_constraints(self): - return self._constraints - constraints = property(_get_constraints) + grid = property(lambda self: self._grid()) + constraints = property(attrgetter('_constraints')) def __init__(self, grid, row, column): - self._grid = proxy(grid) + self._grid = ref(grid) self._row = row self._col = column - self._values = range(self._grid.size) + self._values = range(self.grid.size) self._constraints = [] self._normalized = False @@ -106,7 +106,7 @@ class Grid(object): def _cellidx(self, row, col): """Hashes a row and column into a flat array index.""" - return row * self._size + col + return row * self.size + col @classmethod def _infer_box_size(cls, dimension): @@ -144,21 +144,19 @@ class Grid(object): ### Accessors - def _get_box_height(self): - return self._box_height - box_height = property(_get_box_height) - - def _get_box_width(self): - return self._box_width - box_width = property(_get_box_width) + def get_constraints(self, constraint_class=CellConstraint): + """Returns constraints of a certain type. Returns all of them by + default. + """ - def _get_size(self): - return self._size - size = property(_get_size) + condition = lambda constraint: isinstance(constraint, constraint_class) + return filter(condition, self._constraints) - def _get_constraints(self): - return self._constraints - constraints = property(_get_constraints) + rows = property(lambda self: self.get_constraints(Row)) + box_height = property(attrgetter('_box_height')) + box_width = property(attrgetter('_box_width')) + size = property(attrgetter('_size')) + constraints = property(attrgetter('_constraints')) ### Constructors @@ -170,9 +168,9 @@ class Grid(object): self._box_width = box_width self._size = box_height * box_width - self._cells = range(self._size ** 2) - for row in xrange(self._size): - for col in xrange(self._size): + self._cells = range(self.size ** 2) + for row in xrange(self.size): + for col in xrange(self.size): self._cells[self._cellidx(row, col)] \ = Cell(self, row, col) @@ -190,8 +188,8 @@ class Grid(object): self = cls(box_width=box_width, box_height=box_height) - for row in xrange(self._size): - for col in xrange(self._size): + for row in xrange(self.size): + for col in xrange(self.size): value = rows[row][col] if not value: continue @@ -229,8 +227,8 @@ class Grid(object): self = cls(box_width=box_width, box_height=box_height) - for row in xrange(self._size): - for col in xrange(self._size): + for row in xrange(self.size): + for col in xrange(self.size): ch = grid[ self._cellidx(row, col) ] if ch == '0': continue @@ -246,7 +244,7 @@ class Grid(object): cell.add_constraint(constraint) def add_default_constraints(self): - for i in xrange(self._size): + for i in xrange(self.size): self.add_constraint(Row(self, i)) self.add_constraint(Column(self, i)) self.add_constraint(Box(self, i)) diff --git a/pseudoku/render/__init__.py b/pseudoku/render/__init__.py index fb700bd..a3593df 100644 --- a/pseudoku/render/__init__.py +++ b/pseudoku/render/__init__.py @@ -16,8 +16,8 @@ class GridRenderer(object): parts = [] parts.append(self.before_grid(grid)) - for idx, row in enumerate(grid._rows): - if idx and idx % grid._box_height == 0: + for idx, row in enumerate(grid.rows): + if idx and idx % grid.box_height == 0: parts.append(self.inside_grid(grid)) parts.append(self.render_row(row)) parts.append(self.after_grid(grid)) @@ -30,7 +30,7 @@ class GridRenderer(object): parts.append(self.before_row(row)) for idx, cell in enumerate(row.cells): - if idx and idx % row._grid._box_width == 0: + if idx and idx % row.grid.box_width == 0: parts.append(self.inside_row(row)) parts.extend(self.render_cell(cell)) parts.append(self.after_row(row)) diff --git a/pseudoku/render/text.py b/pseudoku/render/text.py index aab6c36..3a49134 100644 --- a/pseudoku/render/text.py +++ b/pseudoku/render/text.py @@ -21,8 +21,8 @@ class AsciiArtGridRenderer(SquareGridRenderer): """ def inside_grid(self, grid): - box_header = '+' + '-' * grid._box_width - return box_header * grid._box_height + '+\n' + box_header = '+' + '-' * grid.box_width + return box_header * grid.box_height + '+\n' def inside_row(self, row): return '|'