Решение на BiDict от Иво Кънчев

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

Към профила на Иво Кънчев

Резултати

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

Код

import collections
class BiDict(dict):
def __init__(self, *args, **kwargs):
self.update(*args,**kwargs)
def __setitem__(self,key,value):
if isinstance(value,collections.Hashable):
super(BiDict,self).__setitem__(key,value)
else:
raise TypeError("Not hashable value!")
def update(self, *args, **kwargs):
if args:
if len(args) > 1:
raise TypeError("update expected at most 1 arguments, got %d" % len(args))
other = dict(args[0])
for key in other:
self[key] = other[key]
for key in kwargs:
self[key] = kwargs[key]
def inverse(self):
rev_dict = {v:k for k, v in self.items()}
self.clear()
self.update(rev_dict)

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

..EE....F.F.....
======================================================================
ERROR: test_circular_values (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-qqxlhp", 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-qqxlhp", 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-qqxlhp", 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-qqxlhp", 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 15:18 (преди около 12 години)

+import collections
+class BiDict(dict):
+ def __init__(self, *args, **kwargs):
+ self.update(*args,**kwargs)
+
+ def __setitem__(self,key,value):
+ if isinstance(value,collections.Hashable):
+ super(BiDict,self).__setitem__(key,value)
+ else:
+ raise TypeError("Not hashable value!")
+
+ def update(self, *args, **kwargs):
+ if args:
+ if len(args) > 1:
+ raise TypeError("update expected at most 1 arguments, got %d" % len(args))
+ other = dict(args[0])
+ for key in other:
+ self[key] = other[key]
+ for key in kwargs:
+ self[key] = kwargs[key]
+
+ def inverse(self):
+ rev_dict = {v:k for k, v in self.items()}
+ self.clear()
+ self.update(rev_dict)
+