Skip to content

Commit

Permalink
Add a toplevel fileinput method
Browse files Browse the repository at this point in the history
  • Loading branch information
cjrh committed Jan 27, 2025
1 parent 991c131 commit 5e01bab
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
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
55 changes: 55 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,59 @@ 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
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).
:param errors: Error handling mode (default: None).
"""

def yielder():
stream = _fileinput.input(
files=files,
inplace=inplace,
backup=backup,
mode=mode,
openhook=openhook,
encoding=encoding,
errors=errors,
)
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"

0 comments on commit 5e01bab

Please sign in to comment.