author | Tero Marttila <terom@fixme.fi> |
Wed, 10 Jun 2009 23:33:28 +0300 | |
changeset 84 | 891545a38a2b |
parent 75 | 18b3b1926720 |
child 96 | d9cf1e272e90 |
permissions | -rw-r--r-- |
44
533b7e8b5d3b
strip copyright/license boilerplate from modules, except dexif and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
1 |
""" |
533b7e8b5d3b
strip copyright/license boilerplate from modules, except dexif and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
2 |
Miscellaneous utilities |
533b7e8b5d3b
strip copyright/license boilerplate from modules, except dexif and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
3 |
""" |
533b7e8b5d3b
strip copyright/license boilerplate from modules, except dexif and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
4 |
|
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
5 |
import functools |
23 | 6 |
|
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
7 |
class LazyProperty (property) : |
12
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff
changeset
|
8 |
""" |
84
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
9 |
Lazy-loaded cached properties. |
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
10 |
|
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
11 |
This functions by overriding the class's property attribute with the actual value attribute in the instance's |
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
12 |
dict, causing all future attribute lookups to return that directly. |
12
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff
changeset
|
13 |
""" |
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff
changeset
|
14 |
|
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
15 |
def __init__ (self, func) : |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
16 |
""" |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
17 |
Initialize with no value |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
18 |
""" |
12
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff
changeset
|
19 |
|
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
20 |
super(LazyProperty, self).__init__(func) |
75 | 21 |
|
84
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
22 |
# the getter function |
75 | 23 |
self.func = func |
84
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
24 |
|
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
25 |
# the attribute name |
75 | 26 |
self.name = func.__name__ |
19
8d3ffd87cb0b
* move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents:
18
diff
changeset
|
27 |
|
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
28 |
def run (self, obj) : |
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
29 |
""" |
84
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
30 |
Run the background func and return the to-be-cached value. |
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
31 |
|
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
32 |
`obj` is the object instance |
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
33 |
""" |
28 | 34 |
|
75 | 35 |
return self.func(obj) |
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
36 |
|
84
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
37 |
def store (self, obj, value) : |
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
38 |
""" |
84
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
39 |
Store the cached value, overring ourself |
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
40 |
""" |
84
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
41 |
|
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
42 |
obj.__dict__[self.name] = value |
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
43 |
|
75 | 44 |
def __get__ (self, obj, owner) : |
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
45 |
""" |
84
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
46 |
Generate the value, store it as the attribute and return it. |
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
47 |
""" |
84
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
48 |
|
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
49 |
# compute value |
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
50 |
value = self.run(obj) |
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
51 |
|
84
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
52 |
# store it |
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
53 |
self.store(obj, value) |
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
54 |
|
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
55 |
# return it |
891545a38a2b
change utils.LazyProperty to store the cached value using obj.__dict__
Tero Marttila <terom@fixme.fi>
parents:
75
diff
changeset
|
56 |
return value |
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
57 |
|
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
58 |
class LazyIteratorProperty (LazyProperty) : |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
59 |
""" |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
60 |
A lazy-loaded property that automatically converts an iterator/genexp into a list. |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
61 |
""" |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
62 |
|
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
63 |
def run (self, obj) : |
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
64 |
""" |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
65 |
Wrap LazyProperty.run to return a list |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
66 |
""" |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
67 |
|
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
68 |
return list(super(LazyIteratorProperty, self).run(obj)) |
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
69 |
|
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
70 |
lazy_load = LazyProperty |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
71 |
lazy_load_iter = LazyIteratorProperty |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
72 |