Skip to content

general_utils

Camply General Utilities

handle_search_windows(start_date, end_date) #

Handle Multiple Search Windows by the CLI

Source code in camply/utils/general_utils.py
def handle_search_windows(
    start_date: Union[Iterable[str], str, Iterable[datetime.date], datetime.date],
    end_date: Union[Iterable[str], str, Iterable[datetime.date], datetime.date],
) -> Union[List[SearchWindow], SearchWindow]:
    """
    Handle Multiple Search Windows by the CLI
    """
    if isinstance(start_date, (str, date)):
        start_date = (start_date,)
        assert isinstance(end_date, (str, date))
        end_date = (end_date,)
    search_windows: List[SearchWindow] = []
    for field in [start_date, end_date]:
        if field is None or (isinstance(field, (tuple, list)) and len(field) == 0):
            logger.error("Campsite searches require a `start_date` and an `end_date`")
            sys.exit(1)
    if len(start_date) != len(end_date):
        logger.error(
            "When searching multiple date windows, you must provide the same amount "
            "of `--start-dates` as `--end-dates`"
        )
        sys.exit(1)
    for index, date_str in enumerate(start_date):
        search_windows.append(
            SearchWindow(start_date=date_str, end_date=end_date[index])
        )
    if len(search_windows) == 1:
        return search_windows[0]
    else:
        return search_windows

is_list_like(obj) #

Define if an object is list-like

Source code in camply/utils/general_utils.py
def is_list_like(obj: Any) -> bool:
    """
    Define if an object is list-like
    """
    return isinstance(obj, (list, set, tuple))

make_list(obj, coerce=None) #

Make Anything An Iterable Instance

Parameters:

Name Type Description Default
obj
required
coerce Optional[Callable]
None

Returns:

Type Description
List[object]
Source code in camply/utils/general_utils.py
def make_list(obj, coerce: Optional[Callable] = None) -> Optional[List[Any]]:
    """
    Make Anything An Iterable Instance

    Parameters
    ----------
    obj: object
    coerce: Callable

    Returns
    -------
    List[object]
    """
    if obj is None:
        return None
    elif isinstance(obj, CamplyModel):
        return [coerce(obj) if coerce is not None else obj]
    elif is_list_like(obj) is True:
        if coerce is not None:
            return [coerce(item) for item in obj]
        else:
            return list(obj)
    else:
        return [coerce(obj) if coerce is not None else obj]