Debian Codegraph @

doc/sphinx/guide/customizing.rst

raw
1 .. _custom-table:
2
3 *************
4 Custom tables
5 *************
6
7 You can create your own private instance of PeriodicTable and
8 populate it with values.
9
10 Example:
11
12 .. doctest::
13
14 >>> import periodictable
15 >>> from periodictable import core, mass, density, elements, formula
16
17 >>> mytable = core.PeriodicTable(table="H=1")
18 >>> mass.init(mytable)
19 >>> density.init(mytable)
20
21 >>> # Reset mass to H=1 rather than C=12
22 >>> scale = elements.H[1].mass
23 >>> for el in mytable:
24 ... el._mass /= scale
25 ... if hasattr(el,'_density') and el._density is not None:
26 ... el._density /= scale
27 ... for iso in el:
28 ... iso._mass /= scale
29
30 >>> print("%.10f %.10f"%(mytable.H[1].mass, mytable.C[12].mass))
31 1.0000000000 11.9068286857
32 >>> print("%.10f %.10f"%(periodictable.H[1].mass, periodictable.C[12].mass))
33 1.0078250319 12.0000000000
34 >>> print("%.10f"%formula('2H[1]', table=mytable).mass)
35 2.0000000000
36
37 You will need to add individual properties by hand for all additional
38 desired properties using ``module.init(elements)``.
39
40 The table name (*H=1* above) must be unique within the session. If you
41 are pickling elements from a custom table, you must create a custom
42 table of the same name before attempting to restore them. The default
43 table is just a custom table with the name *public*.
44
45 .. Note: If you are using chemical formulas, you will need to
46 define your own parser using::
47
48 from periodictable.formulas import formula_grammar
49 parser = formula_grammar(table=elements)
50
51
52 Support for custom tables could be made much smoother by delegating
53 all properties not defined in the custom table back to the base table,
54 much like is currently done for :class:`Isotopes <periodictable.core.Isotope>`
55 and :class:`Ions <periodictable.core.Ion>`. That way you only
56 need to replace the properties of interest rather than defining all
57 new properties.
58
59 The alternative to using custom tables is to replace a dataset in the
60 base table using e.g., ``custom_mass.init(elements, reload = True)``, where
61 custom_mass is your own version of the periodic table values. Be aware,
62 however, that this will have a global effect, changing the mass used by
63 all packages, so you are strongly discouraged from doing so.
64