-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mod: Refactor collections functions, update ffmpeg_path and ytdl_path…
… arguments for AC.
- Loading branch information
Showing
7 changed files
with
99 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
from typing import Dict, List, TypeVar | ||
|
||
|
||
T = TypeVar("T") | ||
|
||
|
||
def list_dict_to_dict_list( | ||
lst: List[Dict[str, T]], | ||
key_mode: str = "intersect", | ||
) -> Dict[str, List[T]]: | ||
"""Convert list of dicts to dict of lists. | ||
:param lst: The list of dict to merge. | ||
:param key_mode: Can be "same" or "intersect". | ||
If "same", all the dictionaries must contains the same keys otherwise a ValueError will be raised. | ||
If "intersect", only the intersection of all keys will be used in output. | ||
:returns: The dictionary of lists. | ||
""" | ||
if len(lst) == 0: | ||
return {} | ||
keys = set(lst[0].keys()) | ||
if key_mode == "same": | ||
if not all(keys == set(item.keys()) for item in lst[1:]): | ||
raise ValueError("Invalid keys for batch.") | ||
elif key_mode == "intersect": | ||
keys = intersect_lists([list(item.keys()) for item in lst]) | ||
else: | ||
KEY_MODES = ("same", "intersect") | ||
raise ValueError( | ||
f"Invalid argument key_mode={key_mode}. (expected one of {KEY_MODES})" | ||
) | ||
|
||
return {key: [item[key] for item in lst] for key in keys} | ||
|
||
|
||
def intersect_lists(lst_of_lst: List[List[T]]) -> List[T]: | ||
"""Performs intersection of elements in lists (like set intersection), but keep their original order.""" | ||
if len(lst_of_lst) <= 0: | ||
return [] | ||
out = list(dict.fromkeys(lst_of_lst[0])) | ||
for lst_i in lst_of_lst[1:]: | ||
out = [name for name in out if name in lst_i] | ||
if len(out) == 0: | ||
break | ||
return out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
import unittest | ||
|
||
from unittest import TestCase | ||
|
||
from aac_datasets.utils.collections import intersect_lists | ||
|
||
|
||
class TestCollections(TestCase): | ||
def test_intersect_lists(self) -> None: | ||
input_ = [["a", "b", "b", "c"], ["c", "d", "b", "a"], ["b", "a", "a", "e"]] | ||
expected = ["a", "b"] | ||
|
||
output = intersect_lists(input_) | ||
self.assertListEqual(output, expected) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |