Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup duplication of flatten of sequences #1395

Open
havogt opened this issue Dec 12, 2023 · 1 comment
Open

Cleanup duplication of flatten of sequences #1395

havogt opened this issue Dec 12, 2023 · 1 comment
Labels
triage: enhancement New feature or request

Comments

@havogt
Copy link
Contributor

havogt commented Dec 12, 2023

We have many variations of flatten implementations in our code base, we should provide a single (customizable) one for all use-cases.

@DropD
Copy link
Contributor

DropD commented Mar 8, 2024

This is the result of research in #1489 (with reference impl there):

There already are implementations for flatten in eve.utils. They are very general and treat any iterable as something to flatten. One frequent usage that seems to come up is flattening iterables that may contain TupleType (which may contain itself).

If we simply make TupleType iterable like so:

class TupleType(...):
    ...
    def __iter__(self) -> Iterator[DataType]:
        yield from self.types

We can use eve.utils.flatten_iter to achieve most of the desired effects:

nested_types = [
    (type1, type2),
    type3,
    TupleType(types=[type4, type5, TupleType(...)]),
    ...
]

flattened_tuple = tuple(eve.utils.flatten_iter(nested_types))
flattened_list = list(eve_utils.flatten_iter(nested_types))
filtering_comprehension = [t for t in eve_utils.flatten_iter(nested_types) if t ... ]

The same may be possible in other use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants