[Zope3-dev] interface insight

Steve Alexander steve@cat-box.net
Fri, 11 Jul 2003 22:38:22 +0300


Tim Hicks wrote:
> Hi,
> 
> I've been lurking here for a while and this is my first outing on zope3-dev.
> 
> I'm just starting to play around with zope3's interface package and I have a couple
> of questions that I hope you guys can help me with.
> 
> 1) I'm playing with some classes to access IMAP servers and manage emails. 
> Currently, I have defined two sets of interfaces along the lines of: ImyImapClass
> and ICachingMyImapClass.
> The two interfaces are essentially the same (although the caching ones have a couple
> of methods like 'flush_cache' and so on), except for the fact that nearly all of the
> methods in ICaching* take an extra optional argument 'force' which forces the method
> to ignore the cache and fetch data straight from the server.
> 
> So, to my question.  Should I define the non-caching interface methods with the
> optional force=0 argument so that the caching interface can just inherit all the
> methods (of which there are quite a lot) - explaining that the force argument is
> redundant for non-caching instances in the doc-string?  Or should I basically write
> out everything twice, but with the force argument in the caching definition?
> 
> I guess this is just a style thing as both will work, but hey, there's no harm in
> asking :-).

I'd do it like this:

   class IImapConnection(Interface):

       def connect(server, port):
           '''Connect to an imap server'''

       def sendCommand(command):
           '''Send the given command'''

   class ICachedImapConnection(IImapConnection):

       def sendCommand(command, ignore_cache=False):
           '''Send the given command.

           You can optionally ignore the cache.
           '''

       def flushCache():
           '''Flush the cached messages'''


I made up the names of the methods and the arguments, but this should 
give you the idea. Note how the sendCommand method of 
ICachedImapConnection is an extended version of the one in 
IImapConnection, but is still backwards-compatible (or rather upwards, 
compatible, if you consider the "generalised" direction to be up 
relative to the "specialised" direction).


> 2) Is it possible/easy to compile interface/_zope_interface_ospec.c without
> installing the whole of zope3?  I'm guessing 'yes', but I just get scared when I see
> C and a compiler ;-).


I don't know. Compiling Zope 3 should be easy enough.
Then again, as the interfaces package is meant to be independently 
distributable, it should have its own setup.py file.

You can use the interfaces package without the C library by commenting 
out a couple of imports. This "graceful failure in the absence of C 
optimisations" could be made automatic.

--
Steve Alexander