Skip to main content

Simple streams facade

Project description

creek

Simple streams facade.

To install: pip install creek

Documentation here

The Creek base class offers a layer-able wrap of the stream interface.

There are three layering methods -- pre_iter, data_to_obj, and post_iter -- whose use is demonstrated in the iteration code below:

for line in self.pre_iter(self.stream):  # pre_iter: prepare and/or filter the stream
    obj = self.data_to_obj(line)  # data_to_obj: Transforms the data that stream yields
    yield from self.post_iter([obj])  # post_iter: Further process or filter the objects

Examples:

>>> from io import StringIO
>>> src = StringIO(
... '''a, b, c
... 1,2, 3
... 4, 5,6
... '''
... )
>>>
>>> from creek import Creek
>>>
>>> class MyCreek(Creek):
...     def data_to_obj(self, line):
...         return [x.strip() for x in line.strip().split(',')]
...
>>> stream = MyCreek(src)
>>>
>>> list(stream)
[['a', 'b', 'c'], ['1', '2', '3'], ['4', '5', '6']]
>>> stream.seek(0)  # oh!... but we consumed the stream already, so let's go back to the beginning
0
>>> list(stream)
[['a', 'b', 'c'], ['1', '2', '3'], ['4', '5', '6']]
>>> stream.seek(0)  # reverse again
0
>>> next(stream)
['a', 'b', 'c']
>>> next(stream)
['1', '2', '3']

Let's add a filter! There's two kinds you can use. One that is applied to the line before the data is transformed by data_to_obj, and the other that is applied after (to the obj).

>>> from creek import Creek
>>> from io import StringIO
>>>
>>> src = StringIO(
...     '''a, b, c
... 1,2, 3
... 4, 5,6
... ''')
>>> class MyFilteredCreek(MyCreek):
...     def post_iter(self, objs):
...         yield from filter(lambda obj: str.isnumeric(obj[0]), objs)
>>>
>>> s = MyFilteredCreek(src)
>>>
>>> list(s)
[['1', '2', '3'], ['4', '5', '6']]
>>> s.seek(0)
0
>>> list(s)
[['1', '2', '3'], ['4', '5', '6']]
>>> s.seek(0)
0
>>> next(s)
['1', '2', '3']

Recipes:

  • pre_iter: involving itertools.islice to skip header lines
  • pre_iter: involving enumerate to get line indices in stream iterator
  • pre_iter = functools.partial(map, line_pre_proc_func) to preprocess all lines with line_pre_proc_func
  • pre_iter: include filter before obj

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

creek-0.1.34.tar.gz (45.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

creek-0.1.34-py3-none-any.whl (44.2 kB view details)

Uploaded Python 3

File details

Details for the file creek-0.1.34.tar.gz.

File metadata

  • Download URL: creek-0.1.34.tar.gz
  • Upload date:
  • Size: 45.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for creek-0.1.34.tar.gz
Algorithm Hash digest
SHA256 ef311c7f497d0b5f3172632497cf14d8cdddd28b9cf90ed0097fe69df3dd1836
MD5 7869b6bb5a23afb6108e82c1eb427a33
BLAKE2b-256 76d0498e11d7ec4d3a2b1deac828031f69711fc249178b65f093d3f72eb189f5

See more details on using hashes here.

File details

Details for the file creek-0.1.34-py3-none-any.whl.

File metadata

  • Download URL: creek-0.1.34-py3-none-any.whl
  • Upload date:
  • Size: 44.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for creek-0.1.34-py3-none-any.whl
Algorithm Hash digest
SHA256 40987c04be77f0c98ca43fcebbe98fcfdabcdaec6f5b90edc864256f77ded401
MD5 db674fb49b41cc57cd0c370cc29a717d
BLAKE2b-256 a1b91aa3e30cc8a3612c5ef2a6dfcc8aee5210c4b08a2a75a76c6dc234e0c759

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page