Skip to content

Commit

Permalink
♻️ Move check_for_nullables to AutoObject
Browse files Browse the repository at this point in the history
  • Loading branch information
ollyhensby committed Feb 9, 2024
1 parent 753095a commit 7f1546c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
15 changes: 1 addition & 14 deletions src/ipyautoui/autoform.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,10 @@ def bn_shownull(self, value):

def show_hide_bn_nullable(self):
"""Set display of show null button based on if any nullable widgets are found."""
if self.check_for_nullables():
if self.check_for_nullables(): # check_for_nullables is defined in AutoObject
self.display_bn_shownull = True
else:
self.display_bn_shownull = False

def check_for_nullables(self) -> bool:
"""Search through widgets and as soon as a Nullable widget is found, return True.
Else, return False."""
if not hasattr(self, 'di_widgets'):
raise AttributeError(
"The 'di_widgets' attribute is not defined in this class. "
"It must be defined in a subclass before calling this method."
)
for v in self.di_widgets.values():
if isinstance(v, Nullable):
return True
return False


if __name__ == "__main__":
Expand Down
8 changes: 8 additions & 0 deletions src/ipyautoui/autoobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,14 @@ def di_widgets_value(self): # used to set _value
get_value = lambda v: v._value if "_value" in v.traits() else v.value
return {k: get_value(v) for k, v in self.di_widgets.items()}

def check_for_nullables(self) -> bool:
"""Search through widgets and as soon as a Nullable widget is found, return True.
Else, return False."""
for v in self.di_widgets.values():
if isinstance(v, Nullable):
return True
return False


class AutoObjectForm(AutoObject, AutoObjectFormLayout):
def __init__(self, **kwargs):
Expand Down

0 comments on commit 7f1546c

Please sign in to comment.