why does smart_open.s3.Reader doesn't implement __enter__ and __exit__ method
See original GitHub issueProblem description
Hi, I am trying to add alicloud oss support. While I am reading source code of smart_open.s3.Reader,
I found smart_open.s3.Reader doesn’t implement __enter__
and __exit__
method.
How is it possible to write code like:
with open('s3://commoncrawl/robots.txt', 'rb') as fin:
for line in fin:
print(repr(line.decode('utf-8')))
break
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
smart_open/s3.py at develop · RaRe-Technologies ... - GitHub
"""Implements file-like objects for reading and writing from/to AWS S3.""" ... We use the above as a guide only, and do not perform...
Read more >Source code for smart_open.smart_open_lib
The main methods are: * `smart_open()`, which opens the given file for reading/writing * `s3_iter_bucket()`, which goes over all keys in an S3...
Read more >smart-open - PyPI
smart_open is a Python 3 library for efficient streaming of very large files from/to storages such as S3, GCS, Azure Blob Storage, HDFS,...
Read more >Read and write from S3 server (ECS) using Python (Pyspark)
Read and write from S3 server (ECS) using Python (Pyspark) ... However when i do not user smart_open then it says path does...
Read more >GuardDuty S3 finding types - AWS Documentation
If this activity is unexpected for the associated principal, it may indicate that the credentials have been exposed or your S3 permissions are...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The reader/writer in smart_open.s3 does not need to implement the context manager protocol, because it gets wrapped by other classes that implement that protocol.
The example code you posted actually works. Have a look at the readme - it’s the main use case.
@mpenkov , thanks for your explanation, I checked the python io docs, finding that S3.Reader -> io.BufferedIOBase -> io.IOBase. While IOBase is also a context manager and therefore supports the with statement which means that you don’t need to implement
__enter__
method if you just want to return the reader itself. the__enter__
method is implemented in base class io.IOBasedef __enter__(): return self