Completed
Push — master ( 3ab454...5eac4c )
by dotzero
02:15
created

wrapped()   B

Complexity

Conditions 5

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
c 0
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 5
rs 8.5454
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 1
"""This module contains a object that represents a Habrahabr Utils."""
23
24 1
from functools import wraps
25
26
27 1
def accepts(*types):
28
    """Декоратор для фильтрации входящих параметров."""
29 1
    def decorated(f):
30
        # don't forget self
31 1
        assert 1 + len(types) == f.__code__.co_argcount
32
33 1
        @wraps(f)
34
        def wrapped(*args, **kwargs):
35 1
            for (a, t) in zip(args[1:], types):
36 1
                if isinstance(t, list):
37 1
                    t = None if a is None else t[0]
38
39 1
                assert isinstance(a, t), "Arg %r does not match %s" % (a, t)
40 1
            return f(*args, **kwargs)
41
42 1
        return wrapped
43
44 1
    return decorated
45
46
47 1
def lazy(func):
48
    """Декоратор для ленивой загрузки."""
49 1
    @property
50
    def wrapper(self):
51 1
        attr_name = '__%s' % func.__name__
52 1
        try:
53 1
            value = getattr(self, attr_name)
54 1
        except AttributeError:
55 1
            value = func(self)
56 1
            setattr(self, attr_name, value)
57 1
        return value
58
59
    return wrapper
60