Author: bugman Date: Mon Jun 11 22:27:24 2012 New Revision: 16871 URL: http://svn.gna.org/viewcvs/relax?rev=16871&view=rev Log: Shifted the id_match() method to the correct class! Modified: branches/interatomic/data/interatomic.py Modified: branches/interatomic/data/interatomic.py URL: http://svn.gna.org/viewcvs/relax/branches/interatomic/data/interatomic.py?rev=16871&r1=16870&r2=16871&view=diff ============================================================================== --- branches/interatomic/data/interatomic.py (original) +++ branches/interatomic/data/interatomic.py Mon Jun 11 22:27:24 2012 @@ -77,118 +77,6 @@ text += " %s: %s\n" % (name, repr(getattr(self, name))) return text - - - def is_empty(self): - """Method for testing if this InteratomContainer object is empty. - - @return: True if this container is empty, False otherwise. - @rtype: bool - """ - - # An object has been added to the container. - for name in dir(self): - # Skip the objects initialised in __init__(). - if name in ['spin_id1', 'spin_id2']: - continue - - # Skip the SpinContainer methods. - if name == 'is_empty': - continue - - # Skip special objects. - if match("^_", name): - continue - - # An object has been added. - return False - - # The SpinContainer is unmodified. - return True - - - -class InteratomList(list): - """List type data container for interatomic specific data.""" - - def __repr__(self): - """The string representation of the object. - - Rather than using the standard Python conventions (either the string representation of the value or the "<...desc...>" notation), a rich-formatted description of the object is given. - """ - - # Intro. - text = "Interatomic data.\n\n" - - # The data. - text += "%-25s%-25s%-25s" % ("Index", "Spin ID 1", "Spin ID 2") + "\n" - for i in xrange(len(self)): - text += "%-25i%-25s%-25s\n\n" % (i, self[i].spin_id1, self[i].spin_id2) - - return text - - - def add_item(self, spin_id1=None, spin_id2=None): - """Append an empty container to the list. - - @keyword spin_id1: The spin ID string of the first atom. - @type spin_id1: str - @keyword spin_id2: The spin ID string of the first atom. - @type spin_id2: str - @return: The new interatomic data container. - @rtype: InteratomContainer instance - """ - - # Check if the two spin ID have already been added. - for i in range(len(self)): - if self[i].id_match(spin_id1, spin_id2): - raise RelaxError("The spin pair %s and %s have already been added." % (spin_id1, spin_id2)) - - # Append a new InteratomContainer. - cont = InteratomContainer(spin_id1, spin_id2) - self.append(cont) - - # Return the container. - return cont - - - def is_empty(self): - """Method for testing if this InteratomList object is empty. - - @return: True if this list contains no InteratomContainers, False otherwise. - @rtype: bool - """ - - # There are no InteratomContainers. - if len(self) == 0: - return True - - # Otherwise. - return False - - - def from_xml(self, interatom_nodes, file_version=None): - """Recreate an interatomic list data structure from the XML spin nodes. - - @param interatom_nodes: The spin XML nodes. - @type interatom_nodes: xml.dom.minicompat.NodeList instance - @keyword file_version: The relax XML version of the XML file. - @type file_version: int - """ - - # Test if empty. - if not self.is_empty(): - raise RelaxFromXMLNotEmptyError(self.__class__.__name__) - - # Loop over the containers. - for interatom_node in interatom_nodes: - # Get the interatomic spin details and add a container to the InteratomList structure. - spin_id1 = str(interatom_node.getAttribute('spin_id1')) - spin_id2 = str(interatom_node.getAttribute('spin_id2')) - self.add_item(spin_id1=spin_id1, spin_id2=spin_id2) - - # Recreate the current container. - xml_to_object(interatom_node, self[-1], file_version=file_version) def id_match(self, spin_id1, spin_id2): @@ -207,6 +95,118 @@ return True else: return False + + + def is_empty(self): + """Method for testing if this InteratomContainer object is empty. + + @return: True if this container is empty, False otherwise. + @rtype: bool + """ + + # An object has been added to the container. + for name in dir(self): + # Skip the objects initialised in __init__(). + if name in ['spin_id1', 'spin_id2']: + continue + + # Skip the SpinContainer methods. + if name == 'is_empty': + continue + + # Skip special objects. + if match("^_", name): + continue + + # An object has been added. + return False + + # The SpinContainer is unmodified. + return True + + + +class InteratomList(list): + """List type data container for interatomic specific data.""" + + def __repr__(self): + """The string representation of the object. + + Rather than using the standard Python conventions (either the string representation of the value or the "<...desc...>" notation), a rich-formatted description of the object is given. + """ + + # Intro. + text = "Interatomic data.\n\n" + + # The data. + text += "%-25s%-25s%-25s" % ("Index", "Spin ID 1", "Spin ID 2") + "\n" + for i in xrange(len(self)): + text += "%-25i%-25s%-25s\n\n" % (i, self[i].spin_id1, self[i].spin_id2) + + return text + + + def add_item(self, spin_id1=None, spin_id2=None): + """Append an empty container to the list. + + @keyword spin_id1: The spin ID string of the first atom. + @type spin_id1: str + @keyword spin_id2: The spin ID string of the first atom. + @type spin_id2: str + @return: The new interatomic data container. + @rtype: InteratomContainer instance + """ + + # Check if the two spin ID have already been added. + for i in range(len(self)): + if self[i].id_match(spin_id1, spin_id2): + raise RelaxError("The spin pair %s and %s have already been added." % (spin_id1, spin_id2)) + + # Append a new InteratomContainer. + cont = InteratomContainer(spin_id1, spin_id2) + self.append(cont) + + # Return the container. + return cont + + + def is_empty(self): + """Method for testing if this InteratomList object is empty. + + @return: True if this list contains no InteratomContainers, False otherwise. + @rtype: bool + """ + + # There are no InteratomContainers. + if len(self) == 0: + return True + + # Otherwise. + return False + + + def from_xml(self, interatom_nodes, file_version=None): + """Recreate an interatomic list data structure from the XML spin nodes. + + @param interatom_nodes: The spin XML nodes. + @type interatom_nodes: xml.dom.minicompat.NodeList instance + @keyword file_version: The relax XML version of the XML file. + @type file_version: int + """ + + # Test if empty. + if not self.is_empty(): + raise RelaxFromXMLNotEmptyError(self.__class__.__name__) + + # Loop over the containers. + for interatom_node in interatom_nodes: + # Get the interatomic spin details and add a container to the InteratomList structure. + spin_id1 = str(interatom_node.getAttribute('spin_id1')) + spin_id2 = str(interatom_node.getAttribute('spin_id2')) + self.add_item(spin_id1=spin_id1, spin_id2=spin_id2) + + # Recreate the current container. + xml_to_object(interatom_node, self[-1], file_version=file_version) def to_xml(self, doc, element):