Source code for honeybee_ph_utils.input_tools
# -*- coding: utf-8 -*-
# -*- Python Version: 2.7 -*-
"""Utility functions for cleaning and handling user-inputs"""
try:
from typing import Any, List, Optional, TypeVar
T = TypeVar("T")
except ImportError:
pass # Python 3
try:
from scriptcontext import sticky # type: ignore
except ImportError:
pass # Not in Rhino Grasshopper
import re
[docs]
def clean_tree_get(_tree, _i, _default=None):
try:
return _tree.Branch(_i)
except:
try:
return _tree.Branch(0)
except:
return _default
[docs]
def clean_get(_list, _i, _default=None):
# type: (List[T], int, Optional[T]) -> Optional[T]
"""Get list item cleanly based on index pos. If IndexError, will try getting _list[0] instead.
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: The optional default value to use if _list[0] fails.
Returns:
--------
* Any
"""
try:
return _list[_i]
except IndexError:
try:
return _list[0]
except IndexError:
return _default
[docs]
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
[docs]
def memoize(func):
"""Simple caching decorator using function arguments as key.
Uses the Grasshopper 'sticky' dict to store values.
https://book.pythontips.com/en/latest/function_caching.html
Usage:
>>> @memoize
>>> def fibonacci(n):
>>> if n < 2: return n
>>> return fibonacci(n - 1) + fibonacci(n - 2)
>>>
>>> fibonacci(25)
"""
def wrapper(*args, **kwargs):
try:
return sticky[args]
except KeyError:
rv = func(*args, **kwargs)
sticky[args] = rv
return rv
return wrapper