Can anyone give me few ideas how to accomplish my task from my python courses?
I need to:
create a list for simulating the queue
implement the add_document(device, document_name) method which adds the document at the end of the queue to be printed as an ordered tuple;
implement the print_document() method which removes the document from the start of the queue and simulates the printing through print to screen the name of the document and the printer from which came from;
add testing examples in which is simulated adding documents from various printers, and also their removal and printing.
The queue tuple list i guess would look like:
queue = [
("printer-1", "pdf-1"),
("printer-2", "pdf-2"),
("printer-3", "pdf-3"),
("printer-4", "pdf-4"),
("printer-5", "pdf-5"),
("printer-6", "pdf-6")
]
add_document(device, document_name)
print_document()
Are functions?
Elements from a tuple queue list are accesed with index from what ive learned. Basically something like:
for i in queue:
printer_name = i[0]
document_name = i[1]
The ideea is that i dont know how to achieve this task. Here is the code i’ve been trying until now but i dont know how to continue:
the best way to do this is wrapping a list with a class so that you can manage it such that it bahaves like a queue. Recall that a queue is first in, first out (i.e., FIFO).
Point 2
Recall that to add an item to a list, you will have to make use of the append list built-in function. For example, like this:
some_list.append('add this item')
Thus, create a method with this functionality when called.
Point 3
Create a method (ahem, function within the class) such that when it is called, it removes the first item from the queue.
Point 4
I will leave this up to you to ponder.
Definitely need to use a class for this one. At a minimum, you will need two methods: a put method to add an item to the queue(list), and a get method to both obtain and delete (remove) an item from the queue (the oldes item in the queue).
Hope this helps. Good luck!
Update:
I noticed this upon second reading. You should not use a tuple as it is immutable (can neither add or remove items). Use a list - it is mutable.