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

Render user-friendly string representation of open files #316

Closed
piskvorky opened this issue May 19, 2019 · 4 comments
Closed

Render user-friendly string representation of open files #316

piskvorky opened this issue May 19, 2019 · 4 comments

Comments

@piskvorky
Copy link
Owner

piskvorky commented May 19, 2019

Currently, an open S3 object prints like this:

>>> f = smart_open.open('s3://…')
>>> print(f)
<smart_open.s3.SeekableBufferedInputBase object at 0x10ad02b38>

This is not useful, and we can do much better: we have full info about the concrete bucket, key, mode, etc. Compare to boto3's nice native display:

>>> print(f._object)
s3.Object(bucket_name='some-bucket', key='test/bank_form.pdf')

Task: render open objects more nicely in __str__. This is great help during logging and debugging (I consider a human-friendly str() best practice, and define them religiously on custom classes).

It seems easy enough to do for S3, but I'm not sure how much work for our other supported storages (HTTP, FTP or whatever), for consistency.

@piskvorky piskvorky changed the title Use user-friendly string representation of open files Render user-friendly string representation of open files May 19, 2019
@mpenkov mpenkov self-assigned this May 20, 2019
@mpenkov
Copy link
Collaborator

mpenkov commented May 28, 2019

We could implement __str__ and __repr__ for our custom classes, which would solve the problem partially.

What should we do for non-custom wrapper classes? e.g.

import sys
from smart_open import open

print('str read_text: ' + str(open(sys.argv[1])))

gives

str read_text: <encodings.utf_8.StreamReader object at 0x7fa0b1c14eb8>

We don't really have control of that object. What's the best thing to do there?

@piskvorky
Copy link
Owner Author

piskvorky commented May 28, 2019

I'd leave builtins as-is. "Falling back to built-in where possible" is a well-communicated property of smart_open. A nicer display would be welcome, but not essential.

Also, I never bother overriding __repr__ (and never miss it), but I'm open to better approaches :)

@mpenkov mpenkov removed their assignment Oct 1, 2019
@zlatsic
Copy link
Contributor

zlatsic commented Oct 1, 2019

Hi!

I'm new to OSS and am looking to contribute somewhere, mind if I give this a shot?

Just to clarify, the behavior you're looking for is to related to the __str__ and __repr__ methods of objects created with the s3://... descriptor? Which properties would you like to be shown in the representations? Just the bucket and key or additionally, some of the other, optional properties?

@mpenkov
Copy link
Collaborator

mpenkov commented Nov 3, 2019

Closed via #359

@mpenkov mpenkov closed this as completed Nov 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants