Решение на BiDict от Димитър Вачев

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

Към профила на Димитър Вачев

Резултати

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

Код

class BiDict(dict):
def __init__(self, arg):
for val in arg.values():
if isinstance(val,(dict,list)):
raise TypeError ("Error type: ", type(val))
dict.__init__(self, arg)
def __setattr__(self, key, value):
if isinstance((value,key),(dict,list)):
raise TypeError ("Error type: ", type(val))
dict.__setattr__(self, key, value)
def __setitem__(self, key, value):
if isinstance((value,key),(dict,list)):
raise TypeError ("Error type: ", type(val))
dict.__setitem__(self, key, value)
def update(self, arg):
for value in arg.values():
if isinstance(arg,(dict,list)):
raise TypeError ("Error type: ", type(value))
dict.update(self, arg)
def inverse(self):
iterList = list(self.items())
#doubled_list = list(self.keys()) +list( self.values())
#if any([doubled_list.count(_) > 1 for _ in doubled_list]):
# raise TypeError ('Double key value')
for key, val in iterList :
self[val] = key
self.pop(key)

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

..EE....F.F.....
======================================================================
ERROR: test_circular_values (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-gep4v4", 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-gep4v4", 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-gep4v4", 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-gep4v4", 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 коментара)

Димитър обнови решението на 17.04.2012 23:34 (преди около 12 години)

+class BiDict(dict):
+ def __init__(self, arg):
+ for val in arg.values():
+ if isinstance(val,(dict,list)):
+ raise TypeError ("Error type: ", type(val))
+ dict.__init__(self, arg)
+
+ def __setattr__(self, key, value):
+ if isinstance((value,key),(dict,list)):
+ raise TypeError ("Error type: ", type(val))
+ dict.__setattr__(self, key, value)
+
+ def __setitem__(self, key, value):
+ if isinstance((value,key),(dict,list)):
+ raise TypeError ("Error type: ", type(val))
+ dict.__setitem__(self, key, value)
+
+ def update(self, arg):
+ for value in arg.values():
+ if isinstance(arg,(dict,list)):
+ raise TypeError ("Error type: ", type(value))
+ dict.update(self, arg)
+
+ def inverse(self):
+ iterList = list(self.items())
+ #doubled_list = list(self.keys()) +list( self.values())
+ #if any([doubled_list.count(_) > 1 for _ in doubled_list]):
+ # raise TypeError ('Double key value')
+ for key, val in iterList :
+ self[val] = key
+ self.pop(key)