Instruction:
Can you generate a brief explanation for the following Python 3 code
def default_partfactory(part_number=None, content_length=None,
                        content_type=None, content_md5=None):
    """Get default part factory.

    :param part_number: The part number. (Default: ``None``)
    :param content_length: The content length. (Default: ``None``)
    :param content_type: The HTTP Content-Type. (Default: ``None``)
    :param content_md5: The content MD5. (Default: ``None``)
    :returns: The content length, the part number, the stream, the content
        type, MD5 of the content.
    """
    return content_length, part_number, request.stream, content_type, \
        content_md5, None

Response: Get default part factory.


Instruction:
How would you implement a function in Python 3 that
gets default put factory.

Response:
def stream_uploadfactory(content_md5=None, content_length=None,
                         content_type=None):
    """Get default put factory.

    If Content-Type is ``'multipart/form-data'`` then the stream is aborted.

    :param content_md5: The content MD5. (Default: ``None``)
    :param content_length: The content length. (Default: ``None``)
    :param content_type: The HTTP Content-Type. (Default: ``None``)
    :returns: The stream, content length, MD5 of the content.
    """
    if content_type.startswith('multipart/form-data'):
        abort(422)

    return request.stream, content_length, content_md5, parse_header_tags()


Instruction:
Can you generate the documentation for the following Python 3 function
def ngfileupload_partfactory(part_number=None, content_length=None,
                             uploaded_file=None):
    """Part factory for ng-file-upload.

    :param part_number: The part number. (Default: ``None``)
    :param content_length: The content length. (Default: ``None``)
    :param uploaded_file: The upload request. (Default: ``None``)
    :returns: The content length, part number, stream, HTTP Content-Type
        header.
    """
    return content_length, part_number, uploaded_file.stream, \
        uploaded_file.headers.get('Content-Type'), None, None

Response: Part factory for ng - file - upload.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def ngfileupload_uploadfactory(content_length=None, content_type=None,
                               uploaded_file=None):
    """Get default put factory.

    If Content-Type is ``'multipart/form-data'`` then the stream is aborted.

    :param content_length: The content length. (Default: ``None``)
    :param content_type: The HTTP Content-Type. (Default: ``None``)
    :param uploaded_file: The upload request. (Default: ``None``)
    :param file_tags_header: The file tags. (Default: ``None``)
    :returns: A tuple containing stream, content length, and empty header.
    """
    if not content_type.startswith('multipart/form-data'):
        abort(422)

    return uploaded_file.stream, content_length, None, parse_header_tags()

Response: Get default put factory.


Instruction:
Make a summary of the following Python 3 code
def pass_bucket(f):
    @wraps(f)
    def decorate(*args, **kwargs):
        bucket_id = kwargs.pop('bucket_id')
        bucket = Bucket.get(as_uuid(bucket_id))
        if not bucket:
            abort(404, 'Bucket does not exist.')
        return f(bucket=bucket, *args, **kwargs)
    return decorate

Response: Decorate to retrieve a bucket.


Instruction:
How would you explain what the following Python 3 function does
def pass_multipart(with_completed=False):
    def decorate(f):
        @wraps(f)
        def inner(self, bucket, key, upload_id, *args, **kwargs):
            obj = MultipartObject.get(
                bucket, key, upload_id, with_completed=with_completed)
            if obj is None:
                abort(404, 'uploadId does not exists.')
            return f(self, obj, *args, **kwargs)
        return inner
    return decorate

Response: Decorate to retrieve an object.


Instruction:
Explain what the following Python 3 code does
def check_permission(permission, hidden=True):
    if permission is not None and not permission.can():
        if hidden:
            abort(404)
        else:
            if current_user.is_authenticated:
                abort(403,
                      'You do not have a permission for this action')
            abort(401)

Response: Check if permission is allowed.


Instruction:
Can you tell what is the following Python 3 function doing
def need_permissions(object_getter, action, hidden=True):
    def decorator_builder(f):
        @wraps(f)
        def decorate(*args, **kwargs):
            check_permission(current_permission_factory(
                object_getter(*args, **kwargs),
                action(*args, **kwargs) if callable(action) else action,

            ), hidden=hidden)
            return f(*args, **kwargs)
        return decorate
    return decorator_builder

Response: Decorator for functions that need permissions for buckets or aborts.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def multipart_listuploads(self, bucket):
        return self.make_response(
            data=MultipartObject.query_by_bucket(bucket).limit(1000).all(),
            context={
                'class': MultipartObject,
                'bucket': bucket,
                'many': True,
            }
        )

Response: List objects in a bucket.


Instruction:
Create a Python 3 function to
list objects in a bucket.

Response:
def listobjects(self, bucket, versions):
        """List objects in a bucket.

        :param bucket: A :class:`invenio_files_rest.models.Bucket` instance.
        :returns: The Flask response.
        """
        if versions is not missing:
            check_permission(
                current_permission_factory(bucket, 'bucket-read-versions'),
                hidden=False
            )
        return self.make_response(
            data=ObjectVersion.get_by_bucket(
                bucket.id, versions=versions is not missing).limit(1000).all(),
            context={
                'class': ObjectVersion,
                'bucket': bucket,
                'many': True,
            }
        )


Instruction:
Can you tell what is the following Python 3 function doing
def get(self, bucket=None, versions=missing, uploads=missing):
        if uploads is not missing:
            return self.multipart_listuploads(bucket)
        else:
            return self.listobjects(bucket, versions)

Response: Get list of objects in the bucket.


Instruction:
How would you implement a function in Python 3 that
retrieves object and abort if it doesn t exist.

Response:
def check_object_permission(obj):
        """Retrieve object and abort if it doesn't exists."""
        check_permission(current_permission_factory(
            obj,
            'object-read'
        ))
        if not obj.is_head:
            check_permission(
                current_permission_factory(obj, 'object-read-version'),
                hidden=False
            )


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_object(cls, bucket, key, version_id):
        obj = ObjectVersion.get(bucket, key, version_id=version_id)
        if not obj:
            abort(404, 'Object does not exists.')

        cls.check_object_permission(obj)

        return obj

Response: Retrieve object and abort if it doesn t exist.


Instruction:
Create a Python 3 function to
create a new object in the specified bucket.

Response:
def create_object(self, bucket, key):
        """Create a new object.

        :param bucket: The bucket (instance or id) to get the object from.
        :param key: The file key.
        :returns: A Flask response.
        """
        # Initial validation of size based on Content-Length.
        # User can tamper with Content-Length, so this is just an initial up
        # front check. The storage subsystem must validate the size limit as
        # well.
        stream, content_length, content_md5, tags = \
            current_files_rest.upload_factory()

        size_limit = bucket.size_limit
        if content_length and size_limit and content_length > size_limit:
            desc = 'File size limit exceeded.' \
                if isinstance(size_limit, int) else size_limit.reason
            raise FileSizeError(description=desc)

        with db.session.begin_nested():
            obj = ObjectVersion.create(bucket, key)
            obj.set_contents(
                stream, size=content_length, size_limit=size_limit)
            # Check add tags
            if tags:
                for key, value in tags.items():
                    ObjectVersionTag.create(obj, key, value)

        db.session.commit()
        return self.make_response(
            data=obj,
            context={
                'class': ObjectVersion,
                'bucket': bucket,
            },
            etag=obj.file.checksum
        )


Instruction:
Can you generate the documentation for the following Python 3 function
def delete_object(self, bucket, obj, version_id):
        if version_id is None:
            # Create a delete marker.
            with db.session.begin_nested():
                ObjectVersion.delete(bucket, obj.key)
            db.session.commit()
        else:
            # Permanently delete specific object version.
            check_permission(
                current_permission_factory(bucket, 'object-delete-version'),
                hidden=False,
            )
            obj.remove()
            db.session.commit()
            if obj.file_id:
                remove_file_data.delay(str(obj.file_id))

        return self.make_response('', 204)

Response: Delete an existing object.


Instruction:
Write a Python 3 script for
sending an object to a given bucket.

Response:
def send_object(bucket, obj, expected_chksum=None,
                    logger_data=None, restricted=True, as_attachment=False):
        """Send an object for a given bucket.

        :param bucket: The bucket (instance or id) to get the object from.
        :param obj: A :class:`invenio_files_rest.models.ObjectVersion`
            instance.
        :params expected_chksum: Expected checksum.
        :param logger_data: The python logger.
        :param kwargs: Keyword arguments passed to ``Object.send_file()``
        :returns: A Flask response.
        """
        if not obj.is_head:
            check_permission(
                current_permission_factory(obj, 'object-read-version'),
                hidden=False
            )

        if expected_chksum and obj.file.checksum != expected_chksum:
            current_app.logger.warning(
                'File checksum mismatch detected.', extra=logger_data)

        file_downloaded.send(current_app._get_current_object(), obj=obj)
        return obj.send_file(restricted=restricted,
                             as_attachment=as_attachment)


Instruction:
How would you code a function in Python 3 to
return a Flask response for listing the parts of a multipart upload.

Response:
def multipart_listparts(self, multipart):
        """Get parts of a multipart upload.

        :param multipart: A :class:`invenio_files_rest.models.MultipartObject`
            instance.
        :returns: A Flask response.
        """
        return self.make_response(
            data=Part.query_by_multipart(
                multipart).order_by(Part.part_number).limit(1000).all(),
            context={
                'class': Part,
                'multipart': multipart,
                'many': True,
            }
        )


Instruction:
Write a Python 3 function that can
initialize a multipart upload.

Response:
def multipart_init(self, bucket, key, size=None, part_size=None):
        """Initialize a multipart upload.

        :param bucket: The bucket (instance or id) to get the object from.
        :param key: The file key.
        :param size: The total size.
        :param part_size: The part size.
        :raises invenio_files_rest.errors.MissingQueryParameter: If size or
            part_size are not defined.
        :returns: A Flask response.
        """
        if size is None:
            raise MissingQueryParameter('size')
        if part_size is None:
            raise MissingQueryParameter('partSize')
        multipart = MultipartObject.create(bucket, key, size, part_size)
        db.session.commit()
        return self.make_response(
            data=multipart,
            context={
                'class': MultipartObject,
                'bucket': bucket,
            }
        )


Instruction:
How would you explain what the following Python 3 function does
def multipart_uploadpart(self, multipart):
        content_length, part_number, stream, content_type, content_md5, tags =\
            current_files_rest.multipart_partfactory()

        if content_length:
            ck = multipart.last_part_size if \
                part_number == multipart.last_part_number \
                else multipart.chunk_size

            if ck != content_length:
                raise MultipartInvalidChunkSize()

        # Create part
        try:
            p = Part.get_or_create(multipart, part_number)
            p.set_contents(stream)
            db.session.commit()
        except Exception:
            # We remove the Part since incomplete data may have been written to
            # disk (e.g. client closed connection etc.) so it must be
            # reuploaded.
            db.session.rollback()
            Part.delete(multipart, part_number)
            raise
        return self.make_response(
            data=p,
            context={
                'class': Part,
            },
            etag=p.checksum
        )

Response:
Upload a part.

        :param multipart: A :class:`invenio_files_rest.models.MultipartObject`
            instance.
        :returns: A Flask response.


Instruction:
Given the following Python 3 function, write the documentation
def multipart_complete(self, multipart):
        multipart.complete()
        db.session.commit()

        version_id = str(uuid.uuid4())

        return self.make_response(
            data=multipart,
            context={
                'class': MultipartObject,
                'bucket': multipart.bucket,
                'object_version_id': version_id,
            },
            # This will wait for the result, and send whitespace on the
            # connection until the task has finished (or max timeout reached).
            task_result=merge_multipartobject.delay(
                str(multipart.upload_id),
                version_id=version_id,
            ),
        )

Response: Complete a multipart upload.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def multipart_delete(self, multipart):
        multipart.delete()
        db.session.commit()
        if multipart.file_id:
            remove_file_data.delay(str(multipart.file_id))
        return self.make_response('', 204)

Response: Abort a multipart upload.


Instruction:
Create a Python 3 function to
get object or list parts of a multpart upload.

Response:
def get(self, bucket=None, key=None, version_id=None, upload_id=None,
            uploads=None, download=None):
        """Get object or list parts of a multpart upload.

        :param bucket: The bucket (instance or id) to get the object from.
            (Default: ``None``)
        :param key: The file key. (Default: ``None``)
        :param version_id: The version ID. (Default: ``None``)
        :param upload_id: The upload ID. (Default: ``None``)
        :param download: The download flag. (Default: ``None``)
        :returns: A Flask response.
        """
        if upload_id:
            return self.multipart_listparts(bucket, key, upload_id)
        else:
            obj = self.get_object(bucket, key, version_id)
            # If 'download' is missing from query string it will have
            # the value None.
            return self.send_object(bucket, obj,
                                    as_attachment=download is not None)


Instruction:
Create a Python 3 function for
uploading a new object or start a multipart upload.

Response:
def post(self, bucket=None, key=None, uploads=missing, upload_id=None):
        """Upload a new object or start/complete a multipart upload.

        :param bucket: The bucket (instance or id) to get the object from.
            (Default: ``None``)
        :param key: The file key. (Default: ``None``)
        :param upload_id: The upload ID. (Default: ``None``)
        :returns: A Flask response.
        """
        if uploads is not missing:
            return self.multipart_init(bucket, key)
        elif upload_id is not None:
            return self.multipart_complete(bucket, key, upload_id)
        abort(403)


Instruction:
Can you create a Python 3 function that
updates a new object or upload a part of a multipart upload.

Response:
def put(self, bucket=None, key=None, upload_id=None):
        """Update a new object or upload a part of a multipart upload.

        :param bucket: The bucket (instance or id) to get the object from.
            (Default: ``None``)
        :param key: The file key. (Default: ``None``)
        :param upload_id: The upload ID. (Default: ``None``)
        :returns: A Flask response.
        """
        if upload_id is not None:
            return self.multipart_uploadpart(bucket, key, upload_id)
        else:
            return self.create_object(bucket, key)


Instruction:
Write a Python 3 script to
delete an object or abort a multipart upload.

Response:
def delete(self, bucket=None, key=None, version_id=None, upload_id=None,
               uploads=None):
        """Delete an object or abort a multipart upload.

        :param bucket: The bucket (instance or id) to get the object from.
            (Default: ``None``)
        :param key: The file key. (Default: ``None``)
        :param version_id: The version ID. (Default: ``None``)
        :param upload_id: The upload ID. (Default: ``None``)
        :returns: A Flask response.
        """
        if upload_id is not None:
            return self.multipart_delete(bucket, key, upload_id)
        else:
            obj = self.get_object(bucket, key, version_id)
            return self.delete_object(bucket, obj, version_id)


Instruction:
Given the following Python 3 function, write the documentation
def _get_container(self):
        if not hasattr(self, "_container"):
            if self.use_pyrax:
                self._container = self.connection.create_container(self.container_name)
            else:
                self._container = None
        return self._container

Response: Gets or creates the container.


Instruction:
Write a Python 3 function that can
set the container and makes it publicly available.

Response:
def _set_container(self, container):
        """
        Sets the container (and, if needed, the configured TTL on it), making
        the container publicly available.
        """
        if self.use_pyrax:
            if container.cdn_ttl != self.ttl or not container.cdn_enabled:
                container.make_public(ttl=self.ttl)
            if hasattr(self, "_container_public_uri"):
                delattr(self, "_container_public_uri")
        self._container = container


Instruction:
Explain what the following Python 3 code does
def _get_object(self, name):
        if self.use_pyrax:
            try:
                return self.container.get_object(name)
            except pyrax.exceptions.NoSuchObject:
                return None
        elif swiftclient:
            try:
                return self.container.get_object(name)
            except swiftclient.exceptions.ClientException:
                return None
        else:
            return self.container.get_object(name)

Response: Helper function to retrieve the requested object.


Instruction:
Here you have a function in Python 3, explain what it does
def as_object_version(value):
    return value if isinstance(value, ObjectVersion) \
        else ObjectVersion.query.filter_by(version_id=value).one_or_none()

Response: Get an object version object from an object version ID or an object version.


Instruction:
Write a Python 3 script for
decorating to update bucket size after operation.

Response:
def update_bucket_size(f):
    """Decorate to update bucket size after operation."""
    @wraps(f)
    def inner(self, *args, **kwargs):
        res = f(self, *args, **kwargs)
        self.bucket.size += self.file.size
        return res
    return inner


Instruction:
Can you generate a brief explanation for the following Python 3 code
def ensure_state(default_getter, exc_class, default_msg=None):
    def decorator(getter=default_getter, msg=default_msg):
        def ensure_decorator(f):
            @wraps(f)
            def inner(self, *args, **kwargs):
                if not getter(self):
                    raise exc_class(msg) if msg else exc_class()
                return f(self, *args, **kwargs)
            return inner
        return ensure_decorator
    return decorator

Response: Create a decorator factory function that ensures that the state of the current user is in the correct state.


Instruction:
Explain what the following Python 3 code does
def size_limit(self):
        limits = [
            lim for lim in current_files_rest.file_size_limiters(
                self)
            if lim.limit is not None
        ]
        return min(limits) if limits else None

Response: Get the size limit for this bucket.


Instruction:
Here you have a function in Python 3, explain what it does
def snapshot(self, lock=False):
        with db.session.begin_nested():
            bucket = Bucket(
                default_location=self.default_location,
                default_storage_class=self.default_storage_class,
                quota_size=self.quota_size,
            )
            db.session.add(bucket)

        for o in ObjectVersion.get_by_bucket(self):
            o.copy(bucket=bucket)

        bucket.locked = True if lock else self.locked

        return bucket

Response: Create a snapshot of latest objects in bucket.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def create(cls, location=None, storage_class=None, **kwargs):
        with db.session.begin_nested():
            if location is None:
                location = Location.get_default()
            elif isinstance(location, six.string_types):
                location = Location.get_by_name(location)

            obj = cls(
                default_location=location.id,
                default_storage_class=storage_class or current_app.config[
                    'FILES_REST_DEFAULT_STORAGE_CLASS'],
                **kwargs
            )
            db.session.add(obj)
        return obj

Response: r Create a new bucket.


Instruction:
Can you implement a function in Python 3 that
gets a bucket object.

Response:
def get(cls, bucket_id):
        """Get a bucket object (excluding deleted).

        :param bucket_id: Bucket identifier.
        :returns: Bucket instance.
        """
        return cls.query.filter_by(
            id=bucket_id,
            deleted=False
        ).one_or_none()


Instruction:
Implement a Python 3 function for
deleting a bucket. Does not actually delete the Bucket, just marks it as deleted.

Response:
def delete(cls, bucket_id):
        """Delete a bucket.

        Does not actually delete the Bucket, just marks it as deleted.
        """
        bucket = cls.get(bucket_id)
        if not bucket or bucket.deleted:
            return False

        bucket.deleted = True
        return True


Instruction:
Implement a function in Python 3 to
create a new tag for a bucket.

Response:
def create(cls, bucket, key, value):
        """Create a new tag for bucket."""
        with db.session.begin_nested():
            obj = cls(
                bucket_id=as_bucket_id(bucket),
                key=key,
                value=value
            )
            db.session.add(obj)
        return obj


Instruction:
Can you tell what is the following Python 3 function doing
def create_or_update(cls, bucket, key, value):
        obj = cls.get(bucket, key)
        if obj:
            obj.value = value
            db.session.merge(obj)
        else:
            obj = cls.create(bucket, key, value)
        return obj

Response: Create or update a new tag for bucket.


Instruction:
Can you tell what is the following Python 3 function doing
def get_by_uri(cls, uri):
        assert uri is not None
        return cls.query.filter_by(uri=uri).one_or_none()

Response: Get a file instance by URI.


Instruction:
Implement a Python 3 function for
creating a file instance.

Response:
def create(cls):
        """Create a file instance.

        Note, object is only added to the database session.
        """
        obj = cls(
            id=uuid.uuid4(),
            writable=True,
            readable=False,
            size=0,
        )
        db.session.add(obj)
        return obj


Instruction:
Here you have a function in Python 3, explain what it does
def delete(self):
        self.query.filter_by(id=self.id).delete()
        return self

Response: Delete a file instance.


Instruction:
Can you tell what is the following Python 3 function doing
def update_checksum(self, progress_callback=None, chunk_size=None,
                        checksum_kwargs=None, **kwargs):
        """Update checksum based on file."""
        self.checksum = self.storage(**kwargs).checksum(
            progress_callback=progress_callback, chunk_size=chunk_size,
            **(checksum_kwargs or {}))

Response: Update checksum based on file.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def clear_last_check(self):
        with db.session.begin_nested():
            self.last_check = None
            self.last_check_at = datetime.utcnow()
        return self

Response: Clear the checksum of the file.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def verify_checksum(self, progress_callback=None, chunk_size=None,
                        throws=True, checksum_kwargs=None, **kwargs):
        """Verify checksum of file instance.

        :param bool throws: If `True`, exceptions raised during checksum
            calculation will be re-raised after logging. If set to `False`, and
            an exception occurs, the `last_check` field is set to `None`
            (`last_check_at` of course is updated), since no check actually was
            performed.
        :param dict checksum_kwargs: Passed as `**kwargs`` to
            ``storage().checksum``.
        """
        try:
            real_checksum = self.storage(**kwargs).checksum(
                progress_callback=progress_callback, chunk_size=chunk_size,
                **(checksum_kwargs or {}))
        except Exception as exc:
            current_app.logger.exception(str(exc))
            if throws:
                raise
            real_checksum = None
        with db.session.begin_nested():
            self.last_check = (None if real_checksum is None
                               else (self.checksum == real_checksum))
            self.last_check_at = datetime.utcnow()
        return self.last_check

Response: Verify checksum of file instance.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def update_contents(self, stream, seek=0, size=None, chunk_size=None,
                        progress_callback=None, **kwargs):
        """Save contents of stream to this file.

        :param obj: ObjectVersion instance from where this file is accessed
            from.
        :param stream: File-like stream.
        """
        self.checksum = None
        return self.storage(**kwargs).update(
            stream, seek=seek, size=size, chunk_size=chunk_size,
            progress_callback=progress_callback
        )

Response: Save contents of stream to this file.


Instruction:
Write a Python 3 script to
save contents of the object version to this file.

Response:
def set_contents(self, stream, chunk_size=None, size=None, size_limit=None,
                     progress_callback=None, **kwargs):
        """Save contents of stream to this file.

        :param obj: ObjectVersion instance from where this file is accessed
            from.
        :param stream: File-like stream.
        """
        self.set_uri(
            *self.storage(**kwargs).save(
                stream, chunk_size=chunk_size, size=size,
                size_limit=size_limit, progress_callback=progress_callback))


Instruction:
Implement a function in Python 3 to
copy this file instance into another file instance.

Response:
def copy_contents(self, fileinstance, progress_callback=None,
                      chunk_size=None, **kwargs):
        """Copy this file instance into another file instance."""
        if not fileinstance.readable:
            raise ValueError('Source file instance is not readable.')
        if not self.size == 0:
            raise ValueError('File instance has data.')

        self.set_uri(
            *self.storage(**kwargs).copy(
                fileinstance.storage(**kwargs),
                chunk_size=chunk_size,
                progress_callback=progress_callback))


Instruction:
Write a Python 3 function that can
send file to the client.

Response:
def send_file(self, filename, restricted=True, mimetype=None,
                  trusted=False, chunk_size=None, as_attachment=False,
                  **kwargs):
        """Send file to client."""
        return self.storage(**kwargs).send_file(
            filename,
            mimetype=mimetype,
            restricted=restricted,
            checksum=self.checksum,
            trusted=trusted,
            chunk_size=chunk_size,
            as_attachment=as_attachment,
        )


Instruction:
Explain what the following Python 3 code does
def set_uri(self, uri, size, checksum, readable=True, writable=False,
                storage_class=None):
        """Set a location of a file."""
        self.uri = uri
        self.size = size
        self.checksum = checksum
        self.writable = writable
        self.readable = readable
        self.storage_class = \
            current_app.config['FILES_REST_DEFAULT_STORAGE_CLASS'] \
            if storage_class is None else \
            storage_class
        return self

Response: Set a location of a file.


Instruction:
Make a summary of the following Python 3 code
def set_contents(self, stream, chunk_size=None, size=None, size_limit=None,
                     progress_callback=None):
        """Save contents of stream to file instance.

        If a file instance has already been set, this methods raises an
        ``FileInstanceAlreadySetError`` exception.

        :param stream: File-like stream.
        :param size: Size of stream if known.
        :param chunk_size: Desired chunk size to read stream in. It is up to
            the storage interface if it respects this value.
        """
        if size_limit is None:
            size_limit = self.bucket.size_limit

        self.file = FileInstance.create()
        self.file.set_contents(
            stream, size_limit=size_limit, size=size, chunk_size=chunk_size,
            progress_callback=progress_callback,
            default_location=self.bucket.location.uri,
            default_storage_class=self.bucket.default_storage_class,
        )

        return self

Response: Save contents of stream to file instance.


Instruction:
Can you create a Python 3 function that
sets only URI location of for object.

Response:
def set_location(self, uri, size, checksum, storage_class=None):
        """Set only URI location of for object.

        Useful to link files on externally controlled storage. If a file
        instance has already been set, this methods raises an
        ``FileInstanceAlreadySetError`` exception.

        :param uri: Full URI to object (which can be interpreted by the storage
            interface).
        :param size: Size of file.
        :param checksum: Checksum of file.
        :param storage_class: Storage class where file is stored ()
        """
        self.file = FileInstance()
        self.file.set_uri(
            uri, size, checksum, storage_class=storage_class
        )
        db.session.add(self.file)
        return self


Instruction:
How would you implement a function in Python 3 that
wraps around FileInstance s send file method.

Response:
def send_file(self, restricted=True, trusted=False, **kwargs):
        """Wrap around FileInstance's send file."""
        return self.file.send_file(
            self.basename,
            restricted=restricted,
            mimetype=self.mimetype,
            trusted=trusted,
            **kwargs
        )


Instruction:
Make a summary of the following Python 3 code
def copy(self, bucket=None, key=None):
        new_ob = ObjectVersion.create(
            self.bucket if bucket is None else as_bucket(bucket),
            key or self.key,
            _file_id=self.file_id
        )

        for tag in self.tags:
            ObjectVersionTag.create_or_update(object_version=new_ob,
                                              key=tag.key,
                                              value=tag.value)

        return new_ob

Response: Copy an object version to a given bucket + object key.


Instruction:
Make a summary of the following Python 3 code
def create(cls, bucket, key, _file_id=None, stream=None, mimetype=None,
               version_id=None, **kwargs):
        """Create a new object in a bucket.

        The created object is by default created as a delete marker. You must
        use ``set_contents()`` or ``set_location()`` in order to change this.

        :param bucket: The bucket (instance or id) to create the object in.
        :param key: Key of object.
        :param _file_id: For internal use.
        :param stream: File-like stream object. Used to set content of object
            immediately after being created.
        :param mimetype: MIME type of the file object if it is known.
        :param kwargs: Keyword arguments passed to ``Object.set_contents()``.
        """
        bucket = as_bucket(bucket)

        if bucket.locked:
            raise BucketLockedError()

        with db.session.begin_nested():
            latest_obj = cls.query.filter(
                cls.bucket == bucket, cls.key == key, cls.is_head.is_(True)
            ).one_or_none()
            if latest_obj is not None:
                latest_obj.is_head = False
                db.session.add(latest_obj)

            # By default objects are created in a deleted state (i.e.
            # file_id is null).
            obj = cls(
                bucket=bucket,
                key=key,
                version_id=version_id or uuid.uuid4(),
                is_head=True,
                mimetype=mimetype,
            )
            if _file_id:
                file_ = _file_id if isinstance(_file_id, FileInstance) else \
                    FileInstance.get(_file_id)
                obj.set_file(file_)
            db.session.add(obj)
        if stream:
            obj.set_contents(stream, **kwargs)
        return obj

Response: Create a new object in a bucket.


Instruction:
How would you code a function in Python 3 to
fetch a specific object from the database.

Response:
def get(cls, bucket, key, version_id=None):
        """Fetch a specific object.

        By default the latest object version is returned, if
        ``version_id`` is not set.

        :param bucket: The bucket (instance or id) to get the object from.
        :param key: Key of object.
        :param version_id: Specific version of an object.
        """
        filters = [
            cls.bucket_id == as_bucket_id(bucket),
            cls.key == key,
        ]

        if version_id:
            filters.append(cls.version_id == version_id)
        else:
            filters.append(cls.is_head.is_(True))
            filters.append(cls.file_id.isnot(None))

        return cls.query.filter(*filters).one_or_none()


Instruction:
Can you write a function in Python 3 where it
fetches all versions of a specific object.

Response:
def get_versions(cls, bucket, key, desc=True):
        """Fetch all versions of a specific object.

        :param bucket: The bucket (instance or id) to get the object from.
        :param key: Key of object.
        :param desc: Sort results desc if True, asc otherwise.
        :returns: The query to execute to fetch all versions.
        """
        filters = [
            cls.bucket_id == as_bucket_id(bucket),
            cls.key == key,
        ]

        order = cls.created.desc() if desc else cls.created.asc()

        return cls.query.filter(*filters).order_by(cls.key, order)


Instruction:
Write a Python 3 function for
deleting an object. Technically works by creating a new version which works as a delete marker. :param bucket: The bucket (instance or id) to delete the object from. :param key: Key of object. :returns: Created delete marker object if key exists else ``None``.

Response:
def delete(cls, bucket, key):
        """Delete an object.

        Technically works by creating a new version which works as a delete
        marker.

        :param bucket: The bucket (instance or id) to delete the object from.
        :param key: Key of object.
        :returns: Created delete marker object if key exists else ``None``.
        """
        bucket_id = as_bucket_id(bucket)

        obj = cls.get(bucket_id, key)
        if obj:
            return cls.create(as_bucket(bucket), key)
        return None


Instruction:
Given the following Python 3 function, write the documentation
def get_by_bucket(cls, bucket, versions=False, with_deleted=False):
        bucket_id = bucket.id if isinstance(bucket, Bucket) else bucket

        filters = [
            cls.bucket_id == bucket_id,
        ]

        if not versions:
            filters.append(cls.is_head.is_(True))

        if not with_deleted:
            filters.append(cls.file_id.isnot(None))

        return cls.query.filter(*filters).order_by(cls.key, cls.created.desc())

Response: Return a query that fetches all the objects in a given bucket.


Instruction:
Explain what the following Python 3 code does
def relink_all(cls, old_file, new_file):
        assert old_file.checksum == new_file.checksum
        assert old_file.id
        assert new_file.id

        with db.session.begin_nested():
            ObjectVersion.query.filter_by(file_id=str(old_file.id)).update({
                ObjectVersion.file_id: str(new_file.id)})

Response: Relink all object versions for a given file to a new file.


Instruction:
Can you implement a function in Python 3 that
copies a tag to a given object version.

Response:
def copy(self, object_version=None, key=None):
        """Copy a tag to a given object version.

        :param object_version: The object version instance to copy the tag to.
            Default: current object version.
        :param key: Key of destination tag.
            Default: current tag key.
        :return: The copied object version tag.
        """
        return ObjectVersionTag.create(
            self.object_version if object_version is None else object_version,
            key or self.key,
            self.value
        )


Instruction:
How would you code a function in Python 3 to
get the tag object.

Response:
def get(cls, object_version, key):
        """Get the tag object."""
        return cls.query.filter_by(
            version_id=as_object_version_id(object_version),
            key=key,
        ).one_or_none()


Instruction:
Can you create a Python 3 function that
creates a new tag for a given object version.

Response:
def create(cls, object_version, key, value):
        """Create a new tag for a given object version."""
        assert len(key) < 256
        assert len(value) < 256
        with db.session.begin_nested():
            obj = cls(version_id=as_object_version_id(object_version),
                      key=key,
                      value=value)
            db.session.add(obj)
        return obj


Instruction:
Implement a function in Python 3 to
create or update a new tag for a given object version.

Response:
def create_or_update(cls, object_version, key, value):
        """Create or update a new tag for a given object version."""
        assert len(key) < 256
        assert len(value) < 256
        obj = cls.get(object_version, key)
        if obj:
            obj.value = value
            db.session.merge(obj)
        else:
            obj = cls.create(object_version, key, value)
        return obj


Instruction:
Write a Python 3 function for
getting the tag value.

Response:
def get_value(cls, object_version, key):
        """Get the tag value."""
        obj = cls.get(object_version, key)
        return obj.value if obj else None


Instruction:
Can you create a Python 3 function that
deletes tags. :param object_version: The object version instance or id. :param key: Key of the tag to delete. Default: delete all tags.

Response:
def delete(cls, object_version, key=None):
        """Delete tags.

        :param object_version: The object version instance or id.
        :param key: Key of the tag to delete.
            Default: delete all tags.
        """
        with db.session.begin_nested():
            q = cls.query.filter_by(
                version_id=as_object_version_id(object_version))
            if key:
                q = q.filter_by(key=key)
            q.delete()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def last_part_number(self):
        return int(self.size / self.chunk_size) \
            if self.size % self.chunk_size else \
            int(self.size / self.chunk_size) - 1

Response: Get last part number.


Instruction:
Given the following Python 3 function, write the documentation
def is_valid_chunksize(chunk_size):
        min_csize = current_app.config['FILES_REST_MULTIPART_CHUNKSIZE_MIN']
        max_csize = current_app.config['FILES_REST_MULTIPART_CHUNKSIZE_MAX']
        return chunk_size >= min_csize and chunk_size <= max_csize

Response: Check if size is valid.


Instruction:
Write a Python 3 script for
validating max theoretical size.

Response:
def is_valid_size(size, chunk_size):
        """Validate max theoretical size."""
        min_csize = current_app.config['FILES_REST_MULTIPART_CHUNKSIZE_MIN']
        max_size = \
            chunk_size * current_app.config['FILES_REST_MULTIPART_MAX_PARTS']
        return size > min_csize and size <= max_size


Instruction:
How would you explain what the following Python 3 function does
def expected_part_size(self, part_number):
        last_part = self.multipart.last_part_number

        if part_number == last_part:
            return self.multipart.last_part_size
        elif part_number >= 0 and part_number < last_part:
            return self.multipart.chunk_size
        else:
            raise MultipartInvalidPartNumber()

Response: Get the expected part size for a particular part number.


Instruction:
Can you write a function in Python 3 where it
marks a multipart object as complete.

Response:
def complete(self):
        """Mark a multipart object as complete."""
        if Part.count(self) != self.last_part_number + 1:
            raise MultipartMissingParts()

        with db.session.begin_nested():
            self.completed = True
            self.file.readable = True
            self.file.writable = False
        return self


Instruction:
Implement a Python 3 function for
merging parts into object version.

Response:
def merge_parts(self, version_id=None, **kwargs):
        """Merge parts into object version."""
        self.file.update_checksum(**kwargs)
        with db.session.begin_nested():
            obj = ObjectVersion.create(
                self.bucket,
                self.key,
                _file_id=self.file_id,
                version_id=version_id
            )
            self.delete()
        return obj


Instruction:
Write a Python 3 function for
deleting a multipart object.

Response:
def delete(self):
        """Delete a multipart object."""
        # Update bucket size.
        self.bucket.size -= self.size
        # Remove parts
        Part.query_by_multipart(self).delete()
        # Remove self
        self.query.filter_by(upload_id=self.upload_id).delete()


Instruction:
Create a Python 3 function for
creating a new object in a bucket.

Response:
def create(cls, bucket, key, size, chunk_size):
        """Create a new object in a bucket."""
        bucket = as_bucket(bucket)

        if bucket.locked:
            raise BucketLockedError()

        # Validate chunk size.
        if not cls.is_valid_chunksize(chunk_size):
            raise MultipartInvalidChunkSize()

        # Validate max theoretical size.
        if not cls.is_valid_size(size, chunk_size):
            raise MultipartInvalidSize()

        # Validate max bucket size.
        bucket_limit = bucket.size_limit
        if bucket_limit and size > bucket_limit:
            desc = 'File size limit exceeded.' \
                if isinstance(bucket_limit, int) else bucket_limit.reason
            raise FileSizeError(description=desc)

        with db.session.begin_nested():
            file_ = FileInstance.create()
            file_.size = size
            obj = cls(
                upload_id=uuid.uuid4(),
                bucket=bucket,
                key=key,
                chunk_size=chunk_size,
                size=size,
                completed=False,
                file=file_,
            )
            bucket.size += size
            db.session.add(obj)
        file_.init_contents(
            size=size,
            default_location=bucket.location.uri,
            default_storage_class=bucket.default_storage_class,
        )
        return obj


Instruction:
Implement a Python 3 function for
fetching a specific multipart object.

Response:
def get(cls, bucket, key, upload_id, with_completed=False):
        """Fetch a specific multipart object."""
        q = cls.query.filter_by(
            upload_id=upload_id,
            bucket_id=as_bucket_id(bucket),
            key=key,
        )
        if not with_completed:
            q = q.filter(cls.completed.is_(False))

        return q.one_or_none()


Instruction:
Can you implement a function in Python 3 that
queries all uncompleted multipart uploads.

Response:
def query_expired(cls, dt, bucket=None):
        """Query all uncompleted multipart uploads."""
        q = cls.query.filter(cls.created < dt).filter_by(completed=True)
        if bucket:
            q = q.filter(cls.bucket_id == as_bucket_id(bucket))
        return q


Instruction:
Create a Python 3 function for
querying all uncompleted multipart uploads.

Response:
def query_by_bucket(cls, bucket):
        """Query all uncompleted multipart uploads."""
        return cls.query.filter(cls.bucket_id == as_bucket_id(bucket))


Instruction:
How would you implement a function in Python 3 that
gets the end byte of the file for this part.

Response:
def end_byte(self):
        """Get end byte in file for this part."""
        return min(
            (self.part_number + 1) * self.multipart.chunk_size,
            self.multipart.size
        )


Instruction:
Write a Python 3 function for
creating a new part object in a multipart object.

Response:
def create(cls, mp, part_number, stream=None, **kwargs):
        """Create a new part object in a multipart object."""
        if part_number < 0 or part_number > mp.last_part_number:
            raise MultipartInvalidPartNumber()

        with db.session.begin_nested():
            obj = cls(
                multipart=mp,
                part_number=part_number,
            )
            db.session.add(obj)
        if stream:
            obj.set_contents(stream, **kwargs)
        return obj


Instruction:
Create a Python 3 function for
getting or create a new instance of the class based on the given part number.

Response:
def get_or_none(cls, mp, part_number):
        """Get part number."""
        return cls.query.filter_by(
            upload_id=mp.upload_id,
            part_number=part_number
        ).one_or_none()


Instruction:
Implement a function in Python 3 to
get or create a part.

Response:
def get_or_create(cls, mp, part_number):
        """Get or create a part."""
        obj = cls.get_or_none(mp, part_number)
        if obj:
            return obj
        return cls.create(mp, part_number)


Instruction:
How would you code a function in Python 3 to
delete a single entry from the database.

Response:
def delete(cls, mp, part_number):
        """Get part number."""
        return cls.query.filter_by(
            upload_id=mp.upload_id,
            part_number=part_number
        ).delete()


Instruction:
How would you code a function in Python 3 to
return a QuerySet of Part objects for a specific multipart upload.

Response:
def query_by_multipart(cls, multipart):
        """Get all parts for a specific multipart upload.

        :param multipart: A :class:`invenio_files_rest.models.MultipartObject`
            instance.
        :returns: A :class:`invenio_files_rest.models.Part` instance.
        """
        upload_id = multipart.upload_id \
            if isinstance(multipart, MultipartObject) else multipart
        return cls.query.filter_by(
            upload_id=upload_id
        )


