author | Tero Marttila <terom@fixme.fi> |
Fri, 05 Jun 2009 23:42:03 +0300 | |
changeset 76 | e22d9f699081 |
parent 75 | 18b3b1926720 |
child 84 | 891545a38a2b |
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 |
""" |
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
9 |
Lazy-loaded properties |
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
|
10 |
""" |
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
|
11 |
|
75 | 12 |
ATTRNAME = '_LazyProperty_values' |
13 |
||
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
14 |
def __init__ (self, func) : |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
15 |
""" |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
16 |
Initialize with no value |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
17 |
""" |
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
|
18 |
|
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
19 |
super(LazyProperty, self).__init__(func) |
75 | 20 |
|
21 |
self.func = func |
|
22 |
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
|
23 |
|
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
24 |
self.value = None |
26
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
23
diff
changeset
|
25 |
|
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
26 |
def run (self, obj) : |
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
27 |
""" |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
28 |
Run the background func and return the to-be-cached value |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
29 |
""" |
28 | 30 |
|
75 | 31 |
return self.func(obj) |
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
32 |
|
75 | 33 |
def obj_dict (self, obj): |
34 |
""" |
|
35 |
Get the lazy-property dict of the given obj |
|
36 |
""" |
|
37 |
||
38 |
dict = getattr(obj, self.ATTRNAME, None) |
|
39 |
||
40 |
if dict is None : |
|
41 |
dict = {} |
|
42 |
||
43 |
setattr(obj, self.ATTRNAME, dict) |
|
44 |
||
45 |
return dict |
|
46 |
||
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
47 |
def get (self, obj) : |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
48 |
""" |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
49 |
Return the cached value |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
50 |
""" |
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
51 |
|
75 | 52 |
return self.obj_dict(obj)[self.name] |
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
53 |
|
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
54 |
def set (self, obj, value) : |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
55 |
""" |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
56 |
Set the cached value |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
57 |
""" |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
58 |
|
75 | 59 |
self.obj_dict(obj)[self.name] = value |
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
60 |
|
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
61 |
def test (self, obj) : |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
62 |
""" |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
63 |
Tests if a value is set |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
64 |
""" |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
65 |
|
75 | 66 |
return self.name in self.obj_dict(obj) |
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
67 |
|
75 | 68 |
def __get__ (self, obj, owner) : |
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 |
Return the cached value if it exists, otherwise, call the func |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
71 |
""" |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
72 |
|
75 | 73 |
if not obj : |
74 |
return self |
|
75 |
||
76 |
if not self.test(obj) : |
|
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
77 |
# generate it |
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
78 |
self.set(obj, self.run(obj)) |
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
79 |
|
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
80 |
return self.get(obj) |
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
81 |
|
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
82 |
class LazyIteratorProperty (LazyProperty) : |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
83 |
""" |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
84 |
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
|
85 |
""" |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
86 |
|
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
87 |
def run (self, obj) : |
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
88 |
""" |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
89 |
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
|
90 |
""" |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
91 |
|
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
92 |
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
|
93 |
|
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
94 |
lazy_load = LazyProperty |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
95 |
lazy_load_iter = LazyIteratorProperty |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
96 |