Skip to content

Repository files navigation

Typed Stream

License Python Implementation Total lines Code size Style: Black Imports: isort Coverage

Downloads Downloads Downloads

The Stream class is an iterable with utility methods for transforming it.

This library heavily uses itertools for great performance and simple code.

Examples

>>> import typed_stream
>>> # Get sum of 10 squares
>>> typed_stream.Stream.range(stop=10).map(lambda x: x * x).sum()
285
>>> # same as above
>>> sum(typed_stream.Stream.counting().limit(10).map(pow, 2))
285
>>> # sum first 100 odd numbers
>>> typed_stream.Stream.counting(start=1, step=2).limit(100).sum()
10000
>>> (typed_stream.Stream.counting()
...     .filter(typed_stream.functions.is_odd).limit(100).sum())
10000
>>> (typed_stream.Stream.counting()
...     .exclude(typed_stream.functions.is_even).limit(100).sum())
10000
>>> import typed_stream.functions
>>> # Get the longest package name from requirements-dev.txt
>>> (typed_stream.FileStream("requirements-dev.txt")
...     .filter()
...     .exclude(typed_stream.functions.method_partial(str.startswith, "#"))
...     .exclude(typed_stream.functions.method_partial(str.__contains__, "git+"))
...     .map(str.split, "==")
...     .starmap(lambda name, version = None: name)
...     .max(key=len))
'flake8-no-implicit-concat'

In examples are more complex examples using Streams.

CLI

This library also provides a typed_stream command. For good performance it's recommended to use pypy. Never call the binary with user-provided arguments. The programm tries to avoid arbitrary code execution, but the statements get evaluated using python, so no guarantees there.

Examples

$ printf '1\n2\n3' | typed_stream map int map mul 3
3
6
9
$ printf '1\n2\n3\n' | typed_stream map int sum
6
$ printf '1\n2\n3\n1' | typed_stream map int collect builtins.sum
7
$ printf '1\n2\n3\n1' | typed_stream map int collect Counter
{"1": 2, "2": 1, "3": 1}

Not yet asked questions

I'll try to answer questions that could occur.

What do the versions mean?

I try to follow something similar to sem-ver. I try to avoid big breaking changes that break my own code, so even if the major version has been changed, it should probably be safe to upgrade.

Why are there no changelogs?

I'm too lazy. I don't know of anybody using this (except me) and I don't need to write changelogs for myself. If you need changelogs, please create an issue.

Why EUPL-1.2-or-later?

About

Typed Stream class for Python

Topics

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages