Python of generating distinct binary tree - python

I am new programmer in Python. Here is my code, and it gives me error. I really have no idea how to fix it.
Binary Tree class:
class BinaryTree:
def __init__(self, data):
self.data=data
self.right=None
self.left=None
def inOrderTraversal(self, root):
if root == None:
pass
else:
self.inOrderTraversal(root.left)
print root.data,
self.inOrderTraversal(root.right)
def printOrder(self):
self.inOrderTraversal(self)
Generate distinct all distinct trees
def generateAllDistinctTrees(array,start,end):
returnResultList=[]
if start>end or start<0 or end>=len(array):
return returnResultList.append(None)
if start==end:
treeNode = BinaryTree(array[start])
return returnResultList.append(treeNode)
for i in range(-1,end-start):
leftResult = generateAllDistinctTrees(array,start+1,start+1+i)
rightResult = generateAllDistinctTrees(array,start+2+i,end)
for left in leftResult:
for right in rightResult:
treeTemp = BinaryTree(array[start])
treeTemp.left = left
treeTemp.right = right
returnResultList.append(treeTemp)
return returnResultList
I've also tried in this way by using appending
def generateAllDistinctTrees(array,start,end):
returnResultList=[]
if start>end or start<0 or end>=len(array):
return returnResultList.append(None)
if start==end:
treeNode = BinaryTree(array[start])
return returnResultList.append(treeNode)
for i in range(-1,end-start):
leftResult=list()
rightResult=list()
leftResult.append(generateAllDistinctTrees(array,start+1,start+1+i))
rightResult.append(generateAllDistinctTrees(array,start+2+i,end))
for left in leftResult[0]:
for right in rightResult[0]:
treeTemp = BinaryTree(array[start])
treeTemp.left = left
treeTemp.right = right
returnResultList.append(treeTemp)
return returnResultList
Main function
if __name__ == '__main__':
preOrderData=[]
scan = raw_input("Enter Number:")
for i in range(0,int(scan)):
preOrderData=preOrderData + [i+1]
results = []
results.append(generateAllDistinctTrees(preOrderData,0,len(preOrderData)-1))
for eachObject in results[0]:
eachObject.printOrder()
I've used the Java version of this code. And it works good without any error. But in python, it will give me following errors:
For the first version if generateAllDistinctTrees:
Traceback (most recent call last):
File "<stdin>", line 7, in <module>
File "<stdin>", line 10, in generateAllDistinctTrees
File "<stdin>", line 11, in generateAllDistinctTrees
TypeError: 'NoneType' object is not iterable
For the second version of generateAllDistinctTrees: (using appending one)
Traceback (most recent call last):
File "<stdin>", line 7, in <module>
File "<stdin>", line 9, in generateAllDistinctTrees
NameError: global name 'leftResult' is not defined
Thanks in advance!!!
I attached my screen shot here!!

class BinaryTree:
left, right, data = None, None, 0
This is wrong. It will create variables that belong to the class, not to the instances (meaning that there won't be several copies for several trees).
The correct way is to just assign variables in the constructor, using self.variable = value.
I see an indentation problem where a "pass" is at the same level as a "def" that should contain it. In general your indentation is not very consistent, you should always use 4 spaces, the returns should be inside the functions.
Fix those easy mistakes so it's easier to look at the logical ones.

Related

Inheritance in classes does not work in Python 2.7

I am trying to improve my understanding of OOP in Python 2.7 (uses in my University courses). My goal is to print the outcome by using a child class. However, I keep getting the below error and do not know how to fix it and why it pops up.
Can anybody tell me how to fix this code and what I am doing wrong?
Error:
Traceback (most recent call last):
File "*******"", line 36, in <module>
print_grades = CreateReport(ReadFile)
TypeError: __init__() takes exactly 1 argument (2 given)
Code:
# Constants
input_file = 'grades1.in.txt'
class ReadFile():
def __init__(self):
self.text_file =''
def read_file(self, file):
self.text_file = open(file)
def data_to_list(self):
self.list_grades = []
for x in self.text_file:
output = x.strip("\n").split("\n")
temp_list = []
for y in output:
temp_list.append(y)
self.list_grades.append(temp_list)
return self.list_grades
class CreateReport(ReadFile):
def __init__(self):
# ReadFile.__init__(self)
pass
def print_list(self):
data = ReadFile.data_to_list()
print data
# start_program(input_file)
print_grades = CreateReport(ReadFile)
print_grades.print_list()

Understanding variable scopes in python: An Exercise Program

This is an exercise that I wanted to try because I thought it was interesting. The exercise is unnecessarily complex for what it is doing, but it is trying to act as practice for understanding class, function and variable behavior in more complex python programs.
import os
class grabFile:
fileObject = None
def __init__(self, filename):
self.fileObject = open(filename, "r")
def getFile():
return self.fileObject
class counter:
fileC = None
lineCount = 0
def __init__(self, fileObject):
self.fileC = fileObject
def lineCounter(self):
while True:
self.fileC.readline()
print(x)
return lineCount
def Main():
fileGrabber = grabFile("test.txt")
fileObj = fileGrabber.getFile
countObj = counter(fileObj)
lineCount = countObj.lineCounter()
print(lineCount)
Main()
However, when I run this, I get the following error:
Traceback (most recent call last):
File "/home/may/Desktop/Tree/Programming/MiscProjects/TextAnalyzer.py", line 32, in <module>
Main()
File "/home/may/Desktop/Tree/Programming/MiscProjects/TextAnalyzer.py", line 29, in Main
lineCount = countObj.lineCounter()
File "/home/may/Desktop/Tree/Programming/MiscProjects/TextAnalyzer.py", line 19, in lineCounter
self.fileC.readline()
AttributeError: 'function' object has no attribute 'readline'
[Finished in 0.2s with exit code 1]
Can anyone help me understand this program fully? And also, although this is not the correct place to ask, offer any critique on styling or formatting of the program? Especially one the use of "self".
Thank you!
I think you meant to call the method:
fileObj = fileGrabber.getFile()
And you need to change to instance method:
def getFile(self):
return self.fileObject
And your line counter method needs some work:
def lineCounter(self):
self.lineCount = len(self.fileC.readlines())
return self.lineCount

Subclassing builtin types -- Python 2 and 3

I have code that looks like the following:
class Token(object):
'''
Resulting from parse
'''
def __new__(cls,text,begin,end,*args,**kargs):
self = super(Token,cls).__new__(cls,*args,**kargs)
return self
def __init__(self,text,begin,end,*args,**kargs):
super(Token,self).__init__(*args,**kargs)
self.text = text
self.begin = begin
self.end = end
class List(Token,list):
pass
class Str(Token,str):
pass
class Int(Token,int):
pass
s = Str('hey there',0,3,'hey there'[0:3])
print(s)
x = Int('55 12',0,2,'55 12'[0:2])
print(x)
Basically what I want to do is to easily create types that are just normal Python types, but with some extra information to them.
Python 2 seems to be OK with the above code, but Python 3 complains
Traceback (most recent call last):
File "simple.py", line 71, in <module>
s = Str('',1,2,'hey')
File "simple.py", line 12, in __init__
super(Token,self).__init__(*args,**kargs)
TypeError: object.__init__() takes no parameters
I think the interpreters would be happy if I did something like
class List(list):
def __init__(self,text,begin,end,*args,**kargs):
list.__init__(*args,**kargs)
But this would mean I would have to repeat something similar for every new class I want to make... and I would rather stay relatively DRY...
Is there a 'proper' way I should handle this situation so that both Python 2 and Python 3 are happy?
Your best bet is to use exception handling here:
def __init__(self,text,begin,end,*args,**kargs):
try:
super(Token,self).__init__(*args,**kargs)
except TypeError:
# Python 3 and the mixed in type is immutable.
# Ignoring this is fine, `__new__` took care of this.
pass
self.text = text
self.begin = begin
self.end = end

Why can this unbound variable work in Python (pyquery)?

The code is from the guide of pyquery
from pyquery import PyQuery
d = PyQuery('<p class="hello">Hi</p><p>Bye</p>')
d('p').filter(lambda i: PyQuery(this).text() == 'Hi')
My question is this in the 3rd line is an unbound variable and is never defined in current environment, but the above code still works.
How can it work? Why it doesn't complain NameError: name 'this' is not defined?
It seems that something happens at https://bitbucket.org/olauzanne/pyquery/src/c148e4445f49/pyquery/pyquery.py#cl-478 , could anybody explain it?
This is done via Python's func_globals magic, which is
A reference to the dictionary that holds the function’s global variables — the global namespace of the module in which the function was defined.
If you dive into PyQuery code:
def func_globals(f):
return f.__globals__ if PY3k else f.func_globals
def filter(self, selector):
if not hasattr(selector, '__call__'):
return self._filter_only(selector, self)
else:
elements = []
try:
for i, this in enumerate(self):
# The magic happens here
func_globals(selector)['this'] = this
if callback(selector, i):
elements.append(this)
finally:
f_globals = func_globals(selector)
if 'this' in f_globals:
del f_globals['this']
return self.__class__(elements, **dict(parent=self))
Others have correctly point out how this is defined inside that lambda you are talking about.
To elaborate a bit more, try out the following code:
>>> def f():
... print f_global
...
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in f
NameError: global name 'f_global' is not defined
>>> f.__globals__['f_global'] = "whoa!!" #Modify f()'s globals.
>>> f()
whoa!!
This is exactly what is happening there. On line 496, you would see the following code:
for i, this in enumerate(self): #this is the current object/node.
func_globals(selector)['this'] = this #func_globals returns selector.__globals__
This doesn't throw a NameError because the variable might exist at the time the actual function is called.
>>> f = lambda i: some_non_named_var
>>> f(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
NameError: global name 'some_non_named_var' is not defined
The above does not error until you call the function that you've stashed away. In the example code you showed, they are manually setting a variable called this in the func_globals before calling the lambda selector function.

not enough arguments passed to __init__()

What is the error below? Also, is there a better way to implement the following classes?
#!/usr/bin/python
class Datacenters:
def __init__(self,name,location,cpu,mem):
self.name=name
self.location=location
self.cpu=cpu
self.mem=mem
def getparam(self):
return self.name,self.location ,self.cpu,self.mem
def getname(self):
return self.name
class WS(Datacenters):
def __init__(self,name,location,cpu,mem,obj):
#datacentername = Datacenters.__init__(self) #To which data center it is associated
self.dcname =obj.name #To which data center it is associated
Datacenters.__init__(obj,name,location,cpu,mem)
def getparam(self,obj):
self.name,self.location ,self.cpu,self.mem = obj.getparam()
print self.dcname
#return self.name,self.location ,self.cpu,self.mem,obj.name
def getwsname(self):
return self.name
class Pcs(WS):
def __init__(self,name,location,cpu,mem,obj):
self.wsname = obj.getwsname() #To which WS it is associated
WS.__init__(obj,name,location,cpu,mem)
def getparam(self,obj):
print obj.getparam()
print self.wsname
a = Datacenters("dc1","Bl1",20,30)
print a.getparam()
b = WS("WS1","Bl1",21,31,a)
print b.getparam(a)
c = Pcs("PC1","Bl1",20,30,b)
#print c.getparam(b)
output:
Press ENTER or type command to continue
('dc1', 'Bl1', 20, 30)
dc1
None
Traceback (most recent call last):
File "class1.py", line 45, in <module>
c = Pcs("PC1","Bl1",20,30,b)
File "class1.py", line 34, in __init__
WS.__init__(obj,name,location,cpu,mem)
TypeError: __init__() takes exactly 6 arguments (5 given)
The error is that you pass in five arguments, but the __init__ needs six. Count them:
def __init__(self,name,location,cpu,mem,obj):
Six arguments. You call it like so:
WS.__init__(obj,name,location,cpu,mem)
Five arguments. The first one, self is missing. What you should ask yourself is why you don't have to pass in six arguments all the time.
And that is because self is passed in automatically when you call the method on an instance. However, in this case you don't call it on an instance, you call it directly on the class. There is of course no need to do so in this case, the correct syntax is:
WS(obj,name,location,cpu,mem)
As you indeed above note works further up.

Categories