MCPcopy
hub / github.com/microsoft/agent-lightning / random_dict

Function random_dict

tests/benchmark/utils.py:57–107  ·  view source on GitHub ↗

Generate a nested dictionary with configurable depth, breadth, and value sizes. Integer or (low, high) tuples are supported for all structural parameters. Args: depth: Number of nested levels or a tuple specifying a range. breadth: Number of keys per level or a tupl

(
    *,
    depth: Union[int, Tuple[int, int]],
    breadth: Union[int, Tuple[int, int]],
    key_length: Union[int, Tuple[int, int]],
    value_length: Union[int, Tuple[int, int]],
    value_factory: Optional[Callable[[int], Any]] = None,
)

Source from the content-addressed store, hash-verified

55
56
57def random_dict(
58 *,
59 depth: Union[int, Tuple[int, int]],
60 breadth: Union[int, Tuple[int, int]],
61 key_length: Union[int, Tuple[int, int]],
62 value_length: Union[int, Tuple[int, int]],
63 value_factory: Optional[Callable[[int], Any]] = None,
64) -> Dict[str, Any]:
65 """
66 Generate a nested dictionary with configurable depth, breadth, and
67 value sizes. Integer or (low, high) tuples are supported for
68 all structural parameters.
69
70 Args:
71 depth: Number of nested levels or a tuple specifying a range.
72 breadth: Number of keys per level or a tuple range.
73 key_length: Length of each key or a tuple range.
74 value_length: Length of each value or a tuple range.
75 value_factory: Function mapping `value_length` → value.
76
77 Returns:
78 A nested dictionary of arbitrary size.
79 """
80 # Default factory
81 if value_factory is None:
82 value_factory = random_string
83
84 def build(level: int) -> Dict[str, Any]:
85 # For each level, breadth/key/value lengths may vary, so draw fresh each time
86 current_breadth = _resolve_param(breadth, "breadth")
87
88 if current_breadth < 0:
89 raise ValueError("Breadth cannot be negative.")
90
91 target_depth = depth if isinstance(depth, int) else _resolve_param((level, depth[1]), "depth")
92
93 if level == target_depth:
94 # leaf nodes
95 return {
96 random_string(_resolve_param(key_length, "key_length")): value_factory(
97 _resolve_param(value_length, "value_length")
98 )
99 for _ in range(current_breadth)
100 }
101
102 # nested nodes
103 return {
104 random_string(_resolve_param(key_length, "key_length")): build(level + 1) for _ in range(current_breadth)
105 }
106
107 return build(1)
108
109
110def flatten_dict(d: Dict[str, Any], prefix: str = "") -> Dict[str, Any]:

Callers 3

utils.pyFile · 0.85
generate_attributesFunction · 0.85
_make_spanFunction · 0.85

Calls 1

buildFunction · 0.85

Tested by

no test coverage detected