Предизвикателства > International Whores' Day! > Решения > Решението на Милица Лазарова

Резултати
1 точки от тестове
0 точки от учител

1 точки общо

6 успешни теста
1 неуспешни теста
Код

 1class Slot:
 2    def __init__(self, object_limit=None, size_limit=None):
 3        self.object_limit = object_limit
 4        self.size_limit = size_limit
 5        self.name = None
 6
 7    def __set_name__(self, whore_instance, name):
 8        self.name = name;
 9
10    def get_list(self, whore_instance):
11        if self.name not in whore_instance.__dict__:
12            whore_instance.__dict__[self.name] = []
13        return whore_instance.__dict__[self.name]
14
15    def __get__(self, whore_instance, objtype=None):
16        if whore_instance is None:
17            return ()
18        return tuple(self.get_list(whore_instance))
19
20    def __set__(self, whore_instance, value):
21        list = self.get_list(whore_instance)
22
23        if self.object_limit is not None:
24            if len(list) >= self.object_limit:
25                raise ValueError(
26                    "Slot is full!"
27                )
28
29        if self.size_limit is not None:
30            current_size = sum(len(item) for item in list)
31            if current_size + len(value) > self.size_limit:
32                raise ValueError(
33                    "Slot size limit exceeded!"
34                )
35
36        list.append(value)
37
38    def __delete__(self, obj):
39        list = self.get_list(obj)
40        if list:
41            list.pop(0)  

F......
======================================================================
FAIL: test_accessing_slot_through_class_returns_descriptor (test.TestSlot.test_accessing_slot_through_class_returns_descriptor)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 22, in test_accessing_slot_through_class_returns_descriptor
self.assertIsInstance(BasicWhore.front, Slot)
~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: () is not an instance of <class 'solution.Slot'>

----------------------------------------------------------------------
Ran 7 tests in 0.001s

FAILED (failures=1)

Дискусия
История
Това решение има само една версия.