how to print vertex using BFS Algo in python? - python-3.x

The problem is in this line (graph.vertList[currentVert].getConnections()) I am unable to get the list of all connected node which is currently connected to currentVert , when I print this code i get the all connected object print(g.vertList[1].getConnections())
dict_keys([<__main__.Vertex object at 0x7f7a7db9a3c8>, <__main__.Vertex object at 0x7f7a7db9a2e8>])
I am unable to find node(vertex) or id for these object so that ,I can Travers easily from one node to another node and print the result at same time .
I am unable to fine the bug , here is the complete code :-
class Queue:
def __init__(self):
self.queue=[]
def enqueue(self,item):
self.queue.insert(0,item)
def isEmpty(self):
return self.queue == []
def dequeue(self):
return self.queue.pop()
def size(self):
return len(self.queue)
Then I create an another class Vertex :
class Vertex:
def __init__(self,key):
self.id=key
self.connectedTo={}
def addNeighbor(self,nbr,weight=0):
self.connectedTo[nbr]=weight
def __str__(self):
return str(self.id)+' Connected To : '+str([x.id for x in self.connectedTo])
def getConnections(self):
return self.connectedTo.keys()
def getId(self):
return self.id
def getWeight(self,nbr):
return self.connectedTo[nbr]
Another class Graph :
class Graph:
def __init__(self):
self.vertList={}
self.numVertices=0
def addVertex(self,key):
self.numVertices=self.numVertices+1
newVertex=Vertex(key)
self.vertList[key]=newVertex
return newVertex
def addEdges(self,f,t,cost=0):
if f in self.vertList:
if t in self.vertList:
self.vertList[f].addNeighbor(self.vertList[t],cost)
else:
return "Not present in Graph"
else:
return "Not present in Graph"
def getVertex(self,n):
if n in self.vertList:
return self.vertList[n]
else:
return None
def getVertices(self):
return self.vertList.keys()
After that I created a function bfs (Breadth First Search) :
def bfs(graph,start):
#Keep track of all visited nodes
visited=[]
#keep track of nodes to be checked using queue
vertQueue= Queue()
vertQueue.enqueue(start)
#Keep looking until there are nodes still to be checked
while vertQueue:
#pop shallowest node (first node ) from queue
currentVert=vertQueue.dequeue()
print(currentVert,end="")
for nbr in (graph.vertList[currentVert].getConnections()):
if nbr not in visited:
#add node to list of checked nodes
vertQueue.enqueue(nbr)
visited.append(currentVert)
How can I fix this problem ?

The problem is here:
self.vertList[f].addNeighbor(self.vertList[t],cost)
Change this to
self.vertList[f].addNeighbor(t,cost)
and it should work.

Related

Creating a child class from a parent method in python

I am trying to make a class that has a bunch of children that all have their own respective methods but share common methods through the parent. The problem is I need to create an instance of the child class in the parent method but am not sure how to go about it
my code so far looks like this
def filterAttribute(self, attribute, value):
newlist = []
for thing in self._things:
if thing._attributes[attribute] == value:
newlist.append(thing)
return self.__init__(newlist)
the class constructor takes in a list as its sole argument. Does anyone know if there is a standard way of doing this because my code is returning a NoneType object
Here are a few examples of classes I have made
This is the parent class:
class _DataGroup(object):
def __init__(self, things=None):
self._things=things
def __iter__(self):
for x in self._things:
yield x
def __getitem__(self, key):
return self._things[key]
def __len__(self):
return len(self._things)
def extend(self, datagroup):
if(isinstance(datagroup, self.__class__)):
self._things.extend(datagroup._things)
self._things = list(set(self._things))
def filterAttribute(self, attribute, value):
newlist = []
for thing in self._things:
if thing._attributes[attribute] == value:
newlist.append(thing)
#return self.__init__(newlist)
return self.__init__(newlist)
this is one of the child classes
class _AuthorGroup(_DataGroup):
def __init__(self, things=None):
self._things = things
def getIDs(self):
return [x.id for x in self._things]
def getNames(self):
return [x.name for x in self._things]
def getWDs(self):
return [x.wd for x in self._things]
def getUrns(self):
return [x.urn for x in self._things]
def filterNames(self, names, incl_none=False):
newlist = []
for thing in self._things:
if((thing is not None or (thing is None and incl_none)) and thing.name in names):
newlist.append(thing)
return _AuthorGroup(newlist)
The functionality I am looking for is that I can use the parent class's with the child classes and create instances of the child classes instead of the overall DataGroup parent class
So if I correctly understand what you are trying to accomplish:
You want a Base Class 'DataGroup' which has a set of defined attributes and methods;
You want one or mpore child classes with the ability to inherit both methods and attributes from the base class as well as have the ability to over-ride base class methjods if necessary: and
You want to invoke the child class without also having to manually invoke the base class.
If this in fact is your problem, this is how I would proceed:
Note: I have modified several functions, since I think you have several other issues with your code, for example in the base class self._things is set up as a list, but in the functions get_item and filterAttribute you are assuming self._things is a dictionary structure. I have modified the functions so all assume a dict structure for self._things
class _DataGroup:
def __init__(self, things=None):
if things == None:
self._things = dict() #Sets up default empty dict
else:
self._things=things
def __iter__(self):
for x in self._things.keys():
yield x
def __len__(self):
return len(self._things)
def extend(self, datagroup):
for k, v in datagroup.items():
nv = self._things.pop(k, [])
nv.append(v)
self._things[k] = nv
# This class utilizes the methods and attributes of DataGroup
# and adds new methods, unique to the child class
class AttributeGroup(_DataGroup):
def __init__(self, things=None):
super.__init__(things)
def getIDs(self):
return [x for x in self._things]
def getNames(self):
return [x.name for x in self._things]
def getWDs(self):
return [x.wd for x in self._things]
def getUrns(self):
return [x.urn for x in self._things]
# This class over-rides a DataGroup method and adds new attribute
class NewChild(_DataGroup):
def __init__(self, newAttrib, things = None):
self._newattrib = newAttrib
super.__init__(self, things)
def __len__(self):
return max(len(self._newattrib), len(self._things))
These examples are simplified, since I am not absolutely sure of what you really want.

