From bfc07962deafde17e2b41f8d659313b7746dca07 Mon Sep 17 00:00:00 2001 From: Eevee Date: Fri, 12 Dec 2008 19:03:06 -0800 Subject: [PATCH] Turned pseudoku.py into a real project. Gave it a setup.py. Broke it into a main script and a couple modules. Added a .gitignore for vim/python cruft. Otherwise, works exactly the same. --- .gitignore | 3 + lib/pseudoku/__init__.py | 40 +++++++++ pseudoku.py => lib/pseudoku/grid/__init__.py | 120 +-------------------------- lib/pseudoku/grid/cellgroup.py | 96 +++++++++++++++++++++ setup.py | 13 +++ 5 files changed, 154 insertions(+), 118 deletions(-) create mode 100644 .gitignore create mode 100644 lib/pseudoku/__init__.py rename pseudoku.py => lib/pseudoku/grid/__init__.py (76%) create mode 100644 lib/pseudoku/grid/cellgroup.py create mode 100644 setup.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..150b12c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.swp +*.pyc +*.egg-info/ diff --git a/lib/pseudoku/__init__.py b/lib/pseudoku/__init__.py new file mode 100644 index 0000000..83abfc3 --- /dev/null +++ b/lib/pseudoku/__init__.py @@ -0,0 +1,40 @@ +from grid import Grid + +def main(): + grid = Grid.from_string(""" + ...69.... + 9.5..876. + ..4..1.2. + 6...5...3 + 38.....49 + 7...3...2 + .7.9..3.. + .231..4.8 + ....83... + """) + + print grid + + grid.solve() + + print grid + + + + grid = Grid.from_string(""" + 3...2...7 + ...3.1... + ..6...4.. + 89.....54 + 4...5...8 + 61.....32 + ..8...2.. + ...5.9... + 1...6...9 + """) + + print grid + + grid.solve() + + print grid diff --git a/pseudoku.py b/lib/pseudoku/grid/__init__.py similarity index 76% rename from pseudoku.py rename to lib/pseudoku/grid/__init__.py index d48c656..d1906ae 100644 --- a/pseudoku.py +++ b/lib/pseudoku/grid/__init__.py @@ -4,106 +4,13 @@ from math import sqrt import re from weakref import proxy -symbols = [str(x + 1) for x in range(9)] + [chr(x + 97) for x in xrange(26)] +from cellgroup import Row, Column, Box +symbols = [str(x + 1) for x in range(9)] + [chr(x + 97) for x in xrange(26)] class GridSizeError(Exception): pass -class CellGroup(object): - """Represents any group of cells in a grid.""" - - ### Accessors - - cells = [] - - ### Methods - # XXX inherited __init__ - - def find_value(self, value): - """Returns the cells that can be a specific value.""" - possible_cells = [] - for cell in self.cells: - if value in cell._values: - possible_cells.append(cell) - - if len(possible_cells) == 0: - raise Exception # XXX - - return possible_cells - - def resolve_uniques(self): - for value in xrange(self._grid._size): - # XXX cache values that are taken care of - possible_cells = self.find_value(value) - - if len(possible_cells) > 1: - # Not unique - continue - - target_cell = possible_cells[0] - if target_cell.solved: - # Already seen this - # XXX this is what cache is for - continue - - # Only cell in the group that can be value - target_cell.set(value) - - -class Box(CellGroup): - def _get_box_row(self): - return self._pos // self._grid._box_width - box_row = property(_get_box_row) - - def _get_box_column(self): - return self._pos % self._grid._box_height - box_column = property(_get_box_column) - - def _get_cells(self): - # XXX generator + docstring - cells = [] - for row in xrange(self._grid._box_height): - for col in xrange(self._grid._box_width): - cell_row = row + self.box_row * self._grid._box_height - cell_col = col + self.box_column * self._grid._box_width - cells.append(self._grid.cell(cell_row, cell_col)) - return cells - cells = property(_get_cells) - - def __init__(self, grid, position): - self._grid = proxy(grid) - self._pos = position - - -class Row(CellGroup): - def _get_cells(self): - # XXX generator + docstring - cells = [] - for col in xrange(self._grid._size): - cells.append(self._grid.cell(self._pos, col)) - return cells - cells = property(_get_cells) - - def __init__(self, grid, position): - self._grid = proxy(grid) - self._pos = position - - -class Column(CellGroup): - def _get_cells(self): - # XXX generator + docstring - cells = [] - for row in xrange(self._grid._size): - cells.append(self._grid.cell(row, self._pos)) - return cells - cells = property(_get_cells) - - def __init__(self, grid, position): - self._grid = proxy(grid) - self._pos = position - - class Cell(object): """Represents a single cell/value within a sudoku grid.""" @@ -211,8 +118,6 @@ class Cell(object): return '.' - - class Grid(object): """Represents a Sudoku grid.""" @@ -334,7 +239,6 @@ class Grid(object): # Figure out the length of one side/box size = int(sqrt(len(grid))) if size ** 2 != len(grid): - # XXX raise GridSizeError("Provided string does not form a square") # Set height/width.. @@ -413,23 +317,3 @@ class Grid(object): res += "\n" return res - - -grid = Grid.from_string("..... ..... ..... ..... ....") -grid = Grid.from_string(""" - ...69.... - 9.5..876. - ..4..1.2. - 6...5...3 - 38.....49 - 7...3...2 - .7.9..3.. - .231..4.8 - ....83... -""") - -print grid - -grid.solve() - -print grid diff --git a/lib/pseudoku/grid/cellgroup.py b/lib/pseudoku/grid/cellgroup.py new file mode 100644 index 0000000..863ca4e --- /dev/null +++ b/lib/pseudoku/grid/cellgroup.py @@ -0,0 +1,96 @@ +from __future__ import division + +from weakref import proxy + +class CellGroup(object): + """Represents any group of cells in a grid.""" + + ### Accessors + + cells = [] + + ### Methods + # XXX inherited __init__ + + def find_value(self, value): + """Returns the cells that can be a specific value.""" + possible_cells = [] + for cell in self.cells: + if value in cell._values: + possible_cells.append(cell) + + if len(possible_cells) == 0: + raise Exception # XXX + + return possible_cells + + def resolve_uniques(self): + for value in xrange(self._grid._size): + # XXX cache values that are taken care of + possible_cells = self.find_value(value) + + if len(possible_cells) > 1: + # Not unique + continue + + target_cell = possible_cells[0] + if target_cell.solved: + # Already seen this + # XXX this is what cache is for + continue + + # Only cell in the group that can be value + target_cell.set(value) + + +class Box(CellGroup): + def _get_box_row(self): + return self._pos // self._grid._box_width + box_row = property(_get_box_row) + + def _get_box_column(self): + return self._pos % self._grid._box_height + box_column = property(_get_box_column) + + def _get_cells(self): + # XXX generator + docstring + cells = [] + for row in xrange(self._grid._box_height): + for col in xrange(self._grid._box_width): + cell_row = row + self.box_row * self._grid._box_height + cell_col = col + self.box_column * self._grid._box_width + cells.append(self._grid.cell(cell_row, cell_col)) + return cells + cells = property(_get_cells) + + def __init__(self, grid, position): + self._grid = proxy(grid) + self._pos = position + + +class Row(CellGroup): + def _get_cells(self): + # XXX generator + docstring + cells = [] + for col in xrange(self._grid._size): + cells.append(self._grid.cell(self._pos, col)) + return cells + cells = property(_get_cells) + + def __init__(self, grid, position): + self._grid = proxy(grid) + self._pos = position + + +class Column(CellGroup): + def _get_cells(self): + # XXX generator + docstring + cells = [] + for row in xrange(self._grid._size): + cells.append(self._grid.cell(row, self._pos)) + return cells + cells = property(_get_cells) + + def __init__(self, grid, position): + self._grid = proxy(grid) + self._pos = position diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..a13a3a0 --- /dev/null +++ b/setup.py @@ -0,0 +1,13 @@ +from setuptools import setup, find_packages +setup( + name = 'Pseudoku', + version = '0.0', + package_dir = {'': 'lib'}, + packages = find_packages('lib'), + + entry_points = { + 'console_scripts': [ + 'pseudoku = pseudoku:main', + ], + }, +) -- 2.7.4