Visitor pattern (from bottom to top) - python

Please consider the (example) code below before I get to my specific question regarding visitor pattern in python:
class Node:
def __init__(self):
self.children = []
def add(self, node):
self.children.append(node)
def check(self):
print("Node")
return True
def accept(self, visitor):
visitor.visit(self)
class NodeA(Node):
def check(self):
print("NodeA")
return True
class NodeB(Node):
def check(self):
print("NodeB")
return True
class NodeA_A(NodeA):
def check(self):
print("NodeA_A")
return True
class NodeA_B(NodeA):
def check(self):
print("NodeA_B")
return True
class NodeA_A_A(NodeA_A):
def check(self):
print("NodeA_A_A")
return False
class NodeRunner:
def visit(self, node):
node.check()
if len(node.children) > 0:
for child in node.children:
child.accept(self)
if __name__ == "__main__":
n = Node()
n1 = NodeA()
n2 = NodeB()
n11 = NodeA_A()
n12 = NodeA_B()
n111 = NodeA_A_A()
n.add(n1)
n.add(n2)
n1.add(n11)
n1.add(n12)
n11.add(n111)
v = NodeRunner()
v.visit(n)
When I run it, it traverse all the nodes-classes iteratively and returns the following:
Node
NodeA
NodeA_A
NodeA_A_A
NodeA_B
NodeB
This is all fine but now to my question. You may have noticed that each check-method returns a Boolean (lets say this is a complicated method in reality).
In the example above every check-method inside Node classes return True except NodeA_A_A. I would like to store this somehow during visiting so I can fail all the base classes.
This is hard to explain let me illustrate:
if NodeA_A_A returns False, then I would like to fail NodeA_A, NodeA and Node. regardless of what these classes return.
if NodeB returns False, then I would like to fail Node. regardless of what other classes return.
So if a child-class is somewhere failing (check method returns False), I would like to fail all its base classes.
Does anyone have any ideas?

It seems that what you are asking for is not about visitor patter, but about how to implement a depth-first search algorithm. Here is my solution for your question:
class Node:
def __init__(self):
self.children = []
def add(self, node):
self.children.append(node)
def check(self):
print("Node")
return True
def accept(self, visitor):
return visitor.visit(self)
class NodeA(Node):
def check(self):
print("NodeA")
return True
class NodeB(Node):
def check(self):
print("NodeB")
return True
class NodeA_A(NodeA):
def check(self):
print("NodeA_A")
return True
class NodeA_B(NodeA):
def check(self):
print("NodeA_B")
return True
class NodeA_A_A(NodeA_A):
def check(self):
print("NodeA_A_A")
return False
class NodeRunner:
def visit(self, node):
ret = True
# visit all children
for child in node.children:
v = child.accept(self)
if not v and ret: # if some child not accepted, then we think that the parent node should also not be accepted
ret = False
# check the node
if not node.check():
ret = False
return ret
if __name__ == "__main__":
n = Node()
n1 = NodeA()
n2 = NodeB()
n11 = NodeA_A()
n12 = NodeA_B()
n111 = NodeA_A_A()
n.add(n1)
n.add(n2)
n1.add(n11)
n1.add(n12)
n11.add(n111)
v = NodeRunner()
print v.visit(n)

