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
bf88abfc
Commit
bf88abfc
authored
5 years ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
add file writing and reading routines, v2.7.39
parent
99cdd5ba
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
+10
-0
10 additions, 0 deletions
docs/files.rst
satella/__init__.py
+1
-1
1 addition, 1 deletion
satella/__init__.py
satella/files.py
+38
-1
38 additions, 1 deletion
satella/files.py
tests/test_files.py
+8
-1
8 additions, 1 deletion
tests/test_files.py
with
58 additions
and
3 deletions
CHANGELOG.md
+
1
−
0
View file @
bf88abfc
...
...
@@ -2,3 +2,4 @@
*
added
`postcondition`
*
added __slots__ to
`ReprableMixin`
and
`Multirun`
*
added
`read_in_file`
and
`write_to_file`
This diff is collapsed.
Click to expand it.
docs/files.rst
+
10
−
0
View file @
bf88abfc
...
...
@@ -15,3 +15,13 @@ split
-----
.. autofuction:: satella.files.split
read_in_file
------------
.. autofunction:: satella.files.read_in_file
write_to_file
-------------
.. autofunction:: satella.files.write_to_file
This diff is collapsed.
Click to expand it.
satella/__init__.py
+
1
−
1
View file @
bf88abfc
__version__
=
'
2.7.39
_a7
'
__version__
=
'
2.7.39
'
This diff is collapsed.
Click to expand it.
satella/files.py
+
38
−
1
View file @
bf88abfc
import
typing
as
tp
import
re
import
os
__all__
=
[
'
read_re_sub_and_write
'
,
'
find_files
'
,
'
split
'
]
import
codecs
__all__
=
[
'
read_re_sub_and_write
'
,
'
find_files
'
,
'
split
'
,
'
read_in_file
'
,
'
write_to_file
'
]
SEPARATORS
=
{
'
\\
'
,
'
/
'
}
SEPARATORS
.
add
(
os
.
path
.
sep
)
...
...
@@ -29,6 +31,41 @@ def split(path: str) -> tp.List[str]:
return
data
def
write_to_file
(
path
:
str
,
data
:
tp
.
Union
[
bytes
,
str
],
encoding
:
tp
.
Optional
[
str
]
=
None
)
->
None
:
"""
Write provided content as a file, applying given encoding (or data is bytes, if none given)
:param path: Path to put the file under
:param data: Data to write. Must be bytes if no encoding is given, str otherwise
:param encoding: Encoding. Default is None, which means no encoding (bytes will be written)
"""
if
encoding
is
None
:
file
=
open
(
path
,
'
wb
'
)
else
:
file
=
codecs
.
open
(
path
,
'
wb
'
,
encoding
)
try
:
file
.
write
(
data
)
finally
:
file
.
close
()
def
read_in_file
(
path
:
str
,
encoding
:
tp
.
Optional
[
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.
"""
if
encoding
is
None
:
file
=
open
(
path
,
'
rb
'
)
else
:
file
=
codecs
.
open
(
path
,
'
rb
'
,
encoding
)
try
:
return
file
.
read
()
finally
:
file
.
close
()
def
read_re_sub_and_write
(
path
:
str
,
pattern
:
tp
.
Union
[
re
.
compile
,
str
],
repl
:
tp
.
Union
[
tp
.
Callable
[[
tp
.
Any
],
str
]])
->
None
:
"""
...
...
This diff is collapsed.
Click to expand it.
tests/test_files.py
+
8
−
1
View file @
bf88abfc
...
...
@@ -3,7 +3,7 @@ from os.path import join
import
tempfile
import
unittest
import
shutil
from
satella.files
import
read_re_sub_and_write
,
find_files
,
split
from
satella.files
import
read_re_sub_and_write
,
find_files
,
split
,
read_in_file
,
write_to_file
def
putfile
(
path
:
str
)
->
None
:
...
...
@@ -12,6 +12,13 @@ def putfile(path: str) -> None:
class
TestFiles
(
unittest
.
TestCase
):
def
test_read_in_and_write_to
(
self
):
data
=
'
żażółć gęślą jaźń
'
write_to_file
(
'
temp.tmp
'
,
data
,
'
UTF-8
'
)
data2
=
read_in_file
(
'
temp.tmp
'
,
'
UTF-8
'
)
self
.
assertEqual
(
data
,
data2
)
def
test_split
(
self
):
self
.
assertIn
(
split
(
'
c:/windows/system32/system32.exe
'
),
[[
'
c:
'
,
'
windows
'
,
'
system32
'
,
'
system32.exe
'
],
...
...
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