Решение на BiDict от Петър Костов

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

Към профила на Петър Костов

Резултати

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

Код

import collections
class BiDict(dict):
def __init__(self, dictionary):
dict.__init__(self, dictionary)
self.__check_values()
def __check_values(self):
self.inverse()
self.inverse()
def __check_for_hashable(self, value):
if not isinstance(value, collections.Hashable):
raise TypeError("unhashable type: " + type(value).__name__)
def inverse(self):
inversed = {v: k for k, v in self.items()}
self.clear()
self.update(inversed)
def __setitem__(self, key, value):
print(value)
self.__check_for_hashable(value)
if value in self.values():
self.inverse()
del self[value]
self.inverse()
dict.__setitem__(self, key, value)
def update(self, *args, **kwargs):
dictionary = dict(*args, **kwargs)
for value in dictionary.values():
self.__check_for_hashable(value)
dict.update(self, *args, **kwargs)
def setdefault(self, key, value=None):
self.__check_for_hashable(value)
dict.setdefault(self, key, value)

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

..EE....F.F.....
======================================================================
ERROR: test_circular_values (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-xyl918", 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-xyl918", line 57, in test_copied_circular_values
    inversed_circular.inverse()
AttributeError: 'dict' object has no attribute 'inverse'

======================================================================
FAIL: test_insert_existing_key_with_existing_value (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-xyl918", 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-xyl918", 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'])

----------------------------------------------------------------------
Ran 16 tests in 0.004s

FAILED (failures=2, errors=2)

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

Петър обнови решението на 19.04.2012 14:40 (преди над 8 години)

+import collections
+
+
+class BiDict(dict):
+
+ def __init__(self, dictionary):
+ dict.__init__(self, dictionary)
+ self.__check_values()
+
+ def __check_values(self):
+ self.inverse()
+ self.inverse()
+
+ def __check_for_hashable(self, value):
+ if not isinstance(value, collections.Hashable):
+ raise TypeError("unhashable type: " + type(value).__name__)
+
+ def inverse(self):
+ inversed = {v: k for k, v in self.items()}
+ self.clear()
+ self.update(inversed)
+
+ def __setitem__(self, key, value):
+ print(value)
+ self.__check_for_hashable(value)
+ if value in self.values():
+ self.inverse()
+ del self[value]
+ self.inverse()
+ dict.__setitem__(self, key, value)
+
+ def update(self, *args, **kwargs):
+ dictionary = dict(*args, **kwargs)
+ for value in dictionary.values():
+ self.__check_for_hashable(value)
+ dict.update(self, *args, **kwargs)
+
+ def setdefault(self, key, value=None):
+ self.__check_for_hashable(value)
+ dict.setdefault(self, key, value)