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
5d499abf
Commit
5d499abf
authored
5 years ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
add default to read_in_file
parent
7ce5172f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
CHANGELOG.md
+1
-1
1 addition, 1 deletion
CHANGELOG.md
satella/__init__.py
+1
-1
1 addition, 1 deletion
satella/__init__.py
satella/files.py
+18
-5
18 additions, 5 deletions
satella/files.py
tests/test_files.py
+8
-0
8 additions, 0 deletions
tests/test_files.py
with
28 additions
and
7 deletions
CHANGELOG.md
+
1
−
1
View file @
5d499abf
# v2.9.7
*
add binary shift operations to
`AtomicNumber`
*
add
`default`
to
`read_in_file`
This diff is collapsed.
Click to expand it.
satella/__init__.py
+
1
−
1
View file @
5d499abf
__version__
=
'
2.9.7
_a2
'
__version__
=
'
2.9.7
'
This diff is collapsed.
Click to expand it.
satella/files.py
+
18
−
5
View file @
5d499abf
...
...
@@ -81,19 +81,32 @@ def write_to_file(path: str, data: tp.Union[bytes, str],
file
.
close
()
def
read_in_file
(
path
:
str
,
encoding
:
tp
.
Optional
[
str
]
=
None
)
->
tp
.
Union
[
bytes
,
str
]:
def
read_in_file
(
path
:
str
,
encoding
:
tp
.
Optional
[
str
]
=
None
,
default
:
tp
.
Optional
[
tp
.
Union
[
bytes
,
str
]]
=
None
)
->
tp
.
Union
[
bytes
,
str
]:
"""
Opens a file for reading, reads it in, converts to given encoding (or returns as bytes
if not given), and closes it.
:param path: path of file to read
:param encoding: optional encoding. If default (None given), this will be returned as bytes
:param default: value to return when the file does not exist. Default (None) will raise a
FileNotFoundError
:return: file content, either decoded as a str, or not as bytes
"""
if
encoding
is
None
:
file
=
open
(
path
,
'
rb
'
)
else
:
file
=
codecs
.
open
(
path
,
'
rb
'
,
encoding
)
if
os
.
path
.
isdir
(
path
):
if
default
:
return
default
raise
FileNotFoundError
(
'
%s found and is a directory
'
%
(
path
,
))
try
:
if
encoding
is
None
:
file
=
open
(
path
,
'
rb
'
)
else
:
file
=
codecs
.
open
(
path
,
'
rb
'
,
encoding
)
except
FileNotFoundError
:
if
default
:
return
default
raise
try
:
return
file
.
read
()
...
...
This diff is collapsed.
Click to expand it.
tests/test_files.py
+
8
−
0
View file @
5d499abf
...
...
@@ -14,6 +14,14 @@ def putfile(path: str) -> None:
class
TestFiles
(
unittest
.
TestCase
):
def
try_directory
(
self
):
os
.
system
(
'
mkdir test
'
)
self
.
assertRaises
(
FileNotFoundError
,
lambda
:
read_in_file
(
'
test
'
))
self
.
assertEqual
(
b
'
test
'
,
read_in_file
(
'
test
'
,
default
=
b
'
test
'
))
os
.
system
(
'
rm -rf test
'
)
self
.
assertRaises
(
FileNotFoundError
,
lambda
:
read_in_file
(
'
test
'
))
self
.
assertEqual
(
b
'
test
'
,
read_in_file
(
'
test
'
,
default
=
b
'
test
'
))
def
test_make_noncolliding_name
(
self
):
with
open
(
'
test.txt
'
,
'
w
'
)
as
f_out
:
f_out
.
write
(
'
test
'
)
...
...
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