Skip to content

params

Functions to parse parameter values.

list_to_str(mapping)

Convert any lists in a dictionary to a string with comma-separated elements.

Parameters:

Name Type Description Default
mapping ParamsMapping

Dictionary with lists

required

Returns:

Type Description
ParamsMappingValues

Dictionary with lists converted to comma-separated strings

Source code in bavapi/parsing/params.py
def list_to_str(
    mapping: MutableParamsMapping[SequenceOrValues[Union[T, str]]]
) -> MutableParamsMapping[Union[T, str]]:
    """Convert any lists in a dictionary to a string with comma-separated elements.

    Parameters
    ----------
    mapping : ParamsMapping
        Dictionary with lists

    Returns
    -------
    ParamsMappingValues
        Dictionary with lists converted to comma-separated strings
    """
    for key, value in mapping.items():
        if not isinstance(value, str) and isinstance(value, Sequence):
            mapping[key] = ",".join(str(i) for i in value)

    return cast(MutableParamsMapping[Union[T, str]], mapping)

parse_date(value)

Parse date string or datetime value into a date string.

Parameters:

Name Type Description Default
value (str, datetime, date)

Input to parse into a date string.

required

Returns:

Type Description
str

The parsed date as a string.

Source code in bavapi/parsing/params.py
def parse_date(value: Union[str, dt.datetime, dt.date]) -> str:
    """Parse date string or datetime value into a date string.

    Parameters
    ----------
    value : str, dt.datetime, dt.date
        Input to parse into a date string.

    Returns
    -------
    str
        The parsed date as a string.
    """
    fmt_out = "%Y-%m-%d %H:%M:%S"
    if isinstance(value, dt.datetime):
        return value.strftime(fmt_out)
    if isinstance(value, dt.date):
        return dt.datetime.combine(value, dt.datetime.min.time()).strftime(fmt_out)
    try:
        return dt.datetime.strptime(value, fmt_out).strftime(fmt_out)
    except ValueError:
        return dt.datetime.fromisoformat(value).strftime(fmt_out)

to_fount_params(data, param)

Constructs dictionary keys for special Fount API formatting.

The resulting dictionary keys will be formatted to include param as the main parameter name:

to_fount_params({"a":1}, "test")

Parameters:

Name Type Description Default
data dict[str, Any]

Dictionary to format.

required
param str

Parameter name.

required

Returns:

Type Description
dict[str, Any]

Fount API parameter dictionary.

Source code in bavapi/parsing/params.py
def to_fount_params(data: Mapping[str, T], param: str) -> Dict[str, T]:
    """Constructs dictionary keys for special Fount API formatting.

    The resulting dictionary keys will be formatted to include `param` as the
    main parameter name:

    >>> to_fount_params({"a":1}, "test")
    {"test[a]":1}

    Parameters
    ----------
    data : dict[str, Any]
        Dictionary to format.
    param : str
        Parameter name.

    Returns
    -------
    dict[str, Any]
        Fount API parameter dictionary.
    """
    return {f"{param}[{k}]": v for k, v in data.items()}