File: customizing.rst

package info (click to toggle)
python-periodictable 2.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,068 kB
  • sloc: python: 13,338; makefile: 103; sh: 92; javascript: 7
file content (64 lines) | stat: -rw-r--r-- 2,278 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
.. _custom-table:

*************
Custom tables
*************

You can create your own private instance of PeriodicTable and
populate it with values.

Example:

.. doctest::

    >>> import periodictable
    >>> from periodictable import core, mass, density, elements, formula

    >>> mytable = core.PeriodicTable(table="H=1")
    >>> mass.init(mytable)
    >>> density.init(mytable)

    >>> # Reset mass to H=1 rather than C=12
    >>> scale = elements.H[1].mass
    >>> for el in mytable:
    ...    el._mass /= scale
    ...    if hasattr(el,'_density') and el._density is not None:
    ...        el._density /= scale
    ...    for iso in el:
    ...        iso._mass /= scale

    >>> print("%.10f %.10f"%(mytable.H[1].mass, mytable.C[12].mass))
    1.0000000000 11.9068286857
    >>> print("%.10f %.10f"%(periodictable.H[1].mass, periodictable.C[12].mass))
    1.0078250319 12.0000000000
    >>> print("%.10f"%formula('2H[1]', table=mytable).mass)
    2.0000000000

You will need to add individual properties by hand for all additional
desired properties using ``module.init(elements)``.

The table name (*H=1* above) must be unique within the session.  If you
are pickling elements from a custom table, you must create a custom
table of the same name before attempting to restore them. The default
table is just a custom table with the name *public*.

.. Note: If you are using chemical formulas, you will need to
         define your own parser using::

	from periodictable.formulas import formula_grammar
	parser = formula_grammar(table=elements)


Support for custom tables could be made much smoother by delegating
all properties not defined in the custom table back to the base table,
much like is currently done for :class:`Isotopes <periodictable.core.Isotope>`
and :class:`Ions <periodictable.core.Ion>`. That way you only
need to replace the properties of interest rather than defining all
new properties.

The alternative to using custom tables is to replace a dataset in the
base table using e.g., ``custom_mass.init(elements, reload = True)``, where
custom_mass is your own version of the periodic table values.  Be aware,
however, that this will have a global effect, changing the mass used by
all packages, so you are strongly discouraged from doing so.