From 0d0e2140cf747c627a3c45abd0257ac77b3b48e1 Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum Date: Tue, 16 Apr 2024 22:35:56 -0700 Subject: [PATCH] Add a flatten function to Utils.py --- Utils.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Utils.py b/Utils.py index 6d4462651c89..2cb046b36014 100644 --- a/Utils.py +++ b/Utils.py @@ -2,6 +2,7 @@ import asyncio import json +import types import typing import builtins import os @@ -18,7 +19,7 @@ from argparse import Namespace from settings import Settings, get_settings -from typing import BinaryIO, Coroutine, Optional, Set, Dict, Any, Union +from typing import BinaryIO, Coroutine, Generator, List, Optional, Set, TypeVar, Dict, Any, Union from typing_extensions import TypeGuard from yaml import load, load_all, dump @@ -775,6 +776,14 @@ def async_start(co: Coroutine[None, None, typing.Any], name: Optional[str] = Non task.add_done_callback(_faf_tasks.discard) +T = TypeVar("T") +def flatten(l: List[T] | Generator[T] | T) -> List[T]: + if type(l) is list or type(l) is types.GeneratorType: + return [ y for x in l for y in flatten(x) ] + else: + return [ l ] + + def deprecate(message: str): if __debug__: raise Exception(message)