11#!/usr/bin/env python
22# -*- coding: utf-8 -*-
33
4- import collections
5- import inspect
6- import os
7-
84
95class classproperty :
106 """ Decorator for class properties.
@@ -16,54 +12,3 @@ def __init__(self, fget):
1612
1713 def __get__ (self , owner_self , owner_cls ):
1814 return self .fget (owner_cls )
19-
20-
21- def check_type (* args , ** kwargs ):
22- """ Checks the function types
23- """
24- args = tuple (x if isinstance (x , collections .Iterable ) else (x ,) for x in args )
25- kwargs = {x : kwargs [x ] if isinstance (kwargs [x ], collections .Iterable ) else (kwargs [x ],) for x in kwargs }
26-
27- def decorate (func ):
28- types = args
29- kwtypes = kwargs
30- gi = "Got <{}> instead"
31- errmsg1 = "to be of type <{}>. " + gi
32- errmsg2 = "to be one of type ({}). " + gi
33- errar = "{}:{} expected '{}' "
34- errkw = "{}:{} expected {} "
35-
36- def check (* ar , ** kw ):
37- line = inspect .getouterframes (inspect .currentframe ())[1 ][2 ]
38- fname = os .path .basename (inspect .getouterframes (inspect .currentframe ())[1 ][1 ])
39-
40- for arg , type_ in zip (ar , types ):
41- if type (arg ) not in type_ :
42- if len (type_ ) == 1 :
43- raise TypeError ((errar + errmsg1 ).format (fname , line , arg , type_ [0 ].__name__ ,
44- type (arg ).__name__ ))
45- else :
46- raise TypeError ((errar + errmsg2 ).format (fname , line , arg ,
47- ', ' .join ('<%s>' % x .__name__ for x in type_ ),
48- type (arg ).__name__ ))
49- for kwarg in kw :
50- if kwtypes .get (kwarg , None ) is None :
51- continue
52- if type (kw [kwarg ]) not in kwtypes [kwarg ]:
53- if len (kwtypes [kwarg ]) == 1 :
54- raise TypeError ((errkw + errmsg1 ).format (fname , line , kwarg , kwtypes [kwarg ][0 ].__name__ ,
55- type (kw [kwarg ]).__name__ ))
56- else :
57- raise TypeError ((errkw + errmsg2 ).format (
58- fname ,
59- line ,
60- kwarg ,
61- ', ' .join ('<%s>' % x .__name__ for x in kwtypes [kwarg ]),
62- type (kw [kwarg ]).__name__ )
63- )
64-
65- return func (* ar , ** kw )
66-
67- return check
68-
69- return decorate
0 commit comments