Files
HowToSD-ComfyUI-Data-Analysis/modules/cda/utils.py
T

31 lines
1008 B
Python

from typing import Dict, Any
def python_dict_first_value_len(d: Dict[Any, Any]) -> int:
r"""
Computes the length of the first value in a dictionary.
- If the first value is a `list`, returns its length.
- If the first value is a `dict`, returns the number of keys in the dictionary.
- Otherwise, returns `1` for scalar values.
Args:
d (Dict[Any, Any]): A dictionary where the first value's length will be computed.
Returns:
int: The computed length based on the type of the first value.
Raises:
ValueError: If `d` is `None` or empty.
"""
if not d:
raise ValueError("Input dictionary is None or empty.")
first_value = next(iter(d.values())) # Get the first value
if isinstance(first_value, list):
return len(first_value)
elif isinstance(first_value, dict):
return len(first_value.keys()) # Return the number of keys in the dictionary
else:
return 1 # Scalar case (int, float, str, etc.)