Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for rt, rt+, wt, wt+, at, at+ methods #342

Merged
merged 27 commits into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
aa3cdc3
Update README.rst
interpolatio Jun 22, 2019
b869206
Update README.rst
interpolatio Jun 22, 2019
63b5f17
Update README.rst
interpolatio Jul 9, 2019
ebeab51
Merge remote-tracking branch 'remotes/upstream/master'
Jul 21, 2019
c5b6090
Implement seek over HTTPS
Jul 21, 2019
e93605f
Merge branch 'rare'
Jul 27, 2019
4e60f45
Add 'rt' mode in API and test this mode.
Jul 28, 2019
b87e729
Add test api_rt_plus
Jul 31, 2019
0e84917
Add new mode for function 'open'.
Jul 31, 2019
0ebe207
Revert "Merge remote-tracking branch 'remotes/upstream/master'"
Aug 1, 2019
e62af3e
Add test for binary mode:
Aug 1, 2019
51ffc65
Update smart_open/tests/test_smart_open.py
interpolatio Aug 4, 2019
2a5ca61
Add tempfile in tests.
Aug 4, 2019
46a4642
Add new mode function 'open'
Aug 7, 2019
5346272
API test names updated.
Aug 9, 2019
8a8624a
Delete "delete mode" in temp file
interpolatio Aug 17, 2019
fbca313
Update smart_open/tests/test_smart_open.py
interpolatio Aug 17, 2019
878efc6
Update smart_open/tests/test_smart_open.py
interpolatio Aug 17, 2019
b0362ed
Update test_read_str_from_bytes_api_a_plus
Aug 20, 2019
8c2100a
Merge remote-tracking branch 'origin/python_api' into python_api
Aug 20, 2019
ad7a8c3
Update tests api: when the text is compared, the text stream opens.
Aug 20, 2019
35fc206
Update tests api: update docstrings.
Aug 20, 2019
ca9e891
remove unused variable buffer
mpenkov Aug 27, 2019
3f29fb0
refactor unit tests
mpenkov Aug 27, 2019
721fbd1
rename buffer to buf, avoid keyword as variable name
mpenkov Aug 27, 2019
133a094
add explicit rt and wt tests
mpenkov Aug 27, 2019
33373d3
minor unit test improvements
mpenkov Aug 27, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion smart_open/smart_open_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,11 @@ def open(
#
try:
binary_mode = {'r': 'rb', 'r+': 'rb+',
'rt': 'rb', 'rt+': 'rb+',
mpenkov marked this conversation as resolved.
Show resolved Hide resolved
'w': 'wb', 'w+': 'wb+',
'a': 'ab', 'a+': 'ab+'}[mode]
'wt': 'wb', "wt+": 'wb+',
'a': 'ab', 'a+': 'ab+',
'at': 'ab', 'at+': 'ab+'}[mode]
except KeyError:
binary_mode = mode
binary, filename = _open_binary_stream(uri, binary_mode, transport_params)
Expand Down
61 changes: 61 additions & 0 deletions smart_open/tests/test_smart_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ class SmartOpenFileObjTest(unittest.TestCase):
Test passing raw file objects.
"""

def setUp(self):
self.temp_file = tempfile.NamedTemporaryFile(prefix='test', delete=True).name
interpolatio marked this conversation as resolved.
Show resolved Hide resolved

def test_read_bytes(self):
"""Can we read bytes from a byte stream?"""
buffer = make_buffer(initial_value=SAMPLE_BYTES)
Expand Down Expand Up @@ -354,6 +357,64 @@ def test_read_str_from_bytes(self):
data = sf.read()
self.assertEqual(data, SAMPLE_TEXT)

def test_read_str_api_rt(self):
"""Can we read strings from a byte stream?"""
buffer = make_buffer(initial_value=SAMPLE_BYTES)
with smart_open.smart_open(buffer, 'rt') as sf:
data = sf.read()
self.assertEqual(data, SAMPLE_TEXT)

def test_append_str_from_bytes_api_a(self):
"""Can we write, append, and read strings from a real text file?"""
interpolatio marked this conversation as resolved.
Show resolved Hide resolved
buffer = make_buffer()
with smart_open.smart_open(self.temp_file, 'wb') as fout:
fout.write(SAMPLE_BYTES)
with smart_open.smart_open(self.temp_file, 'ab') as fout:
fout.write(SAMPLE_BYTES)
with smart_open.smart_open(self.temp_file, 'rb') as fin:
data = fin.read()
self.assertEqual(data.decode('utf-8'), SAMPLE_TEXT+SAMPLE_TEXT)
interpolatio marked this conversation as resolved.
Show resolved Hide resolved

def test_append_str_api_a_plus(self):
"""Can we read strings from a byte stream?"""
mpenkov marked this conversation as resolved.
Show resolved Hide resolved
buffer = make_buffer()
with smart_open.smart_open(self.temp_file, 'wb') as fout:
fout.write(SAMPLE_BYTES)
with smart_open.smart_open(self.temp_file, 'a+') as fout:
fout.write(SAMPLE_TEXT)
with smart_open.smart_open(self.temp_file, 'rb') as fin:
data = fin.read()
self.assertEqual(data.decode('utf-8'), SAMPLE_TEXT+SAMPLE_TEXT)
mpenkov marked this conversation as resolved.
Show resolved Hide resolved

def test_append_str_api_at(self):
"""Can we read strings from a byte stream?"""
buffer = make_buffer()
with smart_open.smart_open(self.temp_file, 'wb') as fout:
fout.write(SAMPLE_BYTES)
with smart_open.smart_open(self.temp_file, 'at') as fout:
fout.write(SAMPLE_TEXT)
with smart_open.smart_open(self.temp_file, 'rb') as fin:
data = fin.read()
self.assertEqual(data.decode('utf-8'), SAMPLE_TEXT+SAMPLE_TEXT)

def test_append_str_api_at_plus(self):
"""Can we read strings from a byte stream?"""
buffer = make_buffer()
with smart_open.smart_open(self.temp_file, 'wb') as fout:
fout.write(SAMPLE_BYTES)
with smart_open.smart_open(self.temp_file, 'at+') as fout:
fout.write(SAMPLE_TEXT)
with smart_open.smart_open(self.temp_file, 'rb') as fin:
data = fin.read()
self.assertEqual(data.decode('utf-8'), SAMPLE_TEXT+SAMPLE_TEXT)

def test_read_str_api_rt_plus(self):
"""Can we read strings from a byte stream?"""
buffer = make_buffer(initial_value=SAMPLE_BYTES)
with smart_open.smart_open(buffer, 'rt+') as sf:
data = sf.read()
self.assertEqual(data, SAMPLE_TEXT)

def test_write_str_to_bytes(self):
"""Can we write strings to a byte stream?"""
buffer = make_buffer()
Expand Down