Skip to content

Commit

Permalink
fix(util): cleaner_get
Browse files Browse the repository at this point in the history
Add util function to help with type-checking
  • Loading branch information
ed-p-may committed Jun 15, 2024
1 parent 23e3909 commit dabc0ab
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions honeybee_ph_utils/input_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,35 @@ def clean_get(_list, _i, _default=None):
return _default


def cleaner_get(_list, _i, _default):
# type: (List[T], int, T) -> T
"""Get list item cleanly based on index pos. If IndexError, will try getting _list[0] instead.
This function *requires* a default values to be supplied, and therefor will never return None (unless
the default value is None). This is more type-safe than the 'clean_get' function.
This is useful for gh-components with multiple list inputs which are sometimes
the same length, and sometimes not the same length.
Arguments:
---------
* _list: Any iterable to get the item from.
* _i: The index position to try and get
* _default: A required default value to use if _list[0] fails.
Returns:
--------
* Any
"""
try:
return _list[_i]
except IndexError:
try:
return _list[0]
except IndexError:
return _default


def memoize(func):
"""Simple caching decorator using function arguments as key.
Expand Down

0 comments on commit dabc0ab

Please sign in to comment.