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

Exceptions report more information #79

Merged
merged 4 commits into from
Jul 8, 2022
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
8 changes: 6 additions & 2 deletions molsysmt/_private/digestion/argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from ..exceptions import WrongGetArgumentError


def digest_argument(argument, element):
def digest_argument(argument, element, caller=""):
""" Helper function to check the names of keyword
arguments passed to get function.

Expand All @@ -14,6 +14,10 @@ def digest_argument(argument, element):
element: str
The name of an element.

caller: str, optional
Name of the function or method that is being digested.
For debugging purposes.

Returns
-------
str
Expand All @@ -27,4 +31,4 @@ def digest_argument(argument, element):
if output_argument in attributes:
return output_argument
else:
raise WrongGetArgumentError(argument)
raise WrongGetArgumentError(argument, caller)
37 changes: 26 additions & 11 deletions molsysmt/_private/digestion/box.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from ..exceptions import IncorrectShapeError


def digest_box(box):
def digest_box(box, caller=""):
# TODO: Function doesn't do anything.
return box


def digest_box_values(box_values):
def digest_box_values(box_values, caller=""):
""" Checks if box_values has the correct shape.

The array should have shape (n, 3) where n is any integer.
Expand All @@ -23,6 +23,10 @@ def digest_box_values(box_values):
box_values : np.ndarray, list or tuple
A quantity with the box lengths.

caller: str, optional
Name of the function or method that is being digested.
For debugging purposes.

Raises
------
IncorrectShapeError
Expand All @@ -37,17 +41,24 @@ def digest_box_values(box_values):
if shape[0] == 3:
box_values = np.expand_dims(box_values, axis=0)
else:
raise IncorrectShapeError(expected_shape="(1, 3)", actual_shape=str(shape))
raise IncorrectShapeError(expected_shape="(1, 3)",
actual_shape=str(shape),
caller=caller)
elif len(shape) == 2:
if shape[1] != 3:
raise IncorrectShapeError(expected_shape="(n, 3)", actual_shape=str(shape))
raise IncorrectShapeError(expected_shape="(n, 3)",
actual_shape=str(shape),
caller=caller
)
else:
raise IncorrectShapeError(expected_shape="(n, 3)", actual_shape=str(shape))
raise IncorrectShapeError(expected_shape="(n, 3)",
actual_shape=str(shape),
caller=caller)

return box_values


def digest_box_lengths_or_angles(box_parameters):
def digest_box_lengths_or_angles(box_parameters, caller=""):
""" Checks if box_parameters have the correct shape. Can be used
to check box_lengths and box_angles.

Expand All @@ -56,6 +67,10 @@ def digest_box_lengths_or_angles(box_parameters):
box_parameters : puw.Quantity
A quantity with the box lengths or box values.

caller: str, optional
Name of the function or method that is being digested.
For debugging purposes.

Returns
-------
puw.Quantity
Expand All @@ -68,15 +83,15 @@ def digest_box_lengths_or_angles(box_parameters):
"""
unit = puw.get_unit(box_parameters)
box_parameters = puw.get_value(box_parameters)
box_parameters = digest_box_values(box_parameters)
box_parameters = digest_box_values(box_parameters, caller)
return box_parameters * unit


def digest_box_lengths(box_lengths):
def digest_box_lengths(box_lengths, caller=""):
""" Checks if box_lengths have the correct shape. """
return digest_box_lengths_or_angles(box_lengths)
return digest_box_lengths_or_angles(box_lengths, caller)


def digest_box_angles(box_angles):
def digest_box_angles(box_angles, caller=""):
""" Checks if box_angles have the correct shape. """
return digest_box_lengths_or_angles(box_angles)
return digest_box_lengths_or_angles(box_angles, caller)
2 changes: 1 addition & 1 deletion molsysmt/_private/digestion/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from ..exceptions import *


def digest_comparison(comparison):
def digest_comparison(comparison, caller=""):
# TODO: function doesn't do anything
return comparison

2 changes: 1 addition & 1 deletion molsysmt/_private/digestion/coordinates.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from ..exceptions import *


def digest_coordinates(coordinates):
def digest_coordinates(coordinates, caller=""):
# TODO: function doesn't do anything
return coordinates

12 changes: 7 additions & 5 deletions molsysmt/_private/digestion/digest.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ def digest(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):

# if not check_args:
# return func(*args, **kwargs)
if not config.argument_checking:
return func(*args, **kwargs)

Expand All @@ -74,7 +72,8 @@ def wrapper(*args, **kwargs):
if ii >= len(args):
break
try:
digested_value = digestion_functions[argument_name](args[ii])
digested_value = digestion_functions[argument_name](args[ii],
caller=func.__name__)
except KeyError:
digested_value = args[ii]
all_args[argument_name] = digested_value
Expand All @@ -89,13 +88,16 @@ def wrapper(*args, **kwargs):
# it will appear in kwargs even if is not
for argument_name, value in kwargs.items():
try:
digested_value = digestion_functions[argument_name](value)
digested_value = digestion_functions[argument_name](value,
caller=func.__name__)
all_args[argument_name] = digested_value
except KeyError:
if argument_name in args_names:
all_args[argument_name] = value
else:
digested_argument_name = digest_argument(argument_name, element_name)
digested_argument_name = digest_argument(argument_name,
element_name,
caller=func.__name__)
all_args[digested_argument_name] = value

return func(**all_args)
Expand Down
10 changes: 7 additions & 3 deletions molsysmt/_private/digestion/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
}


def digest_element(element):
def digest_element(element, caller=""):
""" Helper function to check an element type. Raises a BadCallError
if the element type is not supported by MolSysMT.

Expand All @@ -34,6 +34,10 @@ def digest_element(element):
element : str
The name of the element.

caller: str, optional
Name of the function or method that is being digested.
For debugging purposes.

Raises
------
BadCallError
Expand All @@ -44,7 +48,7 @@ def digest_element(element):
if element_name_lower in elements_from_plural:
return elements_from_plural[element_name_lower]
if element_name_lower not in elements:
raise WrongElementError("wrong element name")
raise WrongElementError(element, caller)
return element_name_lower

raise WrongElementError("element is not a string")
raise WrongElementError(element, caller)
11 changes: 7 additions & 4 deletions molsysmt/_private/digestion/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@
engines_from_lowercase = {ii.lower(): ii for ii in engines}


def digest_engine(engine):
def digest_engine(engine, caller=""):
""" Check the name of the engine.

Parameters
---------
engine : str
The name of the engine

caller: str, optional
Name of the function or method that is being digested.
For debugging purposes.

Raises
------
BadCallError
Expand All @@ -33,6 +37,5 @@ def digest_engine(engine):
try:
return engines_from_lowercase[engine.lower()]
except KeyError:
# TODO: create a wrong engine error
raise WrongEngineError
raise WrongEngineError
raise WrongEngineError(engine, caller)
raise WrongEngineError(engine, caller)
12 changes: 8 additions & 4 deletions molsysmt/_private/digestion/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from ..exceptions import WrongFormError


def digest_form(form):
def digest_form(form, caller=""):
""" Checks that the name of the form is correct and
returns its name capitalized. For example, if
form is mdanalysis.universe it returns mdanalysis.Universe.
Expand All @@ -12,6 +12,10 @@ def digest_form(form):
form: str or list[str]
The name or names of the forms

caller: str, optional
Name of the function or method that is being digested.
For debugging purposes.

Returns
-------
str or list[str]
Expand All @@ -30,11 +34,11 @@ def digest_form(form):
try:
return _dict_forms_lowercase[form.lower()]
except KeyError:
raise WrongFormError(form)
raise WrongFormError(form, caller)


def digest_to_form(to_form):
def digest_to_form(to_form, caller=""):

if to_form is None:
return None
return digest_form(to_form)
return digest_form(to_form, caller)
28 changes: 16 additions & 12 deletions molsysmt/_private/digestion/indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
from ..lists_and_tuples import is_list_or_tuple


def digest_indices(indices):
def digest_indices(indices, caller=""):
""" Checks if indices are of the expected type and value.

Parameters
----------
indices : str or int or list or tuple or range.
The indices

caller: str, optional
Name of the function or method that is being digested.
For debugging purposes.

Returns
-------
str or np.ndarray
Expand All @@ -28,35 +32,35 @@ def digest_indices(indices):
if indices.lower() == 'all':
return 'all'
else:
raise WrongIndicesError
raise WrongIndicesError(type(indices), caller)
elif isinstance(indices, (int, np.int64, np.int32)):
indices = np.array([indices], dtype='int64')
elif isinstance(indices, (np.ndarray, list, tuple, range)):
indices = np.array(indices, dtype='int64')
else:
raise WrongIndicesError
raise WrongIndicesError(type(indices), caller)

return indices


def digest_atom_indices(atom_indices):
def digest_atom_indices(atom_indices, caller=""):
""" Checks if atom indices are of the expected type and value. """
return digest_indices(atom_indices)
return digest_indices(atom_indices, caller)


def digest_group_indices(group_indices):
def digest_group_indices(group_indices, caller=""):
""" Checks if group indices are of the expected type and value. """
return digest_indices(group_indices)
return digest_indices(group_indices, caller)


def digest_structure_indices(structure_indices):
def digest_structure_indices(structure_indices, caller=""):
""" Checks if structure indices are of the expected type and value. """
return digest_indices(structure_indices)
return digest_indices(structure_indices, caller)


def digest_multiple_structure_indices(structure_indices):
def digest_multiple_structure_indices(structure_indices, caller=""):
""" Checks multiple structure indices. """
if is_list_or_tuple(structure_indices):
return [digest_structure_indices(ii) for ii in structure_indices]
return [digest_structure_indices(ii, caller) for ii in structure_indices]
else:
return digest_structure_indices(structure_indices)
return digest_structure_indices(structure_indices, caller)
8 changes: 6 additions & 2 deletions molsysmt/_private/digestion/item.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from ..exceptions import WrongFormError


def digest_item(item, form):
def digest_item(item, form, caller=""):
""" Check if an item has the expected form.

Examples
Expand All @@ -18,6 +18,10 @@ def digest_item(item, form):
form : str
Name of the form

caller: str, optional
Name of the function or method that is being digested.
For debugging purposes.

Raises
------
WrongFormError
Expand All @@ -29,4 +33,4 @@ def digest_item(item, form):
try:
dict_is_form[form](item)
except KeyError:
raise WrongFormError(form)
raise WrongFormError(form, caller)
Loading