|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# |
|
3
|
|
|
# Copyright (c) 2016 dotzero <[email protected]> |
|
4
|
|
|
# |
|
5
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy |
|
6
|
|
|
# of this software and associated documentation files (the "Software"), to deal |
|
7
|
|
|
# in the Software without restriction, including without limitation the rights |
|
8
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
9
|
|
|
# copies of the Software, and to permit persons to whom the Software is |
|
10
|
|
|
# furnished to do so, subject to the following conditions: |
|
11
|
|
|
# |
|
12
|
|
|
# The above copyright notice and this permission notice shall be included |
|
13
|
|
|
# in all copies or substantial portions of the Software. |
|
14
|
|
|
# |
|
15
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
18
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
20
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
21
|
|
|
# SOFTWARE. |
|
22
|
|
|
|
|
23
|
1 |
|
from functools import wraps |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
1 |
|
def accepts(*types): |
|
27
|
1 |
|
def decorated(f): |
|
28
|
|
|
# don't forget self |
|
29
|
1 |
|
assert 1 + len(types) == f.__code__.co_argcount |
|
30
|
|
|
|
|
31
|
1 |
|
@wraps(f) |
|
32
|
|
|
def wrapped(*args, **kwargs): |
|
33
|
1 |
|
for (a, t) in zip(args[1:], types): |
|
34
|
1 |
|
if isinstance(t, list): |
|
35
|
1 |
|
t = None if a is None else t[0] |
|
36
|
|
|
|
|
37
|
1 |
|
assert isinstance(a, t), "Arg %r does not match %s" % (a, t) |
|
38
|
1 |
|
return f(*args, **kwargs) |
|
39
|
|
|
|
|
40
|
1 |
|
return wrapped |
|
41
|
|
|
|
|
42
|
1 |
|
return decorated |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
1 |
|
def lazy(func): |
|
46
|
1 |
|
@property |
|
47
|
|
|
def wrapper(self): |
|
48
|
1 |
|
attr_name = '__%s' % func.__name__ |
|
49
|
1 |
|
try: |
|
50
|
1 |
|
value = getattr(self, attr_name) |
|
51
|
1 |
|
except AttributeError: |
|
52
|
1 |
|
value = func(self) |
|
53
|
1 |
|
setattr(self, attr_name, value) |
|
54
|
1 |
|
return value |
|
55
|
|
|
|
|
56
|
|
|
return wrapper |
|
57
|
|
|
|