Skip to content
Snippets Groups Projects
Commit 4f934f39 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

add read_lines

parent 1ffce0f3
No related branches found
No related tags found
No related merge requests found
# v2.14.31
* added `sleep_interval` to `hang_until_sig`
* added `read_lines`
......@@ -9,6 +9,10 @@ A file-like object that will dispose of your content.
.. autoclass:: satella.files.DevNullFilelikeObject
:members:
read_lines
----------
.. autofunction:: satella.files.read_lines
read_re_sub_and_write
---------------------
......
__version__ = '2.14.31a3'
__version__ = '2.14.31a4'
......@@ -7,7 +7,7 @@ import typing as tp
__all__ = ['read_re_sub_and_write', 'find_files', 'split', 'read_in_file', 'write_to_file',
'write_out_file_if_different', 'make_noncolliding_name', 'try_unlink',
'DevNullFilelikeObject']
'DevNullFilelikeObject', 'read_lines']
from satella.coding import silence_excs
from satella.coding.typing import Predicate
......@@ -74,6 +74,23 @@ def _has_separator(path: str) -> bool:
return any(map(lambda x: x in path, SEPARATORS))
def read_lines(path: str, delete_empty_lines: bool = True) -> tp.List[str]:
"""
Read lines from a particular file, removing end-of-line characters and optionally
empty lines. Additionally whitespaces (and end-of-line characters) will be removed
from both ends of each line.
:param path: path of file to read
:param delete_empty_lines: set to False if empty lines are not to be removed
:return: each line as a separate entry
"""
with open(path, 'r') as f_in:
lines = [line.strip() for line in f_in.readline()]
if delete_empty_lines:
lines = [line for line in lines if line]
return lines
def make_noncolliding_name(path: str,
exists_checker: Predicate[str] = os.path.exists) -> str:
"""
......
......@@ -5,7 +5,8 @@ import tempfile
import unittest
import shutil
from satella.files import read_re_sub_and_write, find_files, split, read_in_file, write_to_file, \
write_out_file_if_different, make_noncolliding_name, try_unlink, DevNullFilelikeObject
write_out_file_if_different, make_noncolliding_name, try_unlink, DevNullFilelikeObject, \
read_lines
def putfile(path: str) -> None:
......@@ -15,6 +16,10 @@ def putfile(path: str) -> None:
class TestFiles(unittest.TestCase):
def test_read_lines(self):
lines = read_lines('LICENSE')
self.assertTrue(all(lines))
def test_devnullfilelikeobject(self):
null = DevNullFilelikeObject()
self.assertEqual(null.write('ala'), 3)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment