# Решение на BiDict от Лидия Георгиева

Обратно към всички решения

Към профила на Лидия Георгиева

## Резултати

• 4 точки от тестове
• 0 бонус точки
• 4 точки общо
• 10 успешни тест(а)
• 6 неуспешни тест(а)

## Код

class BiDict(dict):
def __init__(self, *args, **kwargs):
for value in dict(*args, **kwargs).values():
self.check_hashable_value(value)
dict.__init__(self, *args, **kwargs)
self.inverse()
self.inverse()
def inverse(self):
inversed_dict = {v: k for k, v in self.items()}
self.clear()
self.update(inversed_dict)
def __repr__(self):
represent = dict.__repr__(self)
return '%s(%s)' % (type(self).__name__, represent)
def __setitem__(self, key, value):
self.check_hashable_value(value)
if value in self.values():
existing_key = [k for k, v in self.items() if v == value][0]
self.__delitem__(existing_key)
dict.__setitem__(self, key, value)
def check_hashable_value(self, value):
if not value.__hash__:
class_name = repr(value.__class__.__name__)
raise TypeError('unhashable type: %s' % class_name)

## Лог от изпълнението

..EE...FF.F.F...
======================================================================
ERROR: test_circular_values (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-1wnsedz", line 49, in test_circular_values
circular = self.solutions.BiDict({1: 2, 2: 3, 3: 1})
AttributeError: 'BiDictTestCase' object has no attribute 'solutions'

======================================================================
ERROR: test_copied_circular_values (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-1wnsedz", line 57, in test_copied_circular_values
inversed_circular.inverse()
AttributeError: 'dict' object has no attribute 'inverse'

======================================================================
FAIL: test_hashing_self (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-1wnsedz", line 65, in test_hashing_self
self.assertRaises(TypeError, self.person.update, {'clone': self.person})
AssertionError: TypeError not raised by update

======================================================================
FAIL: test_insert_existing_key_with_existing_value (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-1wnsedz", line 78, in test_insert_existing_key_with_existing_value
self.assertNotIn('age', new_person.keys())
AssertionError: 'age' unexpectedly found in dict_keys(['age', 'name', 'sex'])

======================================================================
FAIL: test_insert_none_existing_key_with_existing_value (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-1wnsedz", line 92, in test_insert_none_existing_key_with_existing_value
self.assertNotIn('age', new_person.keys())
AssertionError: 'age' unexpectedly found in dict_keys(['age', 'name', 'none', 'sex'])

======================================================================
FAIL: test_invalid_value (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-1wnsedz", line 62, in test_invalid_value
self.assertRaises(TypeError, self.person.update, {'sports': ['boxing',]})
AssertionError: TypeError not raised by update

----------------------------------------------------------------------
Ran 16 tests in 0.003s

FAILED (failures=4, errors=2)

## История (1 версия и 0 коментара)

### Лидия обнови решението на 18.04.2012 01:52 (преди над 8 години)

+class BiDict(dict):
+
+ def __init__(self, *args, **kwargs):
+ for value in dict(*args, **kwargs).values():
+ self.check_hashable_value(value)
+ dict.__init__(self, *args, **kwargs)
+ self.inverse()
+ self.inverse()
+
+ def inverse(self):
+ inversed_dict = {v: k for k, v in self.items()}
+ self.clear()
+ self.update(inversed_dict)
+
+ def __repr__(self):
+ represent = dict.__repr__(self)
+ return '%s(%s)' % (type(self).__name__, represent)
+
+ def __setitem__(self, key, value):
+ self.check_hashable_value(value)
+ if value in self.values():
+ existing_key = [k for k, v in self.items() if v == value][0]
+ self.__delitem__(existing_key)
+ dict.__setitem__(self, key, value)
+
+ def check_hashable_value(self, value):
+ if not value.__hash__:
+ class_name = repr(value.__class__.__name__)
+ raise TypeError('unhashable type: %s' % class_name)