Created
June 20, 2011 16:38
-
-
Save yatt/1035960 to your computer and use it in GitHub Desktop.
django parameter type decorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# coding: utf-8 | |
# ref: http://stackoverflow.com/questions/582056/getting-list-of-parameters-inside-python-function | |
import inspect | |
def base(name, ptype, required): | |
def deco(fun): def gun(*args, **kwargs): frame = inspect.currentframe() _, _, _, values = inspect.getargvalues(frame) try: if required: values[name] = ptype(values[name]) | |
except Exception, e: | |
raise e | |
return fun(*args, **kwargs) | |
return gun | |
return deco | |
def required(name, ptype): | |
return base(name, ptype, True) | |
def option(name, ptype): | |
return base(name, ptype, False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment