Commit 578e25f9 authored by peastman's avatar peastman
Browse files

Merge pull request #153 from rmcgibbo/element

add getByAtomicNumber to element.
parents 82cab8dd 7a44101f
......@@ -44,6 +44,7 @@ class Element:
look up the Element with a particular chemical symbol."""
_elements_by_symbol = {}
_elements_by_atomic_number = {}
def __init__(self, number, name, symbol, mass):
## The atomic number of the element
......@@ -58,6 +59,16 @@ class Element:
s = symbol.strip().upper()
assert s not in Element._elements_by_symbol
Element._elements_by_symbol[s] = self
if number in Element._elements_by_atomic_number:
other_element = Element._elements_by_atomic_number[number]
if mass < other_element.mass:
# If two "elements" share the same atomic number, they're
# probably hydrogen and deuterium, and we want to choose
# the lighter one to put in the table by atomic_number,
# since it's the "canonical" element.
Element._elements_by_atomic_number[number] = self
else:
Element._elements_by_atomic_number[number] = self
@staticmethod
def getBySymbol(symbol):
......@@ -65,6 +76,10 @@ class Element:
s = symbol.strip().upper()
return Element._elements_by_symbol[s]
@staticmethod
def getByAtomicNumber(atomic_number):
return Element._elements_by_atomic_number[atomic_number]
# This is for backward compatibility.
def get_by_symbol(symbol):
s = symbol.strip().upper()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment