Решение на BiDict от Габриела Цанова

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

Към профила на Габриела Цанова

Резултати

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

Код

class BiDict:
"""BiDict: dict that can be inversed"""
def __init__(self, *args, **kwargs):
src = dict(*args, **kwargs)
for value in src.values():
if not value.__hash__: #bool(None) == False
raise TypeError("unhashable type: '%s'" \
% value.__class__.__name__)
self.dict = {}
for key in src:
if src[key] not in self.dict.values():
self.dict[key] = src[key]
self.index = 0
def __repr__(self):
return "BiDict(" + str(self.dict) + ")"
#required string representation matches __repr__
__str__ = __repr__
def __setitem__(self, key, value):
if value in self.dict.values():
self.dict.__delitem__(key)
self.dict[key] = value
def update(self, E, **F):
dict_source = {}
dict_source.update(E, **F)
self = BiDict(dict_source)
def inverse(self):
self.dict = {v: k for (k, v) in self.dict.items()}
#standard stuff
def __getitem__(self, key):
return self.dict[key]
def __contains__(self, key):
return self.dict.__contains__(key)
def __delitem__(self, key):
self.dict.__delitem__(key)
def __eq__(self, other):
return self.dict == other.dict
def __iter__(self):
return self.dict.__iter__()
def __reduce__(self):
raise TypeError("can't pickle BiDict objects")
__reduce_ex__ = __reduce__
def clear(self):
self.dict.clear()
def copy(self):
return self.dict.copy()
def fromkeys(self, *args, **kwargs):
return self.dict.fromkeys(*args, **kwargs)
def get(self, *args, **kwargs):
return self.dict.get(*args, **kwargs)
def items(self):
return self.dict.items()
def keys(self):
return self.dict.keys()
def pop(self, *args, **kwargs):
return self.dict.pop(*args, **kwargs)
def popitem(self):
return self.dict.popitem()
def setdefault(self, *args, **kwargs):
return self.dict.setdefault(*args, **kwargs)
def values(self):
return self.dict.values()

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

..EE....F.F.....
======================================================================
ERROR: test_circular_values (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-psot8p", 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-psot8p", 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-psot8p", 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-psot8p", 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.003s

FAILED (failures=2, errors=2)

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

Габриела обнови решението на 19.04.2012 02:01 (преди над 8 години)

+class BiDict:
+ """BiDict: dict that can be inversed"""
+
+ def __init__(self, *args, **kwargs):
+ src = dict(*args, **kwargs)
+ for value in src.values():
+ if not value.__hash__: #bool(None) == False
+ raise TypeError("unhashable type: '%s'" \
+ % value.__class__.__name__)
+ self.dict = {}
+ for key in src:
+ if src[key] not in self.dict.values():
+ self.dict[key] = src[key]
+ self.index = 0
+
+ def __repr__(self):
+ return "BiDict(" + str(self.dict) + ")"
+
+ #required string representation matches __repr__
+ __str__ = __repr__
+
+ def __setitem__(self, key, value):
+ if value in self.dict.values():
+ self.dict.__delitem__(key)
+ self.dict[key] = value
+
+ def update(self, E, **F):
+ dict_source = {}
+ dict_source.update(E, **F)
+ self = BiDict(dict_source)
+
+ def inverse(self):
+ self.dict = {v: k for (k, v) in self.dict.items()}
+
+ #standard stuff
+ def __getitem__(self, key):
+ return self.dict[key]
+
+ def __contains__(self, key):
+ return self.dict.__contains__(key)
+
+ def __delitem__(self, key):
+ self.dict.__delitem__(key)
+
+ def __eq__(self, other):
+ return self.dict == other.dict
+
+ def __iter__(self):
+ return self.dict.__iter__()
+
+ def __reduce__(self):
+ raise TypeError("can't pickle BiDict objects")
+
+ __reduce_ex__ = __reduce__
+
+ def clear(self):
+ self.dict.clear()
+
+ def copy(self):
+ return self.dict.copy()
+
+ def fromkeys(self, *args, **kwargs):
+ return self.dict.fromkeys(*args, **kwargs)
+
+ def get(self, *args, **kwargs):
+ return self.dict.get(*args, **kwargs)
+
+ def items(self):
+ return self.dict.items()
+
+ def keys(self):
+ return self.dict.keys()
+
+ def pop(self, *args, **kwargs):
+ return self.dict.pop(*args, **kwargs)
+
+ def popitem(self):
+ return self.dict.popitem()
+
+ def setdefault(self, *args, **kwargs):
+ return self.dict.setdefault(*args, **kwargs)
+
+ def values(self):
+ return self.dict.values()