From b6a55b5afe357f2847129cdf2158ececfec0dbf7 Mon Sep 17 00:00:00 2001 From: davidsantana06 Date: Mon, 25 Dec 2023 12:18:04 -0300 Subject: [PATCH] refactor: reduce code spacing and verbosity --- .gitignore | 3 +- {linkeds => src/linkeds}/__init__.py | 8 ++-- {linkeds => src/linkeds}/jsonifier.py | 2 +- {linkeds => src/linkeds}/list.py | 65 +++++++++------------------ {linkeds => src/linkeds}/node.py | 0 {linkeds => src/linkeds}/queue.py | 18 +++----- {linkeds => src/linkeds}/stack.py | 13 +++--- tests/test_list.py | 2 +- tests/test_node.py | 2 +- tests/test_queue.py | 2 +- tests/test_stack.py | 2 +- 11 files changed, 44 insertions(+), 73 deletions(-) rename {linkeds => src/linkeds}/__init__.py (59%) rename {linkeds => src/linkeds}/jsonifier.py (98%) rename {linkeds => src/linkeds}/list.py (91%) rename {linkeds => src/linkeds}/node.py (100%) rename {linkeds => src/linkeds}/queue.py (91%) rename {linkeds => src/linkeds}/stack.py (90%) diff --git a/.gitignore b/.gitignore index 8c0f190..8551a22 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,8 @@ .venv/ __pycache__/ __secret__.py -setup.py dist/ +pyproject.toml +setup.cfg *.egg-info/ *.json \ No newline at end of file diff --git a/linkeds/__init__.py b/src/linkeds/__init__.py similarity index 59% rename from linkeds/__init__.py rename to src/linkeds/__init__.py index 60320f9..6f9a514 100644 --- a/linkeds/__init__.py +++ b/src/linkeds/__init__.py @@ -1,9 +1,9 @@ from .jsonifier import ( - InvalidJsonException, + InvalidJson, Jsonifier ) from .list import ( - EmptyListException, FullListException, IndexListError, InvalidIterableAssignmentException, + EmptyList, FullList, IndexListError, InvalidIterableAssignment, LinkedList, BoundedList, DynamicList ) @@ -12,12 +12,12 @@ DoubleNode, SingleNode ) from .queue import ( - EmptyQueueException, FullQueueException, + EmptyQueue, FullQueue, LinkedQueue, BoundedQueue, DynamicQueue ) from .stack import ( - EmptyStackException, FullStackException, + EmptyStack, FullStack, LinkedStack, BoundedStack, DynamicStack ) diff --git a/linkeds/jsonifier.py b/src/linkeds/jsonifier.py similarity index 98% rename from linkeds/jsonifier.py rename to src/linkeds/jsonifier.py index 8ed9c26..b4a1b23 100644 --- a/linkeds/jsonifier.py +++ b/src/linkeds/jsonifier.py @@ -3,7 +3,7 @@ import json -class InvalidJsonException(Exception): +class InvalidJson(Exception): '''Exception raised for invalid JSON.''' def __init__(self, message: str = 'Invalid JSON') -> None: diff --git a/linkeds/list.py b/src/linkeds/list.py similarity index 91% rename from linkeds/list.py rename to src/linkeds/list.py index a38b02c..4dc100a 100644 --- a/linkeds/list.py +++ b/src/linkeds/list.py @@ -5,14 +5,14 @@ from .node import DoubleNode -class EmptyListException(Exception): +class EmptyList(Exception): '''Exception raised for attempting operations on an empty linked list.''' def __init__(self, message: str = 'List is empty') -> None: super().__init__(message) -class FullListException(Exception): +class FullList(Exception): '''Exception raised for attempting operations on a full linked list.''' def __init__(self, message: str = 'List is full') -> None: @@ -26,7 +26,7 @@ def __init__(self, message: str = 'List index out of range') -> None: super().__init__(message) -class InvalidIterableAssignmentException(Exception): +class InvalidIterableAssignment(Exception): '''Exception raised for invalid iterable assignments to a linked list.''' def __init__(self, message: str = 'Iterable must be a list, tuple, or set') -> None: @@ -34,7 +34,7 @@ def __init__(self, message: str = 'Iterable must be a list, tuple, or set') -> N class LinkedList(ABC): - '''Abstract base class for a doubly linked list.''' + '''Abstract base class for a linked list.''' ASSIGNABLE_ITERABLE_TYPES = (list, tuple, set) @@ -55,14 +55,12 @@ def is_empty(self) -> bool: def _add_first(self, data: object) -> None: '''Internal method to add a new node with the given data to the beginning of the list.''' node = DoubleNode(data) - if self.is_empty(): self._head = self._tail = node else: node.next = self._head self._head.prev = node self._head = node - self._size += 1 @abstractmethod @@ -73,14 +71,12 @@ def add_first(self, data: object) -> None: def _add_last(self, data: object) -> None: '''Internal method to add a new node with the given data to the end of the list.''' node = DoubleNode(data) - if self.is_empty(): self._head = self._tail = node else: node.prev = self._tail self._tail.next = node self._tail = node - self._size += 1 @abstractmethod @@ -105,15 +101,12 @@ def _insert(self, index: int, data: object) -> None: else: node = DoubleNode(data) current = self._head - for _ in range(index): current = current.next - node.next = current node.prev = current.prev current.prev.next = node current.prev = node - self._size += 1 @abstractmethod @@ -140,10 +133,8 @@ def get(self, index: int) -> object: raise IndexListError() node = self._head - for _ in range(index): node = node.next - return node.data def remove_first(self) -> object: @@ -151,21 +142,18 @@ def remove_first(self) -> object: Removes and returns the data of the first element in the list. Raises: - EmptyListException: If the list is empty. + EmptyList: If the list is empty. ''' if self.is_empty(): - raise EmptyListException() + raise EmptyList() data = self._head.data - if self._head is self._tail: self._head = self._tail = None else: self._head = self._head.next self._head.prev = None - self._size -= 1 - return data def remove_last(self) -> object: @@ -173,21 +161,18 @@ def remove_last(self) -> object: Removes and returns the data of the last element in the list. Raises: - EmptyListException: If the list is empty. + EmptyList: If the list is empty. ''' if self.is_empty(): - raise EmptyListException() + raise EmptyList() data = self._tail.data - if self._head is self._tail: self._head = self._tail = None else: self._tail = self._tail.prev self._tail.next = None - self._size -= 1 - return data def remove(self, index: int) -> object: @@ -195,42 +180,35 @@ def remove(self, index: int) -> object: Removes and returns the data of the element at the specified index. Raises: - EmptyListException: If the list is empty. + EmptyList: If the list is empty. IndexListError: If the index is out of range. ''' if self.is_empty(): - raise EmptyListException() + raise EmptyList() elif index < 0 or index >= self._size: raise IndexListError() - if index == 0: data = self.remove_first() elif index == self._size - 1: data = self.remove_last() else: node = self._head - for _ in range(index): node = node.next - data = node.data node.prev.next = node.next node.next.prev = node.prev - self._size -= 1 - return data def _reverse(self, **kwargs) -> 'LinkedList': '''Internal method to create and return a new reversed linked list.''' reverse_list = self.__class__(**kwargs) node = self._tail - while node is not None: reverse_list.add_last(node.data) node = node.prev - return reverse_list @abstractmethod @@ -258,7 +236,6 @@ def to_set(self) -> Set[object]: def __iter__(self) -> Iterator[object]: '''Iterator method to allow iterating through the elements of the linked list.''' node = self._head - while node is not None: yield node.data node = node.next @@ -286,10 +263,10 @@ def add_first(self, data: object) -> None: Adds data to the beginning of the list. Raises: - FullListException: If the list is full. + FullList: If the list is full. ''' if self.is_full(): - raise FullListException() + raise FullList() self._add_first(data) @@ -298,10 +275,10 @@ def add_last(self, data: object) -> None: Adds data to the end of the list. Raises: - FullListException: If the list is full. + FullList: If the list is full. ''' if self.is_full(): - raise FullListException() + raise FullList() self._add_last(data) @@ -310,10 +287,10 @@ def insert(self, index: int, data: object) -> None: Inserts data at the specified index. Raises: - FullListException: If the list is full. + FullList: If the list is full. ''' if self.is_full(): - raise FullListException() + raise FullList() self._insert(index, data) @@ -326,15 +303,14 @@ def assign_iterable(self, iterable: Union[List[object], Tuple[object], Set[objec Assigns data from an iterable to the linked list. Raises: - InvalidIterableAssignmentException: If the iterable type is not supported. + InvalidIterableAssignment: If the iterable type is not supported. ''' if type(iterable) not in self.ASSIGNABLE_ITERABLE_TYPES: - raise InvalidIterableAssignmentException() + raise InvalidIterableAssignment() self._head = self._tail = None self._size = 0 self._capacity = len(iterable) - for item in iterable: self.add_last(item) @@ -379,14 +355,13 @@ def assign_iterable(self, iterable: Union[List[object], Tuple[object], Set[objec Assigns data from an iterable to the linked list. Raises: - InvalidIterableAssignmentException: If the iterable type is not supported. + InvalidIterableAssignment: If the iterable type is not supported. ''' if type(iterable) not in self.ASSIGNABLE_ITERABLE_TYPES: - raise InvalidIterableAssignmentException() + raise InvalidIterableAssignment() self._head = self._tail = None self._size = 0 - for item in iterable: self.add_last(item) diff --git a/linkeds/node.py b/src/linkeds/node.py similarity index 100% rename from linkeds/node.py rename to src/linkeds/node.py diff --git a/linkeds/queue.py b/src/linkeds/queue.py similarity index 91% rename from linkeds/queue.py rename to src/linkeds/queue.py index 89f062a..8c66833 100644 --- a/linkeds/queue.py +++ b/src/linkeds/queue.py @@ -2,14 +2,14 @@ from .node import SingleNode -class EmptyQueueException(Exception): +class EmptyQueue(Exception): '''Exception raised for attempting operations on an empty queue.''' def __init__(self, message: str = 'Queue is empty') -> None: super().__init__(message) -class FullQueueException(Exception): +class FullQueue(Exception): '''Exception raised for attempting to enqueue into a full queue.''' def __init__(self, message: str = 'Queue is full') -> None: @@ -36,13 +36,11 @@ def is_empty(self) -> bool: def _enqueue(self, data: object) -> None: '''Internal method to enqueue a new node with the given data.''' node = SingleNode(data) - if self.is_empty(): self._front = self._rear = node else: self._rear.next = node self._rear = node - self._size += 1 @abstractmethod @@ -55,18 +53,16 @@ def dequeue(self) -> object: Removes and returns the front element from the queue. Raises: - EmptyQueueException: If the queue is empty. + EmptyQueue: If the queue is empty. ''' if self.is_empty(): - raise EmptyQueueException() + raise EmptyQueue() data = self._front.data self._front = self._front.next self._size -= 1 - if self.is_empty(): self._rear = None - return data def peek(self) -> object: @@ -101,13 +97,13 @@ def enqueue(self, data: object) -> None: Enqueues data into the queue. Raises: - FullQueueException: If the queue is full. + FullQueue: If the queue is full. ''' if self.is_full(): - raise FullQueueException() + raise FullQueue() self._enqueue(data) - + class DynamicQueue(LinkedQueue): '''Class representing a dynamic (unbounded) queue.''' diff --git a/linkeds/stack.py b/src/linkeds/stack.py similarity index 90% rename from linkeds/stack.py rename to src/linkeds/stack.py index c1d9e1f..25f2163 100644 --- a/linkeds/stack.py +++ b/src/linkeds/stack.py @@ -2,14 +2,14 @@ from .node import SingleNode -class EmptyStackException(Exception): +class EmptyStack(Exception): '''Exception raised for attempting operations on an empty stack.''' def __init__(self, message: str = 'Stack is empty') -> None: super().__init__(message) -class FullStackException(Exception): +class FullStack(Exception): '''Exception raised for attempting to push into a full stack.''' def __init__(self, message: str = 'Stack is full') -> None: @@ -48,15 +48,14 @@ def pop(self) -> object: Removes and returns the top element from the stack. Raises: - EmptyStackException: If the stack is empty. + EmptyStack: If the stack is empty. ''' if self.is_empty(): - raise EmptyStackException() + raise EmptyStack() data = self._top.data self._top = self._top.next self._size -= 1 - return data def peek(self) -> object: @@ -91,10 +90,10 @@ def push(self, data: object) -> None: Pushes data onto the stack. Raises: - FullStackException: If the stack is full. + FullStack: If the stack is full. ''' if self.is_full(): - raise FullStackException() + raise FullStack() self._push(data) diff --git a/tests/test_list.py b/tests/test_list.py index 1c4c526..15b479d 100644 --- a/tests/test_list.py +++ b/tests/test_list.py @@ -2,7 +2,7 @@ from pytest import fixture import json -from linkeds import BoundedList, DynamicList +from src.linkeds import BoundedList, DynamicList GAMES = ( diff --git a/tests/test_node.py b/tests/test_node.py index 2b08e4d..9179d2a 100644 --- a/tests/test_node.py +++ b/tests/test_node.py @@ -1,7 +1,7 @@ from pytest import fixture from typing import List -from linkeds import DoubleNode, SingleNode +from src.linkeds import DoubleNode, SingleNode NAMES = ( diff --git a/tests/test_queue.py b/tests/test_queue.py index 592da38..0297dd1 100644 --- a/tests/test_queue.py +++ b/tests/test_queue.py @@ -1,5 +1,5 @@ from pytest import fixture -from linkeds import BoundedQueue, DynamicQueue +from src.linkeds import BoundedQueue, DynamicQueue COMPONENTS = ( diff --git a/tests/test_stack.py b/tests/test_stack.py index 920f9e1..05a8b15 100644 --- a/tests/test_stack.py +++ b/tests/test_stack.py @@ -1,5 +1,5 @@ from pytest import fixture -from linkeds import BoundedStack, DynamicStack +from src.linkeds import BoundedStack, DynamicStack PROCESSES = (