[Zope3-Users] PyWebOff

Bernd Dorn zope-mailinglist at mopa.at
Mon Mar 20 13:18:57 EST 2006


On 03.03.2006, at 08:27, Shane Hathaway wrote:

> Hello,
>
> I've been assigned to present Zope in a local upcoming Python user  
> group meeting.  As part of the assignment, I'm supposed to solve  
> the PyWebOff challenge using Zope:
>
> http://pyre.third-bit.com/pyweb/challenge.html
>
> I'd like to do this using Zope 3.  However, I'm really struggling.   
> I feel like I must be using Zope 3 in a really dumb way, because  
> the code so far is highly repetitive, completely dependent on Zope,  
> and more XML than Python.  This is not going to go over well in the  
> presentation.
>
> Can anyone tell me how to do this better with Zope 3?  Maybe my  
> Zope 2 experience is preventing me from seeing something obvious.   
> I've attached the __init__.py and configure.zcml that I created in  
> a package called "challenge".
>
> Shane
>
> from zope.interface import Interface, implements
> from zope.schema import Field, TextLine, Int, Date
> from zope.app.container.interfaces import IContained, IContainer
> from zope.app.container.constraints import ItemTypePrecondition
> from zope.app.container.constraints import ContainerTypesConstraint
> from zope.app.container.btree import BTreeContainer
>
>
> class IBook(Interface):
>     title = TextLine(title=u'title')
>     year = Int(title=u'year')
>     authors = TextLine(title=u'authors')
>
> class ILoan(Interface):
>     username = TextLine(title=u'username')
>     day = Date(title=u'day')
>
> class ILibrary(IContainer):
>     def __setitem__(name, object):
>         pass
>     __setitem__.precondition = ItemTypePrecondition(IBook, ILoan)
>
> class IBookContained(IContained):
>     __parent__ = Field(
>         constraint = ContainerTypesConstraint(ILibrary))
>
> class ILoanContained(IContained):
>     __parent__ = Field(
>         constraint = ContainerTypesConstraint(ILibrary))
>
> class Book(object):
>     implements(IBook, IBookContained)
>
> class Loan(object):
>     implements(ILoan, ILoanContained)
>
> class Library(BTreeContainer):
>     implements(ILibrary)
>

if you use v 3.2 then you should use
from zope.app.container.constraints import contains, containers  
instead of the precondition stuff, so you don't need  
that ...Contained interfaces, because you can define the interfaces  
as strings

well, this just reduces the python code, but looks much cleaner

e.g.

class IBook(IContained):

     title = TextLine(title=u'title')
     year = Int(title=u'year')
     authors = TextLine(title=u'authors')

     containers('challange.interfaces.ILibrary')















More information about the Zope3-users mailing list