python unbound method again - python

This gets me into difficult time (sorry, i am still very new to python)
Thank you for any kind of help.
The error
print Student.MostFrequent() TypeError: unbound method
MostFrequent() must be called with
Student instance as first argument
(got nothing instead)
This Student.MostFrequent() is called all the way in the end (last line) and the def is last def in the class
EDITED - Naming convention
My long code
import csv
class Student:
sports = []
ftopics = []
stopics = []
choice_list = []
choice_dict = {}
def __init__(self, row):
self.lname, self.fname, self.ID, self.gender, self.sport, self.movie, self.movieyr, self.country, self.ftopic, self.stopic = row
self.sports.append(self.sport)
self.ftopics.append(self.ftopic)
self.stopics.append(self.stopic)
def print_information(self):
return (self.lname, self.fname, self.ID, self.gender)
def print_first(self):
return (self.lname, self.fname, self.sport)
def print_second(self):
return (self.lname, self.fname, self.movie, self.movieyr)
def print_third(self):
return (self.lname, self.fname, self.country)
def print_fourth(self):
return (self.lname, self.fname, self.ftopic, self.stopic)
def most_frequent(self):
for choice in self.choice_list:
self.choice_dict[choice] = self.choice_dict.get(choice, 0) + 1
self.mostFrequent = sorted([(v, k) for k, v in self.choice_dict.items()], reverse=True)
print self.mostFrequent
reader = csv.reader(open('new_mondy_csc_data_revise.csv'), delimiter=',', quotechar='"')
header = tuple(reader.next())
print "%-17s|%-10s|%-6s|%s" %header[:4]
print "-" * 45
students = list(map(Student, reader)) # read all remaining lines
for student in students:
print "%-17s|%-10s|%-6s|%3s" % student.print_information()
print "%-17s|%-10s|%s" %(header[0],header[1],header[4])
print "-" * 45
for student in students:
print "%-17s|%-10s|%s" %student.print_first()
print "%-17s|%-10s|%-16s|%s" %(header[0],header[1],header[5],header[6])
print "-" * 45
for student in students:
print "%-17s|%-10s|%-16s|%s" % student.print_second()
print "%-17s|%-10s|%s" %(header[0],header[1],header[7])
print "-" * 45
for student in students:
print "%-17s|%-10s|%s" %student.print_third()
print "%-17s|%-10s|%-15s|%s" %(header[0],header[1],header[8],header[9])
print "-" * 45
for student in students:
print "%-17s|%-10s|%-16s|%s" % student.print_fourth()
k = len(students)
# Printing all sports that are specified by students
for s in set(Student.sports): # class attribute
print s, Student.sports.count(s), round(((float(Student.sports.count(s)) / k) *100),1)
# Printing sports that are not picked
allsports = ['Basketball','Football','Other','Baseball','Handball','Soccer','Volleyball','I do not like sport']
allsports.sort()
for s in set(allsports) - set(Student.sports):
print s, 0, '0%'
Student.choice_list = Student.sports
X = Student()
X.most_frequent()
#class Search(Student):
# def __init__(self):
# Student.__init__

first read PEP-8 on naming conventions:
Method Names and Instance Variables
Use the function naming rules: lowercase with words separated by
underscores as necessary to improve readability.
second you are calling mostFrequest on the class Student, not an instance of it. Use the method on an instance instead:
student = Student(row)
student.MostFrequent()

use Student().MostFrequent()
edit:
beware that you use class attributes and this is dangerous. here an example:
>>> class Person:
... name = None
... hobbies = []
... def __init__(self, name):
... self.name = name
...
>>> a = Person('marco')
>>> b = Person('francesco')
>>> a.hobbies.append('football')
>>> b.hobbies
['football']
>>> a.name
'marco'
>>> b.name
'francesco'
>>> a.name = 'mario'
>>> b.name
'francesco'
>>> a.name
'mario'
>>>
as you can see i modify marco's hobbies and francesco's hobbies are modified consequentially.

What you probably want is to define most_frequent as a classmethod:
#classmethod
def most_frequent(cls):
for choice in cls.choice_list:
cls.choice_dict[choice] = cls.choice_dict.get(choice, 0) + 1
cls.mostFrequent = sorted([(v, k) for k, v in cls.choice_dict.items()], reverse=True)
return cls.mostFrequent

First, I recommend making function names lower case only.
The error you get results from the usage of MostFrequent as a static method. For this to work, you need to explicitly pass an instance of Student as first argument.
If called directly on an instance of Student, the instance will implicitly be passed as first argument.
Consider using the staticmethod decorator for static usage of functions.

You only rarely call methods on a class definition (Student)
Almost always, you create an instance of the class
someStudent = Student(someRow)
Then you call the method on the instance ("object"), someStudent.
someStudent.MostFrequent()

Student.MostFrequent means You're trying to use static method, not instance method. So You must first create instance by calling Student() and then call MostFrequent() on it.
P.S.: If this is not part of some arcane project, I urge you to follow PEP 8 and use most_frequent as method name.

in your class def, the method definition
def MostFrequent(self,mostFrequent):
has the extra variable mostFrequent that you probably don't want there. Try changing to :
def MostFrequent(self):

Related

I dont know where I'm going wrong trying to create an object in a subclass through inputs

I've tried many different things so it's a little all over the place, please help
I've been able to make the first class and then in a different file create some objects for it, but for this subclass I need to use user input and I just can't figure it out.
I have made it so the shift input has to be a 1 or 2 for a day or night shift, I just don't have the knowledge for this.
class Employee:
def __init__(self, name, id, dept, title):
self.__name = name
self.__id = id
self.__dept = dept
self.__title = title
def get_name(self):
return self.__name
def get_id(self):
return self.__id
def get_dept(self):
return self.__dept
def get_title(self):
return self.__title
def __str__(self):
result = ""
result += "Name: " + self.get_name() + "\tID Number: " + str(self.get_id()) + \
"\tDepartment: " + self.get_dept() + "\tJob Title:" + self.get_title()
return result
class ShiftEmployee(Employee):
def __init__(self, name, id, dept, title, shift, pay):
Employee.__init__(self, name, id, dept, title)
self.__shift = shift
self.__pay = pay
#classmethod
def inputs(self):
self.__name = input("Enter name: ")
self.__id = input("Enter ID number: ")
self.__dept = input("Enter department: ")
self.__title = input("Enter Jobe title: ")
self.__shift = input("Enter shift: ")
self.__pay = input("Enter hourly pay: ")
#set_shift(self, shift):
#self.__shift = shift
#def set_pay(self, pay):
#self.__pay = pay
def get_shift(self, shift):
if self.__shift == 1:
return "Day"
elif self.__shift == 0:
return "Night"
else:
return "Invalid entry"
def get_pay(self, pay):
return self.__pay
def __str__(self):
result = ""
#result += Employee.__str__(self)
result += "Name: " + self.get_name(ShiftEmployee) + "\tID Number: " + str(self.get_id(ShiftEmployee)) + \
"\tDepartment: " + self.get_dept(ShiftEmployee) + "\tJob Title:" + self.get_title(ShiftEmployee) + \
"\tShift: " + self.get_shift(ShiftEmployee) + "\tHourly Pay: " + str(self.get_pay(ShiftEmployee))
return result
shift_emp = ShiftEmployee
shift_emp.inputs()
print(shift_emp.__str__(ShiftEmployee))
Don't use a classmethod because
A class method is a method that’s shared among all objects.
Though python itself does not force this behavior, your use of self in the inputs definition indicates that you are not doing what you think. the parameter is traditionally named cls in #classmethod-annotated methods, because the object you're referring to inside the body is not an instance of the class, but the class object itself. This means if you have multiple ShiftEmployee objects, they're going to be writing their data to the same variables. This is not what you want to happen.
you are not instantiating a ShiftEmployee object with shift_emp = ShiftEmployee, but rather assigning the class to the variable shift_emp, which is not what you want to do. so if you remove the #classmethod annotation, I think what you want is
shift_emp = ShiftEmployee() # __init__ gets called when you use this constructor invocation
shift_emp.inputs()
print(shift_emp)
Your __str__ methods don't make a lot of sense. You are passing the class object to each getter, which doesn't seem like it's what you'd want to do. The class object defines the class, what you want are the instances of the class. It's an important, if initially confusing distinction. Posting the error you get would help, but here's what I would expect the methods to look like. I'm not using the getters, because this is internal access, but you can use them instead of directly referring to the state variables if you prefer.
# Employee
def __str__(self):
return f"Name: {self.__name} ID Number: {self.__id} Department: {self.__dept} Job Title: {self.__title}"
# ShiftEmployee
def __str__(self):
return super(ShiftEmployee, self).__str__() + f" Shift: {self.__shift} Hourly Pay: {self.__pay}"
So what's going on here? For one thing, we use format strings because they are easier to work with and exactly the thing you wanted. Then we're using the superclass (Employee) to provide the shared functionality, and using the descendent class to enrich with the ShiftEmployee-only data. I skipped the accessor methods because they're redundant when accessing "private" data from inside the class members. Note that this won't quite do what you expect, either, w.r.t. the shift value that gets printed -- it's going to print the int, not "Night" or "Day". This is where your accessor method comes into play, except that your accessor has an extraneous parameter, shift. So you'd have to remove that value.
Please use the following way to initialize the class and printing the class,
shift_emp = ShiftEmployee() # Added Parenthesis
shift_emp.inputs()
print(str(shift_emp)) # Pass class object to inbuilt str() method to get output from __str__() method from class

Output error using OOP

I'm having trouble finding the error in my code. I'm working with the class ColHe which is a collection of objects of type Header, and the main goal is to read a file's header and obtain a list with it's elements.
This is my Header class:
class Header:
def __init__(self, col):
self._day = 'Day:'
self._date = col[1]
self._time = 'Time:'
self._hours = col[3]
self._company = 'Company:'
self._name = 'NSHF'
def __str__(self):
return str(self._day + self._date + ", " + self._time + self._hours
+ ", " + self._company + self._name)
Along with its methods of getters and setters. And this is my ColHe class:
from collections import UserList
from copy import deepcopy
import constants
from Header import Header
class ColHe(UserList):
def __init__(self, file_name):
super().__init__()
in_file=open(file_name)
for i in range(constants.HEADER):
self.append(Header((in_file.readline().strip())))
in_file.close()
def getData(self):
return deepcopy(self)
def __str__(self):
st = ""
for he in self:
st+=str(he)
return st
Where constants.Header equals 6.
I ran the program with:
a=ColHe("file.txt")
print(a.getData())
And got this as an output:
Day:a, Time::, Company:NSHFDay:6, Time:1, Company:NSHFDay:i, Time:e, Company:NSHFDay:4, Time:5, Company:NSHFDay:o, Time:p, Company:NSHFDay:S, Time:F, Company:NSHF
However, the output I'm looking for looks more like this:
["Day:", self._date, "Time:", self._time, "Company:", "NSHF"]
An example of the content of a file would be:
Day:
06:11:2017
Time:
14:55
Company:
NSHF
When you print something in python, you don't print the name you have it stored under, you print its value. IE:
print(self._date) # print whatever is stored in _date
print("self._date") # print the static string 'self._date'
There's no reasonable way to get to your desired output from your demonstrated input. You're misunderstanding core concepts about what values are, where they are stored, and what happens when you reference them.
Use str.format (or f-strings if your code is python 3.6+ only)
def __str__(self):
return 'Day:\n{self._day}\nTime:\n{self._time}\nCompany:\n{self._company}'.format(self=self)

dynamic instances of a class object overwriting each other

I have a simple class that stores simple data. The class is as follows.
class DataFormater:
def __init__(self, N, P, K, price):
self.N = N
self.P = P
self.K = K
self.price = price
The code that calls this class is
from DataFormater import DataFormater
#global variables
ObjectList = [0,1,2,3,4,5,6,7,8,9,10,
11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30,
31,32,33,34,35,36,37,38,39,40,
41,42,43,44,45,46,47,48,49,50]
ObjectListCounter = 0
# main
print "enter you N-P-K values, followed by a coma, then the price"
print "example ----> 5 5 5 %50 "
print "return as many values as you want to sort, then enter, 'done!' when done."
while True:
RawData = raw_input()
if RawData == 'done!':
break
else:
ObjectList[ObjectListCounter] = DataFormater
ObjectList[ObjectListCounter].N = int(RawData[0])
# very simple test way of putting first indice in ObjectList[ObjectListCounter].N
ObjectListCounter += 1
print ObjectList[0].N
print ObjectList[1].N
My idea is that ObjectList[0] would create that object '1' that I could call with 1.N
But, when I call these, it seems that I have overwritten the previous instances.
this is what prints...
return as many values as you want to sort, then enter, 'done!' when done.
12
1
done!
1
1
Thanks so much! And I know that my post is messy, I don't exactly know how to make it more "pretty"
So, it looks like you are assigning the actual class (instead of an instance of the class) in your loop. Where you do this:
ObjectList[ObjectListCounter] = DataFormater
I think what you actually want is this
ObjectList[ObjectListCounter] = DataFormater(...insert args here....)
EDIT to address the comments:
Your class init method looks like this:
def __init__(self, N, P, K, price):
That means that to create an instance of your class, it would look like this:
my_formater = DataFormater(1, 2, 3, 4)
You would then be able to access my_formater.N which would have a value of 1.
What you are trying to do instead is access a CLASS level attribute, DataFormater.N. This is generally used in situations where you have a constant variable that does not change between instances of the class. For example:
class DataFormater():
CONSTANT_THING = 'my thing that is always the same for every instance'
You would then be able to access that variable directly from the class, like this:
DataFormater.CONSTANT_THING
I hope that clears things up.

python: get object arg and check NPE in one line

I have a method foo() which eventually returns a Student named john.
#!/usr/bin/python3
from random import randint
class Student(object):
name = ""
def foo():
if randint(0,1) == 0:
student = Student()
student.name = 'john'
else:
student = None
return student
I want to get the name of the student returned by foo() (avoiding NPEs and avoiding several calls to foo()).
My actual solution is:
student = foo()
print(student.name if student is not None else None)
Is it possible to do this in one line, and without creating a temporary variable ?
You can use getattr(obj, 'attr_to_get', default_value).
In your example, the solution would be getattr(foo(), 'name', None).

Python nested objects unable to retrieve value in function

I am trying to build a tree structure that represents a parsed configuration file (the configuration file has a hierarchical structure to it). I represented this as:
class configContainer():
treeDict = {}
instances = {}
class configObject():
def __init__(self, name, configLine, parent):
self.name = name # Assign to the line number to make unique
self.parent = parent
self.children = []
self.configLine = configLine # Actual line of the configuration
configContainer.instances[name] = self
The configContainer contains a set of objects. configContainer.instances uses a key of "line#" to map to the object. treeDict does a similar mapping, but with a different key (I create treeDict after the entire container is created).
I then try to reference two objects inside two different configContainers. This works fine from __main__. But when I pass the two configContainers to a function, instances always returns objects from configContainer2
if __name__ == "__main__":
f1 = open('rfile', 'r')
configFile1 = f1.read()
f1.close()
configTree1 = parseConfig(configFile1)
configTree1.treeDict = createTreeDict(configTree1)
zObject1 = configTree1.instances["line10"]
f2 = open('sfile', 'r')
configFile2 = f2.read()
f2.close()
configTree2 = parseConfig(configFile2)
configTree2.treeDict = createTreeDict(configTree2)
zObject2 = configTree2.instances["line10"]
print "\n\nFrom __main__"
print "###########################"
print configTree1
print configTree2
print zObject1
print zObject2
compareConfigurations(configTree1, configTree2)
def compareConfigurations(tmpTree1, tmpTree2):
print "\n\nFrom compareConfigurations"
print "###########################"
print tmpTree1
print tmpTree2
zObject1 = tmpTree1.instances["line10"]
zObject2 = tmpTree2.instances["line10"]
print zObject1
print zObject2
Result is:
### From __main__
<__main__.configContainer instance at 0xb77a34ec>
<__main__.configContainer instance at 0xb740a68c>
<__main__.configObject instance at 0xb740e3ac>
<__main__.configObject instance at 0xb7414bcc>
### From compareConfigurations
<__main__.configContainer instance at 0xb77a34ec>
<__main__.configContainer instance at 0xb740a68c>
<__main__.configObject instance at 0xb7414bcc>
<__main__.configObject instance at 0xb7414bcc>
I can't figure out why I am always getting back the 0xb7414bcc object from inside compareConfigurations?
configContainer.instances is a class attribute, so if you modify it for any instance of a class it will change for all instances of that class. With your current code any time you create a new configObject with the same name it will overwrite the entry in configContainer.instances for that name. You should either make instances an instance attribute of configContainer or make sure your configObjects have different names.
class configContainer():
def __init__(self):
self.instances = {}
...
Here is a quick example of what is happening:
>>> cc1 = configContainer()
>>> cc2 = configContainer()
>>> cc1.instances["line10"] = "foo"
>>> configContainer.instances
{'line10': 'foo'}
>>> cc2.instances["line10"] = "bar"
>>> configContainer.instances
{'line10': 'bar'}
>>> cc1.instances
{'line10': 'bar'}
You are aware that configContainer.instances doesn’t access a instance variable, right?
if you want to refer to the wrapping class, you will have to do something like this:
class configContainer(object):
treeDict = {}
instances = {}
def configObject(self, name, configLine, parent):
return _configObject(self, name, configLine, parent)
class _configObject(object):
def __init__(self, container, name, configLine, parent):
self.name = name # Assign to the line number to make unique
self.parent = parent
self.children = []
self.configLine = configLine # Actual line of the configuration
container.instances[name] = self
Something either in parseConfig or createTreeDict is corrupting your instances dictionary.
Notice that in main you get both zObject1 and zObject2 from configTree1:
zObject1 = configTree1.instances["line10"]
#...
zObject2 = configTree1.instances["line10"]
#
print zObject1
print zObject2
Which you said produces:
<__main__.configObject instance at 0xb740e3ac>
<__main__.configObject instance at 0xb7414bcc>
If you posted the source to parseConfig and createTreeDict we could get to the root of it.

Categories