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

Improve element not found #142

Merged
merged 2 commits into from
Mar 31, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions mendeleev/mendeleev.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from typing import List, Union

import sqlalchemy

from .db import get_session
from .models import Element, Isotope

Expand Down Expand Up @@ -74,15 +76,17 @@ def _get_element(ids) -> Union[Element, List[Element]]:

session = get_session()

if isinstance(ids, str):
if len(ids) <= 3 and ids.lower() != "tin":
return session.query(Element).filter(Element.symbol == str(ids)).one()
else:
return session.query(Element).filter(Element.name == str(ids)).one()
elif isinstance(ids, int):
return session.query(Element).filter(Element.atomic_number == ids).one()
else:
try:
if isinstance(ids, str):
if len(ids) <= 3 and ids.lower() != "tin":
return session.query(Element).filter(Element.symbol == str(ids)).one()
else:
return session.query(Element).filter(Element.name == str(ids)).one()
elif isinstance(ids, int):
return session.query(Element).filter(Element.atomic_number == ids).one()
raise ValueError("Expecting a <str> or <int>, got: {0:s}".format(type(ids)))
except sqlalchemy.exc.NoResultFound:
raise ValueError(f"Element not found: {ids}")


def get_all_elements() -> List[Element]:
Expand Down
17 changes: 5 additions & 12 deletions tests/test_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,60 +10,56 @@

@pytest.fixture
def session():

return get_session()


def test_query(session):

si = session.query(Element).filter(Element.symbol == "Si").one()
assert si.name == "Silicon"


def test_element():

si = element("Si")
assert si.name == "Silicon"


def test_incorrect_element():
with pytest.raises(ValueError, match="Element not found: si"):
element("si")


@pytest.mark.parametrize("atomic_number", list(range(1, 119)))
def test_elements_get_by_atomic_number(atomic_number):

e = element(atomic_number)
assert e.atomic_number == atomic_number


@pytest.mark.parametrize("symbol", SYMBOLS)
def test_elements_get_by_symbol(symbol):

e = element(symbol)
assert e.symbol == symbol


@pytest.mark.parametrize("name", NAMES)
def test_elements_get_by_name(name):

e = element(name)
assert e.name == name


@pytest.mark.parametrize("symbol", SYMBOLS)
def test_elements_str(symbol):

e = element(symbol)
str(e)


@pytest.mark.parametrize("symbol", SYMBOLS)
def test_elements_repr(symbol):

e = element(symbol)
repr(e)


@pytest.mark.parametrize("symbol", SYMBOLS)
def test_isotopes_str(symbol):

e = element(symbol)
for i in e.isotopes:
str(i)
Expand All @@ -72,21 +68,18 @@ def test_isotopes_str(symbol):

@pytest.mark.parametrize("symbol", SYMBOLS)
def test_electrophilicity(symbol):

e = element(symbol)
e.electrophilicity()


def test__eq__():

elements = get_all_elements()
for e in elements:
clone = element(e.symbol)
assert clone == e


def test__ne__():

elements = get_all_elements()
for e1, e2 in zip(elements[:-1], elements[1:]):
assert e1 != e2
Loading