Created
April 17, 2012 11:08
-
-
Save yanolab/2405328 to your computer and use it in GitHub Desktop.
argment type guard
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
# -*- 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