1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 | # Dictionary Helper Models
class Dictionary(models.Model):
"""A model that represents a dictionary. This model implements most of the dictionary interface,
allowing it to be used like a python dictionary.
"""
name = models.CharField(max_length=255)
@staticmethod
def getDict(name):
"""Get the Dictionary of the given name.
"""
df = Dictionary.objects.select_related().get(name=name)
return df
def __getitem__(self, key):
"""Returns the value of the selected key.
"""
return self.keyvaluepair_set.get(key=key).value
def __setitem__(self, key, value):
"""Sets the value of the given key in the Dictionary.
"""
try:
kvp = self.keyvaluepair_set.get(key=key)
except KeyValuePair.DoesNotExist:
KeyValuePair.objects.create(container=self, key=key, value=value)
else:
kvp.value = value
kvp.save()
def __delitem__(self, key):
"""Removed the given key from the Dictionary.
"""
try:
kvp = self.keyvaluepair_set.get(key=key)
except KeyValuePair.DoesNotExist:
raise KeyError
else:
kvp.delete()
def __len__(self):
"""Returns the length of this Dictionary.
"""
return self.keyvaluepair_set.count()
def iterkeys(self):
"""Returns an iterator for the keys of this Dictionary.
"""
return iter(kvp.key for kvp in self.keyvaluepair_set.all())
def itervalues(self):
"""Returns an iterator for the keys of this Dictionary.
"""
return iter(kvp.value for kvp in self.keyvaluepair_set.all())
__iter__ = iterkeys
def iteritems(self):
"""Returns an iterator over the tuples of this Dictionary.
"""
return iter((kvp.key, kvp.value) for kvp in self.keyvaluepair_set.all())
def keys(self):
"""Returns all keys in this Dictionary as a list.
"""
return [kvp.key for kvp in self.keyvaluepair_set.all()]
def values(self):
"""Returns all values in this Dictionary as a list.
"""
return [kvp.value for kvp in self.keyvaluepair_set.all()]
def items(self):
"""Get a list of tuples of key, value for the items in this Dictionary.
This is modeled after dict.items().
"""
return [(kvp.key, kvp.value) for kvp in self.keyvaluepair_set.all()]
def get(self, key, default=None):
"""Gets the given key from the Dictionary. If the key does not exist, it
returns default.
"""
try:
return self[key]
except KeyError:
return default
def has_key(self, key):
"""Returns true if the Dictionary has the given key, false if not.
"""
return self.contains(key)
def contains(self, key):
"""Returns true if the Dictionary has the given key, false if not.
"""
try:
self.keyvaluepair_set.get(key=key)
return True
except KeyValuePair.DoesNotExist:
return False
def clear(self):
"""Deletes all keys in the Dictionary.
"""
self.keyvaluepair_set.all().delete()
def __unicode__(self):
"""Returns a unicode representation of the Dictionary.
"""
return unicode(self.asPyDict())
def asPyDict(self):
"""Get a python dictionary that represents this Dictionary object.
This object is read-only.
"""
fieldDict = dict()
for kvp in self.keyvaluepair_set.all():
fieldDict[kvp.key] = kvp.value
return fieldDict
class KeyValuePair(models.Model):
"""A Key-Value pair with a pointer to the Dictionary that owns it.
"""
container = models.ForeignKey(Dictionary, db_index=True)
key = models.CharField(max_length=240, db_index=True)
value = models.CharField(max_length=240, db_index=True)
|
Comments