Bellow is my current code, I am pretty new to Python. I am trying to create a list of Photo instances, where each Photo instance uses the data from each tuple in the tups_list. and save that list in a variable photo_insts. Currently I am not receiving an error, literally, nothing is happening in terminal when I try to run the file.
photo_insts = []
tups_list = [("Portrait 2","Gordon Parks",["chicago", "society"]),("Children in School","Dorothea Lange",["children","school","1930s"]),("Airplanes","Margaret Bourke-White",["war","sky","landscape"])]
class Photo2(object):
def __init__(self, title_str, photo_by,tags_list):
self.title = title_str
self.artist = photo_by
self.tags = tags_list
for i in tups_list:
photo_tuple = (i[0],i[1],i[2])
photo_insts.append(photo_tuple)
print i
Below are tests to run to check for diffrent values:
class Phototest(unittest.TestCase):
def test_photo_insts1(self):
self.assertEqual(type(photo_insts),type([]))
def test_photo_insts2(self):
self.assertEqual(type(photo_insts[0]),type(Photo("Photo2","Photo Student",["multiple","tags"])))
def test_photo_insts3(self):
self.assertEqual([x.title for x in photo_insts],["Portrait 2", "Children in School", "Airplanes"])
def test_photo_insts4(self):
self.assertEqual([x.artist for x in photo_insts],["Gordon Parks","Dorothea Lange","Margaret Bourke-White"])
def test_photo_insts5(self):
self.assertEqual([x.tags for x in photo_insts],[["chicago","society"],["children", "school","1930s"],["war","sky","landscape"]])
I guess this is a typo:
photo_tuple = (i[0],i[1],i[2])
=>
photo_tuple = Photo2 (i[0],i[1],i[2])
Function __init__ is called on creation of an instance Photo2.
If you call Photo2 () within the function __init__ then you get a recursion !
=> The code should look like :
class Photo2(object):
def __init__(self, title_str, photo_by, tags_list):
self.title = title_str
self.artist = photo_by
self.tags = tags_list
# end class
tups_list = [
("Portrait 2","Gordon Parks",["chicago", "society"])
,("Children in School","Dorothea Lange",["children","school","1930s"])
,("Airplanes","Margaret Bourke-White",["war","sky","landscape"])
]
photo_insts = []
for i in tups_list :
photo_tuple = Photo2 (i[0],i[1],i[2])
photo_insts.append(photo_tuple)
print i[0]
for p in photo_insts :
print repr (p)
Output in console :
Portrait 2
Children in School
Airplanes
<__main__.Photo2 object at 0xb7082cac>
<__main__.Photo2 object at 0xb7082ccc>
<__main__.Photo2 object at 0xb7082cec>
Related
I am trying to add new objects to a class(emne) but the new instances of the class needs to be created using user input. So i need a way to be able to chose the name for the object and set some of the values of the objects with user input.
I have already tried to create a function that passes the value of the user input into a x = emner(x) to create it but it only returns:
AttributeError: 'str' object has no attribute 'fagKode'
so i think my issue is that the value of the input is created as a string so that it is not understood as a way to create the function
emne=[]
class Emne:
def __init__(self,fagKode):
self.fagKode = fagKode
self.karakter = ""
emne.append(self)
def leggTilEmne():
nyttEmne = input("test:")
nyttEmne=Emne(nyttEmne)
expected result is that the code creates a new instance of the class.
If by choosing a name you mean your fagKode attribute, what you need is:
fagKode = input('Enter code: ')
Emne(fagKode)
You're adding the instances of Enme to the list in the constructor, so you don't need to save them to a variable.
Alternatively, you can handle that in the function:
emne=[]
class Emne:
def __init__(self,fagKode):
self.fagKode = fagKode
self.karakter = ""
def leggTilEmne():
nyttEmne = input("test:")
enme.append(Emne(nyttEmne))
I'm not sure what exactly you are asking, since you haven't responded to the comments. So,
emne=[]
class Emne:
def __init__(self,fagKode):
self.fagKode = fagKode
self.karakter = ""
emne.append(self)
def leggTilEmne(self, value): # <--- is this what you want
self.nyttEmne= Emne(value)
This is an example of when to use a class method. __init__ should not be appending to a global variable, though. Either 1) have the class method append to a class attribute, or 2) have it return the object and let the caller maintain a global list.
emne = []
class Emne:
emne = []
def __init__(self, fag_kode):
self.fag_kode = fag_kode
self.karakter = ""
#classmethod
def legg_til_emne_1(cls):
nytt_emne = input("test:")
cls.emne.append(cls(nytt_emne))
#classmethod
def legg_til_emne_2(cls):
nyttEmne = input("test:")
return cls(nyttEmne)
Emne.legg_til_emne_1() # Add to Emne.emne
e = Emne.legg_til_emne_2()
emne.append(e)
class Character(object):
def __init__(self):
self.name = ""
self.inventory = []
def add_inventory(self, item):
self.inventory.append(item)
def view_inventory(self):
for i in range(len(self.inventory)):
print "%r. %s" % (i+1, self.inventory[i])
class Hero(Character):
def __init__(self):
self.name = ""
self.inventory = ["Torch", "Wrench", "Laser Cannon"]
Hero().view_inventory()
Hero().add_inventory("Key")
Hero().view_inventory()
I've tested the inventory instance after appending to it with the add_inventory function which shows "Key" as added to the end of the inventory list, however both Hero().view_inventory() print out the same results, without Key included.
As pointed out in the comments by vaultah, you are creating three separate Hero instances. Change as follows:
hero = Hero() # create one Hero instance
hero.view_inventory() # and make the method calls on this very instance
hero.add_inventory("Key")
hero.view_inventory()
I am learning Python out of a book and have written myself a long quiz/type game which prints a summary at the end. However, the summary looks for attributes that will not always exist depending on what choices have been made by the user.
I have abstracted this into a basic example to show what I am trying to do. Essentially, I just want to run an attribute error check, for every variable that does not have an attribute, create an attribute with a default value of N/A.
In the below example, I would want it to print:
Forename: Joe
Surname: Bloggs
Smith Test: N/A
Test 4: N/A
I created a class called CodeCleaner which I was going to use to set the N/A values, but got very stuck!
class QuestionSet(object):
next_set = 'first_set'
class ClaimEngine(QuestionSet):
def current_set(self):
last_set = "blank"
while_count = int(0)
quizset = Sets.subsets
ParentSet = QuestionSet()
while ParentSet.next_set != last_set and int(while_count)<50:
quizset[ParentSet.next_set].questioning()
while_count = while_count+1
class FirstSet(QuestionSet):
def questioning(self):
self.value1 = raw_input("Forename:\n")
QuestionSet.next_set = "second_set"
class SecondSet(QuestionSet):
def questioning(self):
self.value2 = raw_input("Surname:\n")
if self.value2 == "Smith":
self.value3 = "He's a Smith!"
self.value4 = "Test val 4"
QuestionSet.next_set = "summary"
else:
QuestionSet.next_set = "summary"
class CodeCleaner(QuestionSet):
def questioning(self):
mapping = Sets()
sets = mapping.subsets
variable_list = {
[sets['first_set']].value1,
[sets['second_set']].value2,
[sets['second_set']].value3,
[sets['second_set']].value4
}
#while key_no < 4:
# try:
# print variable_list
# except AttributeError:
class Summary(QuestionSet):
def questioning(self):
mapping = Sets()
sets = mapping.subsets
print "Forename:",sets['first_set'].value1
print "Surname:",sets['second_set'].value2
print "Smith Test:",sets['second_set'].value3
print "Test 4:",sets['second_set'].value4
exit(0)
class Sets(object):
subsets = {
'first_set': FirstSet(),
'second_set': SecondSet(),
'summary': Summary()
}
run = ClaimEngine()
run.current_set()
I feel quite lazy asking this question, however, I've been wrestling with this for a few days now! Any help would be appreciated.
I'm not sure I go exactly your approach, but you can implement a __getattr__ method in an object that would be called when the attribute is not found:
class A(object):
def __getattr__(self, name):
print("Creating attribute %s."%name)
setattr(self, name, 'N/A')
Then:
>>> a = A()
>>> a.a
Creating attribute a.
>>> a.a
'N/A'
I have this dictionary:
{"id":3,"name":"MySQL","description":"MySQL Database Server - Fedora 21 - medium","image":"","flavor":""}
And I have this object:
class Record():
id = None
name = None
description = None
image = None
flavor = None
How can I assign values from the dictionary to their corresponding class fields?
Take a dict object as the parameter of init function:
class Record(object):
def __init__(self,record_dict):
try:
self.id = record_dict['id']
self.name = record_dict['name']
self.description = record_dict['description']
self.image = record_dict['image']
self.flavor = record_dict['flavor']
except KeyError:
print 'KeyError'
def get_name(self):
return self.name
adict = {"id":3,"name":"MySQL","description":"MySQL Database Server - Fedora 21 - medium","image":"","flavor":""}
one_obj = Record(adict)
print one_obj
print one_obj.get_name()
output:
<__main__.Record object at 0x022E4C90>
MySQL
works for me...
You probably want something like this:
class Record:
def __init__(self, myDict):
self.id = myDict[“id”]
self.name = myDict[“name”]
self.description = myDict[“description”]
self.image = myDict[“image”]
self.flavor = myDict[“flavor”]
And call it:
rec = Record(myDict)
See here to understand the difference between class and instance variables.
Long story short, class variables have a single value for every instance of the class while instance variables values are unique to each instance.
A class variable is defined like this:
class myClass:
Classvar = ‘something’
An instance variable is defined like this:
class myClass:
def __init__():
Self.instanceVar = ‘something else’
This has already been answered here:
Convert Python dict to object?
My favorite method is this one: x.__dict__.update(d)
You can assign them as follows, assuming your dictionary name is input
id = input['id']
name = input['name']
description = input['description']
image = input['image']
flavor = input['flavor']
Try this method in which you grab the attributes of the object:
r = Record()
attributes = [i for i in dir(r) if not i.startswith('_')]
Basically, there are a bunch of background attributes that contain a bunch of underscores. The dir method gets all the attributes and we create a list of the ones that we want. At this point:
# attributes = ['count', 'description', 'flavor', 'id', 'image', 'index', 'name']
So now we use __setattr__ to set the attributes we just grabbed according to the my_dict
for i in attributes:
r.__setattr__(i, my_dict[i])
See the code run online here.
When you create the Record, pass in the dictionary. Then map the key to the value.
Another simple method, see the code here
r = Record()
for k, v in my_dict.items():
exec('r.' + k + '="' + str(v) + '"')
I have an object Restaurant
class Restaurant:
def __init__(self, name, location, phone, parent = None):
self.staff = []
self.tables = []
self.menu = []
self.name = name
self.location = location
self.phone = phone
self.dict = {"Staff": self.staff, "Tables": self.tables, "Menu": self.menu}
def addStaff(self, staff_object):
self.staff.append(staff_object)
Then I add new staff object (with some properties) to the list self.staff with class method addStaff.
After the object(s) are added I print the self.dict and it is still empty. What is the right way to "update" the values in self.dict to get the right values and eventually display them in tree view like:
-staff
---staff_1
---staff_2
--...
-tables
---table_1
---table_2
--...
-menus
---menu_1
---menu_2
---menu_3
This is an example of how to add to instantiate your class and add items.
class Restaurant:
def __init__(self, name, location, phone, parent = None):
self.staff = []
self.tables = []
self.menu = []
self.name = name
self.location = location
self.phone = phone
self.rest_dict = {"Staff": self.staff, "Tables": self.tables, "Menu": self.menu}
def addStaff(self, staff_object):
self.staff.append(staff_object)
def addTables(self, table_object):
self.tables.append(table_object)
def addMenu(self, menu_object):
self.menu.append(menu_object)
r = Restaurant("foo","bar",911) # instantiate
r.addStaff("Foo")
r.addStaff("Bar") # append using method
r.staff.append("Foo1") # you can also access attributes directly
r.addMenu("Lunch")
r.addMenu("Dinner")
r.menu.append("Lunch")
r.addTables(1)
r.addTables(2)
r.tables.append(3)
print r.rest_dict
{'Menu': ['Lunch', 'Dinner', 'Lunch'], 'Tables': [1, 2, 3], 'Staff': ['Foo', 'Bar', 'Foo1']}
Your real code contains lines like
self.joints = ...
instead of appending to the lists like the code you posted. That doesn't modify the existing list; that points the self.joints attribute to a new list, while everything that had a reference to the old list is unaffected. From then on, anything that accesses self.joints uses the new list, while anything that uses, say, self.model_view[2], uses the old list.
Don't do that. If you want to set the contents of an existing list, slice-assign the contents in:
self.joints[:] = ...
In general, never assign directly to a variable if what you really want to modify is the object.
I have solved the problem it was in line 84. I changed it from:
if not self.MBD_system_constructed
to:
if self.MBD_system_constructed