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

smart_open/s3: Initial implementations of str and repr #359

Merged
merged 7 commits into from Oct 6, 2019
Merged

smart_open/s3: Initial implementations of str and repr #359

merged 7 commits into from Oct 6, 2019

Conversation

zlatsic
Copy link
Contributor

@zlatsic zlatsic commented Oct 1, 2019

Initial implementations of str and repr for SeekableBufferedInputBase and BufferedOutputBase.

This wasn't too difficult to implement, so I've gone and made the first version before getting the answers to the questions at the issue thread. Please let me know if you'd like these outputs to look differently, if I should add other parameters to them or if I should add these to any other objects.

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.

Hi, thank you for your contribution. Looks good, left you some minor comments. Please have a look.

smart_open/s3.py Outdated
@@ -412,6 +412,14 @@ def truncate(self, size=None):
"""Unsupported."""
raise io.UnsupportedOperation

def __str__(self):
return "s3.SeekableBufferedInputBase(bucket_name='{}', key='{}')".format(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you please use hanging indents?

return Foo(
    bar,
    baz,
    boz,
)

smart_open/s3.py Outdated
self._object.bucket_name, self._object.key)

def __repr__(self):
return "s3.SeekableBufferedInputBase(bucket_name='{}', key='{}')".format(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it makes sense to include the full package name here. It'll be helpful for people outside of smart_open: they'll know what package the class comes from.

Suggested change
return "s3.SeekableBufferedInputBase(bucket_name='{}', key='{}')".format(
return "smart_open.s3.SeekableBufferedInputBase(bucket_name='{}', key='{}')".format(

@mpenkov mpenkov self-assigned this Oct 2, 2019
smart_open/s3.py Outdated
@@ -553,6 +561,13 @@ def __exit__(self, exc_type, exc_val, exc_tb):
else:
self.close()

def __str__(self):
return "s3.BufferedOutputBase(bucket_name='{}', key='{}')".format(
Copy link
Collaborator

Choose a reason for hiding this comment

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

str should prioritize brevity:

Suggested change
return "s3.BufferedOutputBase(bucket_name='{}', key='{}')".format(
return "smart_open.s3.BufferedOutputBase(%r, %r)" % (self._object.bucket_name, self._object.key)

Also please avoid using the .format() stuff (we don't like it).

smart_open/s3.py Outdated
self._object.bucket_name, self._object.key)

def __repr__(self):
return "s3.BufferedOutputBase(bucket_name='{}', key='{}')".format(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please check out this link for an explanation of the difference between str and repr.

My understanding is that with repr, the user should be able to copy-paste the result into the Python interpreter and get back a copy of the original object. This may not always be possible, but this is something we should aim for. So, please include all the parameters passed to the constructor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the feedback!

I've introduced most of the suggested changes, but I'd say that this one is still somewhat incomplete. As you said, sometimes these constructor parameters are not available and here we have a similar case where the values passed to the constructor are used to create other objects and the values themselves aren't stored anywhere.

I could try digging through the implementations of these other objects to find the values, but that seems hacky. Would it be OK if these parameters were stored in SeekableBufferedInputBase or BufferedOutputBase objects as in self._<parameter name>?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sure, I think it makes sense to store them.

smart_open/s3.py Outdated Show resolved Hide resolved
smart_open/s3.py Outdated
def __str__(self):
return "smart_open.s3.BufferedOutputBase(%r, %r)" % (
self._object.bucket_name,
self._object.key
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
self._object.key
self._object.key,

But really, I think the above should fit on one line and still be within limits.

smart_open/s3.py Outdated
self._buffer_size,
self._line_terminator,
self._session,
self._resource_kwargs
Copy link
Collaborator

@mpenkov mpenkov Oct 5, 2019

Choose a reason for hiding this comment

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

Please add commas to multiline expressions, here and everywhere.

Suggested change
self._resource_kwargs
self._resource_kwargs,

This way, if we want to add new parameters later, we only need to change one line, not two.

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.

Looks good to me! Congrats on your first contribution to the smart_open project 🥇

@mpenkov mpenkov merged commit d658190 into piskvorky:master Oct 6, 2019
@zlatsic
Copy link
Contributor Author

zlatsic commented Oct 6, 2019

Great!

Thanks for all the feedback, hope it wasn't too annoying going back and forth with the changes 😁

@mpenkov
Copy link
Collaborator

mpenkov commented Oct 7, 2019

No problem at all.

Hope you enjoyed the experience, if there's anything we can do to improve things for you as contributor, let us know, we'll take it on board.

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.

None yet

2 participants