mailr25378 - /trunk/lib/periodic_table.py


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by edward on August 28, 2014 - 13:52:
Author: bugman
Date: Thu Aug 28 13:52:33 2014
New Revision: 25378

URL: http://svn.gna.org/viewcvs/relax?rev=25378&view=rev
Log:
Converted the periodic table in lib.periodic_table into a dictionary type 
object.

The new Element container has been added for storing the information about 
each element in the
table.  The Periodic_table object used the atomic symbol as a key for each 
Element instance.


Modified:
    trunk/lib/periodic_table.py

Modified: trunk/lib/periodic_table.py
URL: 
http://svn.gna.org/viewcvs/relax/trunk/lib/periodic_table.py?rev=25378&r1=25377&r2=25378&view=diff
==============================================================================
--- trunk/lib/periodic_table.py (original)
+++ trunk/lib/periodic_table.py Thu Aug 28 13:52:33 2014
@@ -34,20 +34,11 @@
 from lib.errors import RelaxError
 
 
-class Periodic_table:
-    """The periodic table object."""
-
-    def __init__(self):
-        """Set up the object."""
-
-        # Initialise some data structures.
-        self.symbol = []
-        self.name = []
-        self.atomic_weights = {}
-
-
-    def _add(self, atomic_number=None, symbol=None, name=None, 
atomic_weight=None):
-        """Add an element to the table.
+class Element:
+    """A special object representing each element."""
+
+    def __init__(self, atomic_number=None, symbol=None, name=None, 
atomic_weight=None):
+        """Set up the element object.
 
         @keyword atomic_number:     The atomic number.
         @type atomic_number:        int
@@ -59,14 +50,30 @@
         @type atomic_weight:        str
         """
 
-        # Check that atomic_number is correctly ordered.
-        if atomic_number != len(self.symbol)+1:
-            raise RelaxError("Incorrect setup.")
-
-        # Append the values.
-        self.symbol.append(symbol)
-        self.name.append(name)
-        self.atomic_weights[symbol] = atomic_weight
+        # Store the values.
+        self.atomic_number = atomic_number
+        self.name = name
+        self.atomic_weight = atomic_weight
+
+
+class Periodic_table(dict):
+    """The periodic table object."""
+
+    def _add(self, atomic_number=None, symbol=None, name=None, 
atomic_weight=None):
+        """Add an element to the table.
+
+        @keyword atomic_number:     The atomic number.
+        @type atomic_number:        int
+        @keyword symbol:            The atomic symbol.
+        @type symbol:               str
+        @keyword name:              The chemical element name.
+        @type name:                 str
+        @keyword atomic_weight:     The atomic weight number for the atom.  
This is a string as it uses the IUPAC notation of, for example, "[1.00784, 
1.00811]" and "4.002602(2)" to represent ranges and uncertainty.
+        @type atomic_weight:        str
+        """
+
+        # Add the element container.
+        self[symbol] = Element(atomic_number=atomic_number, name=name, 
atomic_weight=atomic_weight)
 
 
     def atomic_weight(self, symbol=None):
@@ -79,11 +86,11 @@
         """
 
         # Checks.
-        if symbol not in self.atomic_weights:
-            raise RelaxError("The atomic symbol '%s' is unknown." % symbol)
+        if symbol not in self:
+            raise RelaxError("The atomic symbol '%s' cannot be found in the 
periodic table." % symbol)
 
         # The weight.
-        weight = self.atomic_weights[symbol]
+        weight = self[symbol].atomic_weight
 
         # A range, or an unstable isotope.
         if weight[0] == '[':




Related Messages


Powered by MHonArc, Updated Thu Aug 28 15:20:02 2014