Skip to content

Commit

Permalink
Fixed material find to actually work.
Browse files Browse the repository at this point in the history
  • Loading branch information
MicahGale committed Nov 27, 2024
1 parent 5c00c23 commit 6135576
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
27 changes: 19 additions & 8 deletions montepy/data_inputs/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ def __prep_element_filter(self, filter_obj):
For use by find
"""
if isinstance(filter_obj, "str"):
if isinstance(filter_obj, str):
filter_obj = Element.get_by_symbol(filter_obj).Z
if isinstance(filter_obj, Element):
filter_obj = filter_obj.Z
Expand All @@ -906,9 +906,11 @@ def __prep_filter(self, filter_obj, attr=None):
"""
Makes a filter function wrapper
"""
if filter_obj is None:
return lambda _: True

if callable(filter_obj):
return filter_obj

elif isinstance(filter_obj, slice):

def slicer(val):
Expand All @@ -931,6 +933,8 @@ def slicer(val):
return slicer

else:
if attr:
return lambda val: getattr(val, attr) == filter_obj
return lambda val: val == filter_obj

def find(
Expand Down Expand Up @@ -997,30 +1001,37 @@ def find(
:rtype: Generator[tuple[int, tuple[Nuclide, float]]]
"""
# nuclide type enforcement handled by `Nuclide`
if not isinstance(element, (Element, str, int, slice)):
if not isinstance(element, (Element, str, int, slice, type(None))):
raise TypeError(
f"Element must be only Element, str, int or slice types. {element} of type{type(element)} given."
)
if not isinstance(A, (int, slice)):
if not isinstance(A, (int, slice, type(None))):
raise TypeError(
f"A must be an int or a slice. {A} of type {type(A)} given."
)
if not isinstance(meta_isomer, (int, slice)):
if not isinstance(meta_state, (int, slice, type(None))):
raise TypeError(
f"meta_state must an int or a slice. {meta_state} of type {type(meta_state)} given."
)
if not isinstance(library, (str, slice)):
if not isinstance(library, (str, slice, type(None))):
raise TypeError(
f"library must a str or a slice. {library} of type {type(library)} given."
)
if name:
fancy_nuclide = Nuclide(name)
if fancy_nuclide.A == 0:
element = fancy_nuclide.element
fancy_nuclide = None
else:
fancy_nuclide = None
filters = [
self.__prep_filter(Nuclide(name)),
self.__prep_filter(fancy_nuclide),
self.__prep_element_filter(element),
self.__prep_filter(A, "A"),
self.__prep_filter(meta_state, "meta_state"),
self.__prep_filter(library, "library"),
]
for idx, component in enumerate(self._components):
for idx, component in enumerate(self):
for filt in filters:
found = filt(component[0])
if not found:
Expand Down
6 changes: 4 additions & 2 deletions montepy/data_inputs/nuclide.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ def __init__(
self._library = Library("")
ZAID = ""

if not isinstance(name, (str, int, Element, Nucleus, Nuclide)):
if not isinstance(name, (str, int, Element, Nucleus, Nuclide, type(None))):
raise TypeError(
f"Name must be str, int, Element, or Nucleus. {name} of type {type(name)} given."
)
Expand Down Expand Up @@ -770,7 +770,9 @@ def __str__(self):

def __eq__(self, other):
if not isinstance(other, type(self)):
raise TypeError("")
raise TypeError(
f"Cannot compare Nuclide to other values. {other} of type {type(other)}."
)
return self.nucleus == other.nucleus and self.library == other.library

def __lt__(self, other):
Expand Down

0 comments on commit 6135576

Please sign in to comment.