Adds json_count_leaves utility function (#23899)
Signed-off-by: aditchawdhary <aditxy@hotmail.com>
This commit is contained in:
+12
-2
@@ -1,6 +1,7 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Helper functions to work with nested JSON structures."""
|
||||
|
||||
from collections.abc import Iterable
|
||||
from functools import reduce
|
||||
from typing import Callable, TypeVar, Union, overload
|
||||
@@ -8,8 +9,12 @@ from typing import Callable, TypeVar, Union, overload
|
||||
_T = TypeVar("_T")
|
||||
_U = TypeVar("_U")
|
||||
|
||||
JSONTree = Union[dict[str, "JSONTree[_T]"], list["JSONTree[_T]"],
|
||||
tuple["JSONTree[_T]", ...], _T]
|
||||
JSONTree = Union[
|
||||
dict[str, "JSONTree[_T]"],
|
||||
list["JSONTree[_T]"],
|
||||
tuple["JSONTree[_T]", ...],
|
||||
_T,
|
||||
]
|
||||
"""A nested JSON structure where the leaves need not be JSON-serializable."""
|
||||
|
||||
|
||||
@@ -78,3 +83,8 @@ def json_reduce_leaves(
|
||||
json_iter_leaves(value),
|
||||
initial,
|
||||
)
|
||||
|
||||
|
||||
def json_count_leaves(value: JSONTree[_T]) -> int:
|
||||
"""Count the number of leaves in a nested JSON structure."""
|
||||
return sum(1 for _ in json_iter_leaves(value))
|
||||
|
||||
Reference in New Issue
Block a user