-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d5123e9
commit aa5ed9f
Showing
5 changed files
with
85 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import typing as t | ||
|
||
ElementType = t.TypeVar("ElementType") | ||
|
||
|
||
def flatten( | ||
items: t.Sequence[t.Union[ElementType, t.List[ElementType]]] | ||
) -> t.List[ElementType]: | ||
""" | ||
Takes a sequence of elements, and flattens it out. For example:: | ||
>>> flatten(['a', ['b', 'c']]) | ||
['a', 'b', 'c'] | ||
We need this for situations like this:: | ||
await Band.select(Band.name, Band.manager.all_columns()) | ||
""" | ||
_items: t.List[ElementType] = [] | ||
for item in items: | ||
if isinstance(item, list): | ||
_items.extend(item) | ||
else: | ||
_items.append(item) | ||
|
||
return _items |
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,8 @@ | ||
from unittest import TestCase | ||
|
||
from piccolo.utils.list import flatten | ||
|
||
|
||
class TestFlatten(TestCase): | ||
def test_flatten(self): | ||
self.assertListEqual(flatten(["a", ["b", "c"]]), ["a", "b", "c"]) |