Димитър обнови решението на 17.04.2012 23:34 (преди над 12 години)
+class BiDict(dict):
+ def __init__(self, arg):
+ for val in arg.values():
+ if isinstance(val,(dict,list)):
+ raise TypeError ("Error type: ", type(val))
+ dict.__init__(self, arg)
+
+ def __setattr__(self, key, value):
+ if isinstance((value,key),(dict,list)):
+ raise TypeError ("Error type: ", type(val))
+ dict.__setattr__(self, key, value)
+
+ def __setitem__(self, key, value):
+ if isinstance((value,key),(dict,list)):
+ raise TypeError ("Error type: ", type(val))
+ dict.__setitem__(self, key, value)
+
+ def update(self, arg):
+ for value in arg.values():
+ if isinstance(arg,(dict,list)):
+ raise TypeError ("Error type: ", type(value))
+ dict.update(self, arg)
+
+ def inverse(self):
+ iterList = list(self.items())
+ #doubled_list = list(self.keys()) +list( self.values())
+ #if any([doubled_list.count(_) > 1 for _ in doubled_list]):
+ # raise TypeError ('Double key value')
+ for key, val in iterList :
+ self[val] = key
+ self.pop(key)