How do I "run" a function that is in a queue? - python

Basically I created a queue in Python with some functions (that the user put in the order he wants) and now I want to execute this functions in order, but I really didn't find what which order do that.
The queue:
class Queue:
def __init__(self):
self.elements = []
def enqueue(self, data):
self.elements.append(data)
return data
def dequeue(self):
return self.elements.pop(0)
def rear(self):
return self.elements[-1]
def front(self):
return self.elements[0]
def is_empty(self):
return len(self.elements) == 0
The functions:
if (escolha=='2'):
print("Type your hotkey. Ex: alt+tab+ [3 keys maximum]")
my_var = input("")
my_var = my_var.split('+')
def hotchave():
pyautogui.hotkey(str(my_var[0]),str(my_var[1]),str(my_var[2]))
queue.enqueue(hotchave)
if (escolha=='3'):
write=input('What will be written?: ')
def escrever():
pyautogui.write(write)
queue.enqueue(escrever)
I already tried things like return, front but didn't seem to work.

There's several different ways to do what you want, some of which require to modify your class. Since you say you would like to retrieve and run multiple functions from the queue, I would suggest making it possible to iterate its current contents, which can be done simply by adding an __iter__() method to the class.
The following shows how to do that, along with how to make use of it.
class Queue:
def __init__(self):
self.elements = []
def enqueue(self, data):
self.elements.append(data)
return data
def dequeue(self):
return self.elements.pop(0)
def rear(self):
return self.elements[-1]
def front(self):
return self.elements[0]
def is_empty(self):
return len(self.elements) == 0
def __iter__(self):
'''Iterate current contents.'''
return (elem for elem in self.elements)
Sample usage:
queue = Queue()
my_var = 'alt+shift+k'
my_var = my_var.split('+')
def hotchave():
print('vars:', str(my_var[0]), str(my_var[1]), str(my_var[2]))
queue.enqueue(hotchave)
write = 'This will be written'
def escrever():
print(write)
queue.enqueue(escrever)
# Execute commands in Queue.
for command in queue:
command()
print('-fini-')
Output:
vars: alt shift k
This will be written
-fini-

Related

Queue implementation display function error

class Queue:
def __init__(self): '''initialization of function'''
self.items = []
def is_empty(self): '''Checking if queue is empty or not'''
return self.items == []
def enqueue(self, data): '''Adding value '''
self.items.append(data)
def dequeue(self): ''' Removing value'''
return self.items.pop(0)
def dis(self): '''Printing the stored item in queue'''
print(items)
After that initialization of Queue:
q = Queue()
while True:
print('enqueue <value>')
print('dequeue')
print('dis')
print('quit')
do = input('What would you like to do? ').split()
operation = do[0].strip().lower()
if operation == 'enqueue':
q.enqueue(int(do[1]))
elif operation == 'dequeue':
if q.is_empty():
print('Queue is empty.')
else:
print('Dequeued value: ', q.dequeue())
elif operation == 'dis':
q.dis()
elif operation == 'quit':
break
else:
print("Enter the correct operation")
I'm not able to display the items which are enqueued in the Queue. How could I use dis() method to display items in it?
Think you should print self.items (instead of items)
def dis(self): '''Printing the stored item in queue'''
print(self.items)
That'll output the list using standard formatting, not very pretty so you'll probably want to add some extra logic for pretty-printing.
If you want to restrict the queue size, you could do this by simply ignoring items past a certain limit. For this you'll need to implement a limit, along with logic for checking the limit. For example:
class Queue:
def __init__(self, size=8): '''initialization of function'''
self.items = []
self.size = size
def is_empty(self): '''Checking if queue is empty or not'''
return self.items == []
def enqueue(self, data): '''Adding value '''
if len(self.items) < self.size:
self.items.append(data)
else:
pass # behavior when queue is already full
def dequeue(self): ''' Removing value'''
return self.items.pop(0)
def dis(self): '''Printing the stored item in queue'''
print(items)
class Queue:
items=[]
size=5
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def enqueue(self, data):
self.items.append(data)
def dequeue(self):
return self.items.pop(0)
def dis(self):
print(self.items)
def is_full(self):
if (len(self.zitems)>5):
print("Queue is full")
else:
print("Not full")
Inizialize the items=[] and in dis method add self.items.Also, you could check if the size of the queue is full or not

using queue to add a shape in canvas

I was confused on how to add a shape using a queue in canvas. I'm trying to add a triangle. This is the code I've got. I'm getting an error >>> AttributeError: 'Queue' object has no attribute 'enqueue'
I've been trying this for awhile now and I thought I had the right idea, I just need some guidelines thanks!
from tkinter import *
import random
root = Tk()
from queue import *
class Queue:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def size(self):
return len(self.items)
def enqueue(self, item):
self.items.insert(0, item)
def dequeue(self):
return self.items.pop(0)
def peek(self):
return self.items[0]
class Recta():
def __init__(self, height=60, width=80):
self.queue = Queue()
def create_buttons(self):
self.button5 = Button(self.frame, text = "Add Arc", command = self.random_arc)
self.button5.pack(side = 'left')
def random_arc(self):
w = random.randrange(45)
h = random.randrange(90)
self.queue.enqueue(self.canvas.create_arc(0,w,h,fill= "green"))
tes = Recta()
tes = Queue()
root.mainloop()
For the module queue I don't see a function enqueue().
There is queue.Queue.put(), though.
Update
Is it possible, that python is taking the "wrong" Queue, meaning the one from inside the module queue?
Update
Maybe start by renaming your class "Queue" to something different like "Queue1".
Or just use the queue.Queue from the module.
Update
If you want to use a stack, just use list funcitonalities, no class Stack is necessary:
stack = list()
stack.append('item1')
stack.append('item2')
stack.append('item3')
print stack.pop()
>>> 'item3'
By just using .pop() you make it appear to be a stack.

Understanding Stacks and Queues in python

So i was given this question. Consider the Stack and the Queue class with standard set of operations. Using the Stack and Queue class, what items are contained in them just before the mysteryFunction is called AND just after the mysteryFunction is called?
Here is the code:
def mysteryFunction(s, q):
q.enqueue('csc148')
q.enqueue(True)
q.enqueue(q.front())
q.enqueue('abstract data type')
for i in range(q.size()):
s.push(q.dequeue())
while not s.is_empty():
q.enqueue(s.pop())
if __name__ == '__main__':
s=Stack()
q=Queue()
#About to call mysteryFunction
#What are contents of s and q at this point?
mysteryFunction(s, q)
#mysteryFunction has been called.
#What are contents of s and q at this point?
I'm having trouble understanding object oriented programming as i'm new to this topic. Is there any link that breaks down Stacks and Queues and what they do?
In general, stacks are LIFO and queues are FIFO.
In Python, you can use the collections module to experiment with stacks and queues:
>>> from collections import deque
>>> stack = deque()
>>> stack.append(10)
>>> stack.append(20)
>>> stack.append(30)
>>> stack
deque([10, 20, 30])
>>> stack.pop() # LIFO
30
>>> stack.pop()
20
>>>
>>> queue = deque()
>>> queue.append(10)
>>> queue.append(20)
>>> queue.append(30)
>>> queue
deque([10, 20, 30])
>>> queue.popleft() # FIFO
10
>>> queue.popleft()
20
See following links for more information:
Stack
Queue
Visually these two data structures can be seen in a following way:
Stack:
Description:
There are variations of this data structure. However, in simple terms - as one can observe in the image provided, when you add to this data structure you place on top of what is already there and when you remove you also take from the top. You can view it as a stack of books which you go through one by one starting from the top and all the way down.
Queue
Description:
There are also variations on this particular data structure, however in simple terms - as you can see in the image provided, when you add to this data structure the new element goes in the begining and when you remove its the last element from the list which is being removed. You can imagine it as a queue you got in a shop where you stand behind a lot of people waiting for your turn to come to the counter to pay for your items.
To test this line by line, here are implementations of the classes (wrappers around deque) that are used in the task:
from collections import deque
class Queue(deque):
enqueue = deque.append
dequeue = deque.popleft
def front(self):
return self[-1]
def size(self):
return len(self)
class Stack(deque):
push = deque.append
def is_empty(self):
return not self
STACK #LIFO
class stack(object):
def __init__(self):
self.items = []
def isEmpty(self):
return self.items==[]
def push(self,item):
self.items.append(item)
def pop (self):
return self.items.pop()
def peek(self):
return self.items[len(self.items) - 1]
def size(self):
return (len(self.items))
s = stack()
print (s.isEmpty())
>> True
s.push(1)
s.push('3')
s.peek()
>>'3'
s.size()
>> 2
Queue #FIFO
class Queue(object):
def __init__(self):
self.items = []
def isEmpty(self):
return self.items==[]
def enqueue(self,item):
self.items.insert(0,item)
def dequeue(self):
return self.items.pop()
def size(self):
return (len(self.items))
q = Queue()
q.isEmpty()
>>True
q.enqueue(1)
q.enqueue(2)
q.dequeue()
>>1
The first code explains about the stack , we need to create a list and while pushing the element use append and fill the list , similar to what we have in array stack. While pop , pop out the end from where you pushed it.
class Stack:
def __init__(self):
self.stack = []
def push_element(self,dataval):
self.stack.append(dataval)
return self.stack
def pop_element(self):
if len(self.stack) ==0:
print("Stack is empty")
else:
self.stack.pop()
return self.stack
def peek_element(self):
return self.stack[0]
class Queue:
def __init__(self):
self.stack = []
def push_ele(self,data):
self.stack.append(data)
def pop_ele(self):
self.stack.pop(0)
def display(self):
print(self.stack)
class stack:
def __init__(self,n): #constructor
self.no = n # size of stack
self.Stack = [] # list for store stack items
self.top = -1
def push(self): # push method
if self.top == self.no - 1: # check full condition
print("Stack Overflow.....")
else:
n = int(input("enter an element :: "))
self.Stack.append(n) # in list add stack items use of append method
self.top += 1. # increment top by 1
def pop(self): # pop method
if self.top == -1: # check empty condition
print("Stack Underflow....")
else:
self.Stack.pop(). # delete item from top of stack using pop method
self.top -= 1 # decrement top by 1
def peep(self): #peep method
print(self.top,"\t",self.Stack[-1]) #display top item
def disp (self): # display method
if self.top == -1: # check empty condition
print("Stack Underflow....")
else:
print("TOP \tELEMENT")
for i in range(self.top,-1,-1): # print items and top
print(i," \t",self.Stack[i])
n = int(input("Enter Size :: ")) # size of stack
stk = stack(n) # object and pass n as size
while(True): # loop for choice as a switch case
print(" 1: PUSH ")
print(" 2: POP ")
print(" 3: PEEP ")
print(" 4: PRINT ")
print(" 5: EXIT ")
option = int(input("enter your choice :: "))
if option == 1:
stk.push()
elif option == 2:
stk.pop()
elif option == 3:
stk.peep()
elif option == 4:
stk.disp()
elif option == 5:
print("you are exit!!!!!")
break
else:
print("Incorrect option")

Taking a queue of integers and making copies of that element

"Write a function named stutter that accepts a queue of integers as a parameter and replaces every element of the queue with two copies of that element in the original queue."
Example:
q1 = Queue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
stutter(q1)
while not queue.isempty():
print(queue.dequeue(), end=' ')
should give answer as "1 1 2 2 3 3"
class Queue:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def enqueue(self, item):
self.items.insert(0,item)
def dequeue(self):
return self.items.pop()
def size(self):
return len(self.items)
def check_empty(self):
if self.items == []:
return True
def stutter(Q):
queue=Queue()
stack = Stack()
while not queue.isempty():
stack.push(queue.dequeue())
while not stack.isempty():
queue.enqueue(stack.pop())
That is the code that i have written, with that i can get it to print once and once only, i can't get it to duplicate and sort in order.
Try using this (can't test at the moment):
def stutter(queue):
# iterate all items
for _ in range(queue.size()):
# store item in local context
item = queue.dequeue()
# push two *references* to item into the queue
queue.enqueue(item)
queue.enqueue(item)
This will iterate all items once, immediately pushing two copes to the back of the queue. The first pushed items should be the first once this iteration is over.
Notice that objects will be not be duplicated,and there will be two references to the same object in the queue.
Tip: there is already a queue implementation in Python. You can use it by importing queue
In [17]:
def stutter(q):
for i in range(q.size()):
elm = q.dequeue()
q.enqueue(elm)
q.enqueue(elm)
In [18]:
q1 = Queue()
q1.enqueue(1)
q1.enqueue(2)
q1.enqueue(3)
stutter(q1)
print q1
[3, 3, 2, 2, 1, 1]
Note, I added a str method to the Queue class to see results.
class Queue:
# SAME AS BEFORE
def __str__(self):
return str(self.items)
You can also cheat because python does not have private instance variables. I would not recommend it as an option.
def stutter(q):
q.items = sorted([elm for elm in q.items] + [elm for elm in q.items])
If you want to return using the desired output:
q1 = Queue()
q1.enqueue(1)
q1.enqueue(2)
q1.enqueue(3)
stutter(q1)
print(' '.join([str(elm) for elm in q1.items]))
1 1 2 2 3 3

Using self.xxxx as a default parameter - Python

I'm trying to simplify one of my homework problems and make the code a little better. What I'm working with is a binary search tree. Right now I have a function in my Tree() class that finds all the elements and puts them into a list.
tree = Tree()
#insert a bunch of items into tree
then I use my makeList() function to take all the nodes from the tree and puts them in a list.
To call the makeList() function, I do tree.makeList(tree.root). To me this seems a little repetitive. I'm already calling the tree object with tree.so the tree.root is just a waste of a little typing.
Right now the makeList function is:
def makeList(self, aNode):
if aNode is None:
return []
return [aNode.data] + self.makeList(aNode.lChild) + self.makeList(aNode.rChild)
I would like to make the aNode input a default parameter such as aNode = self.root (which does not work) that way I could run the function with this, tree.makeList().
First question is, why doesn't that work?
Second question is, is there a way that it can work? As you can see the makeList() function is recursive so I cannot define anything at the beginning of the function or I get an infinite loop.
EDIT
Here is all the code as requested:
class Node(object):
def __init__(self, data):
self.data = data
self.lChild = None
self.rChild = None
class Tree(object):
def __init__(self):
self.root = None
def __str__(self):
current = self.root
def isEmpty(self):
if self.root == None:
return True
else:
return False
def insert (self, item):
newNode = Node (item)
current = self.root
parent = self.root
if self.root == None:
self.root = newNode
else:
while current != None:
parent = current
if item < current.data:
current = current.lChild
else:
current = current.rChild
if item < parent.data:
parent.lChild = newNode
else:
parent.rChild = newNode
def inOrder(self, aNode):
if aNode != None:
self.inOrder(aNode.lChild)
print aNode.data
self.inOrder(aNode.rChild)
def makeList(self, aNode):
if aNode is None:
return []
return [aNode.data] + self.makeList(aNode.lChild) + self.makeList(aNode.rChild)
def isSimilar(self, n, m):
nList = self.makeList(n.root)
mList = self.makeList(m.root)
print mList == nList
larsmans answered your first question
For your second question, can you simply look before you leap to avoid recursion?
def makeList(self, aNode=None):
if aNode is None:
aNode = self.root
treeaslist = [aNode.data]
if aNode.lChild:
treeaslist.extend(self.makeList(aNode.lChild))
if aNode.rChild:
treeaslist.extend(self.makeList(aNode.rChild))
return treeaslist
It doesn't work because default arguments are evaluated at function definition time, not at call time:
def f(lst = []):
lst.append(1)
return lst
print(f()) # prints [1]
print(f()) # prints [1, 1]
The common strategy is to use a None default parameter. If None is a valid value, use a singleton sentinel:
NOTHING = object()
def f(arg = NOTHING):
if arg is NOTHING:
# no argument
# etc.
If you want to treat None as a valid argument, you could use a **kwarg parameter.
def function(arg1, arg2, **kwargs):
kwargs.setdefault('arg3', default)
arg3 = kwargs['arg3']
# Continue with function
function("amazing", "fantastic") # uses default
function("foo", "bar", arg3=None) # Not default, but None
function("hello", "world", arg3="!!!")
I have also seen ... or some other singleton be used like this.
def function(arg1, arg2=...):
if arg2 is ...:
arg2 = default

Categories