# Решение на BiDict от Николай Георгиев

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

Към профила на Николай Георгиев

## Резултати

• 3 точки от тестове
• 0 бонус точки
• 3 точки общо
• 8 успешни тест(а)
• 8 неуспешни тест(а)

## Код

#!/usr/bin/env python3
import os
class BiDict:
def __init__(self, new_dict):
self.__check_init_type(new_dict)
self.__dict = new_dict
self.inverse()
self.inverse()
def __str__(self):
return self.__repr__()
def __repr__(self):
return 'BiDict(' + self.__dict.__str__() + ')'
def __len__(self):
return len(self.__dict)
def __setitem__(self, key, value):
self.__check_value_type_against_hash(value)
self.__take_care_of_duplicate_values(key, value, self.__dict)
self.__dict[key] = value
def __getitem__(self, key):
return self.__dict[key]
def inverse(self):
self.__dict = {v: k for (k, v) in self.__dict.items()}
def keys(self):
return self.__dict.keys()
def pop(self, key):
return self.__dict.pop()
def copy(self):
return BiDict(self.__dict.copy())
def update(self, new_dict):
self.__check_init_type(new_dict)
self.__check_values_are_hashable(new_dict)
self.__check_values_for_duplicates(new_dict)
self.__dict.update(new_dict)
def __check_init_type(self, new_dict):
if not isinstance(new_dict, dict):
my_type = type(new_dict)
raise TypeError('Type {0} is not supported'.format(my_type))
def __check_value_type_against_hash(self, value):
if not value.__hash__:
raise TypeError('Cannot set non-hashable value: {0}'.format(value))
def __take_care_of_duplicate_values(self, key, value, dictionary):
temp_key = self.__get_key_for_duplicate_value(value, dictionary)
if (not temp_key is None) and (not temp_key == key):
del self.__dict[temp_key]
def __get_key_for_duplicate_value(self, value, dictionary):
for key in dictionary.keys():
if dictionary.get(key) == value:
return key
def __check_values_are_hashable(self, new_dict):
for k in new_dict.keys():
self.__check_value_type_against_hash(new_dict.get(k))
def __check_values_for_duplicates(self, new_dict):
for key, val in new_dict.items():
self.__take_care_of_duplicate_values(key, val, new_dict)
if __name__ == '__main__':
biDi = BiDict({'name': 'M', 'age': 'M', 'age': 'N'})
print(biDi)
d = {1: 'x', 2: 'y', 'c': 'c'}
biDi.update(d)
print(biDi)
biDi.inverse()
print(biDi)
t = biDi.copy()
t[1] = 'z'
print(biDi)
print(t)

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

..EF..FFEEEE....
======================================================================
ERROR: test_circular_values (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-w9zptp", 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_insert_existing_key_with_existing_value (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-w9zptp", line 77, in test_insert_existing_key_with_existing_value
self.assertIn(18, new_person.values())
AttributeError: 'BiDict' object has no attribute 'values'

======================================================================
ERROR: test_insert_existing_key_with_none_existing_value (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-w9zptp", line 84, in test_insert_existing_key_with_none_existing_value
self.assertIn(-1, new_person.values())
AttributeError: 'BiDict' object has no attribute 'values'

======================================================================
ERROR: test_insert_none_existing_key_with_existing_value (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-w9zptp", line 91, in test_insert_none_existing_key_with_existing_value
self.assertIn(18, new_person.values())
AttributeError: 'BiDict' object has no attribute 'values'

======================================================================
ERROR: test_insert_none_existing_key_with_none_existing_value (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-w9zptp", line 98, in test_insert_none_existing_key_with_none_existing_value
self.assertIn(-1, new_person.values())
AttributeError: 'BiDict' object has no attribute 'values'

======================================================================
FAIL: test_copied_circular_values (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-w9zptp", line 59, in test_copied_circular_values
self.assertEqual(circular, inversed_circular)
AssertionError: BiDict({1: 2, 2: 3, 3: 1}) != BiDict({1: 2, 2: 3, 3: 1})

======================================================================
FAIL: test_has_dict_attrs (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-w9zptp", line 71, in test_has_dict_attrs
self.assertIn('items', dir(self.person))
AssertionError: 'items' not found in ['_BiDict__check_init_type', '_BiDict__check_value_type_against_hash', '_BiDict__check_values_are_hashable', '_BiDict__check_values_for_duplicates', '_BiDict__dict', '_BiDict__get_key_for_duplicate_value', '_BiDict__take_care_of_duplicate_values', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'copy', 'inverse', 'keys', 'pop', 'update']

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

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

FAILED (failures=3, errors=5)

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

### Николай обнови решението на 17.04.2012 23:16 (преди над 8 години)

+#!/usr/bin/env python3
+
+import os
+
+
+class BiDict:
+
+ def __init__(self, new_dict):
+ self.__check_init_type(new_dict)
+ self.__dict = new_dict
+ self.inverse()
+ self.inverse()
+
+ def __str__(self):
+ return self.__repr__()
+
+ def __repr__(self):
+ return 'BiDict(' + self.__dict.__str__() + ')'
+
+ def __len__(self):
+ return len(self.__dict)
+
+ def __setitem__(self, key, value):
+ self.__check_value_type_against_hash(value)
+ self.__take_care_of_duplicate_values(key, value, self.__dict)
+ self.__dict[key] = value
+
+ def __getitem__(self, key):
+ return self.__dict[key]
+
+ def inverse(self):
+ self.__dict = {v: k for (k, v) in self.__dict.items()}
+
+ def keys(self):
+ return self.__dict.keys()
+
+ def pop(self, key):
+ return self.__dict.pop()
+
+ def copy(self):
+ return BiDict(self.__dict.copy())
+
+ def update(self, new_dict):
+ self.__check_init_type(new_dict)
+ self.__check_values_are_hashable(new_dict)
+ self.__check_values_for_duplicates(new_dict)
+ self.__dict.update(new_dict)
+
+ def __check_init_type(self, new_dict):
+ if not isinstance(new_dict, dict):
+ my_type = type(new_dict)
+ raise TypeError('Type {0} is not supported'.format(my_type))
+
+ def __check_value_type_against_hash(self, value):
+ if not value.__hash__:
+ raise TypeError('Cannot set non-hashable value: {0}'.format(value))
+
+ def __take_care_of_duplicate_values(self, key, value, dictionary):
+ temp_key = self.__get_key_for_duplicate_value(value, dictionary)
+ if (not temp_key is None) and (not temp_key == key):
+ del self.__dict[temp_key]
+
+ def __get_key_for_duplicate_value(self, value, dictionary):
+ for key in dictionary.keys():
+ if dictionary.get(key) == value:
+ return key
+
+ def __check_values_are_hashable(self, new_dict):
+ for k in new_dict.keys():
+ self.__check_value_type_against_hash(new_dict.get(k))
+
+ def __check_values_for_duplicates(self, new_dict):
+ for key, val in new_dict.items():
+ self.__take_care_of_duplicate_values(key, val, new_dict)
+
+
+if __name__ == '__main__':
+ biDi = BiDict({'name': 'M', 'age': 'M', 'age': 'N'})
+ print(biDi)
+ d = {1: 'x', 2: 'y', 'c': 'c'}
+ biDi.update(d)
+ print(biDi)
+ biDi.inverse()
+ print(biDi)
+ t = biDi.copy()
+ t[1] = 'z'
+ print(biDi)
+ print(t)