Домашни > Работилница за отвари! > Решения > Решението на Деян Диков

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

2 точки общо

4 успешни теста
16 неуспешни теста
Код
Скрий всички коментари

 1def depleted(effect, count=1):
 2
 3    def inner(*args, **kwargs):
 4        nonlocal count
 5        if count > 0:
 6            while count > 0:
 7                effect(*args, **kwargs)
 8                count -= 1
 9        else:
10            raise TypeError('Effect is depleted!')
11
12    return inner
13
14
15class Potion:
16    def __init__(self, effects: dict[str, callable], duration: float):
17        self.effects = effects
18        self.duration = duration
19        self.intensities = {}
20        for name, effect in zip(effects.keys(), effects.values()):
21            self.__setattr__(name, depleted(effect))
22            self.intensities[name] = 1
23
24    def __add__(self, other):
25        for name, effect in zip(other.effects.keys(), other.effects.values()):
26            if name in self.__dict__:
27                self.intensities[name] += 1
28            else:
29                self.intensities[name] = 1
30            self.__setattr__(name, depleted(effect, self.intensities[name]))
31        return self
32
33    def __mul__(self, n):
34        pass
35
36    def __truediv__(self, n):
37        pass
38
39    def __eq__(self, other):
40        pass
41
42    def __lt__(self, other):
43        pass
44
45    def __gt__(self, other):
46        pass
47
48
49class ГоспожатаПоХимия:
50    pass

.F.FF..FEEEEEEEEEEEE
======================================================================
ERROR: test_dilution (test.TestPotionOperations)
Test dilution of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 119, in test_dilution
half_potion.int_attr_fun(self._target)
AttributeError: 'NoneType' object has no attribute 'int_attr_fun'

======================================================================
ERROR: test_potentiation (test.TestPotionOperations)
Test potentiation of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 111, in test_potentiation
potion.int_attr_fun(self._target)
AttributeError: 'NoneType' object has no attribute 'int_attr_fun'

======================================================================
ERROR: test_purification (test.TestPotionOperations)
Test purification of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 165, in test_purification
potion = potion1 - potion2
TypeError: unsupported operand type(s) for -: 'Potion' and 'Potion'

======================================================================
ERROR: test_separation (test.TestPotionOperations)
Test separation of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 213, in test_separation
potion1, potion2, potion3 = potion / 3
TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'

======================================================================
ERROR: test_applying_depleted_potion (test.TestГоспожатаПоХимия)
Test applying a depleted potion or a potion that was used in a reaction.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 379, in test_applying_depleted_potion
self._dimitrichka.apply(self._target, potion)
AttributeError: 'ГоспожатаПоХимия' object has no attribute 'apply'

======================================================================
ERROR: test_applying_normal_case (test.TestГоспожатаПоХимия)
Test applying a normal potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 350, in test_applying_normal_case
self._dimitrichka.apply(self._target, potion)
AttributeError: 'ГоспожатаПоХимия' object has no attribute 'apply'

======================================================================
ERROR: test_applying_order (test.TestГоспожатаПоХимия)
Test applying order of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 404, in test_applying_order
self._dimitrichka.apply(self._target, potion)
AttributeError: 'ГоспожатаПоХимия' object has no attribute 'apply'

======================================================================
ERROR: test_applying_part_of_potion (test.TestГоспожатаПоХимия)
Test applying only a part of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 365, in test_applying_part_of_potion
self._dimitrichka.apply(self._target, potion)
AttributeError: 'ГоспожатаПоХимия' object has no attribute 'apply'

======================================================================
ERROR: test_ticking_immutable (test.TestГоспожатаПоХимия)
Test ticking after applying a potion with immutable attributes.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 424, in test_ticking_immutable
self._dimitrichka.apply(self._target, potion)
AttributeError: 'ГоспожатаПоХимия' object has no attribute 'apply'

======================================================================
ERROR: test_ticking_multiple_potions (test.TestГоспожатаПоХимия)
Test ticking after applying multiple potions which affect the same attribute.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 454, in test_ticking_multiple_potions
self._dimitrichka.apply(self._target, potion1)
AttributeError: 'ГоспожатаПоХимия' object has no attribute 'apply'

