| 1 |
.. _extending: |
| 2 |
|
| 3 |
***************** |
| 4 |
Adding properties |
| 5 |
***************** |
| 6 |
|
| 7 |
The periodic table is extensible. Third party packages can |
| 8 |
add attributes to the table, and they will appear in all of |
| 9 |
the elements. |
| 10 |
|
| 11 |
In order to add a new property to the table, you need to define |
| 12 |
a python package which contains the required information, and can |
| 13 |
attach the information to the periodic table so that it is |
| 14 |
available on demand. This is done with the function ``init(table)`` in |
| 15 |
your table extension. |
| 16 |
|
| 17 |
This example adds the attribute ``discoverer`` to each element. First |
| 18 |
create the file ``discoverer/discoverer.py``: |
| 19 |
|
| 20 |
.. literalinclude:: /discoverer/discoverer.py |
| 21 |
:language: python |
| 22 |
|
| 23 |
Now that we have defined the ``init(table)`` function, we need a way to call it. |
| 24 |
The simplest solution is to load it directly when your package is imported. |
| 25 |
In the current example, this could be done by adding the following |
| 26 |
line to the end of the file:: |
| 27 |
|
| 28 |
init(periodictable.core.default_table()) |
| 29 |
|
| 30 |
This would be fine for the current example because the table size is |
| 31 |
small and load time is fast. For large tables, you may wish to |
| 32 |
delay loading the table data until it is needed. To do this, we |
| 33 |
use the :func:`delayed_load <periodictable.core.delayed_load>` function in our |
| 34 |
package init file ``discoverer/__init__.py``: |
| 35 |
|
| 36 |
.. literalinclude:: /discoverer/__init__.py |
| 37 |
:language: python |
| 38 |
|
| 39 |
The first argument to delayed_load is the list of all attributes that will |
| 40 |
be defined when the module is loaded. The second argument is the loading |
| 41 |
function, whose docstring will appear as the attribute description for |
| 42 |
each attribute in the first list. |
| 43 |
|
| 44 |
Check that it works: |
| 45 |
|
| 46 |
.. doctest:: |
| 47 |
|
| 48 |
>>> import discoverer |
| 49 |
>>> import periodictable |
| 50 |
>>> print(periodictable.magnesium.discoverer) |
| 51 |
J. Black |
| 52 |
|
| 53 |
|
| 54 |
Isotope and ion specific data is also supported. In this case we |
| 55 |
need a data table that contains information for each isotope of |
| 56 |
each element. The following example uses a dictionary of elements, |
| 57 |
with a dictionary of isotopes for each. It adds the ``shells`` |
| 58 |
attribute to Fe[56] and Fe[58]. |
| 59 |
|
| 60 |
Define ``shelltable/shelltable.py``: |
| 61 |
|
| 62 |
.. literalinclude:: /shelltable/shelltable.py |
| 63 |
:language: python |
| 64 |
|
| 65 |
Again, we are going to initialize the table with delayed loading. |
| 66 |
In this case it is very important that we set the ``isotope=True`` |
| 67 |
keyword in the ``delayed_load`` call. If we don't, then the magic we |
| 68 |
use to return the correct value after loading the new table information |
| 69 |
fails. Since unknown attributes are delegated to the underlying |
| 70 |
element, the value for the natural abundance will be returned |
| 71 |
instead. On subsequent calls the isotope specific value will be |
| 72 |
returned. |
| 73 |
|
| 74 |
This is demonstrated in ``shelltable/__init__.py``: |
| 75 |
|
| 76 |
.. literalinclude:: /shelltable/__init__.py |
| 77 |
:language: python |
| 78 |
|
| 79 |
Check that it works: |
| 80 |
|
| 81 |
.. doctest:: |
| 82 |
|
| 83 |
>>> import shelltable |
| 84 |
>>> import periodictable |
| 85 |
>>> print(periodictable.Fe[56].shells) |
| 86 |
56-Fe shell info |
| 87 |
>>> print(periodictable.Ni[58].shells) |
| 88 |
None |
| 89 |
|
| 90 |
|
| 91 |
Ion specific data is more complicated, particularly because of the |
| 92 |
interaction with isotopes. For example, ``Ni[58].ion[3]`` should have |
| 93 |
the same neutron scattering factors as ``Ni[58]`` (the neutron is |
| 94 |
only sensitive to the nucleus), but different scattering factors |
| 95 |
from ``Ni.ion[3]``. X-rays are sensitive to the electronic structure |
| 96 |
of the atom and not the nucleus, so ``Ni[58].ion[3].xray.f0(Q)`` |
| 97 |
and ``Ni.ion[3].xray.f0(Q)`` are the same but different |
| 98 |
from ``Ni.xray.f0(Q)``. |
| 99 |
|
| 100 |
Current support for ion dependent properties is awkward. The X-ray |
| 101 |
table :mod:`periodictable.xsf` creates a specialized structure |
| 102 |
for each ion as it is requested. The magnetic form factor table |
| 103 |
:mod:`periodictable.magnetic_ff` does not try to support ``ion.magnetic_ff`` |
| 104 |
directly, but instead the user must request ``ion.magnetic_ff[ion.charge]``. |
| 105 |
Support for ion mass, which is isotope mass adjusted for the number of |
| 106 |
electrons is built into the Ion class. There are not yet any examples of |
| 107 |
extension tables that depend on both isotope and ion. |
| 108 |
|
| 109 |
Be sure to use the ``ion=True`` keyword for ``delayed_load`` when the |
| 110 |
table extension contains ion specific information. |
| 111 |
|