author | terom |
Thu, 31 Jan 2008 19:19:05 +0000 | |
changeset 30 | b1d5c32ab771 |
parent 20 | 6c774496bb00 |
permissions | -rw-r--r-- |
20
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
1 |
import shelve |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
2 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
3 |
class TagDB (object) : |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
4 |
_imgs_cache = {} |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
5 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
6 |
def __init__ (self, read_only=True) : |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
7 |
self.img_tags = shelve.open("img_tags", read_only and 'r' or 'c') |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
8 |
self.tag_imgs = shelve.open("tag_imgs", read_only and 'r' or 'c') |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
9 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
10 |
def tag (self, img, tag) : |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
11 |
""" |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
12 |
Associate the given image with the given tag |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
13 |
""" |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
14 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
15 |
if img not in self.img_tags : |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
16 |
self.img_tags[img] = set([tag]) |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
17 |
else : |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
18 |
s = self.img_tags[img] |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
19 |
s.add(tag) |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
20 |
self.img_tags[img] = s |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
21 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
22 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
23 |
if tag not in self.tag_imgs : |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
24 |
self.tag_imgs[tag] = set([img]) |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
25 |
else : |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
26 |
s = self.tag_imgs[tag] |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
27 |
s.add(img) |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
28 |
self.tag_imgs[tag] = s |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
29 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
30 |
print "%s <-> %s" % (img, tag) |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
31 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
32 |
def imgs (self, tags) : |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
33 |
""" |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
34 |
Get the set of images that have the given set of tags |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
35 |
""" |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
36 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
37 |
cache_key = "/".join(tags) |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
38 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
39 |
if cache_key in self._imgs_cache : |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
40 |
return self._imgs_cache[cache_key] |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
41 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
42 |
if not tags : |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
43 |
return set(self.img_tags.keys()) |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
44 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
45 |
img_sets = [self.tag_imgs[tag] for tag in tags] |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
46 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
47 |
res = None |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
48 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
49 |
for img_set in img_sets : |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
50 |
if res : |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
51 |
res = res & img_set |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
52 |
else : |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
53 |
res = img_set |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
54 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
55 |
self._imgs_cache[cache_key] = res |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
56 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
57 |
return res |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
58 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
59 |
def tags (self, tags) : |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
60 |
""" |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
61 |
Get the set of tags that are present in the set of images specified by these tags, sorted by count |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
62 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
63 |
This is currently implemented quite inefficiently... giev SQL db? |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
64 |
""" |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
65 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
66 |
imgs = self.imgs(tags) |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
67 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
68 |
ret = [] |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
69 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
70 |
for tag in self.tag_imgs.keys() : |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
71 |
if tag in tags : |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
72 |
continue |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
73 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
74 |
count = len(self.tag_imgs[tag] & imgs) |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
75 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
76 |
if count : |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
77 |
ret.append((tag, count)) |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
78 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
79 |
def my_cmp ((at, ac), (bt, bc)) : |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
80 |
return cmp((ac, at), (bc, bt)) |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
81 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
82 |
ret.sort(reverse=True) |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
83 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
84 |
return ret |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
85 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
86 |
def close (self) : |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
87 |
self.img_tags.close() |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
88 |
self.tag_imgs.close() |
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff
changeset
|
89 |