Skip to content

Commit

Permalink
smart_open/s3: Initial implementations of str and repr (#359)
Browse files Browse the repository at this point in the history
* smart_open/s3: Initial implementations of str and repr for SeekableBufferedInputBase and BufferedOutputBase

* smart_open/s3: Follow-up on str and repr methods

* Update smart_open/s3.py

Co-Authored-By: Michael Penkov <m@penkov.dev>

* smart_open/s3: Add closed parenthesis to the end of the repr function

* smart_open/s3: Remove unneccessary line breaks, store input arguments in the object, use them in repr fn

* s3 - add comma to the end of multiline tuple
  • Loading branch information
zlatsic authored and mpenkov committed Oct 6, 2019
1 parent dbcd246 commit d658190
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions smart_open/s3.py
Expand Up @@ -352,6 +352,11 @@ class SeekableBufferedInputBase(BufferedInputBase):

def __init__(self, bucket, key, version_id=None, buffer_size=DEFAULT_BUFFER_SIZE,
line_terminator=BINARY_NEWLINE, session=None, resource_kwargs=None):

self._buffer_size = buffer_size
self._session = session
self._resource_kwargs = resource_kwargs

if session is None:
session = boto3.Session()
if resource_kwargs is None:
Expand Down Expand Up @@ -412,6 +417,29 @@ def truncate(self, size=None):
"""Unsupported."""
raise io.UnsupportedOperation

def __str__(self):
return "smart_open.s3.SeekableBufferedInputBase(%r, %r)" % (self._object.bucket_name, self._object.key)

def __repr__(self):
return (
"smart_open.s3.SeekableBufferedInputBase("
"bucket=%r, "
"key=%r, "
"version_id=%r, "
"buffer_size=%r, "
"line_terminator=%r, "
"session=%r, "
"resource_kwargs=%r)"
) % (
self._object.bucket_name,
self._object.key,
self._version_id,
self._buffer_size,
self._line_terminator,
self._session,
self._resource_kwargs,
)


class BufferedOutputBase(io.BufferedIOBase):
"""Writes bytes to S3.
Expand All @@ -427,6 +455,11 @@ def __init__(
resource_kwargs=None,
multipart_upload_kwargs=None,
):

self._session = session
self._resource_kwargs = resource_kwargs
self._multipart_upload_kwargs = multipart_upload_kwargs

if min_part_size < MIN_MIN_PART_SIZE:
logger.warning("S3 requires minimum part size >= 5MB; \
multipart upload may fail")
Expand All @@ -451,6 +484,7 @@ def __init__(
self._total_parts = 0
self._parts = []


#
# This member is part of the io.BufferedIOBase interface.
#
Expand Down Expand Up @@ -553,6 +587,27 @@ def __exit__(self, exc_type, exc_val, exc_tb):
else:
self.close()

def __str__(self):
return "smart_open.s3.BufferedOutputBase(%r, %r)" % (self._object.bucket_name, self._object.key)

def __repr__(self):
return (
"smart_open.s3.BufferedOutputBase("
"bucket=%r, "
"key=%r, "
"min_part_size=%r, "
"session=%r, "
"resource_kwargs=%r, "
"multipart_upload_kwargs=%r)"
) % (
self._object.bucket_name,
self._object.key,
self._min_part_size,
self._session,
self._resource_kwargs,
self._multipart_upload_kwargs,
)


def iter_bucket(bucket_name, prefix='', accept_key=None,
key_limit=None, workers=16, retries=3):
Expand Down

0 comments on commit d658190

Please sign in to comment.