LocalsCachedPropertyProxy   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 10
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __str__() 0 2 1
A object() 0 3 1
A __init__() 0 2 1
1
from functools import partial
2
3
import pytest
4
5
empty = object()
6
7
8
class cached_property(object):
9
    def __init__(self, func):
10
        self.func = func
11
12
    def __get__(self, obj, cls):
13
        value = obj.__dict__[self.func.__name__] = self.func(obj)
14
        return value
15
16
17
class SimpleProxy(object):
18
    def __init__(self, factory):
19
        self.factory = factory
20
        self.object = empty
21
22
    def __str__(self):
23
        if self.object is empty:
24
            self.object = self.factory()
25
        return str(self.object)
26
27
28
class CachedPropertyProxy(object):
29
    def __init__(self, factory):
30
        self.factory = factory
31
32
    @cached_property
33
    def object(self):
34
        return self.factory()
35
36
    def __str__(self):
37
        return str(self.object)
38
39
40
class LocalsSimpleProxy(object):
41
    def __init__(self, factory):
42
        self.factory = factory
43
        self.object = empty
44
45
    def __str__(self, func=str):
46
        if self.object is empty:
47
            self.object = self.factory()
48
        return func(self.object)
49
50
51
class LocalsCachedPropertyProxy(object):
52
    def __init__(self, factory):
53
        self.factory = factory
54
55
    @cached_property
56
    def object(self):
57
        return self.factory()
58
59
    def __str__(self, func=str):
60
        return func(self.object)
61
62
63
@pytest.fixture(scope="module", params=["SimpleProxy", "CachedPropertyProxy", "LocalsSimpleProxy", "LocalsCachedPropertyProxy"])
64
def impl(request):
65
    return globals()[request.param]
66
67
68
def test_proto(benchmark, impl):
69
    obj = "foobar"
70
    proxied = impl(lambda: obj)
71
    result = benchmark(partial(str, proxied))
72
    assert result == obj
73