Last active
April 28, 2021 17:30
-
-
Save rodrigoddc/0422a1d0bda5ca23caa99d0583b79208 to your computer and use it in GitHub Desktop.
Class attr metaprograming
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ### STACK CLASS | |
| class Stack: | |
| def __init__(self, item=None): | |
| self._count = 0 | |
| # TODO: de alguma forma dispensar o tipo list de _items para não precisar do append no __setitem__ | |
| self._items = [item] | |
| def __getitem__(self, index): | |
| try: | |
| return self._items[index] | |
| except: | |
| raise StackPopException('Fail') | |
| def __setitem__(self, value): | |
| self._count += 1 | |
| self._items.append(value) | |
| def is_empty(self): | |
| if self._count == 0: | |
| return True | |
| return False | |
| def push(self, item): | |
| self.__setitem__(item) | |
| def pop(self): | |
| if self._count < 1: | |
| raise StackPopException("No itens to pop() out") | |
| result = self.__getitem__(self._count) | |
| self._count -= 1 | |
| return result | |
| def add(self, item): | |
| self.push(item) | |
| def __len__(self): | |
| return self._count | |
| def __str__(self): | |
| if self._count == 0: | |
| return '' | |
| result = '' | |
| for i in range(1, self._count - 1): | |
| result += f'{self._items[i]} -> {self._items[i + 1]}' | |
| return result | |
| class StackPopException(Exception): | |
| """ Raised when tried to pop() element from stack obj when its is empty """ | |
| def __init__(self, message): | |
| self.message = message | |
| ### TEST STACK CLASS | |
| import unittest | |
| from stack import Stack, StackPopException | |
| class StackTestCase(unittest.TestCase): | |
| def test_is_empty(self): | |
| stack = Stack() | |
| assert stack.is_empty() is True | |
| stack.push(1) | |
| assert stack.is_empty() is False | |
| stack.pop() | |
| assert stack.is_empty() is True | |
| def test_size(self): | |
| stack = Stack() | |
| assert len(stack) == 0 | |
| stack.push(1) | |
| assert len(stack) == 1 | |
| stack.pop() | |
| assert len(stack) == 0 | |
| def test_push_items(self): | |
| stack = Stack() | |
| tests = [1, '0', Stack, lambda x: x, {}, [], None] | |
| for test in tests: | |
| stack.add(test) | |
| assert len(tests) == len(stack) | |
| def test_pop_items(self): | |
| stack = Stack() | |
| with self.assertRaises(StackPopException): | |
| stack.pop() | |
| one = 1 | |
| two = 2 | |
| stack.push(one) | |
| stack.push(two) | |
| assert len(stack) == 2 | |
| assert stack.pop() == two | |
| assert stack.pop() == one | |
| assert len(stack) == 0 | |
| def test_print(self): | |
| stack = Stack() | |
| assert print(stack) == '' | |
| stack.push(3) | |
| stack.push(1) | |
| stack.push(2) | |
| assert print(stack) == '2 -> 1 -> 3' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment