Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer loading element data until attribute access #121

Merged
merged 4 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
150 changes: 149 additions & 1 deletion mendeleev/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,152 @@

from .mendeleev import * # noqa F403
from .models import * # noqa F403
from .elements import * # noqa F403

_symbols = [
"H",
"He",
"Li",
"Be",
"B",
"C",
"N",
"O",
"F",
"Ne",
"Na",
"Mg",
"Al",
"Si",
"P",
"S",
"Cl",
"Ar",
"K",
"Ca",
"Sc",
"Ti",
"V",
"Cr",
"Mn",
"Fe",
"Co",
"Ni",
"Cu",
"Zn",
"Ga",
"Ge",
"As",
"Se",
"Br",
"Kr",
"Rb",
"Sr",
"Y",
"Zr",
"Nb",
"Mo",
"Tc",
"Ru",
"Rh",
"Pd",
"Ag",
"Cd",
"In",
"Sn",
"Sb",
"Te",
"I",
"Xe",
"Cs",
"Ba",
"La",
"Ce",
"Pr",
"Nd",
"Pm",
"Sm",
"Eu",
"Gd",
"Tb",
"Dy",
"Ho",
"Er",
"Tm",
"Yb",
"Lu",
"Hf",
"Ta",
"W",
"Re",
"Os",
"Ir",
"Pt",
"Au",
"Hg",
"Tl",
"Pb",
"Bi",
"Po",
"At",
"Rn",
"Fr",
"Ra",
"Ac",
"Th",
"Pa",
"U",
"Np",
"Pu",
"Am",
"Cm",
"Bk",
"Cf",
"Es",
"Fm",
"Md",
"No",
"Lr",
"Rf",
"Db",
"Sg",
"Bh",
"Hs",
"Mt",
"Ds",
"Rg",
"Cn",
"Nh",
"Fl",
"Mc",
"Lv",
"Ts",
"Og",
]


def __getattr__(name):
"""
Dynamically creates an element object and stores it in the module's namespace.

Args:
name (str): The symbol of the element to be created.

Returns:
Element: The element object corresponding to the provided symbol.

Raises:
ImportError: If the element symbol is not found in _symbols.

Examples:
Usage:
>>> from mendeleev import C
>>> print(C.atomic_number)
6
"""
if name in _symbols:
element_obj = element(name) # noqa: F405
globals()[name] = element_obj
return element_obj
raise ImportError(
f"module 'mendeleev' has no element with symbol '{name}', please check you spelling"
)
31 changes: 0 additions & 31 deletions mendeleev/elements.py

This file was deleted.