I have created a word object, which consists of just two methods, and takes just two parameters. In spite of this apparent simplicity it is behaving in a way that's beyond my comprehension: if I create two instances of the same object, with the same first argument ("dissembling" in this case) the second instance somehow interferes with the first. Printing the instances reveals that they are indeed separate, so why are the interacting in this way?
# Example tested with Python 2.7.3
from collections import namedtuple
DefinitionTuple = namedtuple("Definition", "word word_id text pos")
class Word(object):
def __init__(self, word, defs=None):
""""""
self.definitions = []
self.word = word
if defs != None:
for each in defs:
try:
each.pos
if each.word.lower() == self.word.lower():
self.definitions.append(each)
except AttributeError:
raise AttributeError("Definitions must be named tuples")
self.orderDefinitions()
def orderDefinitions(self):
""""""
ordered = sorted(self.definitions, key=lambda definition: definition.pos)
for i,each in enumerate(ordered):
each.pos = (i+1)
self.definitions = ordered
class Definition(object):
""""""
def __init__(self, definition):
"""Incoming arg is a single namedtuple"""
self.word = definition.word
self.word_id = definition.word_id
self.text = definition.text
self.pos = definition.pos
if __name__ == "__main__":
nt1 = DefinitionTuple("dissemble", 5, "text_string_a", 1)
nt2 = DefinitionTuple("dissemble", 5, "text_string_b)", 2)
nt3 = DefinitionTuple("dissemble", 5, "text_string_c", 3)
# Definiton objects
def_1 = Definition(nt1)
def_2 = Definition(nt2)
def_3 = Definition(nt3)
dissemble = Word("dissemble", [def_1, def_2, def_3])
print "first printing: "
for each in dissemble.definitions:
print each.pos, each.text
# create a new instance of Word ...
a_separate_instance = Word("dissemble", [def_3])
# ... and now the 'pos' ordering of my first instance is messed up!
print "\nnow note how numbers differ compared with first printing:"
for each in dissemble.definitions:
print each.pos, each.text
You create a new instance of Word, but you reuse the same instance of def_3:
a_separate_instance = Word("dissemble", [def_3])
which is stateful. If we look inside using vars:
print vars(def_3)
# create a new instance of Word ...
a_separate_instance = Word("dissemble", [def_3])
print vars(def_3)
We see
{'text': 'text_string_c', 'word': 'dissemble', 'pos': 3, 'word_id': 5}
{'text': 'text_string_c', 'word': 'dissemble', 'pos': 1, 'word_id': 5}
due to orderDefinitions.
In your orderDefinitions method, you are modifying the pos attribute of your Definition objects:
each.pos = (i+1)
So when you call orderDefinitions a second time, you will be doing def_3.pos = 1.
But, dissemble holds a reference to this def_3 object, whose pos attribute has now changed, hence your issue.
Related
I am trying to create a function, that if the user did not enter any value in input field then it sets the text value to 0 or to any other number.
value_text = [self.trig_side_a_value.text, self.trig_side_b_value.text, self.trig_side_c_value.text, self.trig_angle_A_value.text, self.trig_angle_B_value.text, self.trig_angle_C_value.text]
for i in value_text:
if i == "":
i = "0"
else:
pass
Thanks in advance!
OK, so there is quite a bit wrong here. I will try to answer this with as much explanation as possible.
One thing that is wrong:
value_text = [self.trig_side_a_value.text, self.trig_side_b_value.text, self.trig_side_c_value.text, self.trig_angle_A_value.text, self.trig_angle_B_value.text, self.trig_angle_C_value.text]
for i in value_text:
if i == "":
i = "0"
This creates a list value_text and populates it with copies of the text attributes. Changing what is in the list does not affect the elements of self..text.
What needs to happen is that you need to iterate through the attributes of the class, setting each attributes' text member:
Here is a solution:
class TextContainer():
""" This is a class with just one attribute 'text' """
def __init__(self, text=""):
self.text = text
class TestClass():
""" This class contains a set of TextContainer objects as its attributes. """
def __init__(self):
self.trig_side_a_value = TextContainer("a")
self.trig_side_b_value = TextContainer("b")
self.trig_side_c_value = TextContainer()
self.trig_angle_A_value = TextContainer("A")
self.trig_angle_B_value = TextContainer("B")
self.trig_angle_C_value = TextContainer()
def test_me(self):
""" Test the setting of each attribute """
for attr, container in self.__dict__.items():
if not container.text:
container.text = "0"
def __str__(self):
""" Returns a string representation of the class """
return "TestClass: ({}, {}, {}, {}, {}, {})".format(
self.trig_side_a_value.text,
self.trig_side_b_value.text,
self.trig_side_c_value.text,
self.trig_angle_A_value.text,
self.trig_angle_B_value.text,
self.trig_angle_C_value.text)
def main():
test = TestClass()
test.test_me()
print(test)
if __name__ == '__main__':
main()
Hope that helps. Others, please correct my usage of the __dict__ - I'm some shaky ground here.
I have a class User, and a class Theme. The user class can create a Theme, add a Theme to the Theme's dictionary, and should be able to return the dictionary of themes. I'm really new to python so I'm having trouble with the python logic/syntax
class User:
def __init__(self, name):
self.themes = {}
def createTheme(self, name, themeType, numWorkouts, themeID, timesUsed):
newTheme = Theme(name, themeType, numWorkouts, themeID, timesUsed)
return newTheme
and my Theme class:
class Theme:
def __init__(self, name, themeType, numWorkouts, themeID, timesUsed):
#themeType: 1 = genre, 2 = artist, 3 = song
self.name = name
self.themeType = themeType
self.numWorkouts = numWorkouts
self.themeID = themeID
self.timesUsed = timesUsed
I run the test in testUser:
## test createTheme
theme1 = Theme("theme", 2, 5, 1, 0)
self.assertEqual(usr1.createTheme("theme", 2, 5, 1, 0), theme1)
but I get -
Traceback (most recent call last):
File "/Tests/testUser.py", line 52, in test
self.assertEqual(usr1.createTheme("theme", 2, 5, 1, 0), theme1)
AssertionError: !=
I am not sure what I'm doing wrong, can anyone please help?
(Also, I have the following methods in User, but haven't been able to test them yet since my createTheme doesn't work, but I could use some help to see if there are errors in my logic/syntax:
# returns dict
# def getThemes(self):
# return self.themes
#
# def addTheme(self, themeID, theme):
# if theme not in self.themes:
# themes[themeID] = theme
#
# def removeTheme(self, _theme):
# if _theme.timesUsed == _theme.numWorkouts:
# del themes[_theme.themeID]
What is happening
When attempting to determine if two objects are equal, say obj1 == obj2, Python will do the following.
It will first attempt to call obj1.__eq__(obj2), that is a method
defined in the class of obj1 which should determine the logic for
equality.
If this method does not exist, or return NotImplemented, then
Python falls back on calling obj2.__eq__(obj1).
If this is still not conclusive, Python will return id(obj1) == id(obj2),
i.e. it will tell you if both values are the same object in memory.
In your test, Python has to fall back to the third option and your object are two different instances of the class Theme.
What you want to happen
If you expect objects Theme("theme", 2, 5, 1, 0) and usr1.createTheme("theme", 2, 5, 1, 0) to be equal because they have the same attributes, you have to define the Theme.__eq__ method like so.
class Theme:
def __init__(self, name, themeType, numWorkouts, themeID, timesUsed):
#themeType: 1 = genre, 2 = artist, 3 = song
self.name = name
self.themeType = themeType
self.numWorkouts = numWorkouts
self.themeID = themeID
self.timesUsed = timesUsed
def __eq__(self, other)
# You can implement the logic for equality here
return (self.name, self.themeType, self.numWorkouts, self.themeID) ==\
(other.name, other.themeType, other.numWorkouts, other.themeID)
Note that I am wrapping the attributes in tuples and I then compare the tuples for readability, but you could also compare attributes one by one.
As part of a beginners' university Python project, I am currently creating a database of words, be it Nouns, Verbs, Determiners, Adjectives.
Now the problem I am having is that the words being read into the program via the lexicon.readfromfile method are being put into the dictionary via an instance of a class ( be it noun, verb or adjective ). This created the problem that I have absolutely no idea how to call these objects from the dictionary since they do not have variables as keys, but rather memory locations (see the following):
{<__main__.Verb object at 0x02F4F110>, <__main__.Noun object at 0x02F4F130>, <__main__.Adjective object at 0x02F4F1D0>, <__main__.Noun object at 0x02F4F170>}
Does anyone have any idea how I can call these keys in such a way that I can make them usable in my code?
Here is the part I'm stuck on:
Add a method getPast() to the Verb class, which returns the past tense of the Verb. Your getPast() method can simple work by retrieving the value of ‘past’ from the attributes.
Here is a the majority of the code, leaving out the Noun and Adjective classes:
class Lexicon(object):
'A container clas for word objects'
def __init__(self):
self.words = {}
def addword(self, word):
self.words[word.stringrep] = word
def removeword(self, word):
if word in self.words:
del(word)
print('Word has been deleted from the Lexicon' )
else:
print('That word is not in the Lexicon')
def getword(self,wordstring):
if wordstring in self.words:
return self.words[wordstring]
else:
return None
def containsword(self,string):
if string in self.words:
return True
else:
return False
def getallwords(self):
allwordslist = []
for w in self.words:
allwordslist.append(self.words[w])
return set(allwordslist)
def readfromfile(self, x):
filehandle = open(x, 'r')
while True:
line = filehandle.readline()
if line == '':
break
line = line.strip()
info = line.split(',')
if info[1] == 'CN' or info[1] == 'PN':
noun=Noun(info[0],info[1])
noun.setattribute('regular',bool(info[2]))
self.addword(noun)
elif info[1] == 'A':
adjective=Adjective(info[0],info[1])
adjective.setattribute('comparative', bool(info[2]))
self.addword(adjective)
elif info[1] == 'V':
verb=Verb(info[0],info[1])
verb.setattribute('transitive', bool(info[2]))
verb.setattribute('past', info[3])
self.addword(verb)
def writetofile(self, x):
filehandle = open(x, 'w')
for t in self.words.values():
filehandle.write(t.getFormattedString() + '\n')
filehandle.close()
#---------------------------------------------------------------------------#
class Word(object):
'A word of any category'
def __init__(self,stringrep,category):
self.wordattribute = {}
self.stringrep = stringrep
self.category = category
def setattribute(self, attributename, attributevalue):
self.wordattribute[attributename] = attributevalue
def getvalue(self,name):
if name in self.wordattribute:
return self.wordattribute[name]
else:
return none
def __str__(self):
return self.stringrep + ':' + self.category
def __lt__(self,otherword):
return self.stringrep < otherword.stringrep
class Verb(Word):
'"Represents a Verb."'
def __init__(self, stringrep, category):
super().__init__(stringrep,category)
def istransitive(self):
return self.transitive
def getFormattedString(self):
n = '{stringrep},{category}'
n = n.format(stringrep=self.stringrep, category=self.category)
for i in range(1,2):
for v,b in self.wordattribute.items():
n = n+','+str(b)
return n
You have a set there, not a dictionary. A set will let you check to see whether a given instance is in the set quickly and easily, but, as you have found, you can't easily get a specific value back out unless you already know what it is. That's OK because that's not what the set is for.
With a dictionary, you associate a key with a value when you add it to the dictionary. Then you use the key to get the value back out. So make a dictionary rather than a set, and use meaningful keys so you can easily get the value back.
Or, since I see you are already making a list before converting it to a set, just return that; you can easily access the items in the list by index. In other words, don't create the problem in the first place, and you won't have it.
I am fairly new to python. I have tried to define a class, I then want to create an instance from a file, then refer to specific pieces of it, but cannot seem to. This is Python 3.3.0
Here's the class....
class Teams():
def __init__(self, ID = None, Team = None, R = None, W = None, L = None):
self._items = [ [] for i in range(5) ]
self.Count = 0
def addTeam(self, ID, Team, R=None, W = 0, L = 0):
self._items[0].append(ID)
self._items[1].append(Team)
self._items[2].append(R)
self._items[3].append(W)
self._items[4].append(L)
self.Count += 1
def addTeamsFromFile(self, filename):
inputFile = open(filename, 'r')
for line in inputFile:
words = line.split(',')
self.addTeam(words[0], words[1], words[2], words[3], words[4])
def __len__(self):
return self.Count
Here's the code in Main
startFileName = 'file_test.txt'
filename = startFileName
###########
myTestData = Teams()
myTestData.addTeamsFromFile(startFileName)
sample data in file
100,AAAA,106,5,0
200,BBBB,88,3,2
300,CCCC,45,1,4
400,DDDD,67,3,2
500,EEEE,90,4,1
I think I am good to here (not 100% sure), but now how do I reference this data to see... am i not creating the class correctly? How do I see if one instance is larger than another...
ie, myTestData[2][2] > myTestData[3][2] <----- this is where I get confused, as this doesn't work
Why don't you create a Team class like this :
class Team():
def __init__(self, ID, Team, R=None, W = 0, L = 0)
# set up fields here
Then in Teams
class Teams():
def __init__(self):
self._teams = []
def addTeam (self, ID, Team, R=None, W = 0, L = 0)
team = Team (ID, Team, R=None, W = 0, L = 0)
self._teams.append (team)
Now If i got it right you want to overwrite the > operator's behaviour.
To do that overload __gt__(self, other) [link]
So it will be
class Team ():
# init code from above for Team
def __gt__ (self, otherTeam):
return self.ID > otherTeam.ID # for example
Also be sure to convert those strings to numbers because you compare strings not numbers. Use int function for that.
The immediate problem you're running into is that your code to access the team data doesn't account for your myTestData value being an object rather than a list. You can fix it by doing:
myTestData._items[2][2] > myTestData._items[3][2]
Though, if you plan on doing that much, I'd suggest renaming _items to something that's obviously supposed to be public. You might also want to make the addTeamsFromFile method convert some of the values it reads to integers (rather than leaving them as strings) before passing them to the addTeam method.
An alternative would be to make your Teams class support direct member access. You can do that by creating a method named __getitem__ (and __setitem__ if you want to be able to assign values directly). Something like:
def __getitem__(self, index):
return self._items[index]
#Aleksandar's answer about making a class for the team data items is also a good one. In fact, it might be more useful to have a class for the individual teams than it is to have a class containing several. You could replace the Teams class with a list of Team instances. It depends on what you're going to be doing with it I guess.
I will appreciate any effort to clarify the following: is there a way in Python to dynamically create one object per class, where several classes are declared? My silly guess can be described as following:
...
suppose we have some data from db
props = dict_cur.fetchall()
classes_names = []
data = []
for i in props:
classes_names.append(i['client_name'].title())
classes = []
data = []
for i in props:
data.append(dict(i))
for i, d in zip(classes_names, data):
classes.append(type(i, (object,), dict(**d)))
print classes
#printing list of classes
objects = []
for obj in classes:
objects.append(obj())
for obj in objects:
print obj.client_name, obj.client_id
This is very naive approach and it never lets inherit from created classes in a regular way, just like this:
class ClientProcess(Someclient): #Someclient is the name of the created class before
def __init__(self):
print "Someclient stuff"
The goal is pretty simple: create the objects of several classes, preferably with the properties that are stored in the tables, but at the same time have class declaration for every client which will have specific method implemented that will very from class to class. The initial script that works well and uses Python version of Factory method is not sufficient because it only can process one class(client) a time (based on command-line argument which is client id).
If I understand you correctly, you can use the following ways to subclass dynamically created classes:
In : classes = []
In : cls_name = 'BaseCls1'
In : classes.append(type(cls_name, (object, ), {'x': 1}))
In : classes[0].x
Out: 1
In : classes[0].__bases__
Out: (object,)
# two ways to create subclass out of BaseCls1
In : class SubCls1(classes[0]):
: x = 2
:
In : SubCls1.x
Out: 2
In : SubCls1.__bases__
Out: (__main__.BaseCls1,)
In : SubCls2 = type('SubCls2', (classes[0],), {'x': 2})
In : SubCls2.x
Out: 2
In : SubCls2.__bases__
Out: (__main__.BaseCls1,)
class GetConfig(object):
def __init__(self, client_id):
self.client_id = client_id
#construct the query here to get the clients data ...where client_id = self.client_id
d = {'logfile': 'some_long_path', 'contact_name': 'some_name'}
class FirstClient(object):
def __init__(self):
client_id = '111111111'
props = GetConfig(client_id)
#print props.d
def check_source(self):
print "Checking FirstClient source"
return "Something"
#print props.d
def check_downl(self):
print "Checking FirstClient downloaded"
class SecondClient(object):
def __init__(self):
client_id = "222222"
props = GetConfig(client_id)
def check_source(self):
print "Checking SecondClient source"
def check_downl(self):
print "Checking SecondClient downloaded"
myfactory = {
"firstclient" : FirstClient,
"secondclient" : SecondClient,
}
for i in myfactory.values():
i().check_source()
i().check_downl()
collections.namedtuple. done.
Edit: to elaborate,
from collections import namedtuple
rows = dict_cur.fetchall()
# creates the class Row which is a tuple, but each position argument
# corresponds to the column name in that position
# Row can be instantiated as a tuple and then its elements can be accessed
# by name class attributes
Row = namedtuple("Row", zip(*dict_cur.description)[0])
objects = [Row(row) for row in rows]
for o in objects:
print o.client_name, ' is ' , o