- splatnet3_scraper.query.QueryResponse.match_partial_path(partial_path, *args)
Returns a list of all paths in the given data that match the given partial path. For example, if partial_path is
(0, "key1"), this will return all paths in the data that match...[0]["key1"]. If fed a list of partial paths, this will return all paths that match any of the partial paths. Do not confuse tuples with lists, as they are treated differently.The “:” string can be added to the partial path input to match all list indices that match it. This is useful for JSONs with a repeating structure.
The match_partial_path algorithm searches for all paths in a dictionary that match the given partial path. The
partial_pathcan be a string, integer, a special “:” string, or a tuple of strings/integers that represents the path to an item in the dictionary. For example, the path("key1", "key2", 2)corresponds to...["key1"]["key2"][2]in the dictionary. The algorithm returns a list of all paths that match thepartial_path. Each path in the result is represented as a tuple of strings and integers where strings correspond to dictionary keys and integers correspond to list indices.For instance, if
datais:>>> data = QueryResponse({ ... "key1": { ... "key2": [ ... {"key3": 1}, ... {"key3": 2}, ... ] ... }, ... "key4": { ... "key5": [ ... {"key3": 3}, ... {"key3": 4}, ... ] ... } ... })Then
data.match_partial_path((0, "key3"))will return:>>> [ ... ("key1", "key2", 0, "key3"), ... ("key4", "key5", 0, "key3"), ... ]If
data.match_partial_path("key3")is called, the result will be:>>> [ ... ("key1", "key2", 0, "key3"), ... ("key1", "key2", 1, "key3"), ... ("key4", "key5", 0, "key3"), ... ("key4", "key5", 1, "key3"), ... ]If
data.match_partial_path([(0, "key3"), "key2"])is called, the result will be:>>> [ ... ("key1", "key2", 0, "key3"), ... ("key4", "key5", 0, "key3"), ... ("key1", "key2"), ... ]If
data.match_partial_path(("key1", "key2", ":", "key3"))is called, the result will be:>>> [ ... ("key1", "key2", 0, "key3"), ... ("key1", "key2", 1, "key3"), ... ]- Parameters:¶
- partial_path : PathType | list[PathType]¶
The partial path to match. If the partial path is a tuple, this method will treat it as a path. For example, a
partial_pathargument of(0, "key1")will be treated as...[0]["key1"]. If the partial path is a string, this method will treat it as a key in a dictionary. Integers will be treated as indices in a list.- *args : str | int
If *args is not empty, treat each arg as part of the partial path. For example, if
data.match_partial_path(0, "key1")is called, the result is equivalent todata.match_partial_path((0, "key1")). Note that if *args is not empty, thepartial_pathargument must be a string or integer, not a tuple. Use thepartial_pathargument with a list of paths instead of passing a PathType in *args.
- Raises:¶
TypeError – If *args is not empty and the
partial_pathargument is a tuple. Use a list of paths instead of passing a PathType in *args.- Returns:¶
list[tuple[str | int, …]] – A list of all paths that match the given partial path. Each path is represented as a tuple of strings and integers, where strings correspond to dictionary keys and integers correspond to list indices.