I have written a code that uses python deque from collections. I have created an adapetr class queue.py
that handles queue operations. But I am getting error object has no attribute.
queue.py
:
from collections import deque
class Queue:
def __int__(self):
self._items = deque()
def enqueue(self, item):
self._items.append(item)
def extend(self, *elements):
self._items.extend(elements)
def dequeue(self):
try:
return self._items.popleft()
except IndexError:
raise IndexError("Dequeue from empty queue") from Queue
def __len__(self):
return len(self._items)
def __contains__(self, item):
return item in self._items
def __iter__(self):
yield from self._items
def __repr__(self):
return f"Queue({list(self._items)})"
graph.py
:
from queue import Queue
def getNodeName(rootName):
return input(f"Enter child of node {rootName} : ")
def inputChildren(root):
print(f"Enter stop to stop entering children for node {root} : ")
child = list()
while True:
data = getNodeName(root)
if data.lower() == "stop":
break
child.append()
return child
def inputGraph():
graph = dict()
queue = Queue()
queue.enqueue(input("Enter root name : "))
while len(queue) != 0:
root = queue.dequeue()
rootChildren = inputChildren(root)
queue.extend(rootChildren)
if len(rootChildren) == 0:
continue
else:
graph[root] = rootChildren
if __name__=="__main__":
print(inputGraph())
Output :
Enter root name : A
Traceback (most recent call last):
File "D:\graph.py", line 36, in <module>
print(inputGraph())
File "D:\graph.py", line 23, in inputGraph
queue.enqueue(input("Enter root name : "))
File "D:\queue.py", line 9, in enqueue
self._items.append(item)
AttributeError: 'Queue' object has no attribute '_items'
I have tried searching online but cannot figure out why I am getting this error.
from collections import deque
class Queue:
def __init__(self):
self._items = deque()
def enqueue(self, item):
self._items.append(item)
def extend(self, *elements):
self._items.extend(elements)
def dequeue(self):
try:
return self._items.popleft()
except IndexError:
raise IndexError("Dequeue from empty queue") from Queue
def __len__(self):
return len(self._items)
def __contains__(self, item):
return item in self._items
def __iter__(self):
yield from self._items
def __repr__(self):
return f"Queue({list(self._items)})"
def getNodeName(rootName):
return input(f"Enter child of node {rootName} : ")
def inputChildren(root):
print(f"Enter stop to stop entering children for node {root} : ")
child = list()
while True:
data = getNodeName(root)
if data.lower() == "stop":
break
child.append(data)
return child
def inputGraph():
graph = dict()
queue = Queue()
queue.enqueue(input("Enter root name : "))
while len(queue) != 0:
root = queue.dequeue()
rootChildren = inputChildren(root)
queue.extend(rootChildren)
if len(rootChildren) == 0:
continue
else:
graph[root] = rootChildren
return graph
if __name__=="__main__":
print(inputGraph())