16 |
16 |
17 super(LazyProperty, self).__init__(func) |
17 super(LazyProperty, self).__init__(func) |
18 |
18 |
19 self.value = None |
19 self.value = None |
20 |
20 |
21 def run (self) : |
21 def run (self, obj) : |
22 """ |
22 """ |
23 Run the background func and return the to-be-cached value |
23 Run the background func and return the to-be-cached value |
24 """ |
24 """ |
25 |
25 |
26 return super(LazyProperty, self).__call__() |
26 return super(LazyProperty, self).__call__(obj) |
|
27 |
|
28 def get (self, obj) : |
|
29 """ |
|
30 Return the cached value |
|
31 """ |
27 |
32 |
28 def __call__ (self) : |
33 xxx |
|
34 |
|
35 def set (self, obj, value) : |
|
36 """ |
|
37 Set the cached value |
|
38 """ |
|
39 |
|
40 xxx |
|
41 |
|
42 def test (self, obj) : |
|
43 """ |
|
44 Tests if a value is set |
|
45 """ |
|
46 |
|
47 xxx |
|
48 |
|
49 def __call__ (self, obj) : |
29 """ |
50 """ |
30 Return the cached value if it exists, otherwise, call the func |
51 Return the cached value if it exists, otherwise, call the func |
31 """ |
52 """ |
32 |
53 |
33 if not self.value : |
54 if self.test(obj): |
34 # generate it |
55 # generate it |
35 self.value = self.run() |
56 self.set(obj, self.run(obj)) |
36 |
57 |
37 return self.value |
58 return self.get(obj) |
38 |
59 |
39 class LazyIteratorProperty (LazyProperty) : |
60 class LazyIteratorProperty (LazyProperty) : |
40 """ |
61 """ |
41 A lazy-loaded property that automatically converts an iterator/genexp into a list. |
62 A lazy-loaded property that automatically converts an iterator/genexp into a list. |
42 """ |
63 """ |
43 |
64 |
44 def run (self) : |
65 def run (self, obj) : |
45 """ |
66 """ |
46 Wrap LazyProperty.run to return a list |
67 Wrap LazyProperty.run to return a list |
47 """ |
68 """ |
48 |
69 |
49 return list(super(LazyIteratorProperty, self).run()) |
70 return list(super(LazyIteratorProperty, self).run(obj)) |
50 |
71 |
51 lazy_load = LazyProperty |
72 lazy_load = LazyProperty |
52 lazy_load_iter = LazyIteratorProperty |
73 lazy_load_iter = LazyIteratorProperty |
53 |
74 |