-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patht.py
76 lines (50 loc) · 1.46 KB
/
t.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import inspect
class InjectedDependency:
def __init__(self, value):
self.value = value
def inject(name):
return InjectedDependency(42)
print(name)
f = inspect.currentframe().f_back
f = inspect.stack()[1]
print(f)
def getown(func, *args, **kwargs):
print(func.__name__)
return func(*args, **kwargs)
import random
class Injector:
def get(self, name):
return random.randint(0, 20)
def get_later(self, name, persist_on=None):
return DeferredProperty(
self, name, persist_on=persist_on
)
class DeferredProperty:
def __init__(self, injector, name, persist_on=None):
self.injector = injector
self.name = name
self.injector = injector
self.persisted = False
self.cached = None
self.persist_on = persist_on
def __get__(self, destobj, *args, **kwargs):
if not self.persisted:
val = self.injector.get(self.name)
if self.persist_on:
setattr(destobj, self.persist_on, val)
self.persisted = True
return val
self.persisted = True
self.cached = val
return self.cached
injector = Injector()
class Something:
b = injector.get_later('depname', 'b')
c = injector.get_later('depname', 'c')
def __init__(self):
pass
x = Something()
print(x.b)
print(x.b)
print(x.c)
print(x.c)