Skip to content Skip to sidebar Skip to footer

Using Class, Methods To Define Variables

I have a number of chemicals with corresponding data held within a database, how do I go about returning a specific chemical, and its data, via its formula, eg o2. class SourceNotD

Solution 1:

The issue is the two lines

for chemical in allChemical(["o2"]):
    print chemical.chemicalName

allChemical is a list, and you can't just do a_list(). It looks like you're trying to find either ['o2'] or just 'o2' in a list. To do that, you can get the index of the item and then get that index from the list.

allChemical[allChemical.index("o2")]

Solution 2:

Try this function:

defchemByString(chemName,chemicals,priority="echo"):
    for chemical in chemicals:
        chemDict = chemical.toDict(priority)
        if chemDict["chemicalName"] == chemName
            return chemical
    returnNone

This function is using the toDict() method found in the Chemical class. The code you pasted from the Chemical class explains that this method returns a dictionary from the chemical object:

deftoDict(self, priority="echo"):
    """Returns a dictionary of all the variables, in the form {"mass":<>, "charge":<>, ...}.
    Design used is to be passed into the Echo and TV style line format statements."""if priority in ["echo", "tv"]:
    # Creating the dictionary by a large, to avoid repeated textreturndict([(attributeName, self.__getattribute__(attributeName).__getattribute__(priority))
            for attributeName in ["chemicalName", "mass", "charge"]])
    else:
        raise SourceNotDefinedException("{0} source type not recognised.".format(priority)) # Otherwise print

This dictionary looks like this:

"chemicalName" : <the chemical name>
"mass" :         <the mass>
"charge" :       <the charge>

What the function I created above does is iterate through all of the chemicals in the list, finds the first one with a name equal to "o2", and returns that chemical. Here's how to use it:

chemByString("o2",allChemicals).chemicalName

If the above does not work, may want to try using the alternative priority ("tv"), though I'm unsure if this will have any effect:

chemByString("o2",allChemicals,"tv").chemicalName

If the chemical isn't found, the function returns None:

chemByString("myPretendChemical",allChemicals).chemicalName

Solution 3:

EDIT: See my new answer. Leaving this one here since it might still be helpful info.

In python, a list object is a structure holding other objects with an index for each object it contains. Like this:

IndexObject0"hello"1"world"2"spam"

If you want to get to one of those objects, you have to know its index:

objList[0] #returns "hello"stringobject

If you don't know the index, you can find it using the index method:

objList.index("hello") #returns 0

Then you can get the object out of the list using the found index:

objList[objList.index("hello")]

However this is kind of silly, since you can just do:

"hello"

Which in this case will produce the same result.

Your allChemical object is a list. It looks like the line chemicalFiles = ("/home/temp.txt") is filling your list with some type of object. In order to answer your question, you have to provide more information about the objects which the list contains. I assume that information is in the ParseClasses module you are using.

If you can provide more information about the Chemical object you are importing, that may go a long way to helping solve your problem.

IF the objects contained in your list are subclassed from str, this MAY work:

allChemical[allChemical.index("o2")].chemicalName

"02" is a str object, so index is going to look for a str object (or an object subclassed from str) in your list to find its index. However, if the object isn't a string, it will not find it.

As a learning exercise, try this:

classChemical(str):
'''A class which is a subclass of string but has additional attributes such as chemicalName'''def__init__(self,chemicalName):
        self.chemicalName = chemicalName

someChemicals = [Chemical('o2'),Chemical('n2'),Chemical('h2')]

for chemical in someChemicals: print(chemical.chemicalName) 
#prints all the chemical namesprint(someChemicals[0].chemicalName) 
#prints "o2"; notice you have to know the index ahead of timeprint(someChemicals[someChemicals.index("o2")].chemicalName) 
#prints "o2" again; this time index found it for you, but#you already knew the object ahead of time anyway, sot it's a little silly

This works because index is able to find what you are looking for. If it isn't a string it can't find it, and if you don't know what index 'o2' is at, if you want to get to a specific chemical in your list of chemicals you're going to have to learn more about those objects.

Post a Comment for "Using Class, Methods To Define Variables"