-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueues_example.py
75 lines (66 loc) · 2.65 KB
/
queues_example.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
# Initialize the Printer class
class Printer():
"""
The Printer class allow users to enter a queue to print a document.
It is implemented following the FIFO method to process the requests.
"""
class Node():
"""
Each Node in the queue will have a value (the document to print) assigned to it and the user who sent the document.
"""
def __init__(self, document, user):
self.value = document
self.user = user
def __str__(self):
"""
Graphical representation of the node
"""
return "Document: {} sent from {}".format(self.value, self.user)
def __init__(self):
"""
Initialize an empty queue
"""
self.queue = []
def enqueue_document(self, value, user):
"""
Add a new Node at the back of the queue with a value and a user.attached. The Node will always be added at the back of the queue.
"""
new_node = Printer.Node(value, user)
self.queue.append(new_node)
def dequeue_documents(self):
"""
Dequeue all the elements, starting from the first.
Print all the documents until the queue is empty
"""
# Check if the queue is empty
if len(self.queue) == 0:
print("There are no documents in the printer queue")
return None
while len(self.queue) != 0:
item_to_print = self.queue.pop(0)
print(f"\nPrinting item {item_to_print.value} from user {item_to_print.user}")
print("Printing complete... Ready to print next item")
if len(self.queue) == 0:
print("\nPrinting complete. The queue is empty.")
def __len__(self):
"""
Support the len() function
"""
return len(self.queue)
def __str__(self):
"""
Suppport the str() function to provide a string representation of the queue.
"""
string = "["
for node in self.queue:
string += str(node) # This uses the __str__ from the Node class
string += ", "
string += "]"
return string
# Test cases
printer_queue = Printer() # Create the Printer object
printer_queue.enqueue_document('text1.txt', 'Anne') # Add a document to the queue
printer_queue.enqueue_document('text2.txt', 'Bob') # Add a document to the queue
printer_queue.enqueue_document('text3.txt', 'Charlie') # Add a document to the queue
print(printer_queue) # [Document: text1.txt sent from Anne, Document: text2.txt sent from Bob, Document: text3.txt sent from Charlie, ]
printer_queue.dequeue_documents() # Print all the documents in the queue