Random ...
 
December 2017
S M T W T F S
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Tags ...
Links



python+zope++:: Zudoku - a zope wrapper to SuSolver.py
Posted at 05.Jul,2005 12:28  Comments 2 / Trackbacks 0 / Like this post!
Technorati tag(s):

Sudoku is breaking out here in Malaysia. Briefly, sudoku is a numbers game, where one needs to arrange numbers 1-9 in a 9x9 grid so that there are only a number in each row and column.

SuSolver.py is a python script that solves, or tries to solve, the sudoku puzzle. Zudoku is a zope wrapper. Why? For me to learn zope/python again and for the heck of it :))

As usual there are questions to the way to make the wrapper, and I will put it up for input from pythonistas.

To use SuSolver.py:

    $ python 
    Python 2.3.5 (#1, May 24 2005, 15:52:33)
    [GCC 3.4.3 20050227 (Red Hat 3.4.3-22.fc3)] on linux2
    Type "help", "copyright", "credits" or "license" for more   information.
    >>> import SuSolver
    >>> pattern=SuSolver.example
    >>> pattern
    ['-31--78--', '9---2---1', '7----8-5-', '---8---32', '8-34---6-',  
     '46---1--5', '-5-2-----', '-79-4---8', '--49--71-']
    >>> sos=SuSolver.SuSolver()
    >>>for i in range(9):
    ...      sos.fixRow(i,pattern[i])
    ...
    >>> while sos.removeSingleton():
    ...        pass
    ...
    >>>sos.printGrid()  #will print the solution

Here's Zudoku.py, the zope wrapper:

   class Zudoku(SuSolver, Item, Persistent, Implicit, RoleManager):
      """SuSolver wrapper"""

      meta_type = 'Zudoku'

      security = ClassSecurityInfo()

      manage_options=(
        Item.manage_options + RoleManager.manage_options
          )

      def __init__(self,id, title, pattern=[]):
          """init with pattern"""
          SuSolver.__init__(self)
          self.id = id
          self.title = title
          self.pattern=pattern
          self.genStuff()

      def genStuff(self):
          """gen all necessary stuff"""
          for i in range(9):
              self.fixRow(i, self.pattern[i])
          while self.removeSingleton():
              pass

Now, my question: is it more pythonic doing it as above? or for me to do this [1], where instead of subclassing SuSolver, I instantiate it during zope instantiation? (see _postinit):

   [1]
     def manage_addZudoku(self, id,  title='',pattern=[],REQUEST=None):
        """the add method"""
        ob = Zudoku(id, title, pattern)
        self._setObject(id,ob)
        ob=getattr(self,id)
        ob._postinit()
        if REQUEST is not None:
            return REQUEST.RESPONSE.redirect('manage_workspace')

     class Zudoku(Item, Persistent, Implicit, RoleManager):
        """SuSolver wraper"""

        meta_type = 'Zudoku'

        security = ClassSecurityInfo()

        manage_options=(
            Item.manage_options + RoleManager.manage_options
            )

        def __init__(self,id, title, pattern=[]):
            """init with pattern"""
            self.id = id
            self.title = title
            self.pattern=pattern

        def _postinit(self):
            sinst = SuSolver()
            for i in range(9):
                sinst.fixRow(i, self.pattern[i])
            while sinst.removeSingleton():
                pass
            self.grid = sinst.grid

Both would work. The first is more pythonic, methinks.

Later, I'll try and use Five and see whether making zope product is made simpler. A good time for me to take up Five.

There's currently no python sudoku composer, so I need to get one from the archives, or use Composer (a java sudoku composer at sudoku.sf.net)

Anybody knows of one? or want to create one?


Bookmark and Share

Is this entry helpful? Comments/Donate/Click some google ads.  
Trackback is http://myzope.kedai.com.my/blogs/kedai/33/tbping 

Comments
Re: Zudoku - a zope wrapper to SuSolver.py

I think I've written a very simple little sudoku composer in one python class, if that's what you need. It currently just populates the grid.

Ag Stephens  at 07.Jul,2005 17:12
Re: Zudoku - a zope wrapper to SuSolver.py

cool! that'd be a great addition to the current package.

how can I get a hold of it?

tia

kedai  at 07.Jul,2005 23:37
Post a comment