I used the visitor pattern to visit all the nodes.
One visitor visits and runs all the nodes, the other visitor bubbles up the result.
The code and output is provided below:
class Node(object):
def __init__(self):
self.children = []
self.result = None
def add(self, node):
self.children.append(node)
def check(self):
self.result = True
print "Node: result:%s" % self.result
return self.result
def accept(self, visitor):
visitor.visit(self)
class Node_A(Node):
def __init__(self):
super(Node_A, self).__init__()
def check(self):
self.result = True
print "Node_A: result:%s" % self.result
return self.result
class Node_A_A(Node_A):
def __init__(self):
super(Node_A_A, self).__init__()
def check(self):
self.result = True
print "Node_A_A: result:%s" % self.result
return self.result
class Node_A_B(Node_A):
def __init__(self):
super(Node_A_B, self).__init__()
def check(self):
self.result = True
print "Node_A_B: result:%s" % self.result
return self.result
class Node_A_A_A(Node_A_A):
def __init__(self):
super(Node_A_A_A, self).__init__()
def check(self):
self.result = True
print "Node_A_A_A: result:%s" % self.result
return self.result
class Node_A_A_B(Node_A_A):
def __init__(self):
super(Node_A_A_B, self).__init__()
def check(self):
self.result = False
print "Node_A_A_B: result:%s" % self.result
return self.result
class Node_A_B_A(Node_A_B):
def __init__(self):
super(Node_A_B_A, self).__init__()
def check(self):
self.result = True
print "Node_A_B_A: result:%s" % self.result
return self.result
class NodeRunner:
def visit(self, node):
if len(node.children) > 0:
for child in node.children:
child.accept(self)
node.check()
class NodeChecker:
def visit(self, node):
results = []
if len(node.children) > 0:
for child in node.children:
child.accept(self)
results.append(child.result)
node.result = all(results)
if __name__ == "__main__":
node = Node()
node_a = Node_A()
node_a_a = Node_A_A()
node_a_b = Node_A_B()
node_a_a_a = Node_A_A_A()
node_a_a_b = Node_A_A_B()
node_a_b_a = Node_A_B_A()
node.add(node_a)
node_a.add(node_a_a)
node_a_a.add(node_a_a_a)
node_a_a.add(node_a_a_b)
node_a.add(node_a_b)
node_a_b.add(node_a_b_a)
print("-------------------")
nVisitor = NodeRunner()
nVisitor.visit(node)
print("-------------------")
nVisitor = NodeChecker()
nVisitor.visit(node)
print("-------------------")
print "node_a_a_a: result: %s" % node_a_a_a.result
print "node_a_a_b: result: %s" % node_a_a_b.result
print "node_a_a: result: %s" % node_a_a.result
print "node_a_b_a: result: %s" % node_a_b_a.result
print "node_a_b: result: %s" % node_a_b.result
print "node_a: result: %s" % node_a.result
print "node: result: %s" % node.result
The output is provided below:
-------------------
Node_A_A_A: result:True
Node_A_A_B: result:False
Node_A_A: result:True
Node_A_B_A: result:True
Node_A_B: result:True
Node_A: result:True
Node: result:True
-------------------
-------------------
node_a_a_a: result: True
node_a_a_b: result: False
node_a_a: result: False
node_a_b_a: result: True
node_a_b: result: True
node_a: result: False
node: result: False

Related

why the pointer is not incrementing at line 27

i am implementing the trie data structure, why my pointer is not incrementing at line# 27. all the characters are getting into forst node only.
this is my code
class Trienode:
data:str
next:list = [None]*26
isTerminal:bool = False
def __init__(self,data):
self.data = data
class Trie:
def __init__(self):
self.root = Trienode('#')
def insert(self,data):
temp = self.root
for ch in data:
index = ord(ch)- ord('a')
# print(temp.next[index])
if temp.next[index]==None:
temp.next[index] = Trienode(ch)
temp = temp.next[index]
else:
temp = temp.next[index]
temp.isTerminal = True
def display(self):
temp = self.root
for i in range(26):
print(temp.next[i])
if __name__=='__main__':
root = Trie()
root.insert("apple")
root.insert("pineapple")
root.display()
This is the output on console i am printing the pointer array of first node
console output
i tried the same logic to increment pointer in Linkedlist it is working fine.
I've modified your sample a little bit.
Relevant changes are highlighted with a comment
class Trienode:
def __init__(self, data):
self.data: str = data
self.next: list = [None] * 26
self.isTerminal: bool = False
def __str__(self): # get more info on display
return f"{self.data} - {''.join(str(n) for n in self.next if n is not None)}"
def __repr__(self): # appears nicely in my IDE ;)
return f'Trienode({self.data})'
class Trie:
def __init__(self):
self.root = Trienode('#')
def insert(self, data):
temp = self.root
for ch in data:
index = ord(ch) - ord('a')
if temp.next[index] is None: # error was there I guess
temp.next[index] = Trienode(ch)
temp = temp.next[index] # and here also
temp.isTerminal = True
def display(self):
self._display(self.root)
def _display(self, node): # must use a recursion to display all the children
print(node)
for child in node.next:
if child is not None:
self._display(child)
if __name__ == '__main__':
root = Trie()
root.insert("apple")
root.insert("pineapple")
root.display
Hope this helps

Don't understand this "Recursion depth exceeded" error for singly linked list implementation

I'm having a little bit of trouble running this code in linux for my class. This week we're on singly linked list and the assignment my teacher gave me was to use nodes to represent polynomials and list them in descending order I keep coming across a maximum recursion depth exceed error in my Node class.
Here is the code for Node:
#!/usr/bin/python
import sys
sys.setrecursionlimit(4500)
"""A model containing Node class"""
class Node(object):
"""A single node in a data structure"""
def __init__(self, coefficient, exponent):
self.coefficient=coefficient
self.exponent=exponent
#property
def coefficient(self):
return self.coefficient
#coefficient.setter
def coefficient(self, c):
self.coefficient=c
#coefficient.deleter
def coefficient(self):
del self.coefficient
#property
def exponent(self):
return self.exponent
#exponent.setter
def exponent(self, e):
self.exponent=e
#exponent.deleter
def exponent(self):
del self.exponent
#property
def next(self):
return self.next
#next.setter
def next(self, n):
self.next=n
#next.deleter
def next(self):
del self.next
def __eq__(self, other):
if self.exponent==other.exponent:
return True
else:
return False
def __It__(self, other):
if self.exponent<other.exponent:
return True
else:
return False
def __str(self):
if self.coefficient>=0:
sign="+"
else:
sign=""
return sign +str(self.coefficient) + "X^" +str(self.exponent)
Here is the code for my List class:
#!/usr/bin/python
from NodeModule import Node
class List(Node):
"""Linked list with pre-defined Node class"""
def __init__(self):
self.head=None
self.count=0
def isEmpty(self):
return self.count==0
def getSize(self):
return self.count
def insert(self, index, o, p):
if index<0 or index > self.count:
return False
n=Node(o, p)
if index==0:
n.next=self.head
self.head=n
self.count+=1
return True
walker=self.head
for i in range(index-1):
walker=walker.next
n.next=walker.next
walker.next=n
self.count+=1
return True
def delete(self, index):
if index < 0 or index > self.count:
return False
if index==0:
self.head=self.head.next
self.count-=1
return True
walker=self.head
for i in range(index-1):
walker=walker.next
walker.next=walker.next.next
self.count-=1
return True
def sort(self):
temp1=self.head.exponent
walker=self.head
j=0
while j < self.count:
for i in self.getsize():
walker=walker.next
temp2=walker.next.exponent
if walker.next.exponent > temp1:
insert(0, walker.next.coefficient, walker.next.exponent)
delete(walker.next)
while walker.next is not None:
if walker.next.exponent < walker.next.next.exponent:
insert(self.getsize(), walker.next.next.coefficient, walker.next.next.exponent)
delete(walker.next)
j+=1
def str(self):
if self.isEmpty():
return "\nEnd of Polynomial"
walker=self.head
output=[]
while walker is not None:
output.append(str(walker))
walker=walker.next
return " + " .join(output)
And here's what I'm using to test my code:
#!/usr/bin/python
from NodeModule import Node
from ListModule import List
def readPoly(message):
l=List()
n=input(message)
for i in range(n):
c=input("Enter the coefficient of term %d" % i)
e=input("Enter the exponent of term %d" % i)
l.insert(0, Node(c,e))
return l
def main():
l=readPoly("Enter the number of terms of the polynomial: ")
print l
l.sort()
print l
l.delete(0)
print (l)
if __name__=='__main__':
main()
The interpreter is telling me that the error is on my self.coefficient=c line in my Node class. How can I go about fixing this issue?
You should not have a variable called coefficient and a property with the same name.
Look at your definition of the setter:
#coefficient.setter
def coefficient(self, c):
self.coefficient=c
When calling self.coefficient = c, the same setter will be called again, recursively.
To solve it, you could rename your variable with an underscore (and change all the other parts of your code):
#coefficient.setter
def coefficient(self, c):
self._coefficient = c
On further reading , it looks like you could omit the setters altogether, since they do not serve a purpose in your code. A reduced version of your class Node (with the same functionality) could be:
class Node:
def __init__(self, coefficient, exponent):
self.coefficient = coefficient
self.exponent = exponent
self.next = None
def __str__(self):
return '{}{}X^{}'.format(
'+' if self.coefficient >= 0 else '',
self.coefficient,
self.exponent)

