On 12 Jan 2008, at 03:35, kapil wrote:

>
> hi folks,
>
> i'm trying to refactor a set of resource controllers to use a common
> base class. all the subclasses need customized validators, and
> authorization but their method implementations are generic. both
> validation and authorization where previously applied using
> decorators. what i'm trying to figure out is how to apply the
> decorators to the subclass when their inheriting the method that they
> need to apply to. ideally i'd rather not redefine the methods just to
> call the base implementation, as it defeats the purpose of eliminating
> duplicate code. i would prefer instead to retrieve the function apply
> the decorator and place the result in the subclass after its
> construction.
>
> class GenericResourceController( BaseControlller ):
>
>   def index( self ):
>         ...
>
> class PizzaController( GenericResourceController ):
>
>   form_schema = ...
>   permission = ....
>
> pc = PizzaController
>
> class SandwichController( GenericResourceController ):
>  form_schema = ...
>  permission = ...
>
> sc = SandwhichController
>
> and then do something along the lines of... (not working.. )
>
> PizzaController.index = authorize( validator( pc.index.im_func,
> pc.form_schema ), pc.permission )
> SandwhichController.index = authorize( validator( sc.index.im_func ),
> sc.permission )
>
> is this possible?
>
> thanks,
>
> kapil
>

Hey,

If you're trying to do what I think you are, I'd probably just do this  
using auxiliary methods.

class Base:
        @decorator
        def foo(self):
                self._do_foo()

        def _do_foo(self):
                <whatever>

class Sub(Base):
        def _do_foo(self):
                <whatever>

then you can have lovely generic methods and just add in an auxiliary  
method at any point where you need to change behaviour depending on  
subclass.

Cheers,

Steven


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to