-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.py
executable file
·152 lines (128 loc) · 4.3 KB
/
console.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/python3
"""
Module Name:
console
Module Description:
This module contains one function and one class which manage the console
Module Functions:
- get_data_structure() -> LinkedList | List
Module Classes:
- LIFO_FIFO_CONSOLE(cmd.Cmd)
Module Attributes:
- None
"""
import cmd
from os import getenv
from classes.doubly_linked_list import LinkedList
from classes.list import List
from typing import Union
import sys
def get_data_structure() -> Union[LinkedList, List]:
"""
Obtaining the data structure of a environment variable
"""
data_structure_type = getenv('DATA_STRUCTURE', 'list')
if data_structure_type.lower() == 'linkedlist':
return LinkedList()
else:
return List()
# Obtaining the data structure and reloading the data
data_structure = get_data_structure()
data_structure.reload()
class LIFO_FIFO_CONSOLE(cmd.Cmd):
"""
This class is a command-line interface (CLI) that allows you to
interact with a LIFO (Last-In, First-Out) or FIFO (First-In, First-Out)
data structure, depending on the implementation used.
"""
if sys.__stdin__.isatty():
intro = f"You're using {data_structure.__class__.__name__} as a data_structure"
prompt = f"({data_structure.__class__.__name__})> "
else:
prompt = ""
def do_push(self, args: str) -> None:
"""
This method adds an element to the top of the data structure.
The argument 'args' is expected to contain the number to be added.
If the argument is not an integer, an error message is displayed.
"""
try:
# destructuring the args and discarding the following fields
number, *_ = args
# Checking if can be parse to an integer
number = int(number)
except ValueError:
print("Invalid data, must be an integer")
return
# Adding as a second argument
data_structure.append(number)
def do_add(self, args: str) -> None:
"""
This method adds the top two member values of the data structure.
If the argument is not an integer, an error message is displayed.
"""
try:
# destructuring the args and discarding the following fields
number, *_ = args
# Checking if can be parse to an integer
number = int(number)
except ValueError:
print("Invalid data, must be an integer")
return
# Adding as a second argument
data_structure.add(number)
def do_swap(self, args: str) -> None:
"""
This method swaps the order of the first and second
elements in the data structure.
"""
try:
data_structure.swap()
except IndexError:
print("There are no enough elements in the data structure")
return
def do_pall(self, args: str) -> None:
"""
This method displays every member of the data structure.
"""
data_structure.print()
def do_pint(self, args: str) -> None:
"""
This method displays the member value at the top of the data structure.
"""
try:
data_structure.pint()
except IndexError:
print("There are no elements in the data structure")
return
def do_pop(self, args: str) -> None:
"""
This method removes an element from the top
of the data structure and displays it.
"""
try:
print(data_structure.pop())
except IndexError:
print("There are no elements in the data structure")
return
def do_quit(self, args: str) -> bool:
"""
This method saves the data structure and quits from the CLI.
"""
data_structure.save()
return True
def do_EOF(self, args: str) -> bool:
"""
This method saves the data structure and quits from the CLI.
"""
data_structure.save()
return True
def cmdloop(self, intro=None):
try:
return super().cmdloop(intro=intro)
except KeyboardInterrupt:
print("KeyboardInterrupt detected. Saving data and quitting...")
data_structure.save()
return True
if __name__ == "__main__":
LIFO_FIFO_CONSOLE().cmdloop()