Simulate a printing queue of documents located in a tuple list

Hi!,

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:

queue =[]
def add_document(device, document_name):
    
    items = [
        ("printer-1", "pdf-1"),
        ("printer-2", "pdf-2"),
        ("printer-3", "pdf-3"),
        ("printer-4", "pdf-4"),
        ("printer-5", "pdf-5"),
        ("printer-6", "pdf-6")
    ]    
    for i in items:
        printer_name = i[0]
        document_name = i[1]
        

Hello,

Point 1

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.

I asked my teacher about a solution and he sent me one. Basically i didn’t learned about Class yet.

This is the solution he proposed to me based on lessons and modules learned until now.

items_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")
    ]

def print_element_queue(queue):
    print("display ...")
    for elm in queue:
        print(f"For device {elm[0]} was send element {elm[1]}" )

def add_document(queue, device, document_name):
    t = (device, document_name)
    queue.append(t)

def dequeue_elem(queue):
    elem = queue.pop(0)
    return elem

# if __name__ == "__main__":
def  main():
    print("Simulation of the functioning of a queue")
    print_element_queue(items_queue)
    add_document(items_queue, "display", "pdf_new")
    print_element_queue(items_queue)
    elm_rm = dequeue_elem(items_queue)
    print(f"Was remove for device {elm_rm[0]} with document {elm_rm[1]}")
    print_element_queue(items_queue)

main()