viralogic / py-enumerable

A Python module used for interacting with collections of objects using LINQ syntax
MIT License
187 stars 24 forks source link

Enumerable.chunk #81

Open FracturedCode opened 1 year ago

FracturedCode commented 1 year ago

Hi, I wanted to contribute to this project, but the setup was a bit of a barrier. I followed the instructions in the README, and even set it up in a clean environment (see, container) to try to get it working. There's a few issues I've encountered trying to develop, and I just don't have the willpower to spend 5 hours to figure them out. If you can give me a working dev container or Dockerfile, I will gladly do things myself.

Anyway, with that preamble out of the way, I suppose I have a feature request and a half-baked implementation for the chunk function Chunk.cs. ChunkTests.cs

Here is what I came up with:

def chunk(self, n) -> TEnumerable:
    """
    Split elements into Enumerable chunks of no greater than n
    :param n: Maximum chunk size
    :return: new Enumerable object
    """
    it = iter(self)
    items = [item for item in itertools.takewhile(lambda x: x, (Enumerable(data=itertools.islice(it, n)) for _ in itertools.count()))]
    return Enumerable(items)

Criticism welcome.

I also attempted to add this test. Clearly, I do not understand the framework going on here. It kept throwing this error that does not make sense to me.

# Attempt 1
(Enumerable(_simple).chunk(2), [[1, 2], [3]])
# Attempt 2
(Enumerable(_simple).chunk(2), [Enumerable([1, 2]), Enumerable([3])])

Here's partial output from the debug console:

def test_non_executors(enumerable: Enumerable, expected: List) -> None:
>       assert enumerable.to_list() == expected
E       assert [[1, 2], [3]] == [[1, 2], [3]]
E         At index 0 diff: [1, 2] != [1, 2]
E         Full diff:
E           [[1, 2], [3]]

tests/test_functions.py:593: AssertionError
--------------- generated xml file: /tmp/tmp-290lAdGLKlVoyy8.xml ---------------
=========================== short test summary info ============================
FAILED tests/test_functions.py::test_non_executors[enumerable18-expected18]
============================== 1 failed in 0.48s ===============================