# Periodic table of elements.
#
# Author       : Chris X. Weichenberger
# Created      : 2012-08-02
# License
# Twilight is a GUI for displaying ligands incompatible with electron density.
#    Copyright (C) 2012  Christian X. Weichenberger, Edwin Pozharski, and
#    Bernhard Rupp.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Modifications:
# YYYY-MM-DD (WHO)  what

from misc import ELEMENTSFN, CTable;

class CElement:
    def __init__(s, Z, symbol, element, weight):
        s._Z       = int(Z);
        s._Symbol  = symbol;
        s._Element = element;
        s._Weight  = float(weight);

    def __str__(s):
        return "%d %s (%s) %7.5f" % (s._Z, s._Symbol, s._Element, s._Weight);
    
    def GetZ(s):
        return s._Z;

    def GetSymbol(s):
        return s._Symbol;

    def GetElement(s):
        return s._Element;

    def GetWeight(s):
        return s._Weight;


class CPeriodicTable(dict):
    """Periodic table of the elements.
    This is a Dictionary with the element symbol as key and a CElement
    instance as its value.
    """
    def __init__(s):
        tab = CTable(ELEMENTSFN, ints={"Z": "%d"}, floats={"weight": "%7.5f"});
        for line in tab.AsTable():
            z, sym, elem, wt = line;
            s[sym.upper()] = CElement(z, sym, elem, wt);

__PERIODIC_TABLE = CPeriodicTable();

def GetPeriodicTable():
    """Get the global CPeriodicTable object (performance related)
    """
    return __PERIODIC_TABLE;

if( __name__=="__main__" ):
    for sym, elem in GetPeriodicTable().items():
        print elem
