Added some real accessors.
authorEevee <ferrox@veekun.com>
Tue, 6 Jan 2009 06:16:10 +0000 (22:16 -0800)
committerEevee <ferrox@veekun.com>
Tue, 6 Jan 2009 06:16:10 +0000 (22:16 -0800)
Wrapped grid, size, box_height/width around the real values.
Added a method to fetch constraints.
Fixed the rendering code to use the above.

pseudoku/grid/__init__.py
pseudoku/render/__init__.py
pseudoku/render/text.py

index e91a1bd..a62f18f 100644 (file)
@@ -1,10 +1,11 @@
 from __future__ import division
 
 from math import sqrt
 from __future__ import division
 
 from math import sqrt
+from operator import attrgetter
 import re
 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)]
 
 
 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)
 
         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._grid = ref(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
 
@@ -106,7 +106,7 @@ class Grid(object):
 
     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):
@@ -144,21 +144,19 @@ class Grid(object):
 
     ### Accessors
 
 
     ### 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
 
 
     ### Constructors
 
@@ -170,9 +168,9 @@ class Grid(object):
         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)
 
@@ -190,8 +188,8 @@ class Grid(object):
 
         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
@@ -229,8 +227,8 @@ class Grid(object):
 
         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
@@ -246,7 +244,7 @@ class Grid(object):
             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))
index fb700bd..a3593df 100644 (file)
@@ -16,8 +16,8 @@ class GridRenderer(object):
         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))
@@ -30,7 +30,7 @@ class GridRenderer(object):
 
         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))
index aab6c36..3a49134 100644 (file)
@@ -21,8 +21,8 @@ class AsciiArtGridRenderer(SquareGridRenderer):
     """
 
     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 '|'