How do I return values of one class method to another class method?

I am new to Classes and want to know how to return values from one class method to another. Below is the code and I want to return the queues from arrive() function to next_customer() function.
class DonutQueue():
def __init__(self):
self.queue=[]
self.queue2= []
def arrive(self,name,vip):
self.name = name
self.vip = vip
if self.vip==1:
self.queue2.append(self.name)
return self.queue2
else:
self.queue.append(self.name)
return self.queue
def next_customer(self):
while not self.queue2== []:
if not self.queue2==[]:
return self.queue
else:
return self.queue2
def main():
n = int(input("Enter the number of customers you want to add"))
for i in range(0,n):
name = input("Enter their name")
vip= int(input("Are they a VIP"))
DonutQueue().arrive(name,vip)
print(DonutQueue().next_customer())
If I add the below statement in the next_customer function, I get the mentioned error:
a = self.arrive(self.name,self.vip)
Error:
AttributeError: 'DonutQueue' object has no attribute 'name'
The problem is when I call next_customer() it returns None because I am getting values from init instead of arrives()
in python you have to define members in the __init__ function means. in your case you defined the self.name member only in arrive thats why its not available (and throws an exception) in your case.

Python Class, Operator Overloading

Recently while getting my hands on with Python Class concepts, I came upon this observation and was not able to understand.
When I try and create instance out of the below class interactively(Python console), I also get the Finding __len__ line in output.
class MyClass(object):
counter = 0
data = 'Class Variable'
def __init__(self):
self.counter += 1
self.value = -1
def __str__(self):
return "Instance {} is the {} instance".format(self.__class__.__name__, self.counter)
def __getattr__(self, item):
print(f'Finding {item}')
return self.__dict__.get(item, f'Attr {item} not available, {self.__dict__}')
def __setattr__(self, key, value):
if key not in self.__dict__:
self.__dict__[key] = value
def __delattr__(self, item):
print(f'Deleting attr: {item}')
if item in self.__dict__:
del self.__dict__[item]
else:
print(f'Cannot find {item} in {self.__dict__}')
if __name__ == '__main__':
inst = MyClass()
print(inst.id)
But running it as a top level module, doesn't add this additional line in output.
Can someone help me understand, why Finding __len__ output would be displayed interactively.
Below is an interactive output,
import WS1
x = WS1.MyClass()
Finding __len__
x.name = 'Yathin'
Finding __len__

AttributeError: 'int' object has no attribute 'data'

