7 def __init__(self
, init_list
=None, sort_key
=None):
9 self
.sort_key
= sort_key
12 for item
in init_list
:
15 def isvalid(self
, dict):
16 if type(dict) == sdict
:
20 return self
.lst
.__str
__()
31 def append(self
, dict):
32 if not self
.isvalid(dict):
33 raise Exception, "argument type of %s: %s invalid %s" %\
34 (dict, type(dict), sdict
)
36 self
= self
.sort(lst
=self
.lst
)
38 def remove(self
, index
):
39 try: del(self
.lst
[index
])
48 def get_top(self
, pred
=None):
49 if pred
: return filter(pred
, self
.lst
)[0]
52 def get_all(self
, pred
=None):
53 if pred
: return filter(pred
, self
.lst
)
56 def sort(self
, lst
=None, sort_key
=None, reverse
=False):
58 # insulate against unset keys
59 if sort_key
not in x
and sort_key
not in y
: return 0
60 elif sort_key
not in x
: return 1
61 if sort_key
not in y
: return -1
63 # numeric sort if string begins with digits
64 m
= regex
.find1("(-?[0-9]*)", x
[sort_key
])
65 n
= regex
.find1("(-?[0-9]*)", y
[sort_key
])
67 return cmp(int(n
), int(m
)) # reverse order to get desc
69 return cmp(x
[sort_key
].lower(), y
[sort_key
].lower())
71 if not lst
: lst
= self
.lst
72 if not sort_key
: sort_key
= self
.sort_key
73 lst
= sorted(lst
, compare
)
74 if reverse
: lst
.reverse()
77 l
.__dict
__['lst'] = lst
80 def ammend(self
, old
, new
): # update old dict with new data
81 if not new
: return old
82 for key
in new
.keys():
83 if not new
[key
] or new
[key
] == "":
85 for key
in [x
for x
in old
.keys() if x
not in new
.keys()]:
89 # merge new_dict into dictlist
90 def merge1(self
, new_dict
, key
):
93 for (i
, dict) in enumerate(dicts
):
94 if dict[key
] == new_dict
[key
]:
96 dicts
[i
] = self
.ammend(dict, new_dict
)
98 dicts
.append(new_dict
)
99 return self
.sort(lst
=dicts
)
101 # merge new dictlist into dictlist
102 def merge(self
, new_dicts
, key
):
104 if not new_dicts
: return dicts
105 for new_dict
in new_dicts
:
106 dicts
.merge1(new_dict
, key
)
107 return self
.sort(lst
=dicts
)
111 def __init__(self
, *args
):
114 if args
and len(args
) == 1:
116 for (key
, value
) in init_dict
.items():
117 self
.__setattr
__(key
, value
)
122 def ___value_is_null___(self
, value
):
124 # ignore null values and strings with only whitespace
125 if not value
or (type(value
) == str and value
.strip() == ""):
129 # Dictionary interface
131 def __setitem__(self
, key
, value
):
132 self
.__setattr
__(key
, value
)
134 def __getitem__(self
, key
):
135 return self
.__getattribute
__(key
)
137 def __delitem__(self
, key
):
138 self
.__delattr
__(key
)
141 for key
in dict.keys(self
):
142 self
.__delitem
__(key
)
144 def fromkeys(self
, keys
, value
=None):
147 for key
in keys
: new_sdict
.__setitem
__(key
, value
)
150 def pop(self
, key
, default
=None):
151 value
= self
.__getitem
__(key
)
152 self
.__delitem
__(key
)
153 if not value
: return default
159 return (key
, self
.pop(key
))
160 except IndexError: return None
162 def setdefault(self
, key
, value
=None):
163 v
= self
.__getitem
__(key
)
165 self
.__setitem
__(key
, value
)
168 def update(self
, pairs
):
169 for (key
, value
) in pairs
:
170 self
.__setattr
__(key
, value
)
173 # Attribute interface
175 def __setattr__(self
, key
, value
):
176 if not self
.___value
_is
_null
___(value
):
177 if type(value
) == int: value
= str(value
)
178 dict.__setattr
__(self
, key
, value
)
179 dict.__setitem
__(self
, key
, value
)
181 def __getattribute__(self
, key
):
183 return dict.__getattribute
__(self
, key
)
184 except AttributeError: return None
186 def __delattr__(self
, key
):
188 dict.__delattr
__(self
, key
)
189 dict.__delitem
__(self
, key
)
194 if __name__
== "__main__":
197 print "d['a']:", d
['a']
223 dl
= sdictlist(sort_key
="a")
230 print dl
.get_top(pred
=lambda x
: x
.a
!= None)
232 dl2
= sdictlist(sort_key
="c")
235 print dl
.merge(dl2
, "c")