Instruction:
Can you tell what is the following Python 3 function doing
def set_contents(self, stream, progress_callback=None):
        size, checksum = self.multipart.file.update_contents(
            stream, seek=self.start_byte, size=self.part_size,
            progress_callback=progress_callback,
        )
        self.checksum = checksum
        return self

Response: Save contents of stream to part of file instance.


Instruction:
How would you explain what the following Python 3 function does
def cdn_url(request):
    cdn_url, ssl_url = _get_container_urls(CumulusStorage())
    static_url = settings.STATIC_URL

    return {
        "CDN_URL": cdn_url + static_url,
        "CDN_SSL_URL": ssl_url + static_url,
    }

Response: A context processor that exposes the full CDN URL in templates.


Instruction:
Here you have a function in Python 3, explain what it does
def static_cdn_url(request):
    cdn_url, ssl_url = _get_container_urls(CumulusStaticStorage())
    static_url = settings.STATIC_URL

    return {
        "STATIC_URL": cdn_url + static_url,
        "STATIC_SSL_URL": ssl_url + static_url,
        "LOCAL_STATIC_URL": static_url,
    }

Response: A context processor that exposes the full static CDN URL as static URL in templates.


Instruction:
Implement a Python 3 function for
serializing some parameters to match python native types with formats

Response:
def _serialize_parameters(parameters):
        """Serialize some parameters to match python native types with formats
        specified in google api docs like:
        * True/False -> "true"/"false",
        * {"a": 1, "b":2} -> "a:1|b:2"

        :type parameters: dict oif query parameters
        """

        for key, value in parameters.items():
            if isinstance(value, bool):
                parameters[key] = "true" if value else "false"
            elif isinstance(value, dict):
                parameters[key] = "|".join(
                    ("%s:%s" % (k, v) for k, v in value.items()))
            elif isinstance(value, (list, tuple)):
                parameters[key] = "|".join(value)
        return parameters


Instruction:
Make a summary of the following Python 3 code
def _make_request(self, url, parameters, result_key):
        url = urlparse.urljoin(urlparse.urljoin(self.base, url), "json")

        # drop all None values and use defaults if not set
        parameters = {key: value for key, value in parameters.items() if
                      value is not None}
        parameters.setdefault("sensor", self.sensor)
        parameters = self._serialize_parameters(parameters)
        if self.api_key:
            parameters["key"] = self.api_key

        raw_response = requests.get(url, params=parameters)
        response = raw_response.json()

        if response["status"] == status.OK and result_key is not None:
            return response[result_key]
        elif response["status"] == status.OK:
            del response["status"]
            return response
        else:
            response["url"] = raw_response.url
            raise errors.EXCEPTION_MAPPING.get(
                response["status"],
                errors.GmapException
            )(response)

Response: Make http request to Google API.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def reverse(self, lat, lon, result_type=None, location_type=None,
                language=None, sensor=None):
        """Reverse geocode with given latitude and longitude.

        :param lat: latitude of queried point
        :param lon: longitude of queried point
        :param result_type: list of result_type for filtered search.
             Accepted values:
             https://developers.google.com/maps/documentation/geocoding/intro#Types
             **Important**: this feature may require using API key to work.
        :param location_type: list of location_type for filtered search.
        :param language: the language in which to return results. For full
             list of laguages go to Google Maps API docs
        :param sensor: override default client sensor parameter

        .. note:: Google API allows to specify both latlng and address params
            but it makes no sense and would not reverse geocode your query, so
            here geocoding and reverse geocoding are separated
        """
        parameters = dict(
            latlng="%f,%f" % (lat, lon),
            result_type=result_type,
            location_type=location_type,
            language=language,
            sensor=sensor,
        )
        return self._make_request(self.GEOCODE_URL, parameters, "results")

Response: Reverse geocoding with given latitude and longitude.


Instruction:
How would you explain what the following Python 3 function does
def check_sizelimit(size_limit, bytes_written, total_size):
    if size_limit is not None and bytes_written > size_limit:
        desc = 'File size limit exceeded.' \
            if isinstance(size_limit, int) else size_limit.reason
        raise FileSizeError(description=desc)

    # Never write more than advertised
    if total_size is not None and bytes_written > total_size:
        raise UnexpectedFileSizeError(
            description='File is bigger than expected.')

Response: Checks if the size limit is exceeded.


Instruction:
Explain what the following Python 3 code does
def send_file(self, filename, mimetype=None, restricted=True,
                  checksum=None, trusted=False, chunk_size=None,
                  as_attachment=False):
        """Send the file to the client."""
        try:
            fp = self.open(mode='rb')
        except Exception as e:
            raise StorageError('Could not send file: {}'.format(e))

        try:
            md5_checksum = None
            if checksum:
                algo, value = checksum.split(':')
                if algo == 'md5':
                    md5_checksum = value

            # Send stream is responsible for closing the file.
            return send_stream(
                fp,
                filename,
                self._size,
                self._modified,
                mimetype=mimetype,
                restricted=restricted,
                etag=checksum,
                content_md5=md5_checksum,
                chunk_size=chunk_size,
                trusted=trusted,
                as_attachment=as_attachment,
            )
        except Exception as e:
            fp.close()
            raise StorageError('Could not send file: {}'.format(e))

Response: Send a file to the client.


Instruction:
Write a Python 3 script for
computing checksum of file.

Response:
def checksum(self, chunk_size=None, progress_callback=None, **kwargs):
        """Compute checksum of file."""
        fp = self.open(mode='rb')
        try:
            value = self._compute_checksum(
                fp, size=self._size, chunk_size=None,
                progress_callback=progress_callback)
        except StorageError:
            raise
        finally:
            fp.close()
        return value


Instruction:
How would you code a function in Python 3 to
copy data from another file instance.

Response:
def copy(self, src, chunk_size=None, progress_callback=None):
        """Copy data from another file instance.

        :param src: Source stream.
        :param chunk_size: Chunk size to read from source stream.
        """
        fp = src.open(mode='rb')
        try:
            return self.save(
                fp, chunk_size=chunk_size, progress_callback=progress_callback)
        finally:
            fp.close()


Instruction:
How would you code a function in Python 3 to
compute checksum of a file.

Response:
def _compute_checksum(self, stream, size=None, chunk_size=None,
                          progress_callback=None, **kwargs):
        """Get helper method to compute checksum from a stream.

        Naive implementation that can be overwritten by subclasses in order to
        provide more efficient implementation.
        """
        if progress_callback and size:
            progress_callback = partial(progress_callback, size)
        else:
            progress_callback = None

        try:
            algo, m = self._init_hash()
            return compute_checksum(
                stream, algo, m,
                chunk_size=chunk_size,
                progress_callback=progress_callback,
                **kwargs
            )
        except Exception as e:
            raise StorageError(
                'Could not compute checksum of file: {0}'.format(e))


Instruction:
Can you write a function in Python 3 where it
gets helper to save stream from src to dest + compute checksum. :param src: Source stream. :param dst: Destination stream. :param size: If provided, this exact amount of bytes will be written to the destination file. :param size_limit: ``FileSizeLimit`` instance to limit number of bytes to write.

Response:
def _write_stream(self, src, dst, size=None, size_limit=None,
                      chunk_size=None, progress_callback=None):
        """Get helper to save stream from src to dest + compute checksum.

        :param src: Source stream.
        :param dst: Destination stream.
        :param size: If provided, this exact amount of bytes will be
            written to the destination file.
        :param size_limit: ``FileSizeLimit`` instance to limit number of bytes
            to write.
        """
        chunk_size = chunk_size_or_default(chunk_size)

        algo, m = self._init_hash()
        bytes_written = 0

        while 1:
            # Check that size limits aren't bypassed
            check_sizelimit(size_limit, bytes_written, size)

            chunk = src.read(chunk_size)

            if not chunk:
                if progress_callback:
                    progress_callback(bytes_written, bytes_written)
                break

            dst.write(chunk)

            bytes_written += len(chunk)

            if m:
                m.update(chunk)

            if progress_callback:
                progress_callback(None, bytes_written)

        check_size(bytes_written, size)

        return bytes_written, '{0}:{1}'.format(
            algo, m.hexdigest()) if m else None


Instruction:
Write a Python 3 function for
getting unix time stamp from that given datetime.

Response:
def unixtimestamp(datetime):
    """Get unix time stamp from that given datetime. If datetime
    is not tzaware then it's assumed that it is UTC
    """
    epoch = UTC.localize(datetime.utcfromtimestamp(0))
    if not datetime.tzinfo:
        dt = UTC.localize(datetime)
    else:
        dt = UTC.normalize(datetime)
    delta = dt - epoch
    return total_seconds(delta)


Instruction:
Write a Python 3 script for
getting time offset data for given location.

Response:
def timezone(self, lat, lon, datetime,
                 language=None, sensor=None):
        """Get time offset data for given location.

        :param lat: Latitude of queried point
        :param lon: Longitude of queried point
        :param language: The language in which to return results. For full list
             of laguages go to Google Maps API docs
        :param datetime: Desired time. The Time Zone API uses the timestamp to
             determine whether or not Daylight Savings should be applied.
             datetime should be timezone aware. If it isn't the UTC timezone
             is assumed.
        :type datetime: datetime.datetime
        :param sensor: Override default client sensor parameter
        """

        parameters = dict(
            location="%f,%f" % (lat, lon),
            timestamp=unixtimestamp(datetime),
            language=language,
            sensor=sensor,
        )
        return self._make_request(self.TIMEZONE_URL, parameters, None)


Instruction:
Make a summary of the following Python 3 code
def encode(locations):
    encoded = (
        (_encode_value(lat, prev_lat), _encode_value(lon, prev_lon))
        for (prev_lat, prev_lon), (lat, lon)
        in _iterate_with_previous(locations, first=(0, 0))
    )
    encoded = chain.from_iterable(encoded)
    return ''.join(c for r in encoded for c in r)

Response: Encodes a list of locations into a polyline string.


Instruction:
Can you generate the documentation for the following Python 3 function
def _writable(method):
    @wraps(method)
    def wrapper(self, *args, **kwargs):
        """Send record for indexing.

        :returns: Execution result of the decorated method.

        :raises InvalidOperationError: It occurs when the bucket is locked or
            deleted.
        """
        if self.bucket.locked or self.bucket.deleted:
            raise InvalidOperationError()
        return method(self, *args, **kwargs)
    return wrapper

Response: Decorator for methods that check that record is in defined status.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_version(self, version_id=None):
        return ObjectVersion.get(bucket=self.obj.bucket, key=self.obj.key,
                                 version_id=version_id)

Response: Return specific version of the object.


Instruction:
Make a summary of the following Python 3 code
def dumps(self):
        self.data.update({
            'bucket': str(self.obj.bucket_id),
            'checksum': self.obj.file.checksum,
            'key': self.obj.key,  # IMPORTANT it must stay here!
            'size': self.obj.file.size,
            'version_id': str(self.obj.version_id),
        })
        return self.data

Response: Create a dump of the metadata associated to the record.


Instruction:
Create a Python 3 function for
flushing changes to record.

Response:
def flush(self):
        """Flush changes to record."""
        files = self.dumps()
        # Do not create `_files` when there has not been `_files` field before
        # and the record still has no files attached.
        if files or '_files' in self.record:
            self.record['_files'] = files


Instruction:
Explain what the following Python 3 code does
def sort_by(self, *ids):
        # Support sorting by file_ids or keys.
        files = {str(f_.file_id): f_.key for f_ in self}
        # self.record['_files'] = [{'key': files.get(id_, id_)} for id_ in ids]
        self.filesmap = OrderedDict([
            (files.get(id_, id_), self[files.get(id_, id_)].dumps())
            for id_ in ids
        ])
        self.flush()

Response:
Update files order.

        :param ids: List of ids specifying the final status of the list.


Instruction:
Can you generate the documentation for the following Python 3 function
def rename(self, old_key, new_key):
        assert new_key not in self
        assert old_key != new_key

        file_ = self[old_key]
        old_data = self.filesmap[old_key]

        # Create a new version with the new name
        obj = ObjectVersion.create(
            bucket=self.bucket, key=new_key,
            _file_id=file_.obj.file_id
        )

        # Delete old key
        self.filesmap[new_key] = self.file_cls(obj, old_data).dumps()
        del self[old_key]

        return obj

Response: Rename an object in the object store.


Instruction:
Explain what the following Python 3 code does
def dumps(self, bucket=None):
        return [
            self.file_cls(o, self.filesmap.get(o.key, {})).dumps()
            for o in sorted_files_from_bucket(bucket or self.bucket, self.keys)
        ]

Response: Serialize files from a bucket.


Instruction:
Create a Python 3 function for
getting files iterator. :returns: Files iterator.

Response:
def files(self):
        """Get files iterator.

        :returns: Files iterator.
        """
        if self.model is None:
            raise MissingModelError()

        records_buckets = RecordsBuckets.query.filter_by(
            record_id=self.id).first()

        if not records_buckets:
            bucket = self._create_bucket()
            if not bucket:
                return None
            RecordsBuckets.create(record=self.model, bucket=bucket)
        else:
            bucket = records_buckets.bucket

        return self.files_iter_cls(self, bucket=bucket, file_cls=self.file_cls)


Instruction:
Given the following Python 3 function, write the documentation
def files(self, data):
        current_files = self.files
        if current_files:
            raise RuntimeError('Can not update existing files.')
        for key in data:
            current_files[key] = data[key]

Response: Set files from data.


Instruction:
Explain what the following Python 3 code does
def delete(self, force=False):
        if force:
            RecordsBuckets.query.filter_by(
                record=self.model,
                bucket=self.files.bucket
            ).delete()
        return super(Record, self).delete(force)

Response: Delete a record and also remove the RecordsBuckets if necessary.


Instruction:
Here you have a function in Python 3, explain what it does
def create(cls, record, bucket):
        rb = cls(record=record, bucket=bucket)
        db.session.add(rb)
        return rb

Response: Create a new RecordsBuckets and adds it to the session.


Instruction:
Can you implement a function in Python 3 that
returns files from bucket sorted by given keys.

Response:
def sorted_files_from_bucket(bucket, keys=None):
    """Return files from bucket sorted by given keys.

    :param bucket: :class:`~invenio_files_rest.models.Bucket` containing the
        files.
    :param keys: Keys order to be used.
    :returns: Sorted list of bucket items.
    """
    keys = keys or []
    total = len(keys)
    sortby = dict(zip(keys, range(total)))
    values = ObjectVersion.get_by_bucket(bucket).all()
    return sorted(values, key=lambda x: sortby.get(x.key, total))


Instruction:
Write a Python 3 function for
returning a file from a record.

Response:
def record_file_factory(pid, record, filename):
    """Get file from a record.

    :param pid: Not used. It keeps the function signature.
    :param record: Record which contains the files.
    :param filename: Name of the file to be returned.
    :returns: File object or ``None`` if not found.
    """
    try:
        if not (hasattr(record, 'files') and record.files):
            return None
    except MissingModelError:
        return None

    try:
        return record.files[filename]
    except KeyError:
        return None


Instruction:
Implement a function in Python 3 to
file download view for a given record.

Response:
def file_download_ui(pid, record, _record_file_factory=None, **kwargs):
    """File download view for a given record.

    Plug this method into your ``RECORDS_UI_ENDPOINTS`` configuration:

    .. code-block:: python

        RECORDS_UI_ENDPOINTS = dict(
            recid=dict(
                # ...
                route='/records/<pid_value/files/<filename>',
                view_imp='invenio_records_files.utils:file_download_ui',
                record_class='invenio_records_files.api:Record',
            )
        )

    If ``download`` is passed as a querystring argument, the file is sent as an
    attachment.

    :param pid: The :class:`invenio_pidstore.models.PersistentIdentifier`
        instance.
    :param record: The record metadata.
    """
    _record_file_factory = _record_file_factory or record_file_factory
    # Extract file from record.
    fileobj = _record_file_factory(
        pid, record, kwargs.get('filename')
    )

    if not fileobj:
        abort(404)

    obj = fileobj.obj

    # Check permissions
    ObjectResource.check_object_permission(obj)

    # Send file.
    return ObjectResource.send_object(
        obj.bucket, obj,
        expected_chksum=fileobj.get('checksum'),
        logger_data={
            'bucket_id': obj.bucket_id,
            'pid_type': pid.pid_type,
            'pid_value': pid.pid_value,
        },
        as_attachment=('download' in request.args)
    )


Instruction:
Make a summary of the following Python 3 code
def default_bucket_link_factory(pid):
    try:
        record = Record.get_record(pid.get_assigned_object())
        bucket = record.files.bucket

        return url_for('invenio_files_rest.bucket_api',
                       bucket_id=bucket.id, _external=True)
    except AttributeError:
        return None

Response: Factory for record bucket generation.


Instruction:
Create a Python 3 function for
printing an error message to stdout.

Response:
def flake(self, message):
        """Print an error message to stdout."""
        self.stdout.write(str(message))
        self.stdout.write('\n')


Instruction:
Write a Python 3 script to
return node. id or node. name

Response:
def node_name(node):
    """
        Convenience function: Returns node.id, or node.name, or None
    """
    return hasattr(node, 'id') and node.id or hasattr(node, 'name') and node.name


Instruction:
Can you generate the documentation for the following Python 3 function
def names(self):
        names = []
        if isinstance(self.source, ast.List):
            for node in self.source.elts:
                if isinstance(node, ast.Str):
                    names.append(node.s)
        return names

Response: Return a list of the names referenced by this binding.


Instruction:
Here you have a function in Python 3, explain what it does
def unusedAssignments(self):
        for name, binding in self.items():
            if (not binding.used and name not in self.globals
                    and not self.uses_locals
                    and isinstance(binding, Assignment)):
                yield name, binding

Response: Return a generator for the assignments which have not been used.


Instruction:
Write a Python 3 function that can
collect plugins from entry point frosted. plugins and run their check method passing the filename SetException

Response:
def check_plugins(self):
        """ collect plugins from entry point 'frosted.plugins'

        and run their check() method, passing the filename
        """
        checkers = {}
        for ep in pkg_resources.iter_entry_points(group='frosted.plugins'):
            checkers.update({ep.name: ep.load()})

        for plugin_name, plugin in checkers.items():
            if self.filename != '(none)':
                messages = plugin.check(self.filename)
                for message, loc, args, kwargs in messages:
                    self.report(message, loc, *args, **kwargs)


Instruction:
Can you generate the documentation for the following Python 3 function
def defer_function(self, callable):
        self._deferred_functions.append((callable, self.scope_stack[:], self.offset))

Response: Schedule a function handler to be called just before completion.


Instruction:
Given the following Python 3 function, write the documentation
def defer_assignment(self, callable):
        self._deferred_assignments.append((callable, self.scope_stack[:], self.offset))

Response:
Schedule an assignment handler to be called just after deferred
        function handlers.


Instruction:
Here you have a function in Python 3, explain what it does
def run_deferred(self, deferred):
        for handler, scope, offset in deferred:
            self.scope_stack = scope
            self.offset = offset
            handler()

Response: Run the callables in deferred using their associated scope stack and offset.


Instruction:
Given the following Python 3 function, write the documentation
def check_dead_scopes(self):
        for scope in self.dead_scopes:
            export = isinstance(scope.get('__all__'), ExportBinding)
            if export:
                all = scope['__all__'].names()
                # Look for possible mistakes in the export list
                if not scope.importStarred and os.path.basename(self.filename) != '__init__.py':
                    undefined = set(all) - set(scope)
                    for name in undefined:
                        self.report(messages.UndefinedExport, scope['__all__'].source, name)
            else:
                all = []

            # Look for imported names that aren't used without checking imports in namespace definition
            for importation in scope.values():
                if isinstance(importation, Importation) and not importation.used and importation.name not in all:
                    self.report(messages.UnusedImport, importation.source, importation.name)

Response:
Look at scopes which have been fully examined and report names in
            they which were imported but unused.


Instruction:
Create a Python 3 function for
truing if lnode and rnode are located on different forks of IF or TRY.

Response:
def different_forks(self, lnode, rnode):
        """True, if lnode and rnode are located on different forks of
        IF/TRY."""
        ancestor = self.get_common_ancestor(lnode, rnode)
        if isinstance(ancestor, ast.If):
            for fork in (ancestor.body, ancestor.orelse):
                if self.on_fork(ancestor, lnode, rnode, fork):
                    return True
        elif isinstance(ancestor, ast.Try):
            body = ancestor.body + ancestor.orelse
            for fork in [body] + [[hdl] for hdl in ancestor.handlers]:
                if self.on_fork(ancestor, lnode, rnode, fork):
                    return True
        elif isinstance(ancestor, ast.TryFinally):
            if self.on_fork(ancestor, lnode, rnode, ancestor.body):
                return True
        return False


Instruction:
Explain what the following Python 3 code does
def add_binding(self, node, value, report_redef=True):
        redefinedWhileUnused = False
        if not isinstance(self.scope, ClassScope):
            for scope in self.scope_stack[::-1]:
                existing = scope.get(value.name)
                if (isinstance(existing, Importation)
                        and not existing.used
                        and (not isinstance(value, Importation) or
                             value.fullName == existing.fullName)
                        and report_redef
                        and not self.different_forks(node, existing.source)):
                    redefinedWhileUnused = True
                    self.report(messages.RedefinedWhileUnused,
                                node, value.name, existing.source)

        existing = self.scope.get(value.name)
        if not redefinedWhileUnused and self.has_parent(value.source, ast.ListComp):
            if (existing and report_redef
                    and not self.has_parent(existing.source, (ast.For, ast.ListComp))
                    and not self.different_forks(node, existing.source)):
                self.report(messages.RedefinedInListComp,
                            node, value.name, existing.source)

        if (isinstance(existing, Definition)
                and not existing.used
                and not self.different_forks(node, existing.source)):
            self.report(messages.RedefinedWhileUnused,
                        node, value.name, existing.source)
        else:
            self.scope[value.name] = value

Response: Called when a binding is added to the current scope.


Instruction:
Can you generate the documentation for the following Python 3 function
def is_docstring(self, node):
        return isinstance(node, ast.Str) or (isinstance(node, ast.Expr) and
                                             isinstance(node.value, ast.Str))

Response: Determine if the given node is a docstring.


Instruction:
Explain what the following Python 3 code does
def find_return_with_argument(self, node):
        for item in node.body:
            if isinstance(item, ast.Return) and item.value:
                return item
            elif not isinstance(item, ast.FunctionDef) and hasattr(item, 'body'):
                return_with_argument = self.find_return_with_argument(item)
                if return_with_argument:
                    return return_with_argument

Response: Finds and returns a return statment that has an argument.


Instruction:
Write a Python 3 function that can
check whether a function is a generator by looking for a yield statement or expression.

Response:
def is_generator(self, node):
        """Checks whether a function is a generator by looking for a yield
        statement or expression."""
        if not isinstance(node.body, list):
            # lambdas can not be generators
            return False
        for item in node.body:
            if isinstance(item, (ast.Assign, ast.Expr)):
                if isinstance(item.value, ast.Yield):
                    return True
            elif not isinstance(item, ast.FunctionDef) and hasattr(item, 'body'):
                if self.is_generator(item):
                    return True
        return False


Instruction:
Can you generate the documentation for the following Python 3 function
def GLOBAL(self, node):
        if isinstance(self.scope, FunctionScope):
            self.scope.globals.update(node.names)

Response: Keep track of globals declarations.


Instruction:
Here you have a function in Python 3, explain what it does
def FOR(self, node):
        vars = []

        def collectLoopVars(n):
            if isinstance(n, ast.Name):
                vars.append(n.id)
            elif isinstance(n, ast.expr_context):
                return
            else:
                for c in ast.iter_child_nodes(n):
                    collectLoopVars(c)

        collectLoopVars(node.target)
        for varn in vars:
            if (isinstance(self.scope.get(varn), Importation)
                    # unused ones will get an unused import warning
                    and self.scope[varn].used):
                self.report(messages.ImportShadowedByLoopVar,
                            node, varn, self.scope[varn].source)

        self.handle_children(node)

Response: Process bindings for loop variables.


Instruction:
Create a Python 3 function to
handle occurrence of Name in function or globals.

Response:
def NAME(self, node):
        """Handle occurrence of Name (which can be a load/store/delete
        access.)"""
        # Locate the name in locals / function / globals scopes.
        if isinstance(node.ctx, (ast.Load, ast.AugLoad)):
            self.handle_node_load(node)
            if (node.id == 'locals' and isinstance(self.scope, FunctionScope)
                    and isinstance(node.parent, ast.Call)):
                # we are doing locals() call in current scope
                self.scope.uses_locals = True
        elif isinstance(node.ctx, (ast.Store, ast.AugStore)):
            self.handle_node_store(node)
        elif isinstance(node.ctx, ast.Del):
            self.handle_node_delete(node)
        else:
            # must be a Param context -- this only happens for names in function
            # arguments, but these aren't dispatched through here
            raise RuntimeError("Got impossible expression context: %r" % (node.ctx,))


Instruction:
Explain what the following Python 3 code does
def CLASSDEF(self, node):
        for deco in node.decorator_list:
            self.handleNode(deco, node)
        for baseNode in node.bases:
            self.handleNode(baseNode, node)
        if not PY2:
            for keywordNode in node.keywords:
                self.handleNode(keywordNode, node)
        self.push_scope(ClassScope)
        if self.settings.get('run_doctests', False):
            self.defer_function(lambda: self.handle_doctests(node))
        for stmt in node.body:
            self.handleNode(stmt, node)
        self.pop_scope()
        self.add_binding(node, ClassDefinition(node.name, node))

Response: Check names used in a class definition.


Instruction:
Explain what the following Python 3 code does
def check(codeString, filename, reporter=modReporter.Default, settings_path=None, **setting_overrides):

    if not settings_path and filename:
        settings_path = os.path.dirname(os.path.abspath(filename))
    settings_path = settings_path or os.getcwd()

    active_settings = settings.from_path(settings_path).copy()
    for key, value in itemsview(setting_overrides):
        access_key = key.replace('not_', '').lower()
        if type(active_settings.get(access_key)) in (list, tuple):
            if key.startswith('not_'):
                active_settings[access_key] = list(set(active_settings[access_key]).difference(value))
            else:
                active_settings[access_key] = list(set(active_settings[access_key]).union(value))
        else:
            active_settings[key] = value
    active_settings.update(setting_overrides)

    if _should_skip(filename, active_settings.get('skip', [])):
        if active_settings.get('directly_being_checked', None) == 1:
            reporter.flake(FileSkipped(filename))
            return 1
        elif active_settings.get('verbose', False):
            ignore = active_settings.get('ignore_frosted_errors', [])
            if(not "W200" in ignore and not "W201" in ignore):
                reporter.flake(FileSkipped(filename, None, verbose=active_settings.get('verbose')))
        return 0

    # First, compile into an AST and handle syntax errors.
    try:
        tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
    except SyntaxError:
        value = sys.exc_info()[1]
        msg = value.args[0]

        (lineno, offset, text) = value.lineno, value.offset, value.text

        # If there's an encoding problem with the file, the text is None.
        if text is None:
            # Avoid using msg, since for the only known case, it contains a
            # bogus message that claims the encoding the file declared was
            # unknown.
            reporter.unexpected_error(filename, 'problem decoding source')
        else:
            reporter.flake(PythonSyntaxError(filename, msg, lineno, offset, text,
                                             verbose=active_settings.get('verbose')))
        return 1
    except Exception:
        reporter.unexpected_error(filename, 'problem decoding source')
        return 1
    # Okay, it's syntactically valid.  Now check it.
    w = checker.Checker(tree, filename, None, ignore_lines=_noqa_lines(codeString), **active_settings)
    w.messages.sort(key=lambda m: m.lineno)
    for warning in w.messages:
        reporter.flake(warning)
    return len(w.messages)

Response: Check the Python source given by codeString for unfrosted flakes.


Instruction:
How would you code a function in Python 3 to
check the given path and print out any warnings detected.

Response:
def check_path(filename, reporter=modReporter.Default, settings_path=None, **setting_overrides):
    """Check the given path, printing out any warnings detected."""
    try:
        with open(filename, 'U') as f:
            codestr = f.read() + '\n'
    except UnicodeError:
        reporter.unexpected_error(filename, 'problem decoding source')
        return 1
    except IOError:
        msg = sys.exc_info()[1]
        reporter.unexpected_error(filename, msg.args[1])
        return 1
    return check(codestr, filename, reporter, settings_path, **setting_overrides)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def check_recursive(paths, reporter=modReporter.Default, settings_path=None, **setting_overrides):
    warnings = 0
    for source_path in iter_source_code(paths):
        warnings += check_path(source_path, reporter, settings_path=None, **setting_overrides)
    return warnings

Response: Recursively check all source files defined in paths.


Instruction:
How would you explain what the following Python 3 function does
def get_files_from_storage(paths):
        for path in paths:
            f = default_storage.open(path)
            f.name = os.path.basename(path)
            try:
                yield f
            except ClientError:
                logger.exception("File not found: %s", path)

Response: Return S3 files where the name does not include the path.


Instruction:
How would you implement a function in Python 3 that
echoes the user message.

Response:
def echo(bot, update):
    """Echo the user message."""
    message = update.get_effective_message()
    bot.reply(update, message)


Instruction:
Can you generate the documentation for the following Python 3 function
def error(bot, update, error):
    logger.error('Update {} caused error {}'.format(update, error), extra={"tag": "err"})

Response: Log Errors caused by Updates.


