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

Handle "+", fix file objects and refactor SmartOpenHttpTest #263

Merged
merged 5 commits into from
Mar 4, 2019

Conversation

vmarkovtsev
Copy link
Contributor

Signed-off-by: Vadim Markovtsev [email protected]

It all started with the refactoring suggestion in #260 (comment) and went a little bit too far:

  • I found out that the file objects were handled loosely. We used to check only for the existence of read() disregarding the actual open mode. I added _is_stream() function and fixed that.
  • Added name attribute check to the file objects handler. Thus it becomes possible to open a regular stream, specify name and read/write compressed data. This was a prerequisite for the "file-less" SmartOpenHttpTest-s.

Then I started writing the tests and found a few minor bugs:

  • rw modes (r+, w+, a+) were not checked in _compression_wrapper(). All three compression codecs do not support reading and writing at the same time.
  • rw modes were not correctly handled in _encoding_wrapper(): only the reader's encoder was applied. I changed that to chaining reader's and writer's encoders in rw modes.

Copy link
Owner

@piskvorky piskvorky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thus it becomes possible to open a regular stream, specify name and read/write compressed data.

Can you expand on this? Who would specify name and why?

smart_open/smart_open_lib.py Outdated Show resolved Hide resolved
smart_open/smart_open_lib.py Outdated Show resolved Hide resolved
smart_open/smart_open_lib.py Show resolved Hide resolved
@vmarkovtsev
Copy link
Contributor Author

@piskvorky CI passed, review suggestions addressed.

Regarding the name, I was initially guided by tempfile.NamedTemporaryFile convention. Besides,

print(open("smart_open/tests/test_smart_open.py").name)

does print the opened file name. I added comments at that line.

@vmarkovtsev
Copy link
Contributor Author

vmarkovtsev commented Feb 23, 2019

As a nice side effect, this will work:

with open("file.gz", "rb") as f:
    decompressed_data = smart_open(f).read()

or this

with smart_open(open("file.gz", "wb"), "w") as f:
    f.write("data")

@piskvorky
Copy link
Owner

Thanks a lot! That "side effect" is awesome, love it :)

Copy link
Collaborator

@mpenkov mpenkov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution! Left you some comments. Please have a look.

smart_open/tests/test_smart_open.py Outdated Show resolved Hide resolved
smart_open/tests/test_smart_open.py Outdated Show resolved Hide resolved
smart_open/tests/test_smart_open.py Outdated Show resolved Hide resolved
smart_open/tests/test_smart_open.py Outdated Show resolved Hide resolved
smart_open/tests/test_smart_open.py Outdated Show resolved Hide resolved
smart_open/tests/test_smart_open.py Outdated Show resolved Hide resolved
smart_open/tests/test_smart_open.py Outdated Show resolved Hide resolved
smart_open/smart_open_lib.py Outdated Show resolved Hide resolved
smart_open/tests/test_smart_open.py Outdated Show resolved Hide resolved
smart_open/tests/test_smart_open.py Outdated Show resolved Hide resolved
@vmarkovtsev vmarkovtsev changed the title Handle "+", fix file objects and refactor SmartOpenHttpTest [WIP] Handle "+", fix file objects and refactor SmartOpenHttpTest Feb 24, 2019
@vmarkovtsev vmarkovtsev force-pushed the master branch 2 times, most recently from 5e11016 to d576123 Compare February 25, 2019 10:30
Signed-off-by: Vadim Markovtsev <[email protected]>
@vmarkovtsev
Copy link
Contributor Author

@mpenkov Review applied, the controversial function removed, ready for the next round.

Copy link
Collaborator

@mpenkov mpenkov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's my next round of comments, mostly questions about the tests.

smart_open/tests/test_smart_open.py Outdated Show resolved Hide resolved
smart_open/tests/test_smart_open.py Outdated Show resolved Hide resolved
smart_open/tests/test_smart_open.py Outdated Show resolved Hide resolved
smart_open/tests/test_smart_open.py Show resolved Hide resolved
smart_open/tests/test_smart_open.py Outdated Show resolved Hide resolved
smart_open/tests/test_smart_open.py Outdated Show resolved Hide resolved
smart_open/tests/test_smart_open.py Outdated Show resolved Hide resolved
smart_open/tests/test_smart_open.py Outdated Show resolved Hide resolved
smart_open/tests/test_smart_open.py Outdated Show resolved Hide resolved
smart_open/tests/test_smart_open.py Show resolved Hide resolved
Signed-off-by: Vadim Markovtsev <[email protected]>
@vmarkovtsev
Copy link
Contributor Author

Addressed

Copy link
Collaborator

@mpenkov mpenkov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, getting there. Please see my next round of comments below.

smart_open/tests/test_smart_open.py Outdated Show resolved Hide resolved
smart_open/tests/test_smart_open.py Outdated Show resolved Hide resolved
smart_open/tests/test_smart_open.py Outdated Show resolved Hide resolved
smart_open/tests/test_smart_open.py Outdated Show resolved Hide resolved
smart_open/tests/test_smart_open.py Show resolved Hide resolved
smart_open/tests/test_smart_open.py Outdated Show resolved Hide resolved
Signed-off-by: Vadim Markovtsev <[email protected]>
@vmarkovtsev
Copy link
Contributor Author

Addressed

@mpenkov
Copy link
Collaborator

mpenkov commented Feb 26, 2019

@piskvorky Looks good enough to merge for me, WDYT?

@vmarkovtsev vmarkovtsev changed the title [WIP] Handle "+", fix file objects and refactor SmartOpenHttpTest Handle "+", fix file objects and refactor SmartOpenHttpTest Feb 26, 2019
@mpenkov
Copy link
Collaborator

mpenkov commented Mar 4, 2019

This PR has been heavily reviewed and is fairly low risk. In the interest of keeping the ball rolling, I'm going to merge it. @piskvorky Please let me know if there are any issues.

@mpenkov mpenkov merged commit 83e4930 into piskvorky:master Mar 4, 2019
@mpenkov
Copy link
Collaborator

mpenkov commented Mar 4, 2019

@vmarkovtsev Thanks again for your contribution, keep them coming 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants