This is a wrapper class around ``defaultdict(list)`` enabling it to support an API consistent with `Storage`
| 202 | |
| 203 | |
| 204 | class DictListStorage(OrderedStorage): |
| 205 | '''This is a wrapper class around ``defaultdict(list)`` enabling |
| 206 | it to support an API consistent with `Storage` |
| 207 | ''' |
| 208 | def __init__(self, config): |
| 209 | self._dict = defaultdict(list) |
| 210 | |
| 211 | def keys(self): |
| 212 | return self._dict.keys() |
| 213 | |
| 214 | def get(self, key): |
| 215 | return self._dict.get(key, []) |
| 216 | |
| 217 | def remove(self, *keys): |
| 218 | for key in keys: |
| 219 | del self._dict[key] |
| 220 | |
| 221 | def remove_val(self, key, val): |
| 222 | self._dict[key].remove(val) |
| 223 | |
| 224 | def insert(self, key, *vals, **kwargs): |
| 225 | self._dict[key].extend(vals) |
| 226 | |
| 227 | def size(self): |
| 228 | return len(self._dict) |
| 229 | |
| 230 | def itemcounts(self, **kwargs): |
| 231 | '''Returns a dict where the keys are the keys of the container. |
| 232 | The values are the *lengths* of the value sequences stored |
| 233 | in this container. |
| 234 | ''' |
| 235 | return {k: len(v) for k, v in self._dict.items()} |
| 236 | |
| 237 | def has_key(self, key): |
| 238 | return key in self._dict |
| 239 | |
| 240 | |
| 241 | class DictSetStorage(UnorderedStorage, DictListStorage): |