Wrapped grid, size, box_height/width around the real values.
Added a method to fetch constraints.
Fixed the rendering code to use the above.
from __future__ import division
from math import sqrt
from __future__ import division
from math import sqrt
+from operator import attrgetter
-from weakref import proxy
-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)]
symbols = [str(x + 1) for x in range(9)] + [chr(x + 97) for x in xrange(26)]
return None
value = property(_get_value)
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):
def __init__(self, grid, row, column):
- self._grid = proxy(grid)
self._row = row
self._col = column
self._row = row
self._col = column
- self._values = range(self._grid.size)
+ self._values = range(self.grid.size)
self._constraints = []
self._normalized = False
self._constraints = []
self._normalized = False
def _cellidx(self, row, col):
"""Hashes a row and column into a flat array index."""
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):
@classmethod
def _infer_box_size(cls, dimension):
- 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'))
self._box_width = box_width
self._size = box_height * box_width
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)
self._cells[self._cellidx(row, col)] \
= Cell(self, row, col)
self = cls(box_width=box_width, box_height=box_height)
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
value = rows[row][col]
if not value:
continue
self = cls(box_width=box_width, box_height=box_height)
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
ch = grid[ self._cellidx(row, col) ]
if ch == '0':
continue
cell.add_constraint(constraint)
def add_default_constraints(self):
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))
self.add_constraint(Row(self, i))
self.add_constraint(Column(self, i))
self.add_constraint(Box(self, i))
parts = []
parts.append(self.before_grid(grid))
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))
parts.append(self.inside_grid(grid))
parts.append(self.render_row(row))
parts.append(self.after_grid(grid))
parts.append(self.before_row(row))
for idx, cell in enumerate(row.cells):
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))
parts.append(self.inside_row(row))
parts.extend(self.render_cell(cell))
parts.append(self.after_row(row))
"""
def inside_grid(self, grid):
"""
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 '|'
def inside_row(self, row):
return '|'