Skip to content

Instantly share code, notes, and snippets.

@yatt
Created June 20, 2011 16:38
Show Gist options
  • Save yatt/1035960 to your computer and use it in GitHub Desktop.
Save yatt/1035960 to your computer and use it in GitHub Desktop.
django parameter type decorator
#!/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