I just started playing with decorators in Python. I am currently having trouble figuring out the best possible way to handle this use cases. Since a decorated function cannot access local variables from the decorator.
schema = {
'lang': {'type': 'string', 'required':True}
}
def validate(schema):
def my_decorator(func):
def wrapper(*args, **kwargs):
# Read from request body args[1]
# Validate the json is in fact correct
valid_json = json.loads(body.decode('utf-8'))
# Compare valid_json to the schema using "Cerberus"
return args, kwargs
return wrapper
return my_decorator
@validate(schema)
def main_function(self, req, resp):
# call database with valid_json
My question is: How do I access valid_json from my decorated function to be able to insert into my database afterwards. What is the best practice for this situation?
Edit: I am running pypy 2.4