6

I'm writing a Python module, and I want many of the functions to have access to information on the periodic table; namely, atomic numbers and their corresponding atomic symbols. The information should never change. I'm struggling with how to implement this.

Hash vs. tuple: A hash would provide very convenient lookup, but could easily be changed. A tuple at least is immutable.

Variable vs. class: I've been trying to keep everything in my module in classes when possible, but I'm not sure that makes sense here, since there should only ever be one periodic table. One source of truth.

Maybe I'm missing something entirely. I've just never seen someone hardcode that much information in the Python projects I've looked at. Guidance would be very appreciated.

pique
  • 96
  • 1
  • 1
  • 6
  • `dict` is the python term for hash. Why does it matter if the `dict` can easily be changed? You're not planning on changing it, are you? I would definitely go with a `dict`. Also, I don't see any point in a class. "Simple is better than complex." – saulspatz Nov 06 '15 at 00:31
  • See also [`pymatgen.core.periodic_table`](https://pymatgen.org/pymatgen.core.periodic_table.html) – Sterling Aug 02 '22 at 05:36

2 Answers2

10

You could always start by installing periodictable it seems very complete - even if you have to do this yourself you can take a look at the code. I used:

sudo pip install periodictable

and then was able to do:

>>> import periodictable as pt
>>> g = pt.Au
>>> g.isotopes
[171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205]
>>> g.density
19.3
>>> g.mass
196.96655
>>> g.name
'gold'
>>> 

Personally I would make a class for element and then encode the members appropriate in instances of the class. e.g.:

class Element(object):
    """ This class represents a single element in the periodic table """
    def __init__(self, Symbol, Name, Number, Group, Period, etc):
       """ 
       Initialises a single element instance all the above prarmeters are required 
       """
       self.Symbol = Symbol
       #etc

You can then have either a definition of each element in your periodic table file, or organise them into named blocks or a single class, etc.

Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
  • Yes, I found this. While I could just use it, it's not a very good example of what I want to do (it's much more elaborate). Most of periodictable is unnecessary for me. When you say "encode the members appropriate in instances of the class," I'm not sure exactly what you mean? – pique Dec 07 '13 at 11:24
  • @pique I have added the start of an element class. – Steve Barnes Dec 07 '13 at 11:45
  • Okay, I had what basically amounts to an element class like that, I was just confused on how to pass it information and where to keep that information, and the advantages and disadvantages of the options. – pique Dec 07 '13 at 11:49
  • Store the information in self. as named attributes pass it in as part of the initialisation process as it is never supposed to change, if you override self.__setattr__ in your base class you can make base characteristics read only. – Steve Barnes Dec 07 '13 at 12:04
  • As for how to store your elements I would suggest trying to capture the structure of the table as it has lessons to learn from that structure. But don't forget that you can have multiple ways to access the data, e.g. by name, symbol, number, (row, col), etc. – Steve Barnes Dec 07 '13 at 12:08
2

Lots of people have already encoded the periodic table in textual form. Ask them for their data. For example if your software is compatible with Wikipedia's licence, then grab the data from them programmatically and put it in a database of some sort, or just grab it on demand.

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
  • 1
    I guess part of my question then is the best way to pull that data in and manipulate it. I have no problem putting the data into a text file, I'm asking more about implementation in Python. – pique Dec 07 '13 at 10:52