[Zope-dev] an Interface expressing a list of generator function
Hello, I have two objects which interoperate to solve a sudoku: the Sudoku class and a vector (a list actually) of generator functions. The Sudoku interface is simple to write as it requires three functions and an attribute. class ISudoku: board = Attribute() def free(col, line): frees the slot at position col, line def set(col, line, value): Sets the value in the slot at position col, line def candidates(col, line): Returns the candidate digits for the slot at position col,line But how to express vector of generator functions with Zope interfaces? I want to express the possibility to access the vector object with the [n] index notation, and this returns a function returning a generator (an object with an iter and next function). Thank you for your help, PS: the vector of generator functions is the input of the algorithm called conjoin, used to solve the eight queen problem and knight's tour problem. It is described in the Python sources (Python-2.6.5/Lib/test/test_generators.py). PPS: I say vector instead of list even when the vector is a list, because list usually implies the possibility to append elements dynamically which is not pertinent in this problem. The length of the vector won't change and is equal to the number of slots on a sudoku board. ___ Zope-Dev maillist - Zope-Dev@zope.org https://mail.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - https://mail.zope.org/mailman/listinfo/zope-announce https://mail.zope.org/mailman/listinfo/zope )
Re: [Zope-dev] an Interface expressing a list of generator function
Ok, If I attach an item_type to the the list, I do not think I will able to use the native Python list, and should use a subclass instead. It is a bit heavyweight, especially when the intent of the use of the interface was documentation. List and generators are very common in Python and are expressed in very few and clear characters, I thought there would be a shorter way to document their interfaces. Thank you, On Mon, Nov 8, 2010 at 2:24 PM, Tres Seaver tsea...@palladion.com wrote: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 11/08/2010 04:40 AM, Jean-Daniel wrote: Hello, I have two objects which interoperate to solve a sudoku: the Sudoku class and a vector (a list actually) of generator functions. The Sudoku interface is simple to write as it requires three functions and an attribute. class ISudoku: board = Attribute() def free(col, line): frees the slot at position col, line def set(col, line, value): Sets the value in the slot at position col, line def candidates(col, line): Returns the candidate digits for the slot at position col,line But how to express vector of generator functions with Zope interfaces? I want to express the possibility to access the vector object with the [n] index notation, and this returns a function returning a generator (an object with an iter and next function). Thank you for your help, PS: the vector of generator functions is the input of the algorithm called conjoin, used to solve the eight queen problem and knight's tour problem. It is described in the Python sources (Python-2.6.5/Lib/test/test_generators.py). PPS: I say vector instead of list even when the vector is a list, because list usually implies the possibility to append elements dynamically which is not pertinent in this problem. The length of the vector won't change and is equal to the number of slots on a sudoku board. I would specify the contracts omething like so:: class IVector(Interface): item_type = Attribute(uInterface of items.) def __len__(): Fixed length of vector. def __getitem__(index): Return the 'index'th item. Items conform to 'item_type'. 'index' must be an integer between 0 and __len__() - 1, else raise IndexError. class IIterable(Interface): def __iter__(): Return an iterator for this object. - -- === Tres Seaver +1 540-429-0999 tsea...@palladion.com Palladion Software Excellence by Designhttp://palladion.com -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.10 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkzX+ggACgkQ+gerLs4ltQ7WQwCeNu4lF+WysIjSYM86EjMhoPFH 9ZsAnRB7T4Pz+bIVeVYxHbvRYaqN6iE7 =U5bo -END PGP SIGNATURE- ___ Zope-Dev maillist - Zope-Dev@zope.org https://mail.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - https://mail.zope.org/mailman/listinfo/zope-announce https://mail.zope.org/mailman/listinfo/zope ) ___ Zope-Dev maillist - Zope-Dev@zope.org https://mail.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - https://mail.zope.org/mailman/listinfo/zope-announce https://mail.zope.org/mailman/listinfo/zope )
Re: [Zope-dev] an Interface expressing a list of generator function
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.htmlhttp://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 ___ Zope-Dev maillist - Zope-Dev@zope.org https://mail.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - https://mail.zope.org/mailman/listinfo/zope-announce https://mail.zope.org/mailman/listinfo/zope )
Re: [Zope-dev] an Interface expressing a list of generator function
The end of the last message was not finished... Ideally, here is (maybe) what I wish I could write: from zope.interface import IList, IGenerator, implements, requires def make_generator_functions(): implements(IList(IGenerator)) [...] def stack_assumption(gen_funcs): requires(IList(IGenerator), gen_funcs) [ ... ] Regards, ___ Zope-Dev maillist - Zope-Dev@zope.org https://mail.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - https://mail.zope.org/mailman/listinfo/zope-announce https://mail.zope.org/mailman/listinfo/zope )
[Zope-dev] Examples of ZODB.event
Hello, I would like to build a config repository on the ZODB for an appliance where many processes would boot and talk to at boot to load their configuration. Some processes might modify or add new conf, and some different component should take this change of configuration into account. Is there any ZODB.event example app that I could read from? ZODB.event is a little too low level for me. I wish the subscribers would get a notification message like: - property modified. Ex: '(VideoComponent', 'server_port') - original value, new value. Ex: '8080', '1234' Thanks, bye ___ Zope-Dev maillist - Zope-Dev@zope.org https://mail.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - https://mail.zope.org/mailman/listinfo/zope-announce https://mail.zope.org/mailman/listinfo/zope )
Re: [Zope-dev] Examples of ZODB.event
Ok thanks, I can implement that in the controler code wrapping the ZODB. The Model-View-Controller On Mon, Feb 11, 2013 at 6:21 PM, Jim Fulton j...@zope.com wrote: On Mon, Feb 11, 2013 at 11:45 AM, Jean-Daniel jeandaniel.bro...@gmail.com wrote: Hello, I would like to build a config repository on the ZODB for an appliance where many processes would boot and talk to at boot to load their configuration. Some processes might modify or add new conf, and some different component should take this change of configuration into account. Is there any ZODB.event example app that I could read from? ZODB.event is a little too low level for me. I wish the subscribers would get a notification message like: - property modified. Ex: '(VideoComponent', 'server_port') - original value, new value. Ex: '8080', '1234' This isn't a feature in ZODB yet. Sorry. When there is such a feature, it will be lower-level than that. You'll be able to find out that an object changed, but not what part of the object changed. Jim -- Jim Fulton http://www.linkedin.com/in/jimfulton Jerky is better than bacon! http://zo.pe/Kqm ___ Zope-Dev maillist - Zope-Dev@zope.org https://mail.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - https://mail.zope.org/mailman/listinfo/zope-announce https://mail.zope.org/mailman/listinfo/zope )
Re: [Zope-dev] Examples of ZODB.event
Ok thanks, I can implement that in the controller code wrapping the ZODB. The Model-View-Controller says the model can update the views. I find it hard actually find a model which really has the the capability to notify the views directly. What we usually get, unlike the diagram from Wikipedia, is that the views only talk to the controller, which only talks to the model. Wikipedia calls this variant the passive MVC. http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Regards, On Tue, Feb 12, 2013 at 7:38 AM, Jean-Daniel jeandaniel.bro...@gmail.comwrote: Ok thanks, I can implement that in the controler code wrapping the ZODB. The Model-View-Controller On Mon, Feb 11, 2013 at 6:21 PM, Jim Fulton j...@zope.com wrote: On Mon, Feb 11, 2013 at 11:45 AM, Jean-Daniel jeandaniel.bro...@gmail.com wrote: Hello, I would like to build a config repository on the ZODB for an appliance where many processes would boot and talk to at boot to load their configuration. Some processes might modify or add new conf, and some different component should take this change of configuration into account. Is there any ZODB.event example app that I could read from? ZODB.event is a little too low level for me. I wish the subscribers would get a notification message like: - property modified. Ex: '(VideoComponent', 'server_port') - original value, new value. Ex: '8080', '1234' This isn't a feature in ZODB yet. Sorry. When there is such a feature, it will be lower-level than that. You'll be able to find out that an object changed, but not what part of the object changed. Jim -- Jim Fulton http://www.linkedin.com/in/jimfulton Jerky is better than bacon! http://zo.pe/Kqm ___ Zope-Dev maillist - Zope-Dev@zope.org https://mail.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - https://mail.zope.org/mailman/listinfo/zope-announce https://mail.zope.org/mailman/listinfo/zope )