======================================================================
ERROR: test_ticking_multiple_targets (test.TestГоспожатаПоХимия)
Test ticking after applying a potion with mutable attributes.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 485, in test_ticking_multiple_targets
self._dimitrichka.apply(target1, potion1)
AttributeError: 'ГоспожатаПоХимия' object has no attribute 'apply'

======================================================================
ERROR: test_ticking_mutable (test.TestГоспожатаПоХимия)
Test ticking after applying a potion with mutable attributes.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 438, in test_ticking_mutable
self._dimitrichka.apply(self._target, potion)
AttributeError: 'ГоспожатаПоХимия' object has no attribute 'apply'

======================================================================
FAIL: test_depletion (test.TestBasicPotion)
Test depletion of a potion effect.
----------------------------------------------------------------------
TypeError: Effect is depleted!

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/tmp/test.py", line 78, in test_depletion
with self.assertRaisesRegex(TypeError, 'Effect is depleted\.'):
AssertionError: "Effect is depleted\." does not match "Effect is depleted!"

======================================================================
FAIL: test_equal (test.TestPotionComparison)
Test equality of potions.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 292, in test_equal
self.assertEqual(potion1 + potion2, potion3)
AssertionError: <solution.Potion object at 0x7f265c2a3160> != <solution.Potion object at 0x7f265c2a3040>

======================================================================
FAIL: test_superbness (test.TestPotionComparison)
Test superbness of potions.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 317, in test_superbness
self.assertLess(potion1, potion2)
AssertionError: <solution.Potion object at 0x7f265c2a3040> not less than None

======================================================================
FAIL: test_deprecation (test.TestPotionOperations)
Test deprecation of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 257, in test_deprecation
with self.assertRaisesRegex(TypeError, 'Potion is now part of something bigger than itself\.'):
AssertionError: TypeError not raised

----------------------------------------------------------------------
Ran 20 tests in 0.002s

FAILED (failures=4, errors=12)

Дискусия
История

n1def depleted(effect):n1def depleted(effect, count=1):
2    count = 0
32
4    def inner(*args, **kwargs):3    def inner(*args, **kwargs):
5        nonlocal count4        nonlocal count
n6        if count == 0:n5        if count > 0:
6            while count > 0:
7            effect(*args, **kwargs)7                effect(*args, **kwargs)
8            count += 18                count -= 1
9        else:9        else:
10            raise TypeError('Effect is depleted!')10            raise TypeError('Effect is depleted!')
1111
12    return inner12    return inner
1313
1414
15class Potion:15class Potion:
n16    def __init__(self, effects: dict, duration: float):n16    def __init__(self, effects: dict[str, callable], duration: float):
17        self.effects = effects17        self.effects = effects
18        self.duration = duration18        self.duration = duration
nn19        self.intensities = {}
19        for name, effect in zip(effects.keys(), effects.values()):20        for name, effect in zip(effects.keys(), effects.values()):
20            self.__setattr__(name, depleted(effect))21            self.__setattr__(name, depleted(effect))
n21   n22            self.intensities[name] = 1
2223
23    def __add__(self, other):24    def __add__(self, other):
t24       return self.effects.update(other.effects)t25        for name, effect in zip(other.effects.keys(), other.effects.values()):
26            if name in self.__dict__:
27                self.intensities[name] += 1
28            else:
29                self.intensities[name] = 1
30            self.__setattr__(name, depleted(effect, self.intensities[name]))
31        return self
2532
26    def __mul__(self, n):33    def __mul__(self, n):
27        pass34        pass
2835
29    def __truediv__(self, n):36    def __truediv__(self, n):
30        pass37        pass
3138
32    def __eq__(self, other):39    def __eq__(self, other):
33        pass40        pass
3441
35    def __lt__(self, other):42    def __lt__(self, other):
36        pass43        pass
3744
38    def __gt__(self, other):45    def __gt__(self, other):
39        pass46        pass
4047
4148
42class ГоспожатаПоХимия:49class ГоспожатаПоХимия:
43    pass50    pass
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op