Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
satella
Manage
Activity
Members
Labels
Plan
Issues
1
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
public
satella
Commits
4f934f39
Commit
4f934f39
authored
4 years ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
add read_lines
parent
1ffce0f3
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
CHANGELOG.md
+1
-0
1 addition, 0 deletions
CHANGELOG.md
docs/files.rst
+4
-0
4 additions, 0 deletions
docs/files.rst
satella/__init__.py
+1
-1
1 addition, 1 deletion
satella/__init__.py
satella/files.py
+18
-1
18 additions, 1 deletion
satella/files.py
tests/test_files.py
+6
-1
6 additions, 1 deletion
tests/test_files.py
with
30 additions
and
3 deletions
CHANGELOG.md
+
1
−
0
View file @
4f934f39
# v2.14.31
*
added
`sleep_interval`
to
`hang_until_sig`
*
added
`read_lines`
This diff is collapsed.
Click to expand it.
docs/files.rst
+
4
−
0
View file @
4f934f39
...
...
@@ -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
---------------------
...
...
This diff is collapsed.
Click to expand it.
satella/__init__.py
+
1
−
1
View file @
4f934f39
__version__
=
'
2.14.31a
3
'
__version__
=
'
2.14.31a
4
'
This diff is collapsed.
Click to expand it.
satella/files.py
+
18
−
1
View file @
4f934f39
...
...
@@ -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
:
"""
...
...
This diff is collapsed.
Click to expand it.
tests/test_files.py
+
6
−
1
View file @
4f934f39
...
...
@@ -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
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment