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

Add a toplevel fileinput method #57

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,39 @@ other threads to supply and consume data.



.. _fileinput:


|source| ``fileinput(files=None, inplace=False, backup="", mode="r", openhook=None, encoding=None, errors=None) -> Iter``
*************************************************************************************************************************


A wrapper around fileinput.input that returns an Excitertools Iter_ instance.

The documentation for the stdlib fileinput module is here:
https://docs.python.org/3/library/fileinput.html

Here is an example of use:

.. code-block:: python

>>> from excitertools import fileinput
>>> # Read from a file
>>> fileinput(['data.txt']).take(3).collect()
['1', '2', '3']
>>> # Read from stdin OR files listed on the command line (sys.argv[1:])
>>> fileinput().take(3).collect()
['1', '2', '3']

:param files: A list of filenames or '-' for stdin (default: sys.argv[1:]).
:param inplace: Whether to allow in-place editing (default: False).
:param backup: Backup extension for in-place editing (default: "").
:param mode: File mode, e.g., 'r' or 'rb' (default: 'r').
:param openhook: Optional hook to customize file opening.
:param encoding: File encoding (default: None).
:param errors: Error handling mode (default: None).



The ``Iter`` Class
##################
Expand Down
62 changes: 62 additions & 0 deletions excitertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
import collections.abc
import queue
import re
import fileinput as _fileinput

import more_itertools

Expand All @@ -226,6 +227,7 @@
"chain_from_iterable",
"compress",
"dropwhile",
"fileinput",
"filterfalse",
"groupby",
"islice",
Expand Down Expand Up @@ -907,6 +909,66 @@ def shutdown():
return Iter.from_queue(q, timeout=timeout, sentinel=sentinel)


def fileinput(
files=None,
inplace=False,
backup="",
mode="r",
openhook=None,
encoding=None,
errors=None
) -> Iter:
"""
|source|
A wrapper around fileinput.input that returns an Excitertools Iter_ instance.

The documentation for the stdlib fileinput module is here:
https://docs.python.org/3/library/fileinput.html

Note that the ``encoding`` and ``errors`` arguments are only available
in Python 3.10 and later.

Here is an example of use:

.. code-block:: python

>>> from excitertools import fileinput
>>> # Read from a file
>>> fileinput(['data.txt']).take(3).collect() # doctest: +SKIP
['1', '2', '3']
>>> # Read from stdin OR files listed on the command line (sys.argv[1:])
>>> fileinput().take(3).collect() # doctest: +SKIP
['1', '2', '3']

:param files: A list of filenames or '-' for stdin (default: sys.argv[1:]).
:param inplace: Whether to allow in-place editing (default: False).
:param backup: Backup extension for in-place editing (default: "").
:param mode: File mode, e.g., 'r' or 'rb' (default: 'r').
:param openhook: Optional hook to customize file opening.
:param encoding: File encoding (default: None). Note: Python 3.10+ only.
:param errors: Error handling mode (default: None). Note: Python 3.10+ only.
"""

def yielder():
if sys.version_info < (3, 10): # pragma: no cover
extra_kwargs = dict()
else:
extra_kwargs = dict(encoding=encoding, errors=errors)

stream = _fileinput.input(
files=files,
inplace=inplace,
backup=backup,
mode=mode,
openhook=openhook,
**extra_kwargs
)
with stream:
yield from stream

return Iter(yielder())


"""

The ``Iter`` Class
Expand Down
3 changes: 3 additions & 0 deletions tests/fileinput_data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
one
two
three
12 changes: 12 additions & 0 deletions tests/test_fileinput.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from pathlib import Path
import excitertools


def test_fileinput():
data = Path(__file__).parent / "fileinput_data.txt"
output = (
excitertools.fileinput(files=[data])
.map(str.upper)
.concat("")
)
assert output == "ONE\nTWO\nTHREE\n"
Loading