multiple python decorators retrieve original function -


i have class running checks, each function performs check. have thought decorators add conditions, maybe not wise.

i understand concept of decorators, each taking input function decorates , returning next 1 function decorated. working instance functions returning strings, output can edited decorator , returned next decorator.

what want modify function attributes, putting instance don't exec flag checks not run, require auth checks performed if authentication has been granted, or change function attribute order, sequence check launch.

# return f if is_authenticated flag true def auth_required(is_authenticated):     def check_authentication(f):         if is_authenticated:             return f     return check_authentication  # edit order variable def assignorder(order):     def do_assignment(f):         f.order = order         return f     return do_assignment # instanciate checks class , provide authentication; # login , password tried , is_authenticat flag set accordingly c = checks(target, login, password)  # sort on order variable , launch checks functions = sorted(     [         getattr(c, field) field in dir(c)         if hasattr(getattr(c, field), 'order')     ],key = (lambda field : field.order)  ) function in functions:     function()  # assign decorators order variable set  # auth_required triggers launch if auth performed @auth_required(true) @assignorder(100) def check_shares(self):     # check defined here 

this working great assignorder, , checks launched in proper order.

but @auth_required applied assignorder not want.

is there way retrieve original function decorated? or usage of decorator not relevant in case? , solution?

many thanks

there no generic way of solving problem. decorators have no built-in facility cooperative, , don't need retain reference original function.

so need come protocol yourself, e.g. making assignorder save original function do_assignment.

then in is_authenticated, have if there "real" function behind passed one, , use instead.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -