Unable to run a simple python program - python

I started learning python. Here is a simple program:
class StudentRepo:
def __init__(self):
self.student_list = []
def add(self, student):
self.student_list.append(student)
def get_list(self):
self.student_list
class Student:
def __init__(self, name, age):
self.age = age
self.name = name
from models.student.Student import Student
from services.student.StudentRepo import StudentRepo
s1 = Student("A", 10)
s2 = Student("B", 11)
# What is the issue here ?
StudentRepo.add(s1)
StudentRepo.add(s2)
studentList = StudentRepo.get_list()
for student in studentList:
print(student.name)
What is the issue with s1 = Student("A", 10) ?

There are two mistakes in your code. First, this:
def get_list(self):
self.student_list
should be:
def get_list(self):
return self.student_list
Second, you're using the class StudentRepo where you should be using an instance of StudentRepo:
s1 = Student("A", 10)
s2 = Student("B", 11)
my_roster = StudentRepo()
my_roster.add(s1)
my_roster.add(s2)
studentList = my_roster.get_list()
for student in studentList:
print(student.name)

Related

iterating objects in a list through a loop

So I'm just doing a learning project and I need some help.
class Car:
def __init__(self):
self.speed = 0
self.color = ""
self.weight = 0
self.engine = 4
self.name = ""
self.mpg = 25
self.maintenanceLog = []
self.oilChanges = []
#mutator methods
def setSpeed(self, sp):
self.speed = sp
def setColor(self, cl):
self.color = cl
def setWeight(self, w):
self.weight = w
def setEngine(self, e):
self.engine = e
def setName(self, n):
self.name = n
def setMpg(self, mpg):
self.mpg = mpg
def addOilChange(self, oc):
self.oilChanges.append(oc)
def addMaintenance(self, ml):
self.maintenanceLog.append(ml)
#accessor methods
def getSpeed(self):
return self.speed
def getColor(self):
return self.color
def getWeight(self):
return self.weight
def getEngine(self):
return self.engine
def getName(self):
return self.name
def getMPG(self):
return self.mpg
def getAllOilChanges(self):
print("")
print("----------OIL CHANGES----------")
for oc in self.oilChanges:
print(oc)
def getMaintenanceLogs(self):
print("")
print("----------MAINTENANCE LOGS----------")
for ml in self.maintenanceLog:
print(ml)
def setInfo(car):
car.setSpeed(int(input(f"Speed of {car}")))
car.setWeight(int(input(f"Weight of {car}")))
car.setName(input(f"Name of {car}"))
car.setColor(input(f"Color of {car}"))
car.setEngine(int(input(f"Engine of {car}")))
car.setMpg(int(input(f"Miles per Gallon of {car}")))
def getInfo(car):
print(f"Speed of {car} is {car.getSpeed()} mph.")
print(f"Weight of {car} is {car.getWeight()} pounds.")
print(f"Name of {car} is {car.getName()}.")
print(f"Color of {car} is {car.getColor()}.")
print(f"Engine cylinders of {car} are {car.getEngine()}.")
print(f"Miles per Gallon of {car} is {car.getMPG()}.")
def main():
carList = []
Object1= Car()
carList.append(Object1)
print(carList)
for obj in carList:
setInfo(obj)
getInfo(obj)
main()
Thats's the code, now, whenever I run it, I want to get asked Speed of Object1:
Instead, I get asked Speed of <main.Car object at 0x0000024B093CEFD0>:
How can I see the name of the object instead of that hash value... I want to keep adding objects with the class Car and then pass them through a loop to fill in the information regarding the object, but if the list of objects was [Object 1, Object 2, Object 3... Object N] I want it to refer to that name (Object 1) instead of <main.Car object at 0x0000024B093CEFD0>
You can pass the name to use in the prompts as an argument to setInfo().
def setInfo(car, name=None):
if name is None:
name = car.name
car.setSpeed(int(input(f"Speed of {name}")))
car.setWeight(int(input(f"Weight of {name}")))
car.setName(input(f"Name of {name}"))
car.setColor(input(f"Color of {name}"))
car.setEngine(int(input(f"Engine of {name}")))
car.setMpg(int(input(f"Miles per Gallon of {name}")))
and then use that in the loop.
for i, obj in enumerate(carList, 1):
setInfo(obj, f"Object {i}")
getInfo(obj)

print and sort a list of class in oop python

I have a class name Student like this:
class Student:
def __init__(self, name = '', age = 0, test_score = 0):
self.name = name
self.age = age
self.test_Scores = test_score
def __str__(self):
return "({0},{1},{2})".format(self.name,self.age, self.test_Scores)
and class name Students:
class Students():
stds = list()
def __init__(self) -> None:
pass
def Input(self):
while True:
inputName = input('Enter name: ')
inputAge = int(input('Enter age: '))
inputTestScore = int(input('Enter test score: '))
std = Student(inputName, inputAge, inputTestScore)
if inputAge == 0:
break
self.stds.append(std)
def __str__(self):
return str(self.stds)
Here are some code print out a list of students:
stds = Students()
stds.Input()
print(stds)
With 2 elements in the list, the result after excute look like this:
[<main.Student object at 0x0000026EDEBC5FA0>, <main.Student object at 0x0000026EDEBC5CD0>]
I can't print out stds under a string, how can i fix it. And how to sort stds by the decreasing of age ?
Pls help me !
You shouldn't have class Students, you should have a list of Student. To get the data from the class variables override the __repr__ method. To sort the list you can use lambda with the attribute to sort by as key in sort() function
class Student:
def __init__(self, name='', age=0, test_score=0):
self.name = name
self.age = age
self.test_Scores = test_score
def __repr__(self):
return "({0},{1},{2})".format(self.name, self.age, self.test_Scores)
if __name__ == '__main__':
stds = list()
while True:
inputName = input('Enter name: ')
inputAge = int(input('Enter age: '))
inputTestScore = int(input('Enter test score: '))
std = Student(inputName, inputAge, inputTestScore)
if inputAge == 0:
break
stds.append(std)
stds.sort(key=lambda s: s.name)
print(stds)
You need to write the function str for students class.
def __str__(self):
return '\n'.join(self.stds)
try this =>
class Student:
def __init__(self, name = '', age = 0, test_score = 0):
self.name = name
self.age = age
self.test_Scores = test_score
def __str__(self):
return "({0},{1},{2})".format(self.name,self.age, self.test_Scores)
def __repr__(self):
return "({0},{1},{2})".format(self.name,self.age, self.test_Scores)
def __lt__(self, other):
return self.age < other.age
class Students():
stds = list()
def __init__(self) -> None:
pass
def Input(self):
while True:
inputName = input('Enter name: ')
inputAge = int(input('Enter age: '))
inputTestScore = int(input('Enter test score: '))
std = Student(inputName, inputAge, inputTestScore)
if inputAge == 0:
break
self.stds.append(std)
def __str__(self):
return str(self.stds)
stds = Students()
stds.Input()
print(stds)
stds.stds.sort(reverse=True)
print(stds)
Define a natural order for the students based on their age using #functools.total_ordering:
import functools
#functools.total_ordering
class Student:
def __init__(self, name='', age=0, test_score=0):
self.name = name
self.age = age
self.test_Scores = test_score
def __lt__(self, other):
return self.age < other.age
def __str__(self):
return "({0},{1},{2})".format(self.name, self.age, self.test_Scores)
def test_sorting_students():
alice: Student = Student("Alice", 21, 86)
bob: Student = Student("Bob", 19, 75)
caroline: Student = Student("Caroline", 20, 93)
students = [alice, bob, caroline]
students.sort()
assert students[0] == bob
assert students[1] == caroline
assert students[2] == alice
Then just sort them:
print(stds.sort())

Get object from a list in Python?

I am trying to get one account from some branch but somewhere i am missing something. This line is from method -> but the result is <main.SavingAccount object at 0x000001F2563CEFD0>
class Branch:
def __init__(self, branch_code, city):
self.branch_code = branch_code
self.city = city
self.account_list = []
self.loan_list = []
def getAccount(self, acc_no):
for account in self.account_list:
if account.acc_no == acc_no:
return account
print(f2.getAccount(300005))
try this:
class Branch:
def __init__(self, branch_code, city):
self.branch_code = branch_code
self.city = city
self.account_list = []
self.loan_list = []
def getAccount(self, acc_no):
for account in self.account_list:
if account == acc_no:
return account
f2 = Branch(123,"NY")
f2.account_list=[111,222,300005]
print(f2.getAccount(300005))

Python data structure for class varying by year

I have a class like this...
class person:
def __init__(self, name):
self.name = name
self.carData = ""
self.HouseData = ""
#other assets
And am loading data like this...
for someone in person:
someone.carData = runCarQuery()
someone.HouseData = runHouseQuery()
#load other assets
But the data varies by year, e.g. 2019, 2020 etc and I'm struggling to visualise an appropriate way to represent that in the data structure. I know I could in theory do this...
class person:
def __init__(self, name):
self.name = name
self.carData2019 = ""
self.HouseData2019 = ""
self.carData2020 = ""
self.HouseData2020 = ""
#other assets
But it feels super clumsy so I'm hoping there's a way to point to a 2019 version of the carData object. For example, a data model that supports something like...
for someone in person:
someone.carData['2019'] = runCarQuery('2019')
someone.carData['2020'] = runCarQuery('2020')
someone.HouseData['2019'] = runHouseQuery('2019')
someone.HouseData['2020'] = runHouseQuery('2020')
#load other assets
I know that's not valid but am a bit lost on how to achieve the scenario in python.
You mean:
# __init__
…
someone.carData = {}
someone.HouseData = {}
and then:
for someone in person:
for year in (2019, 2020):
someone.carData[year] = runCarQuery(year)
someone.HouseData[year] = runHouseQuery(year)
?
You can use #property https://docs.python.org/3/library/functions.html#property
class person:
def __init__(self, name):
self.name = name
self.HouseData = ""
#other assets
#property
def carData(self):
this_year = get_this_year()
return runCarQuery(this_year)
some_person = Person()
some_person.carData
You can create a method that does the filtering for you:
class Person:
def __init__(self, name):
self.name = name
self.car_data = []
self.house_data = []
def car_by_year(self, year):
return filter(lambda x: x['date'].year() == year, self.car_data)
def house_by_year(self, year):
return filter(lambda x: x['date'].year() == year, self.house_data)
You'll have to get some data structure in the car_data and house_data objects, perhaps a dictionary to store the year or date values.
p = Person('John')
p.car_data.append({'date': date(2018, 01, 01), 'model': 'Ford'})
for car in p.car_by_year(2018):
print(car)

Python - Remove specific object in list by condition

I have a class "PushInfo"
And generate 300 PushInfo object in list
I want remove duplicate userid and ip in the list
Here is my code:
from faker import Faker
import random
def RemovePustListDuplicateData(PushList):
return list(set([(x.userid, x.ip) for x in PushList]))
def FakeData(number):
PushList = []
fake = Faker()
accountList = [('john','127.0.0.1'),('john','127.0.0.1'),('amy','127.0.0.1'),
('lia','140.112.1.9'),('julia','140.112.1.9'),
('asuka','140.112.1.9'),('roy','140.112.1.9'),('stacie','140.112.1.9'),('ben','123.964.123.41'),
('yich','127.0.0.1'),('beef','127.0.0.1'),('aloha','235.151.123.1'),('yamaha','235.151.123.1')]
for i in range(0,number):
user = random.choice(accountList)
PushList.append(PushInfo(fake.name(),
user[0],
fake.text(max_nb_chars=10),
fake.date(pattern="%Y-%m-%d"),
user[1]
))
return PushList
class PushInfo:
def __init__(self, name, userid, content, time,ip=''):
self.name = name
self.userid = userid
self.content = content
self.time = time
self.ip = ip
PushList = FakeData(300)
print("top 10 push in list:")
for push in PushList[:10]:
print("name:"+push.name+" id:"+push.userid+" content:"+push.content+" time:"+push.time+" ip:"+push.ip)
print("\nremove duplicate userid and ip data")
print(RemovePustListDuplicateData(PushList))
https://repl.it/#YichLin/Remove-object-in-list/
The example code is return tuple list
[(userid,ip),(userid,ip)....]
But the result I want is
[PushInfo(some data),PushInfo(some data),.....]
How to achieve this result?
Try this:
from faker import Faker
import random
def RemovePustListDuplicateData(PushList):
return list(set(PushList))
def FakeData(number):
PushList = []
fake = Faker()
accountList = [('john','127.0.0.1'),('john','127.0.0.1'),('amy','127.0.0.1'),
('lia','140.112.1.9'),('julia','140.112.1.9'),
('asuka','140.112.1.9'),('roy','140.112.1.9'),('stacie','140.112.1.9'),('ben','123.964.123.41'),
('yich','127.0.0.1'),('beef','127.0.0.1'),('aloha','235.151.123.1'),('yamaha','235.151.123.1')]
for i in range(0,number):
user = random.choice(accountList)
PushList.append(PushInfo(fake.name(),
user[0],
fake.text(max_nb_chars=10),
fake.date(pattern="%Y-%m-%d"),
user[1]
))
return PushList
class PushInfo:
def __init__(self, name, userid, content, time,ip=''):
self.name = name
self.userid = userid
self.content = content
self.time = time
self.ip = ip
def __eq__(self, other):
return self.userid==other.userid and self.ip==other.ip
def __hash__(self):
return hash(('userid', self.userid, 'ip', self.ip))
def __repr__(self):
return str(self.userid) + ' ' + str(self.ip)
PushList = FakeData(300)
print("top 10 push in list:")
for push in PushList[:10]:
print("name:"+push.name+" id:"+push.userid+" content:"+push.content+" time:"+push.time+" ip:"+push.ip)
print("\nremove duplicate userid and ip data")
print(RemovePustListDuplicateData(PushList))
You need to implement eq and hash methods in order to check whether two objects are same.
Change the RemovePustListDuplicateData(PushList) function as follows:-
def RemovePustListDuplicateData(PushList):
object_memo = set()
final_list = []
for object in PushList:
if (object.userid, object.ip) in object_memo:
continue
else:
final_list.append(object)
object_memo.add((object.userid, object.ip))
return final_list
I hope it helps!

Categories

Resources