generated from pykit3/tmpl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fixed_keys_dict.py
44 lines (33 loc) · 1.21 KB
/
fixed_keys_dict.py
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
#!/bin/env python2
# coding: utf-8
class FixedKeysDict(dict):
"""
It provides the base class for dict with explicit keys.
:param args: as builtin **dict**.
:param argkv: as builtin **dict**.
:return: An instance of dictutil.FixedKeysDict.
"""
# {'key', value_constructor}
keys_default = {}
# ordered keys as ident
ident_keys = ()
def __init__(self, *args, **argkv):
super(FixedKeysDict, self).__init__(*args, **argkv)
# Convert present keys
for k in self:
self[k] = self.keys_default[k](self[k])
# Add default value to absent keys
for k in self.keys_default:
if k not in self:
self[k] = self.keys_default[k]()
def __setitem__(self, key, value):
try:
value = self.keys_default[key](value)
except KeyError:
raise KeyError('key: {key} is invalid'.format(key=key))
except (ValueError, TypeError) as e:
raise ValueError('value: {value} is invalid. {e}'.format(
value=repr(value), e=repr(e)))
super(FixedKeysDict, self).__setitem__(key, value)
def ident(self):
return tuple([self[k] for k in self.ident_keys])