[Grok-dev] Re: Does it make sense: registering factories that are functions not classes

Darryl Cousins darryl at darrylcousins.net.nz
Tue Jun 5 16:00:58 EDT 2007


Hi,

On Tue, 2007-06-05 at 17:44 +0200, Jan-Wijbrand Kolman wrote:
> > I checked out the branch and tried the decorators. It seems then you can
> > only use a method for the adaptor. Is this expected or is there a way to
> > use classes that I'm missing? For example to get ISized I needed to use
> > the method to return a class:
> >
> > @grok.adapter(JanW)
> > @grok.implementer(ISized)
> > def sizedForJanW(context):
> >         return ActualSizedAdapterForJanW(context)
> >
> > Which gets me an adapter class *and* a decorated method. Surely just as
> > easy to use an adapter sub-classing grok.Adapter. From memory all my
> > adapters to date need to be classes.
> >
> > I'm sure I'm missing something and am keen to be enlightened.
> 
> It is complementary to grok.Adapter.

Ahh. Now I understand.

The couple of simple cases that I made to try out the decorators worked
as expected.

I'm not really qualified to review your code but ...

+1 to merge.

Thanks for the info.

Best regards,
Darryl

> Sometimes its very useful to have a simple function that acts as an
> adapter factory. That is what this decorator notation is for. One
> real-world example for which this could be useful is:
> 
>   vocabulary = SimpleVocabulary.fromValues(('a', 'b', 'c', 'd', 'e'))
> 
>   @grok.adapter(ISet, IBrowserRequest)
>   @grok.implementer(IInputWidget)
>   def selection_widget(field, request):
>       return MultiCheckBoxWidget(field, field.value_type.vocabulary, request)
> 
>   class Index(grok.Form):
> 
>       form_fields = grok.Fields(
>           selection=schema.Set(
>               title=u"Selection",
>               value_type=schema.Choice(vocabulary=vocabulary))
>           )
> 
>       @grok.action('Submit')
>       def handle_submit(self, **data):
>           pass
> 
> But see also the start post for this thread.
> 
> kind regards,
> jw



More information about the Grok-dev mailing list