[Grok-dev] model schema declaration

Leonardo Rochael Almeida leorochael at gmail.com
Sun Jul 15 10:43:29 EDT 2007


The other day, Luciano and Martijn where talking about the deprecated
inner class idiom for schema declaration and this made me think of
ways of making the schema declaration more lightweght, specially since
I was reading the new section on CRUD that the Brazillian grok
sprinters wrote, and then it struck me:

If a module contains a model, and an interface with the same name as
the model but with an "I" before the name, this would automatically
mean the model implements the interface. For instance, in the example
below, the "interface.implements(IMammoth)" would be unnecessary as it
would be declared automatically when the module is grokked:

 class IMammoth(interface.Interface):
     name = schema.TextLine(title=u"Name", required=True)
     weight = schema.Int(title=u'Weight', min=0)

 class Mammoth(grok.Model):
     interface.implements(IMammoth) # <-- the default when the class is grokked
     def __init__(self, name, weight):
         self.name = name
         self.weight = weight

Taking the conv-over-conf mantra even further, we could make it so
that the above interface wouldn't even need to be in the same module
as the model class, as long as it was in the top-level of the
"interfaces" package/module that is a neighbour to the model module.
However, this might be taking the CoC mantra too far.

One disadvantage with this approach, however, is that the interface
wouldn't be associated at "module import" time, but only at "grokking"
time, which might be okay most of the time, but which would probably
mean that the "interface-association" grokker has to be one of the
first to run. And it could make reuse of the class without grokking
the module slightly more involved, if this use requires knowing the
interface association.

Anyway, just an idea. And I wouldn't be too surprised if this was
implemented already (I didn't test).

Cheers, Leo

More information about the Grok-dev mailing list