Instruction:
Given the following Python 3 function, write the documentation
def server_from_config(config=None, server_class=None, additional_kwargs=None):
    global global_config
    if not config:
        config = global_config

    queue_store_factory = resolve_name(config.get('coilmq', 'qstore.factory'))
    subscriber_scheduler_factory = resolve_name(config.get(
            'coilmq', 'scheduler.subscriber_priority_factory'))
    queue_scheduler_factory = resolve_name(config.get(
            'coilmq', 'scheduler.queue_priority_factory'))

    if config.has_option('coilmq', 'auth.factory'):
        authenticator_factory = resolve_name(
                config.get('coilmq', 'auth.factory'))
        authenticator = authenticator_factory()
    else:
        authenticator = None

    server = ThreadedStompServer((config.get('coilmq', 'listen_addr'), config.getint('coilmq', 'listen_port')),
                                 queue_manager=QueueManager(store=queue_store_factory(),
                                                            subscriber_scheduler=subscriber_scheduler_factory(),
                                                            queue_scheduler=queue_scheduler_factory()),
                                 topic_manager=TopicManager(),
                                 authenticator=authenticator,
                                 protocol=STOMP11)
    logger.info("Created server:%r" % server)
    return server

Response: Returns a configured StompServer from a config file.


Instruction:
Write a Python 3 script for
starting a server within a context.

Response:
def context_serve(context, configfile, listen_addr, listen_port, logfile,
                  debug, daemon, uid, gid, pidfile, umask, rundir):
    """
    Takes a context object, which implements the __enter__/__exit__ "with" interface
    and starts a server within that context.

    This method is a refactored single-place for handling the server-run code whether
    running in daemon or non-daemon mode.  It is invoked with a dummy (passthrough)
    context object for the non-daemon use case.

    @param options: The compiled collection of options that need to be parsed.
    @type options: C{ConfigParser}

    @param context: The context object that implements __enter__/__exit__ "with" methods.
    @type context: C{object}

    @raise Exception: Any underlying exception will be logged but then re-raised.
    @see: server_from_config()
    """
    global global_config

    server = None
    try:
        with context:
            # There's a possibility here that init_logging() will throw an exception.  If it does,
            # AND we're in a daemon context, then we're not going to be able to do anything with it.
            # We've got no stderr/stdout here; and so (to my knowledge) no reliable (& cross-platform),
            # way to display errors.
            level = logging.DEBUG if debug else logging.INFO
            init_logging(logfile=logfile, loglevel=level,
                         configfile=configfile)

            server = server_from_config()
            logger.info("Stomp server listening on %s:%s" % server.server_address)

            if debug:
                poll_interval = float(global_config.get(
                        'coilmq', 'debug.stats_poll_interval'))
                if poll_interval:  # Setting poll_interval to 0 effectively disables it.
                    def diagnostic_loop(server):
                        log = logger
                        while True:
                            log.debug(
                                    "Stats heartbeat -------------------------------")
                            store = server.queue_manager.store
                            for dest in store.destinations():
                                log.debug("Queue %s: size=%s, subscribers=%s" % (
                                    dest, store.size(dest), server.queue_manager.subscriber_count(dest)))

                            # TODO: Add number of subscribers?

                            time.sleep(poll_interval)

                    diagnostic_thread = threading.Thread(
                            target=diagnostic_loop, name='DiagnosticThread', args=(server,))
                    diagnostic_thread.daemon = True
                    diagnostic_thread.start()

            server.serve_forever()

    except (KeyboardInterrupt, SystemExit):
        logger.info("Stomp server stopped by user interrupt.")
        raise SystemExit()
    except Exception as e:
        logger.error("Stomp server stopped due to error: %s" % e)
        logger.exception(e)
        raise SystemExit()
    finally:
        if server:
            server.server_close()


Instruction:
Make a summary of the following Python 3 code
def main(config, host, port, logfile, debug, daemon, uid, gid, pidfile, umask, rundir):

    _main(**locals())

Response: This is the main function for the socket server.


Instruction:
Write a Python 3 script to
return a SQLAlchemy queue store

Response:
def make_sa():
    """
    Factory to creates a SQLAlchemy queue store, pulling config values from the CoilMQ configuration.
    """
    configuration = dict(config.items('coilmq'))
    engine = engine_from_config(configuration, 'qstore.sqlalchemy.')
    init_model(engine)
    store = SAQueue()
    return store


Instruction:
Make a summary of the following Python 3 code
def init_model(engine, create=True, drop=False):
    meta.engine = engine
    meta.metadata = MetaData(bind=meta.engine)
    meta.Session = scoped_session(sessionmaker(bind=meta.engine))
    model.setup_tables(create=create, drop=drop)

Response: Initializes the shared SQLAlchemy model.


Instruction:
How would you explain what the following Python 3 function does
def enqueue(self, destination, frame):
        session = meta.Session()
        message_id = frame.headers.get('message-id')
        if not message_id:
            raise ValueError("Cannot queue a frame without message-id set.")
        ins = model.frames_table.insert().values(
            message_id=message_id, destination=destination, frame=frame)
        session.execute(ins)
        session.commit()

Response: Enqueue a message to the queue for this destination.


Instruction:
How would you implement a function in Python 3 that
removes and returns an item from the queue.

Response:
def dequeue(self, destination):
        """
        Removes and returns an item from the queue (or C{None} if no items in queue).

        @param destination: The queue name (destinationination).
        @type destination: C{str}

        @return: The first frame in the specified queue, or C{None} if there are none.
        @rtype: C{stompclient.frame.Frame}
        """
        session = meta.Session()

        try:

            selstmt = select(
                [model.frames_table.c.message_id, model.frames_table.c.frame])
            selstmt = selstmt.where(
                model.frames_table.c.destination == destination)
            selstmt = selstmt.order_by(
                model.frames_table.c.queued, model.frames_table.c.sequence)

            result = session.execute(selstmt)

            first = result.fetchone()
            if not first:
                return None

            delstmt = model.frames_table.delete().where(model.frames_table.c.message_id ==
                                                        first[model.frames_table.c.message_id])
            session.execute(delstmt)

            frame = first[model.frames_table.c.frame]

        except:
            session.rollback()
            raise
        else:
            session.commit()
            return frame


Instruction:
Create a Python 3 function for
returning C { bool } if the specified queue has any frames in the specified queue.

Response:
def has_frames(self, destination):
        """
        Whether specified queue has any frames.

        @param destination: The queue name (destinationination).
        @type destination: C{str}

        @return: Whether there are any frames in the specified queue.
        @rtype: C{bool}
        """
        session = meta.Session()
        sel = select([model.frames_table.c.message_id]).where(
            model.frames_table.c.destination == destination)
        result = session.execute(sel)

        first = result.fetchone()
        return first is not None


Instruction:
Create a Python 3 function for
returning the number of frames in the specified queue.

Response:
def size(self, destination):
        """
        Size of the queue for specified destination.

        @param destination: The queue destination (e.g. /queue/foo)
        @type destination: C{str}

        @return: The number of frames in specified queue.
        @rtype: C{int}
        """
        session = meta.Session()
        sel = select([func.count(model.frames_table.c.message_id)]).where(
            model.frames_table.c.destination == destination)
        result = session.execute(sel)
        first = result.fetchone()
        if not first:
            return 0
        else:
            return int(first[0])


Instruction:
Implement a function in Python 3 to
provide a list of destinations available.

Response:
def destinations(self):
        """
        Provides a list of destinations (queue "addresses") available.

        @return: A list of the detinations available.
        @rtype: C{set}
        """
        session = meta.Session()
        sel = select([distinct(model.frames_table.c.destination)])
        result = session.execute(sel)
        return set([r[0] for r in result.fetchall()])


Instruction:
Can you generate a brief explanation for the following Python 3 code
def setup_tables(create=True, drop=False):
    global frames_table
    frames_table = Table('frames', meta.metadata,
                         Column('message_id', String(255), primary_key=True),
                         Column('sequence', BigInteger,
                                primary_key=False, autoincrement=True),
                         Column('destination', String(255), index=True),
                         Column('frame', PickleType),
                         Column('queued', DateTime, default=func.now()))

    if drop:
        meta.metadata.drop_all()

    if drop or create:
        meta.metadata.create_all()

Response: Create and drop the tables in the database.


Instruction:
Can you tell what is the following Python 3 function doing
def subscribe(self, connection, destination):
        self.log.debug("Subscribing %s to %s" % (connection, destination))
        self._topics[destination].add(connection)

Response: Subscribes a connection to the specified topic destination.


Instruction:
Explain what the following Python 3 code does
def unsubscribe(self, connection, destination):
        self.log.debug("Unsubscribing %s from %s" % (connection, destination))
        if connection in self._topics[destination]:
            self._topics[destination].remove(connection)

        if not self._topics[destination]:
            del self._topics[destination]

Response: Unsubscribes a connection from the specified topic destination.


Instruction:
Can you create a Python 3 function that
disconnects a subscriber connection from all topics.

Response:
def disconnect(self, connection):
        """
        Removes a subscriber connection.

        @param connection: The client connection to unsubscribe.
        @type connection: L{coilmq.server.StompConnection}
        """
        self.log.debug("Disconnecting %s" % connection)
        for dest in list(self._topics.keys()):
            if connection in self._topics[dest]:
                self._topics[dest].remove(connection)
            if not self._topics[dest]:
                # This won't trigger RuntimeError, since we're using keys()
                del self._topics[dest]


Instruction:
Write a Python 3 script for
sending a message to all subscribers of destination.

Response:
def send(self, message):
        """
        Sends a message to all subscribers of destination.

        @param message: The message frame.  (The frame will be modified to set command
                            to MESSAGE and set a message id.)
        @type message: L{stompclient.frame.Frame}
        """
        dest = message.headers.get('destination')
        if not dest:
            raise ValueError(
                "Cannot send frame with no destination: %s" % message)

        message.cmd = 'message'

        message.headers.setdefault('message-id', str(uuid.uuid4()))

        bad_subscribers = set()
        for subscriber in self._topics[dest]:
            try:
                subscriber.send_frame(message)
            except:
                self.log.exception(
                    "Error delivering message to subscriber %s; client will be disconnected." % subscriber)
                # We queue for deletion so we are not modifying the topics dict
                # while iterating over it.
                bad_subscribers.add(subscriber)

        for subscriber in bad_subscribers:
            self.disconnect(subscriber)


Instruction:
Explain what the following Python 3 code does
def unbind(self):
        self.connected = False
        self.queue_manager.disconnect(self.connection)
        self.topic_manager.disconnect(self.connection)

Response: Unbinds this connection from queue and topic managers.