Python: Attribute error Nonetype object has no attribute 'nxt'

I am having trouble sorting this singly linked list. The goal is to sort a polynomial by its exponent in descending order. However I keep getting attribute error: Nonetype has no attribute 'nxt' and I can't understand why. Here is my code below
NodeModule.py
!/usr/bin/python
import sys
sys.setrecursionlimit(4500)
"""A model containing Node class"""
class Node(object):
"""A single node in a data structure"""
def __init__(self, _coefficient, _exponent):
self._coefficient=_coefficient
self._exponent=_exponent
self.nxt=None
#property
def coefficient(self):
return self._coefficient
#coefficient.setter
def coefficient(self, c):
self._coefficient=c
#coefficient.deleter
def coefficient(self):
del self.coefficient
#property
def exponent(self):
return self._exponent
#exponent.setter
def exponent(self, e):
self._exponent=e
#exponent.deleter
def exponent(self):
del self._exponent
#property
def nxt(self):
return self._next
#nxt.setter
def nxt(self, n):
self._next=n
#nxt.deleter
def nxt(self):
del self._next
def __eq__(self, other):
if self._exponent==other._exponent:
return True
else:
return False
def __It__(self, other):
if self._exponent<other._exponent:
return True
else:
return False
def __str(self):
if self._coefficient>=0:
sign="+"
else:
sign=""
return sign +str(self._coefficient) + "X^" +str(self._exponent)
ListModule.py
!/usr/bin/python
from NodeModule import Node
class List(Node):
"""Linked list with pre-defined Node class"""
def __init__(self):
self.head=None
self.count=0
def isEmpty(self):
return self.count==0
def getSize(self):
return self.count
def setNode(self, a=5, b=2):
n=Node(a, b)
return n
def insert(self, index, n):
if index<0 or index > self.count:
return False
if index==0:
n.next=self.head
self.head=n
self.count+=1
return True
walker=self.head
for i in range(index-1):
walker=walker.nxt
n.nxt=walker.nxt
walker.next=n
self.count+=1
return True
def delete(self, index):
if index < 0 or index > self.count:
return False
if index==0:
self.head=self.head.nxt
self.count-=1
return True
walker=self.head
for i in range(index-1):
walker=walker.nxt
walker.nxt=walker.nxt.nxt
self.count-=1
return True
def sort(self):
temp1=self.head.exponent
walker=self.head
j=0
while j < self.count:
for i in range(self.count):
walker=walker.nxt
if i==0:
if walker.nxt.exponent > temp1:
self.insert(0, walker.nxt)
self.delete(walker.nxt)
return True
while walker.nxt is not None and walker.nxt.nxt is not None:
if walker.nxt.exponent < walker.nxt.nxt.exponent:
self.insert(self.getsize(), walker.nxt.nxt)
self.delete(walker.nxt)
return True
return False
j+=1
def str(self):
if self.isEmpty():
return "\nEnd of Polynomial"
walker=self.head
output=[]
while walker is not None:
output.append(str(walker))
walker=walker._next
return " + " .join(output)
main.py
!/usr/bin/python
from NodeModule import Node
from ListModule import List
def readPoly(message):
l=List()
n=input(message)
for i in range(n):
c=input("Enter the coefficient of term %d " % i)
e=input("Enter the exponent of term %d " % i)
l.insert(0, Node(c,e))
return l
def main():
l=readPoly("Enter the number of terms of the polynomial: ")
print l
l.sort()
print l
l.delete(0)
print (l)
if name=='main':
main()
It appears self.head=None, walker=self.head and walker=walker.nxt within the second code block. This means by inference you're trying to get the .nxt property of None (a NoneType), which you set as self.head at the start.

Class Representation of Half Adder Circuit

I'm working through this book and am trying to extend its code to make a half adder and eventually a full adder.
I think I should be implementing a way to return the carry and bits from the half adder separately to facilitate connection to later gates. However, that's where I'm blanking. I also need some way to call performGateLogic on the HalfAdder and 'pipe' the inputs to both the XorGate and AndGate and have tried so with Connectors. My brain is just being tied in a pretzel over the method calling and class relations here and I'd appreciate someone straightening it out.
class LogicGate:
def __init__(self,n):
self.name = n
self.output = None
def getName(self):
return self.name
def getOutput(self):
self.output = self.performGateLogic()
return self.output
class BinaryGate(LogicGate):
def __init__(self,n):
LogicGate.__init__(self,n)
self.pinA = None
self.pinB = None
def getPinA(self):
if self.pinA == None:
return int(input("Enter Pin A input for gate "+self.getName()+"-->"))
else:
return self.pinA.getFrom().getOutput()
def getPinB(self):
if self.pinB == None:
return int(input("Enter Pin B input for gate "+self.getName()+"-->"))
else:
return self.pinB.getFrom().getOutput()
def setNextPin(self,source):
if self.pinA == None:
self.pinA = source
else:
if self.pinB == None:
self.pinB = source
else:
print("Cannot Connect: NO EMPTY PINS on this gate")
class AndGate(BinaryGate):
def __init__(self,n):
BinaryGate.__init__(self,n)
def performGateLogic(self):
a = self.getPinA()
b = self.getPinB()
if a==1 and b==1:
return 1
else:
return 0
class XorGate(BinaryGate):
def __init__(self,n):
BinaryGate.__init__(self,n)
def performGateLogic(self):
a = self.getPinA()
b = self.getPinB()
if (a ==1 and b==0) or (a==0 and b==1):
return 1
else:
return 0
class Connector:
def __init__(self, fgate, tgate):
self.fromgate = fgate
self.togate = tgate
tgate.setNextPin(self)
def getFrom(self):
return self.fromgate
def getTo(self):
return self.togate
class HalfAdder(BinaryGate):
def __init__(self,n):
BinaryGate.__init__(self,n)
self.bits, self.carry = None, None
self.a, self.b = None, None
self.xor = XorGate('XO')
self.and_ = AndGate('A')
self.c1 = Connector(self.xor.getPinA(), self.and_.getPinA())
self.c2 = Connector(self.xor.getPinB(), self.and_.getPinB())
def performGateLogic(self):
self.a = self.getPinA()
self.b = self.getPinB()
self.bits = 1 if (self.a ==1 and self.b==0) or (self.a==0 and self.b==1) else 0
self.carry = 1 if (self.a==1 and self.b ==1) else 0
return self.bits + self.carry
Running print(HalfAdder('HA').getOutput()) yields
File "./circuits.py", line 178, in __init__
self.c1 = Connector(self.xor.getPinA(), self.and_.getPinA())
File "./circuits.py", line 161, in __init__
tgate.setNextPin(self)
AttributeError: 'int' object has no attribute 'setNextPin'
Something is being set to an int but where, I can't find.

I keep getting this error:RecursionError: maximum recursion depth exceeded while calling a Python object

I am trying to use BFS to search through a tree with three letter words and find the way in between a given start and end word and print the way in between. The printing is supposed to be done with a recursive function.
I keep getting this error:
RecursionError: maximum recursion depth exceeded while calling a Python object
and an infinite loop with the endword can anyone see whats wrong? (I copied my imported classes as well).
from array import array
class Node1:
#håller koll på värdena
def __init__(self, data, next = None):
self.data = data
self.next = next
def __str__(self):
if self.data.parent == None:
return None
else:
print(self.data.parent)
return str(self.data.parent)
def __str__(self):
return str(self.data.word)
class Queue:
def __init__(self):
self.first = None
self.last = None
def enqueue(self,x):
"""Stoppar in x sist i kön """
x = Node1(x)
if self.first == None: # Om kön är tom
self.first = self.last = x
else: # om kön inte är tom
self.last.next = x
self.last = x
def dequeue(self):
first = self.first
self.first = self.first.next
#if self.first == None:
# self.last=None
return first
def isEmpty(self):
if self.first == None:
xx = True
else:
xx = False
#print('I IsEmpty: Första elementet i listan:',self.first)
#print('I IsEmpty: Sista elementet i listan:',self.last)
return xx
def __str__(self):
node=self.first
node_strang = 'Efter trolleriet får man: '
while node != None:
node_strang += str(node)
node = node.next
return node_strang
class Node:
'''Nodklass med rekursiva metoder som anropas i Bintree-klassen'''
def __init__(self, word):
self.word = word
self.left = None
self.right = None
def insert(self, new_word):
if self.word == new_word:
return False
elif new_word < self.word:
if self.left:
return self.left.insert(new_word)
else:
self.left = Node(new_word)
return True
else:
if self.right:
return self.right.insert(new_word)
else:
self.right = Node(new_word)
return True
def find(self, new_word):
if self.word == new_word:
return True
elif new_word < self.word:
if self.left:
return self.left.find(new_word)
else:
return False
else:
if self.right:
return self.right.find(new_word)
else:
return False
def preorder(self):
if self:
print(self.word)
if self.left:
self.left.preorder()
if self.right:
self.right.preorder()
def postorder(self):
if self:
if self.left:
self.left.postorder()
if self.right:
self.right.postorder()
print(self.word)
def inorder(self):
if self:
if self.left:
self.left.inorder()
print(self.word)
if self.right:
self.right.inorder()
from linkedQFile import Queue,Node1
from bintreeFile import Node
import string
class Bintree:
def __init__(self):
self.root = None
def put(self, new_word):
if self.root:
return self.root.insert(new_word)
else:
self.root = Node(new_word)
return True
def __contains__(self, new_word):
if self.root:
return self.root.find(new_word)
else:
return False
class ParentNode:
def __init__(self, word, parent = None):
self.word = word
self.parent = parent
def maketree():
svenska = Bintree()
gamla = Bintree()
with open('word3.txt', 'r') as ordfil:
for rad in ordfil:
ord = rad.strip()
if ord in svenska:
gamla.put(ord)
svenska.put(ord)
ordfil.close()
return svenska,gamla
def writechain(kidzen, paronen):
if paronen is not None:
print(kidzen)
writechain(kidzen, paronen)
else:
pass
def countchain(barn_obj):
if barn_obj.parent==None:
return 0
else:
return 1+countchain(barn_obj.parent)
def makechildren(nod, q, gamla, svenska):
for i in range(3):
bokstavslista = list(nod.data.word)
alfabetslista = list(string.ascii_lowercase) + ['å', 'ä', 'ö']
for bokstav in alfabetslista:
bokstavslista[i] = bokstav
barn = ''.join(bokstavslista)
if barn in svenska:
barn_obj = ParentNode(barn, nod.data)
#print('parent to', barn, 'is', str(barn_obj.parent))
if barn not in gamla:
#print("i makechildren: barn = ", barn)
q.enqueue(barn_obj)
gamla.put(barn_obj.word)
def main():
(svenska,gamla) = maketree()
q=Queue()
start = input('Startord:')
slut = input('Slutord:')
startord= ParentNode(start, parent=None)
q.enqueue(startord)
while not q.isEmpty():
nod = q.dequeue()
makechildren(nod, q, gamla, svenska)
nod_for=nod.data
kidzen=nod_for.word
paronen=nod_for.parent
#print ('word =', kidzen)
#print ('parent =', paronen)
if q.isEmpty():
print('Det finns ingen väg till', slut)
break
elif kidzen==slut:
writechain(kidzen, paronen)
print('Det finns en väg till', slut)
break
main()
In your writechain function you are not walking a tree.
You keep recursively calling it with the same arguments. That will continue until you reach the recursion limit.

Categories