Search code examples
pythonlistcall

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 SourceNotDefinedException(Exception):
def __init__(self, message):
    super(SourceNotDefinedException, self).__init__(message)

class tvorechoObject(object):
"""The class stores a pair of objects, "tv" objects, and "echo" objects. They are accessed
simply by doing .tv, or .echo. If it does not exist, it will fall back to the other variable.
If neither are present, it returns None."""
def __init__(self, echo=None, tv=None):
    self.tv = tv
    self.echo = echo

def __repr__(self):
    return str({"echo": self.echo, "tv": self.tv}) # Returns the respective strings

def __getattribute__(self, item):
    """Altered __getattribute__() function to return the alternative of .echo / .tv if the requested
    attribute is None."""

    if item in ["echo", "tv"]:    
        if object.__getattribute__(self,"echo") is None: # Echo data not present
            return object.__getattribute__(self,"tv") # Select TV data
        elif object.__getattribute__(self,"tv") is None: # TV data not present
            return object.__getattribute__(self,"echo") # Select Echo data
        else:
            return object.__getattribute__(self,item) # Return all data

    else:
        return object.__getattribute__(self,item) # Return all data


class Chemical(object):
    def __init__(self, inputLine, sourceType=None):
        self.chemicalName = TVorEchoObject()    
        self.mass = TVorEchoObject()
        self.charge = TVorEchoObject()


        self.readIn(inputLine, sourceType=sourceType)

def readIn(self, inputLine, sourceType=None):

    if sourceType.lower() == "echo": # Parsed chemical line for Echo format 


        chemicalName            = inputLine.split(":")[0].strip()
        mass               = inputLine.split(":")[1].split(";")[0].strip()
        charge                 = inputLine.split(";")[1].split("]")[0].strip()


        # Store the objects
        self.chemicalName.echo = chemicalName
        self.mass.echo = mass
        self.charge.echo = charge


    elif sourceType.lower() == "tv": # Parsed chemical line for TV format


        chemicalName          = inputLine.split(":")[0].strip()
        charge               = inputLine.split(":")[1].split(";")[0].strip()
        mass                 = inputLine.split(";")[1].split("&")[0].strip()


        # Store the objects
        self.chemicalName.tv = chemicalName
        self.charge.tv = charge
        self.mass.tv  = molecularWeight

    else:
        raise SourceNotDefinedException(sourceType + " is not a valid `sourceType`") # Otherwise print 


def toDict(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 text
        return dict([(attributeName, self.__getattribute__(attributeName).__getattribute__(priority))
            for attributeName in ["chemicalName", "mass", "charge"]])
    else:
        raise SourceNotDefinedException("{0} source type not recognised.".format(priority)) # Otherwise print






from ParseClasses import Chemical
allChemical = []
chemicalFiles = ("/home/temp.txt")


for fileName in chemicalFiles:
    with open(fileName) as sourceFile:
        for line in sourceFile:
        allChemical.append(Chemical(line, sourceType=sourceType))

for chemical in allChemical:
    print chemical.chemicalName #Prints all chemicals and their data in list format

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

outputs the following error which I have tried to remedy with no luck; TypeError: 'list' object is not callable


Solution

  • Try this function:

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

    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:

    def toDict(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 text
            return dict([(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