Given one or more keyword arguments which may be either a single value or a list of values, return a list of keyword dictionaries by broadcasting the single valuesacross all the dicts. If more than one item in the input is a list, all lists must be the same length. Parameters -
(**kwargs: dict)
| 44 | |
| 45 | |
| 46 | def broadcast_args_to_dicts(**kwargs: dict) -> List[dict]: |
| 47 | """ |
| 48 | Given one or more keyword arguments which may be either a single value or a list of values, |
| 49 | return a list of keyword dictionaries by broadcasting the single valuesacross all the dicts. |
| 50 | If more than one item in the input is a list, all lists must be the same length. |
| 51 | |
| 52 | Parameters |
| 53 | ---------- |
| 54 | **kwargs: dict |
| 55 | The keyword arguments |
| 56 | |
| 57 | Returns |
| 58 | ------- |
| 59 | list of dicts |
| 60 | A list of dictionaries |
| 61 | |
| 62 | Raises |
| 63 | ------ |
| 64 | ValueError |
| 65 | If any of the input lists are not the same length |
| 66 | """ |
| 67 | # Check that all list arguments have the same length, |
| 68 | # and find out what that length is |
| 69 | # If there are no list arguments, length is 1 |
| 70 | list_lengths = [len(v) for v in tuple(kwargs.values()) if isinstance(v, list)] |
| 71 | if list_lengths and len(set(list_lengths)) > 1: |
| 72 | raise ValueError("All list arguments must have the same length.") |
| 73 | list_length = list_lengths[0] if list_lengths else 1 |
| 74 | |
| 75 | # Expand all arguments to lists of the same length |
| 76 | expanded_kwargs = { |
| 77 | k: [v] * list_length if not isinstance(v, list) else v |
| 78 | for k, v in kwargs.items() |
| 79 | } |
| 80 | # Reshape into a list of dictionaries |
| 81 | # Each dictionary represents the keyword arguments for a single function call |
| 82 | list_of_kwargs = [ |
| 83 | {k: v[i] for k, v in expanded_kwargs.items()} for i in range(list_length) |
| 84 | ] |
| 85 | |
| 86 | return list_of_kwargs |
| 87 | |
| 88 | |
| 89 | def plotly_cdn_url(cdn_ver=get_plotlyjs_version()): |
no test coverage detected