I was trying to implement some basic operations of Binary search tree in Python3.7. I have just started coding problems from Hackerrank and I got stuck while implementing levelOrder traversal in BST. What should I do to resolve this AttributeError: 'int' object has no attribute 'data'?
I am using queue to solve this problem, initially, the queue will point to root node, then checks for left and right children. If found, the children will get append to the queue and the process continues until the queue is empty. I am popping out the first element in each iteration and storing it in temp variable. This way i will get nodes at each level of tree.
class Node:
def __init__(self, data):
self.data = data
self.leftChild = None
self.rightChild = None
def insert(self, data):
if self.data is not None:
if data < self.data:
if self.leftChild is None:
self.leftChild = Node(data)
else:
self.leftChild.insert(data)
elif data > self.data:
if self.rightChild is None:
self.rightChild = Node(data)
else:
self.rightChild.insert(data)
else:
self.data = data
def traverseLevelOrder(self):
queue = []
queue.append(self.data)
while queue:
# Print front of queue and remove it from queue
print(queue[0].data)
temp = queue.pop(0)
# Enqueue left child
if temp.leftChild is not None:
queue.append(temp.leftChild)
# Enqueue right child
if temp.rightChild is not None:
queue.append(temp.rightChild)
class BST:
def __init__(self):
self.rootNode = None
def insert(self, data):
if self.rootNode is None:
self.rootNode = Node(data)
else:
self.rootNode.insert(data)
def traverseLevelOrder(self):
if self.rootNode is None:
return
else:
self.rootNode.traverseLevelOrder()
bst = BST()
bst.insert(2)
bst.insert(4)
bst.insert(1)
bst.insert(3)
bst.traverseLevelOrder()
The code should return the level traversal order like one given below(within a level it should print first left node then right node):
2
1
4
3
Instead, I am having the below error:
Traceback (most recent call last):
print(queue[0].data)
AttributeError: 'int' object has no attribute 'data'
You're appending an integer, self.data to the queue, then attempting to access a property on the integer with queue[0].data, causing the AttributeError to be raised.
Instead, append the node itself with:
queue.append(self)
queue.append(self.data)
Did you mean:
queue.append(self)
?
Right now you're only adding a number to the queue, not the whole object.

PyQt 5 update list model

I am trying to update listview model in QML quick controls with python. Every item in this model contains address and status property. Right now my code is this:
ListModel:
class ServersModel(QAbstractListModel):
def __init__(self, parent=None):
super(ServersModel, self).__init__(parent)
self.list = []
for address, status in (
("server 1", "green"),
("server 2", "green"),
("server 3", "red")):
self.list.append(server(address, status))
def rowCount(self, parent=QModelIndex()):
return len(self.list)
def data(self, index, role=None):
if role == Qt.DisplayRole: #show just the name
person = self.list[index.row()]
return QVariant(person.name)
elif role == Qt.UserRole: #return the whole python object
person = self.list[index.row()]
return person
return QVariant()
def removeRow(self, position):
self.list = self.list[:position] + self.list[position+1:]
self.reset()
ServerObject:
class server(object):
'''
a custom data structure, for example purposes
'''
def __init__(self, address, status):
self.address = address
self.status = status
def __repr__(self):
return "%s\n%s"% (self.address, self.status)
Python connection handler:
class ServersListViewHandler(QObject):
def __init__(self):
QObject.__init__(self)
listLoaded = pyqtSignal(QAbstractListModel, arguments=['model'])
data_changed = pyqtSignal(QModelIndex, QModelIndex)
# Slot for detecting clicks in listView
#pyqtSlot(str)
def listViewItemClicked(self, name):
self.listLoaded.emit(ServersModel())
And this is my connectin at qml:
Connections {
target: serversListViewHandler
onListLoaded: {
serversListView.model = model
}
}
Address property should go to text as value and status is color of circle. This works fine if I implement model directly to QML but I want to update it dynamically and load it form python. Any ideas on how to accomplish this? I am probably doing it totally wrong but there are almost none tutorial or references about pyqt 5
I'm working on a similar experiment at the moment. I succeeded doing this by using dedicated role names for columns (in my case in the list view).
For your example this would kinda look like this:
class ServersModel(QAbstractListModel):
AddressRole = Qt.UserRole + 1
StatusRole = Qt.UserRole + 2
_roles = {AddressRole: b"address", StatusRole: b"status"}
...
def roleNames(self):
return self._roles
def data(self, index, role=None):
if role == AddressRole: #show just the name
person = self.list[index.row()]
return QVariant(person.name)
elif role == StatusRole: #return the whole python object
person = self.list[index.row()]
return person
return QVariant()
Though this may only target the displaying part, not the complete update procedure. Would be interesting to see the QML where you access the model.
Also I'm not really sure if there is some kind of best practice for this kind of thing.

Resources