Created
February 12, 2012 17:57
-
-
Save kalloc/1809922 to your computer and use it in GitHub Desktop.
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
from itertools import izip, chain | |
from functools import * | |
from inspect import * | |
import sys | |
def accepts(**test_kwargs): | |
def check(param, cls): | |
if type(cls) not in (list, set, tuple): | |
cls = (cls,) | |
values = filter(lambda el: type(el).__name__ not in ('type', 'classobj'), cls) | |
cls = filter(lambda el: type(el).__name__ in ('classobj', 'type'), cls) | |
if values and param in values: | |
return True | |
if cls and isinstance(param, cls): | |
return True | |
return False | |
def wrap_func(fn): | |
argspec = getargspec(fn) | |
@wraps(fn) | |
def wrap_cls(*wrap_args, **wrap_kwargs): | |
args = getcallargs(fn, *wrap_args, **wrap_kwargs) | |
print 'call accepts.wrap_func.wrap_cls', fn | |
print '\ttype of fn %r' % type(fn) | |
print '\ttest_kwargs - %r' % (test_kwargs,) | |
print '\targs - %r' % args | |
for key in test_kwargs.keys(): | |
assert check(args[key], test_kwargs[key]), 'accept error for %s with param %s as %s ' \ | |
% (fn.__name__, repr(test_kwargs[key]), type(args[key])) | |
return fn(*wrap_args, **wrap_kwargs) | |
return wrap_cls | |
return wrap_func | |
class TestVar(object): | |
def __init__(self, string): | |
self.string = string | |
def __str__(self): | |
return self.string | |
def __repr__(self): | |
return str(self) | |
def __eq__(self, right): | |
return isinstance(right, self.__class__) and self.string == right.string | |
class TestVar_with_Eq(object): | |
def __init__(self, var): | |
self.var = var | |
def __eq__(self, right): | |
return self.var == right.var | |
class Test1: | |
@accepts(a=int, b=int, c=(None, str), d=(None, str)) | |
def __init__(self, a, b, c=None, d=None): | |
self.a = a | |
self.b = b | |
self.c = c | |
self.d = d | |
print '\t\tTest1.__init__' | |
print '\t\t\ta is %r' % type(a) | |
print '\t\t\tb is %r' % type(b) | |
print '\t\t\tc is %r' % type(c) | |
print '\t\t\td is %r' % type(d) | |
@classmethod | |
@accepts(data=str) | |
def decode(cls, data): | |
print '\t\tTest1.decode' | |
print '\t\t\tdata is %r' % type(data) | |
(a,b,c) = data.split(',') | |
return cls(int(a), int(b), c) | |
class Test2(object): | |
@accepts(var=TestVar_with_Eq, string=(None, TestVar('some text'))) | |
def __init__(self, var, string = None): | |
self.var = var | |
self.string = string | |
print '\t\tTest2.__init__' | |
print '\t\t\tvar is %r' % type(var) | |
print '\t\t\tstring is %r' % type(string) | |
@staticmethod | |
@accepts(data=TestVar_with_Eq(1), string=TestVar) | |
def decode(data, string): | |
print '\t\tTest2.decode' | |
print '\t\t\t data is %r' % type(data) | |
print '\t\t\tstring is %r' % type(string) | |
return Test2(data, string) | |
@accepts(a=int, b=int, c=(None, float, long, int)) | |
def test3(a, b, c=None): | |
print '\t\ttest3' | |
print '\t\t\ta is %r' % type(a) | |
print '\t\t\tb is %r' % type(b) | |
print '\t\t\tc is %r' % type(c) | |
try: | |
test3(1, 2, 3.0) | |
except Exception as e: | |
print '[ERROR] %r' % e | |
try: | |
Test1(1, 2) | |
except Exception as e: | |
print '[ERROR] %r' % e | |
try: | |
Test1.decode('1,2,bla') | |
except Exception as e: | |
print '[ERROR] %r' % e | |
try: | |
Test1.decode('1,2,3') | |
except Exception as e: | |
print '[ERROR] %r' % e | |
try: | |
Test2.decode(TestVar_with_Eq(1),TestVar('some text')) | |
except Exception as e: | |
print '[ERROR] %r' % e | |
try: | |
Test2(TestVar_with_Eq(1), None) | |
except Exception as e: | |
print '[ERROR] %r' % e | |
try: | |
test3(1, 2) | |
except Exception as e: | |
print '[ERROR] %r' % e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment