Tried the following, where "objectname" contains a string name, to be assigned on creation of an object.
for record in result:
objectname = 'Customer' + str(record[0])
print objectname
customername = str(record[1])
objectname = Customer(customername)
Where Customer is a class.
In my test, this loop runs twice printing "objectname" as Customer1 and Customer2, yet creates 2 objects, but the objects are called "objectname" (it overwrites each loop), opposed to the 2 unique objects Customer1 or Customer2.
Its simply not assigning strings(customer1,2) inside the variable, but purely the variables name.
I've tried assigning strings to the object name, but that gives a syntax error
Surely this must be done all the time, thanks for your help in advance.
Instead of using a new variable for each customer you could store your object in a Python dictionary:
d = dict()
for record in result:
objectname = 'Customer' + str(record[0])
customername = str(record[1])
d[objectname] = Customer(customername)
print d
An example of objects stored in dictionaries
I just could'nt help my self writting some code (more than I set out to do). It's like addictive. Anyway, I would'nt use objects for this kind of work. I probably would use a sqlite database (could be saved in memory if you want). But this piece of code show you (hopefully) how you can use dictionaries to save objects with customer data in:
# Initiate customer dictionary
customers = dict()
class Customer:
def __init__(self, fname, lname):
self.fname = fname
self.lname = lname
self.address = None
self.zip = None
self.state = None
self.city = None
self.phone = None
def add_address(self, address, zp, state, city):
self.address = address
self.zip = zp
self.state = state
self.city = city
def add_phone(self, number):
self.phone = number
# Observe that these functions are not belonging to the class.
def _print_layout(object):
print object.fname, object.lname
print '==========================='
print 'ADDRESS:'
print object.address
print object.zip
print object.state
print object.city
print '\nPHONE:'
print object.phone
print '\n'
def print_customer(customer_name):
_print_layout(customers[customer_name])
def print_customers():
for customer_name in customers.iterkeys():
_print_layout(customers[customer_name])
if __name__ == '__main__':
# Add some customers to dictionary:
customers['Steve'] = Customer('Steve', 'Jobs')
customers['Niclas'] = Customer('Niclas', 'Nilsson')
# Add some more data
customers['Niclas'].add_address('Some road', '12312', 'WeDon\'tHaveStates', 'Hultsfred')
customers['Steve'].add_phone('123-543 234')
# Search one customer and print him
print 'Here are one customer searched:'
print 'ooooooooooooooooooooooooooooooo'
print_customer('Niclas')
# Print all the customers nicely
print '\n\nHere are all customers'
print 'oooooooooooooooooooooo'
print_customers()
It is generally not that useful to have dynamically generated variable names. I would definitely suggest something like Niclas' answer instead, but if you know this is what you want here is how you can do it:
for record in result:
objectname = 'Customer' + str(record[0])
print objectname
customername = str(record[1])
exec '%s = Customer(%r)' % (customername, customername)
This will result in the variables Customer1 and Customer2 being added to the innermost scope, exactly like if you had executed the following lines:
Customer1 = Customer('Customer1')
Customer2 = Customer('Customer2')
When doing it this way you need to make sure that customername is a valid Python identifier.
What you need is a dictionary:
customers = {}
for record in result:
objectname = 'Customer' + str(record[0])
customers[customername] = Customer(str(record[1])) #assignment to dictionary
Related
I have the following code that records job candidates' personal details in a class Employee:
class Employee:
def __init__(self, name, role, id):
self.name = name
self.role = role
self.id = id
self.interviewed = False
def __str__(self):
text = f'Candidate {self.name}; {self.id}. '
if self.interviewed == False:
return text + 'Not interviewed yet.'
else:
return text + 'Interviewed.'
def interview(self):
self.interviewed = True
I also have another class Database that lists all the candidates in a database for a particular employer:
class Database:
def __init__(self, company, employer):
self.company = company
self.employer = employer
self.candidates = []
def __str__(self):
text = f'{The hiring company is {self.company} and the employers name is {self.employer}'
return text
def add_candidate(self, candidate):
self.candidates.append(candidate)
Now, if we record personal details of two candidates in the class Employee and add them to the class Database using the method add_candidate, how do I create a new method called list_interviewed_candidates(self) in the class Database that will print all candidates that have self.interviewed set to True?
This is what I tried:
class Database:
def __init__(self, company, employer):
self.company = company
self.employer = employer
self.candidates = []
def __str__(self):
text = f'{The hiring company is {self.company} and the employers name is {self.employer}'
return text
def add_candidate(self, candidate):
self.candidates.append(candidate)
def list_interviewed_candidates(self):
for employee in self.candidates:
if employee.interviewed == True:
return employee
But that doesn't work. I have also tried list comprehension but it seems I just cannot access the boolean value that was set in the first class. Ideally, the output should look something like this:
database1 = Database('Google', 'Jack H')
print(database1)
'The hiring company is Google and the employers name is Jack H'
candidate1 = Employee('Anna S', 'web-designer', 12)
database1.add_candidate(candidate1)
print(database1.list_interviewed_candidates())
[]
candidate1.interview()
print(database1.list_interviewed_candidates())
['Candidate Ana S; 12 - Interviewed']
In your Database.list_interviewed_candidates method you are returning the first employee that was interviewed. Keep in mind that return exits from the current function (method in this case) as soon as is hit.
So it starts looking at your candidates and as soon as one interviewed is found, it returns that one.
You probably want to gather them all in a list and return that:
def list_interviewed_candidates(self):
retval = []
for employee in self.candidates:
if employee.interviewed == True:
retval.append(employee)
return retval
Something pretty interesting you could also use is yield:
def list_interviewed_candidates(self):
for employee in self.candidates:
if employee.interviewed == True:
yield employee
Which... you can try by doing:
print(list(database1.list_interviewed_candidates()))
Pretty cool. This opens the really fun world of iterators!
A list comprehension works, but realize __str__ is used by print but __repr__ is used for items displayed in a list. __repr__ is also used if __str__ isn't defined.
Try the following, but change __str__ to __repr__ in Employee:
def list_interviewed_candidates(self):
return [employee for employee in self.candidates if employee.interviewed]
Then:
database1 = Database('Google', 'Jack H')
print(database1)
candidate1 = Employee('Anna S', 'web-designer', 12)
candidate2 = Employee('Mark T', 'pythonista', 13)
database1.add_candidate(candidate1)
database1.add_candidate(candidate2)
print(database1.list_interviewed_candidates())
candidate1.interview()
candidate2.interview()
print(database1.list_interviewed_candidates())
Outputs:
The hiring company is Google and the employers name is Jack H
[]
[Candidate Anna S; 12. Interviewed., Candidate Mark T; 13. Interviewed.]
Customize __str__ and __repr__ individually if you want differing output between direct print of Employee and how it is displayed in a list.
I am generating a class of persons and want to get information about a certain person by input. I would like to use the str funtction because I am trying to understand it better. My Idea goes as follows:
class Person:
__init__(self, f_name, l_name):
self.f_name = f_name
self.l_name = l_name
__str__(self):
return "The persons full name is:" + f_name + l_name
person1 = Person(Peter, Punk)
person2 = Person(Mia, Munch)
person = input("What persons full name would you like to know?")
print(person) #I am aware that this just fills in the string saved in person, but how do I connect it to the variable?
another idea was to do it as follows:
#class stays the same except:
__init__(self, f_name, l_name):
self.f_name = f_name
self.l_name = l_name
list.append(self)
#and then for the main:
list = []
person1 = Person(Peter, Punk)
person2 = Person(Mia, Munch)
person = input("What persons full name would you like to know?")
index = list(person)
print(list[index])
Thankful for any edvice since I am obviously new to Python :D
I think OP has some concept problems here which this answer may go some way to help with.
Start by building a robust class definition. Simple in this case as there are just 2 attributes. Note the use of setters, getters and str, repr and eq dunder overrides.
A small function that checks if a given Person can be found in a list of Persons and reports accordingly.
Create a list with 2 different Person instances
Create another Person that is known not to match anything already in the list.
Run check()
Modify the 'standalone' Person to make it equivalent to something previously constructed.
Run check()
class Person:
def __init__(self, forename, surname):
self._forename = forename
self._surname = surname
#property
def forename(self):
return self._forename
#forename.setter
def forename(self, forename):
self._forename = forename
#property
def surname(self):
return self._surname
#surname.setter
def surname(self, surname):
self._surname = surname
def __str__(self):
return f'{self.forename} {self.surname}'
def __repr__(self):
return f'{self.forename=} {self.surname=}'
def __eq__(self, other):
if isinstance(other, type(self)):
return self.forename == other.forename and self.surname == other.surname
return False
def check(list_, p):
if p in list_:
print(f'Found {p}')
else:
print(f'Could not find {p}')
plist = [Person('Pete', 'Piper'), Person('Joe', 'Jones')]
person = Person('Pete', 'Jones')
check(plist, person)
person.surname = 'Piper'
check(plist, person)
Output:
Could not find Pete Jones
Found Pete Piper
You probably want a mapping between a name and an object. This is what Python's dict dictionary structure is for:
people = {} # an empty dictionary
people[f'{person1.f_name} {person1.l_name}'] = person1
people[f'{person2.f_name} {person2.l_name}'] = person2
This is creating a string of the first and last name.
You can then lookup the Person object using the full name:
print(people['Peter Punk'])
You could do this with list comprehension like so (also allowing multiple people to have the same first name)
class Person:
__init__(self, f_name, l_name):
self.f_name = f_name
self.l_name = l_name
__str__(self):
return "The persons full name is:" + f_name + l_name
personList= []
personList.append(Person(Peter, Punk))
personList.append(Person(Mia, Munch))
personName = input("What persons full name would you like to know?")
print([str(person) for person in personList if person.f_name == personName])
I have a class with several objects. I am supposed to take a text file, open it and parse it into a list of objects, then return the list of Entry objects
I'm struggling to understand how to convert the list I made from the file, into a list of objects.
class Entry:
def __init__(self, account_num, name, balance, phone, city):
self.account_num = account_num
self.name = name
self.balance = balance
self.phone = phone
self.city = city
def read_file(file_given):
open_file = open(file_given)
entry_obj = []
for line in open_file:
word = line.split()
entry_obj.append(word)
open_file.close()
return entry_obj
the text file I'm reading contains:
100 Alan Jones 348.17 8053564820 SLO
700 Suzy Green -14.22 8052586912 SLO
Maybe something like this should work:
def map_to_entry(line):
attribute_list = line.split()
account_num = attribute_list[0]
name = "{} {}".format(attribute_list[1], attribute_list[2])
balance = attribute_list[3]
phone = attribute_list[4]
city = attribute_list[5]
return Entry(account_num, name, balance, phone, city)
def read_file(file_given):
open_file = open(file_given)
entry_obj = []
for line in open_file:
entry_obj.append(map_to_entry(line))
open_file.close()
return entry_obj
It could be as easy as using
objList = [Entry(*li) for li in data]
But sadly, for some reason your names are split like this. Alan Jones and it creates a list like
[['100', 'Alan', 'Jones', '348.17', '8053564820', 'SLO'], ['700', 'Suzy', 'Green', '-14.22', '8052586912', 'SLO']]
therefor we will get the error, TypeError: __init__() takes 6 positional arguments but 7 were given.
So we have to join these elements.
new = []
for l in read_file():
l[1:2] = [' '.join(l[1:3])]
l.pop(2)
new.append(l)
objList = [Entry(*li) for li in new]
objList will look like.
[<__main__.Entry object at 0x7fe78db6a970>, <__main__.Entry object at 0x7fe78db6acd0>]
You can unpack all the elements during an iteration. If your names are always assumed to be First Name and Last Name, you can concatenate them before creating the Entry object. The following example is quite readable:
entries = []
for account_num, first_name, last_name, balance, phone, city in read_file('data.txt'):
name = first_name + " " + last_name
entries.append(Entry(account_num, name, balance, phone, city))
Also one-liner if you're into that:
entries = [Entry(account_num, first_name + " " + last_name, balance, phone, city) for account_num, first_name, last_name, balance, phone, city in read_file('ok.txt')]
Unpacking in python means to take an iterable with a size you know before-hand and unpack every single element into different variables. Something like:
a, b, c = ['a', 'b', 'c']
The same idea can be applied to elements in a for loop, on which element is another iterable (of a fixed size) itself, as is the case of each row in your file. This allows you to convert a list into named variables, which are usually easier to read.
If your original question was to modify the read_file function, you can use a similar idea and unpack while iterating the file:
def read_file(file_given):
open_file = open(file_given)
entry_obj = []
for line in open_file:
account_num, first_name, last_name, balance, phone, city = line.split()
name = first_name + " " + last_name
entry_obj.append(Entry(account_num, name, balance, phone, city))
open_file.close()
return entry_obj
You defined your class already, but in order to 'instantiate' (create an instance of) class objects, now you must call the class. (seen in example below as Entry(), where the () lets us know that we are calling / invoking the thing)
You definitely want to keep your arguments spelled out in the init() method, just as you have them. It's good to keep things concise and human-readable.
*edit - i changed the name args to be _first, and _last, separately. Reason below.
Note that in the below example, the loop variable line is a complete, single set of Entry data. Ergo, by using an asterisk before it, Python will look for a list (or really, any iterable) and it will automatically send each list element to the function call as its own arg.
e.g. if line == 'a b c', then Entry(*line.split()) is equivalent to Entry('a', 'b', 'c')
class Entry:
def __init__(self, account_num, name_first, name_last, balance, phone, city):
self.account_num = account_num
self.name = name_first + ' ' + name_last
self.balance = balance
self.phone = phone
self.city = city
def read_file(file_given):
open_file = open(file_given)
lines = open_file.readlines()
entry_obj = []
for line in lines:
entry_obj.append(Entry(*line.split()))
open_file.close()
return entry_obj
**edit - this, of course, assumes that we know the order of the data columns as they are received. there are much more robust ways of making this code future-proof, but I digress.
I wrote Class and created two lists. The first for the users and the second for the user-attributes.
I would now like to loop trough the two lists in order to create multiple Class instances with the respective data.
class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '#company.com'
emp_1 = Employee('Corey', 'Schafer', '50000')
emp_2 = Employee('Test', 'User', '60000')
by printing print(amp_1.email) i can create the instance of the class:
will print
Corey.Schafer#company.com
Now i dont want to write it out manually so i want to loop trough it:
for user in users:
for user_atr in user_atrs:
print(user + '.' + user_atr)
will print:
empy_1.first
empy_1.last
empy_1.pay
empy_1.email
empy_2.first
empy_2.last
empy_2.pay
empy_2.email
Instead of:
Corey
Schafer
50000
Corey.Schafer#email.com
Test
User
60000
Test.User#email.com
How can i use that loop to actually create the instance of the class and not just the blueprint?
Basic Solution
a list of string of the users : ['emp_1', 'emp_2']
a list of string that are attributs name ['first', 'last', 'pay', 'email']
Then use the builtin method globals() to get the variable and getattr(obj, name, default) but that isn't nice and requires to type variable names
emp_1 = Employee('Corey', 'Schafer', 50000)
emp_2 = Employee('Test', 'User', '60000')
for user in ['emp_1', 'emp_2']:
for user_atr in ['first', 'last', 'pay', 'email']:
print(getattr(globals()[user], user_atr))
Better Solution
a list of Employee instances : [emp_1, emp_2]
Access object properties with __dict__ (key is name, value are property's value)
for user in [emp_1, emp_2]:
for user_atr in user.__dict__.values():
print(user_atr)
Corey
Schafer
50000
Corey.Schafer#company.com
...
To read both name and value at the same time
for user in [emp_1, emp_2]:
for user_atr in user.__dict__.items():
print(user_atr)
('first', 'Corey')
('last', 'Schafer')
('pay', 50000)
('email', 'Corey.Schafer#company.com')
...
class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '#company.com'
emp_1 = Employee('Corey', 'Schafer', '50000')
emp_2 = Employee('Test', 'User', '60000')
users = {"emp_1": emp_1,
"emp_2": emp_2}
for user in users.values():
for attribute in list(user.__dict__.keys()):
print(f"{name}.{attribute}")
This will do that for you by storing the users in a dictionary with the name of the user as the dictionary key and then using the __dict__ attribute from the Employee class to get a dictionary of all atributes and then printing the name of each attribute from that dictionary.
If you are trying to print the value of each of these attributes then it can be changed to the following:
users = {"emp_1": emp_1,
"emp_2": emp_2}
for name, user in users.items():
for attribute in list(user.__dict__.keys()):
print(getattr(user,attribute))
Using this method means that any more attributes added to the employee class will be printed also.
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 8 years ago.
I have tried using list and/or dictionaries to store the objects that I create, but my attempts have failed.
A reasons to use a list or a dictionary is to have python keep a reference to the object so as prevent the garbage collection component from discarding our object. My issues seems to be more with the initialization of the object itself, and with naming the object.
My script takes data out of a database and then automatically creates objects for each item in the list, in our case, employees.
Here is my class:
class Employee:
empCount = 0
def __init__(self, employee_id, name, age):
self.employee_id = employee_id
self.name = name
self.age = age
Employee.empCount += 1
def display_employee_count(self):
print("Total Employee %d" % Employee.empCount)
def display_employee(self):
print("Employee ID : ", self.employee_id,
" First name : ", self.name,
' Age : ', self.age)
I know that to create an object I would call
joe1883 = Employee('joe1883', 'Joe', 21)
To display the objects attributes I would
joe1883.display_employee()
which yields:
Employee ID : joe1883 First name : Joe Age : 21
My question is, how do I do this through a loop ?
# Data set has, employee id, first name, and age
my_list = [['joe1883', 'joe', 21],
['bob1492', 'bob', 22],
['frank1889','frank',34]]
for names in my_list:
employee_id = names[0]
name = names[1]
age = names[2]
#print("Employee ID : " + employee_id + " First name : " + name + ' Age : ' + str(age))
# This is the line that creates my error !
employee_id = Employee(employee_id, name, age)
If I insert this line into the loop statement
employee_id = Employee(employee_id, name, age)
And then call
joe1883 = Employee('joe1883', 'Joe', 21)
I get a #NameError: name 'joe1883' is not defined.
You need to keep a reference to an object around not just so it isn't garbage-collect, but also so you can reference it. The reference can be by giving it a variable name, like joe1883, or it can be by storing the object in a container, like a list or dictionary.
If you put it in a list, each element in the list can be reference with an integer index, such as employees[42]. To find an employee in such a container you will need to search through potentially all of them, by index, until you find the one you want.
If you want to find an employee quickly given their id, you should store them in a dictionary instead, then you could do something like employees['joe1883'] to directly access one of them.
Since you don't know kind of container an Employee instance will be put in, if any, it makes little sense to keep an empCount in the class. If you put a bunch of them in a list or dictionary, and ever need to know how many, you can find that by using the built-in len() function on the container object, i.e. len(employee_list) or len(employee_dict).
class Employee:
def __init__(self, id, name, age):
self.id = id
self.name = name
self.age = age
def display(self):
print("Employee ID : ", self.id,
" First name : ", self.name,
' Age : ', self.age)
# create a single employee object and name it joe1883
joe1883 = Employee('joe1883', 'Joe', 21)
joe1883.display()
# now create a list of employees by converting dataset
# my_list into a list of Employee instances named employees
# dataset
my_list = [
['joe1883', 'joe', 21],
['bob1492', 'bob', 22],
['frank1889','frank', 34],
]
employees = [Employee(item[0], item[1], item[2]) for item in my_list]
# display each employee in list
for employee in employees:
employee.display()
# or you could do it this (non-pythonic) way:
for i in range(len(employees)):
employees[i].display()
# to print an employee with a given id
# you'd need to search for it in the list
for employee in employees:
if employee.id == 'joe1883':
employee.display()
break # found, so stop loop
# Here's how to create a dictionary of employees by converting dataset
# my_list into Employee instances keyed by their id
employees = {item[0]: Employee(item[0], item[1], item[2]) for item in my_list}
# look one of them up
if 'joe1883' in employees:
employees['joe1883'].display()
If I understand your question correctly, you are trying to create a set of objects-employees based on the list with the arguments for each of the objects.
I modified/simplified your code a little bit and this is working fine:
class Employee:
empCount = 0
def __init__(self, employee_id, name, age):
self.employee_id = employee_id
self.name = name
self.age = age
Employee.empCount += 1
def display_employee_count(self):
print("Total Employee %d" % Employee.empCount)
def display_employee(self):
print("Employee ID : {0} First name : {1} Age : {2}".format(self.employee_id, self.name,self.age))
my_list = [('joe1883', 'joe', 21), ('bob1492', 'bob', 22), ('frank1889', 'frank', 34)] # in this format arguments will be easier to pass as parameters to your class constructor
list_of_employees = [] # list where you can put created objects
for emp in my_list:
list_of_employees.append(Employee(*emp)) # now you just pass your tuple of arguments like that
for emp in list_of_employees: #looping through the list with objects you created and printing results
emp.display_employee()
#eduard You are doing 2 things wrong.
1) the indentation is bad, so the methods of the class Employee are not inside the class.
2) You are not storing the objects anywhere.
Id suggest you to do it in this way:
employ_dic={}
class Employee:
def __init__(self, employee_id, name, age):
self.employee_id = employee_id
self.name = name
self.age = age
employ_dic[employee_id]=self
def display_employee(self):
print '''Employee ID: {0}, First Name: {1}, Age: {2}'''.format(self.employee_id, self.name, self.age)
lists = [['joe1883', 'joe', 21],['bob1492', 'bob', 22],['frank1889','frank',34]]
for list in lists:
employee_id = Employee(list[0], list[1], list[2])
employ_dic['joe1883'].display_employee()
# to display the count:
print len(employ_dic)