Skip to content

Instantly share code, notes, and snippets.

@yanolab
Created April 17, 2012 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yanolab/2405328 to your computer and use it in GitHub Desktop.
Save yanolab/2405328 to your computer and use it in GitHub Desktop.
argment type guard
# -*- coding: utf-8 -*-
import operator
def guard(*guardArgs, **guardKw):
def _argGuard(f):
def _inner(*args, **kw):
argTypes = map(lambda x: type(x), args)
if not all(map(operator.eq, guardArgs, argTypes)):
raise Exception("Type Error: not match type {0}, {1}".format(guardArgs, argTypes))
for key in kw:
argType = guardKw.get(key, None)
if argType is None:
raise Exception("Type not defined: {0}".format(key))
if not operator.eq(argType, type(kw[key])):
raise Exception("Type Error: not match type {0}".format(key))
return f(*args, **kw)
return _inner
return _argGuard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment