[Zope-dev] an Interface expressing "a list of generator function"
Jean-Daniel
jeandaniel.browne at gmail.com
Tue Nov 9 10:44:35 EST 2010
>
> What exactly are you hoping to accomplish?
>
In brief, I wish there were a concise and clear way to express that an
object implements a "list of generator functions", in the context of CS
students learning the concept of interface.
I am writing an article on a sudoku solver for an audience of CS
students, They do not know much of Python, have recently heard about
recursivity and the yield keyword was not obvious for them. The goal is that
they meet a nice collaboration between three objects, in Python, to
manipulate the concepts of code reuse and interface.
There is 1. a Sudoku class modelling a sudoku board and rules,
2. a backtracking algorithm,
3. and a function which returns the data structure (a list of generator
functions) used by the backtracking algorithm, and which uses an instance of
the Sudoku class,
(The solver code can be read at
jdb.github.com/modules/sudoku.html<http://jdb.github.com/modules/sudoku.html#Sudoku>,
a draft of the article is at : jdb.github.com/sudoku.html, the nice
backtracking algorithm comes from the conjoin algorithm described in
test_generators.py in the Python sources)
As the naive implementation the students would write is one big fat
function, I have to convince them that going through a more sophisticated
design is a better path. The idea is to exemplify code reuse by swapping the
Sudoku class with an optimized equivalent class, for instance. Also the same
backtracking algorithm can "cook" a solution to the eight queens problems or
the knight's tour problem, without modification but given the adapted list
of generator functions.
The interfaces help designing formalizing the collaboration between objects
and the article wishes to convey this idea. The Sudoku interface is nice and
clean but explaining the interface for a "list of generator functions" is
likely to raise more questions from the student than it brings clarity.
The interface will require the students to know that "list" and its bracket
notation implies the __getitem__ function, and that generator implies
__iter__ and next(). Roughly a dozen elegant lines are needed to implement
the interface with the Python keywords and syntax but the expected
__getitem__, __iter__ and next() will be absent from the implementation
which uses "[]" and "yield".
I understand that the interfaces presented by Tres Seaver will work well for
the purpose of code reuse in a real application, and it is the way to go if
I want to use a global registry and/or a validation of the interface
provided by a class. At this point, I think it is easier to explain the
interface in plain english in the documentation with the help of the sphinx
documentation directives extracting the public methods of the class.
Here is (maybe) what I wish I could write:
"""
from zope.interface import IList, IGenerator, Interface, provides, validates
def make_generator_functions():
[...]
make_generator_functions = , make_generator_functions)
def stack_assumption(gen_funcs):
validates(IList(IGenerator), gen_funcs)
[ ... ]
"""
Thanks for your attention,
> Jim
>
> --
> Jim Fulton
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.zope.org/pipermail/zope-dev/attachments/20101109/232b8b40/attachment.html
More information about the Zope-Dev
mailing list