Elementable
- class elementable.Elementable(units: typing.Dict[str, typing.Any] = {}, converters: typing.Dict[str, typing.Callable] = {'name': <function Elementable.<lambda>>, 'symbol': <function Elementable.<lambda>>}, element_cls: typing.Type = <function NamedTuple>, json_file: typing.Optional[str] = None, decimals: typing.Optional[int] = 4, key_attr: str = 'symbol', key_transform: typing.Callable = <function Elementable.<lambda>>)[source]
Class factory for generating Elements in a container
- Parameters
units (Dict[str, Any]) – A dictionary of units multiplied with element data for the final value. A key is an attribute (e.g. “mass”). A value should be a unit (e.g. unit.angstrom)
converters (Dict[str, Callable]) – A dictionary of converters to transform element data. A key is an attribute (e.g. “name”). A value is a function (e.g.
lambda x: x.lower()
)element_cls (Type) – The base class that is subclassed to create an Element class.
json_file (str) – The JSON file used to read in the data and create elements. This should be formatted as a list of dictionaries. Each key in the dictionary should be an attribute name. Each value in the dictionary should be the corresponding data value.
decimals (int) – The number of decimals to round floating point data to. The rounding only occurs when registering elements in dictionaries, or when searching for an element. No rounding occurs for the attribute value on the
Element
. IfNone
, no rounding will occur. If units are given, all values are converted into that unit prior to roundind.key_attr (str) – The returned elements container will include all elements as attributes for direct access. By default, key_attr=”symbol”, meaning that the attributes are created from the element symbol. The chosen key must correspond to string values on each element, and each value must be unique.
key_transform (Callable) – A function to transform the key for key_attr. This is useful for values that are not valid Python identifiers. For example, in the default Elements, the empty Element (symbol=”*”) cannot be set as an attribute
elements.*
. The defaultkey_transform
function converts * to X.
- Returns
elements_container – This object holds all the created elements and registries.
- Return type
namedtuple
Examples
The most basic:
elements = Elementable() assert elements.H is elements.registry.name["hydrogen"] assert elements.O is elements(atomic_number=8)
Or with all bells and whistles:
from openff.units import unit from pydantic import BaseModel class Element(BaseModel): class Config: # necessary for openff.unit type arbitrary_types_allowed = True elements = Elementable( units=dict( mass=unit.amu, covalent_radius=unit.angstrom ), element_cls=Element, json_file="my_fancy_json_file.json", decimals=10, key_attr="name" ) assert elements.hydrogen is elements.registry.atomic_number[1]