author | Tero Marttila <terom@fixme.fi> |
Fri, 05 Jun 2009 23:06:06 +0300 | |
changeset 73 | 8897352630a5 |
parent 59 | fbbe956229cc |
child 75 | 18b3b1926720 |
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 |
|
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
12 |
def __init__ (self, func) : |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
13 |
""" |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
14 |
Initialize with no value |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
15 |
""" |
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
|
16 |
|
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
17 |
super(LazyProperty, self).__init__(func) |
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
|
18 |
|
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
19 |
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
|
20 |
|
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
21 |
def run (self, obj) : |
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
22 |
""" |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
23 |
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
|
24 |
""" |
28 | 25 |
|
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
26 |
return super(LazyProperty, self).__call__(obj) |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
27 |
|
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
28 |
def get (self, obj) : |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
29 |
""" |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
30 |
Return the cached value |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
31 |
""" |
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
32 |
|
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
33 |
xxx |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
34 |
|
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
35 |
def set (self, obj, value) : |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
36 |
""" |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
37 |
Set the cached value |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
38 |
""" |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
39 |
|
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
40 |
xxx |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
41 |
|
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
42 |
def test (self, obj) : |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
43 |
""" |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
44 |
Tests if a value is set |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
45 |
""" |
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
46 |
|
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
47 |
xxx |
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 |
def __call__ (self, obj) : |
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
50 |
""" |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
51 |
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
|
52 |
""" |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
53 |
|
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
54 |
if self.test(obj): |
59
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
55 |
# generate it |
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
56 |
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
|
57 |
|
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
58 |
return self.get(obj) |
59
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 |
class LazyIteratorProperty (LazyProperty) : |
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 |
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
|
63 |
""" |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
64 |
|
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
65 |
def run (self, obj) : |
59
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 |
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
|
68 |
""" |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
69 |
|
73
8897352630a5
misc. stuff in gallery.py utils.py
Tero Marttila <terom@fixme.fi>
parents:
59
diff
changeset
|
70 |
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
|
71 |
|
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
72 |
lazy_load = LazyProperty |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
73 |
lazy_load_iter = LazyIteratorProperty |
fbbe956229cc
remove old utils and write some new ones
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
74 |