A Swiss Army knife iterator for files (or any iterator of strings)
Project description
File Iter
A Swiss Army knife iterator for files (or any iterator of strings)
- Strips lines
- Keeps track of the current line:
current_line
- Keeps track of the line number:
position
- Specify position if entering in the middle of a file:
FileIter(f, position=10)
- Specify position if entering in the middle of a file:
- Peek at the next line:
peek()
- Jump ahead
n
linesjump(n)
- Check if empty:
isempty()
- Filter out unimportant lines:
- Always filter:
FileIter(f, filter_func=is_data)
- Filter only single next():
filter_next(filter_func)
- Always filter:
>>> def is_data(line: str) -> bool:
... return len(line) > 0 and (line[0] != "#")
>>> my_iter = FileIter(
... ["Hello", "", "# comment", "World", "How", "are", "you?"],
... filter_func=is_data
... )
>>> next(my_iter)
'Hello'
>>> my_iter.peek() # peek does not respect filter_func
''
>>> next(my_iter) # skips "" and "# comment"
'World'
>>> my_iter.position
3
>>> my_iter.current_line
'World'
>>> my_iter.jump(3) # jump does not respect filter_func
'you?'
>>> my_iter.position
6
>>> my_iter.isempty()
True
>>> my_iter.peek(default="default")
'default'
Credits
This package was created with Cookiecutter and the jevandezande/poetry-cookiecutter project template.
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
file_iter-0.1.1.tar.gz
(4.6 kB
view hashes)
Built Distribution
Close
Hashes for file_iter-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb9c1050ee9d9ced29e53aba0d2bc517983af927a5e4e13ba2bcc02ccc07052b |
|
MD5 | 49548f491e48ddc1d3237f03fdab524a |
|
BLAKE2b-256 | 89b2b05750e8756e57680bc2b46c797d9a26330c4362d4435099a58effdc5ab7 |