Skip to content

utils

Utils init file

filter_json(json, filters) #

Extension Method to Dictionaries, allows easy filtering

Parameters:

Name Type Description Default
json dict
required
filters Union[str, List[str]]
required

Returns:

Name Type Description
filtered_object object

Resulting JSON Filtered Object

Source code in camply/utils/api_utils.py
def filter_json(json: dict, filters: Union[str, List[str]]) -> object:
    """
    Extension Method to Dictionaries, allows easy filtering

    Parameters
    ----------
    json
    filters

    Returns
    -------
    filtered_object: object
        Resulting JSON Filtered Object
    """
    if not isinstance(filters, list):
        filters = [filters]
    object_layers = {0: json}
    try:
        for index, filter_layer in enumerate(filters):
            layer_index = index + 1
            object_layers[layer_index] = object_layers[index][filter_layer]
            del object_layers[index]
    except KeyError as key_error:
        error_message = (
            f"Unable to find matching JSON Filtering | {key_error} | {filters}"
        )
        logger.error(error_message)
        raise KeyError from key_error
    return object_layers[len(filters)]

generate_url(scheme, netloc, path='', params='', query='', fragment='') #

Build a URL

Parameters:

Name Type Description Default
scheme str

URL scheme specifier

required
netloc str

Network location part

required
path str

Hierarchical path

''
params str

Parameters for last path element

''
query str

Query component

''
fragment str

Fragment identifier

''

Returns:

Name Type Description
url str

Compiled URL

Source code in camply/utils/api_utils.py
def generate_url(
    scheme: str,
    netloc: str,
    path: str = "",
    params: str = "",
    query: str = "",
    fragment: str = "",
):
    """
    Build a URL

    Parameters
    ----------
    scheme: str
        URL scheme specifier
    netloc: str
        Network location part
    path: str
        Hierarchical path
    params: str
        Parameters for last path element
    query: str
        Query component
    fragment: str
        Fragment identifier

    Returns
    -------
    url: str
        Compiled URL
    """
    url_components = {
        "scheme": scheme,
        "netloc": netloc,
        "path": path,
        "params": params,
        "query": query,
        "fragment": fragment,
    }
    return parse.urlunparse(components=tuple(url_components.values()))

log_camply(self, message, *args, **kwargs) #

Custom Logging Notification Level for Pushover Logging

Between logging.ERROR and logging.CRITICAL (45)

Parameters:

Name Type Description Default
self Logger
required
message str

Message String

required
args
()
kwargs
{}

Returns:

Type Description
None
Source code in camply/utils/logging_utils.py
def log_camply(self: logging.Logger, message: str, *args, **kwargs) -> None:
    """
    Custom Logging Notification Level for Pushover Logging

    Between logging.ERROR and logging.CRITICAL (45)

    Parameters
    ----------
    self: logging.Logger
    message: str
        Message String
    args
    kwargs

    Returns
    -------
    None
    """
    notification_level = logging.INFO + 1
    logging.addLevelName(level=notification_level, levelName="CAMPLY")
    if self.isEnabledFor(level=notification_level):
        self._log(level=notification_level, msg=message, args=args, **kwargs)

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]