Instruction:
How would you explain what the following Python 3 function does
def list_objects(self, path='', relative=False, first_level=False,
                     max_request_entries=None):
        """
        List objects.

        Args:
            path (str): Path or URL.
            relative (bool): Path is relative to current root.
            first_level (bool): It True, returns only first level objects.
                Else, returns full tree.
            max_request_entries (int): If specified, maximum entries returned
                by request.

        Returns:
            generator of tuple: object name str, object header dict
        """
        entries = 0
        max_request_entries_arg = None

        if not relative:
            path = self.relpath(path)

        # From root
        if not path:
            locators = self._list_locators()

            # Yields locators
            if first_level:
                for locator in locators:

                    entries += 1
                    yield locator
                    if entries == max_request_entries:
                        return
                return

            # Yields each locator objects
            for loc_path, loc_header in locators:

                # Yields locator itself
                loc_path = loc_path.strip('/')

                entries += 1
                yield loc_path, loc_header
                if entries == max_request_entries:
                    return

                # Yields locator content is read access to it
                if max_request_entries is not None:
                    max_request_entries_arg = max_request_entries - entries
                try:
                    for obj_path, obj_header in self._list_objects(
                            self.get_client_kwargs(loc_path), '',
                            max_request_entries_arg):

                        entries += 1
                        yield ('/'.join((loc_path, obj_path.lstrip('/'))),
                               obj_header)
                        if entries == max_request_entries:
                            return

                except ObjectPermissionError:
                    # No read access to locator
                    continue
            return

        # From locator or sub directory
        locator, path = self.split_locator(path)

        if first_level:
            seen = set()

        if max_request_entries is not None:
            max_request_entries_arg = max_request_entries - entries

        for obj_path, header in self._list_objects(
                self.get_client_kwargs(locator), path, max_request_entries_arg):

            if path:
                try:
                    obj_path = obj_path.split(path, 1)[1]
                except IndexError:
                    # Not sub path of path
                    continue
            obj_path = obj_path.lstrip('/')

            # Skips parent directory
            if not obj_path:
                continue

            # Yields first level locator objects only
            if first_level:
                # Directory
                try:
                    obj_path, _ = obj_path.strip('/').split('/', 1)
                    obj_path += '/'

                    # Avoids to use the header of the object instead of the
                    # non existing header of the directory that only exists
                    # virtually in object path.
                    header = dict()

                # File
                except ValueError:
                    pass

                if obj_path not in seen:
                    entries += 1
                    yield obj_path, header
                    if entries == max_request_entries:
                        return
                    seen.add(obj_path)

            # Yields locator objects
            else:
                entries += 1
                yield obj_path, header
                if entries == max_request_entries:
                    return

Response: A generator function that returns all objects in the specified path.


Instruction:
Can you tell what is the following Python 3 function doing
def stat(self, path=None, client_kwargs=None, header=None):
        # Should contain at least the strict minimum of os.stat_result
        stat = OrderedDict((
            ("st_mode", 0), ("st_ino", 0), ("st_dev", 0), ("st_nlink", 0),
            ("st_uid", 0), ("st_gid", 0), ("st_size", 0), ("st_atime", 0),
            ("st_mtime", 0), ("st_ctime", 0)))

        # Populate standard os.stat_result values with object header content
        header = self.head(path, client_kwargs, header)
        for key, method in (
                ('st_size', self._getsize_from_header),
                ('st_ctime', self._getctime_from_header),
                ('st_mtime', self._getmtime_from_header),):
            try:
                stat[key] = int(method(header))
            except UnsupportedOperation:
                continue

        # File mode
        if self.islink(path=path, header=header):
            # Symlink
            stat['st_mode'] = S_IFLNK
        elif ((not path or path[-1] == '/' or self.is_locator(path)) and not
                stat['st_size']):
            # Directory
            stat['st_mode'] = S_IFDIR
        else:
            # File
            stat['st_mode'] = S_IFREG

        # Add storage specific keys
        sub = self._CHAR_FILTER.sub
        for key, value in tuple(header.items()):
            stat['st_' + sub('', key.lower())] = value

        # Convert to "os.stat_result" like object
        stat_result = namedtuple('stat_result', tuple(stat))
        stat_result.__name__ = 'os.stat_result'
        stat_result.__module__ = 'pycosio'
        return stat_result(**stat)

Response: Returns os. stat_result object.


Instruction:
Implement a Python 3 function for
initializing a connection. Create tables if requested.

Response:
def init(uri=None, alembic_ini=None, engine=None, create=False):
    """ Initialize a connection.  Create tables if requested."""

    if uri and engine:
        raise ValueError("uri and engine cannot both be specified")

    if uri is None and not engine:
        uri = 'sqlite:////tmp/datanommer.db'
        log.warning("No db uri given.  Using %r" % uri)

    if uri and not engine:
        engine = create_engine(uri)

    if 'sqlite' in engine.driver:
        # Enable nested transaction support under SQLite
        # See https://stackoverflow.com/questions/1654857/nested-transactions-with-sqlalchemy-and-sqlite
        @event.listens_for(engine, "connect")
        def do_connect(dbapi_connection, connection_record):
            # disable pysqlite's emitting of the BEGIN statement entirely.
            # also stops it from emitting COMMIT before any DDL.
            dbapi_connection.isolation_level = None

        @event.listens_for(engine, "begin")
        def do_begin(conn):
            # emit our own BEGIN
            conn.execute("BEGIN")

    # We need to hang our own attribute on the sqlalchemy session to stop
    # ourselves from initializing twice.  That is only a problem is the code
    # calling us isn't consistent.
    if getattr(session, '_datanommer_initialized', None):
        log.warning("Session already initialized.  Bailing")
        return
    session._datanommer_initialized = True

    session.configure(bind=engine)
    DeclarativeBase.query = session.query_property()

    # Loads the alembic configuration and generates the version table, with
    # the most recent revision stamped as head
    if alembic_ini is not None:
        from alembic.config import Config
        from alembic import command
        alembic_cfg = Config(alembic_ini)
        command.stamp(alembic_cfg, "head")

    if create:
        DeclarativeBase.metadata.create_all(engine)


Instruction:
Can you write a function in Python 3 where it
takes a dict - like fedmsg envelope and store the headers and message in the table.

Response:
def add(envelope):
    """ Take a dict-like fedmsg envelope and store the headers and message
        in the table.
    """
    message = envelope['body']
    timestamp = message.get('timestamp', None)
    try:
        if timestamp:
            timestamp = datetime.datetime.utcfromtimestamp(timestamp)
        else:
            timestamp = datetime.datetime.utcnow()
    except Exception:
        pass

    headers = envelope.get('headers', None)
    msg_id = message.get('msg_id', None)
    if not msg_id and headers:
        msg_id = headers.get('message-id', None)
    if not msg_id:
        msg_id = six.text_type(timestamp.year) + six.u('-') + six.text_type(uuid.uuid4())
    obj = Message(
        i=message.get('i', 0),
        msg_id=msg_id,
        topic=message['topic'],
        timestamp=timestamp,
        username=message.get('username', None),
        crypto=message.get('crypto', None),
        certificate=message.get('certificate', None),
        signature=message.get('signature', None),
    )

    obj.msg = message['msg']
    obj.headers = headers

    try:
        session.add(obj)
        session.flush()
    except IntegrityError:
        log.warning('Skipping message from %s with duplicate id: %s',
                    message['topic'], msg_id)
        session.rollback()
        return

    usernames = fedmsg.meta.msg2usernames(message)
    packages = fedmsg.meta.msg2packages(message)

    # Do a little sanity checking on fedmsg.meta results
    if None in usernames:
        # Notify developers so they can fix msg2usernames
        log.error('NoneType found in usernames of %r' % msg_id)
        # And prune out the bad value
        usernames = [name for name in usernames if name is not None]

    if None in packages:
        # Notify developers so they can fix msg2packages
        log.error('NoneType found in packages of %r' % msg_id)
        # And prune out the bad value
        packages = [pkg for pkg in packages if pkg is not None]

    # If we've never seen one of these users before, then:
    # 1) make sure they exist in the db (create them if necessary)
    # 2) mark an in memory cache so we can remember that they exist without
    #    having to hit the db.
    for username in usernames:
        if username not in _users_seen:
            # Create the user in the DB if necessary
            User.get_or_create(username)
            # Then just mark an in memory cache noting that we've seen them.
            _users_seen.add(username)

    for package in packages:
        if package not in _packages_seen:
            Package.get_or_create(package)
            _packages_seen.add(package)

    session.flush()

    # These two blocks would normally be a simple "obj.users.append(user)" kind
    # of statement, but here we drop down out of sqlalchemy's ORM and into the
    # sql abstraction in order to gain a little performance boost.
    values = [{'username': username, 'msg': obj.id} for username in usernames]
    if values:
        session.execute(user_assoc_table.insert(), values)

    values = [{'package': package, 'msg': obj.id} for package in packages]
    if values:
        session.execute(pack_assoc_table.insert(), values)

    # TODO -- can we avoid committing every time?
    session.flush()
    session.commit()


Instruction:
Given the following Python 3 function, write the documentation
def get_or_create(cls, name):
        obj = cls.query.filter_by(name=name).one_or_none()
        if obj:
            return obj
        try:
            with session.begin_nested():
                obj = cls(name=name)
                session.add(obj)
                session.flush()
            return obj
        except IntegrityError:
            log.debug('Collision when adding %s(name="%s"), returning existing object',
                      cls.__name__, name)
            return cls.query.filter_by(name=name).one()

Response: Get the instance of the class with the specified name. If it doesn t exist create it.


Instruction:
Explain what the following Python 3 code does
def isdir(path):
    system = get_instance(path)

    # User may use directory path without trailing '/'
    # like on standard file systems
    return system.isdir(system.ensure_dir_path(path))

Response: Returns True if path is an existing directory.


Instruction:
Make a summary of the following Python 3 code
def relpath(path, start=None):
    relative = get_instance(path).relpath(path)
    if start:
        # Storage relative path
        # Replaces "\" by "/" for Windows.
        return os_path_relpath(relative, start=start).replace('\\', '/')
    return relative

Response: Returns a relative file path to path either from the current directory or from a start directory.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def samefile(path1, path2):
    # Handles path-like objects and checks if storage
    path1, path1_is_storage = format_and_is_storage(path1)
    path2, path2_is_storage = format_and_is_storage(path2)

    # Local files: Redirects to "os.path.samefile"
    if not path1_is_storage and not path2_is_storage:
        return os_path_samefile(path1, path2)

    # One path is local, the other storage
    if not path1_is_storage or not path2_is_storage:
        return False

    with handle_os_exceptions():
        # Paths don't use same storage
        system = get_instance(path1)
        if system is not get_instance(path2):
            return False

        # Relative path are different
        elif system.relpath(path1) != system.relpath(path2):
            return False

    # Same files
    return True

Response: Returns True if both pathname arguments refer to the same file or directory.


Instruction:
Create a Python 3 function to
split the path into a pair of drive tail.

Response:
def splitdrive(path):
    """
    Split the path into a pair (drive, tail) where drive is either a
    mount point or the empty string. On systems which do not use drive
    specifications, drive will always be the empty string.

    In all cases, drive + tail will be the same as path.

    Equivalent to "os.path.splitdrive".

    Args:
        path (path-like object): Path or URL.

    Returns:
        tuple of str: drive, tail.
    """
    relative = get_instance(path).relpath(path)
    drive = path.rsplit(relative, 1)[0]
    if drive and not drive[-2:] == '//':
        # Keep "/" tail side
        relative = '/' + relative
        drive = drive.rstrip('/')
    return drive, relative


Instruction:
Can you implement a function in Python 3 that
handles pycosio exceptions and raises standard OS exceptions.

Response:
def handle_os_exceptions():
    """
    Handles pycosio exceptions and raise standard OS exceptions.
    """
    try:
        yield

    # Convert pycosio exception to equivalent OSError
    except ObjectException:
        exc_type, exc_value, _ = exc_info()
        raise _OS_EXCEPTIONS.get(exc_type, OSError)(exc_value)

    # Re-raise generic exceptions
    except (OSError, same_file_error, UnsupportedOperation):
        raise

    # Raise generic OSError for other exceptions
    except Exception:
        exc_type, exc_value, _ = exc_info()
        raise OSError('%s%s' % (
            exc_type, (', %s' % exc_value) if exc_value else ''))


Instruction:
Implement a function in Python 3 to
return a list containing the names of the entries in the directory given by path.

Response:
def listdir(path='.'):
    """
    Return a list containing the names of the entries in the directory given by
    path.

    Equivalent to "os.listdir".

    Args:
        path (path-like object): Path or URL.

    Returns:
        list of str: Entries names.
    """
    return [name.rstrip('/') for name, _ in
            get_instance(path).list_objects(path, first_level=True)]


Instruction:
Create a Python 3 function for
creating a directory and all intermediate ones.

Response:
def makedirs(name, mode=0o777, exist_ok=False):
    """
    Super-mkdir; create a leaf directory and all intermediate ones.
    Works like mkdir, except that any intermediate path segment
    (not just the rightmost) will be created if it does not exist.

    Equivalent to "os.makedirs".

    Args:
        name (path-like object): Path or URL.
        mode (int): The mode parameter is passed to os.mkdir();
            see the os.mkdir() description for how it is interpreted.
            Not supported on cloud storage objects.
        exist_ok (bool): Don't raises error if target directory already
            exists.

    Raises:
        FileExistsError: if exist_ok is False and if the target directory
            already exists.
    """
    system = get_instance(name)

    # Checks if directory not already exists
    if not exist_ok and system.isdir(system.ensure_dir_path(name)):
        raise ObjectExistsError("File exists: '%s'" % name)

    # Create directory
    system.make_dir(name)


Instruction:
Can you tell what is the following Python 3 function doing
def mkdir(path, mode=0o777, dir_fd=None):
    system = get_instance(path)
    relative = system.relpath(path)

    # Checks if parent directory exists
    parent_dir = dirname(relative.rstrip('/'))
    if parent_dir:
        parent = path.rsplit(relative, 1)[0] + parent_dir + '/'
        if not system.isdir(parent):
            raise ObjectNotFoundError(
                "No such file or directory: '%s'" % parent)

    # Checks if directory not already exists
    if system.isdir(system.ensure_dir_path(path)):
        raise ObjectExistsError("File exists: '%s'" % path)

    # Create directory
    system.make_dir(relative, relative=True)

Response: Creates a directory named path with numeric mode mode.


Instruction:
Write a Python 3 script to
remove a file. Equivalent to "os.remove" and "os.unlink". Args: path (path-like object): Path or URL. dir_fd: directory descriptors; see the os.remove() description for how it is interpreted. Not supported on cloud storage objects.

Response:
def remove(path, dir_fd=None):
    """
    Remove a file.

    Equivalent to "os.remove" and "os.unlink".

    Args:
        path (path-like object): Path or URL.
        dir_fd: directory descriptors;
            see the os.remove() description for how it is interpreted.
            Not supported on cloud storage objects.
    """
    system = get_instance(path)

    # Only support files
    if system.is_locator(path) or path[-1] == '/':
        raise is_a_directory_error("Is a directory: '%s'" % path)

    # Remove
    system.remove(path)


Instruction:
Can you create a Python 3 function that
removes a directory. Equivalent to "os.rmdir". Args: path (path-like object): Path or URL. dir_fd: directory descriptors; see the os.rmdir() description for how it is interpreted. Not supported on cloud storage objects.

Response:
def rmdir(path, dir_fd=None):
    """
    Remove a directory.

    Equivalent to "os.rmdir".

    Args:
        path (path-like object): Path or URL.
        dir_fd: directory descriptors;
            see the os.rmdir() description for how it is interpreted.
            Not supported on cloud storage objects.
    """
    system = get_instance(path)
    system.remove(system.ensure_dir_path(path))


Instruction:
Implement a function in Python 3 to
return an iterator of os. DirEntry objects corresponding to the entries in the directory given by path.

Response:
def scandir(path='.'):
    """
    Return an iterator of os.DirEntry objects corresponding to the entries in
    the directory given by path. The entries are yielded in arbitrary order,
    and the special entries '.' and '..' are not included.

    Equivalent to "os.scandir".

    Args:
        path (path-like object): Path or URL.
             If path is of type bytes (directly or indirectly through the
             PathLike interface), the type of the name and path attributes
             of each os.DirEntry will be bytes; in all other circumstances,
             they will be of type str.

    Returns:
        Generator of os.DirEntry: Entries information.
    """
    # Handles path-like objects
    scandir_path = fsdecode(path).replace('\\', '/')

    if not is_storage(scandir_path):
        return os_scandir(scandir_path)

    return _scandir_generator(
        is_bytes=isinstance(fspath(path), (bytes, bytearray)),
        scandir_path=scandir_path, system=get_instance(scandir_path))


Instruction:
Implement a function in Python 3 to
return the base filename of the entry.

Response:
def name(self):
        """
        The entry’s base filename, relative to the scandir() path argument.

        Returns:
            str: name.
        """
        name = self._name.rstrip('/')
        if self._bytes_path:
            name = fsencode(name)
        return name


Instruction:
Create a Python 3 function for
returning the full path name of the entry.

Response:
def path(self):
        """
        The entry’s full path name:
        equivalent to os.path.join(scandir_path, entry.name)
        where scandir_path is the scandir() path argument.

        The path is only absolute if the scandir() path argument was absolute.

        Returns:
            str: name.
        """
        path = self._path.rstrip('/')
        if self._bytes_path:
            path = fsencode(path)
        return path


Instruction:
Create a Python 3 function to
return True if this entry is a file.

Response:
def is_file(self, follow_symlinks=True):
        """
        Return True if this entry is a file or a symbolic link pointing to a
        file; return False if the entry is or points to a directory or other
        non-file entry, or if it doesn’t exist anymore.

        The result is cached on the os.DirEntry object.

        Args:
            follow_symlinks (bool): Follow symlinks.
                Not supported on cloud storage objects.

        Returns:
            bool: True if directory exists.
        """
        return self._system.isfile(
            path=self._path, client_kwargs=self._client_kwargs)


Instruction:
Explain what the following Python 3 code does
def stat(self, follow_symlinks=True):
        return self._system.stat(
            path=self._path, client_kwargs=self._client_kwargs,
            header=self._header)

Response: Return a stat_result object for this entry.


Instruction:
Can you generate the documentation for the following Python 3 function
def close(self):
        if self._writable and not self._closed:
            self._closed = True
            with self._seek_lock:
                self._flush_raw_or_buffered()
            if self._seek:
                with handle_os_exceptions():
                    self._close_writable()

Response:
Flush the write buffers of the stream if applicable and
        close the object.


Instruction:
Can you tell what is the following Python 3 function doing
def flush(self):
        if self._writable:
            with self._seek_lock:
                self._flush_raw_or_buffered()

                # Clear the buffer
                self._write_buffer = bytearray(self._buffer_size)
                self._buffer_seek = 0

Response: Flush the write buffers of the stream if applicable.


Instruction:
Write a Python 3 script to
flush raw or buffered data.

Response:
def _flush_raw_or_buffered(self):
        """
        Flush using raw of buffered methods.
        """
        # Flush only if bytes written
        # This avoid no required process/thread
        # creation and network call.
        # This step is performed by raw stream.
        if self._buffer_seek and self._seek:
            self._seek += 1
            with handle_os_exceptions():
                self._flush()

        # If data lower than buffer size
        # flush data with raw stream to reduce IO calls
        elif self._buffer_seek:
            self._raw._write_buffer = self._get_buffer()
            self._raw._seek = self._buffer_seek
            self._raw.flush()


Instruction:
Here you have a function in Python 3, explain what it does
def peek(self, size=-1):
        if not self._readable:
            raise UnsupportedOperation('read')

        with self._seek_lock:
            self._raw.seek(self._seek)
            return self._raw._peek(size)

Response: Reads the next size bytes from the stream without advancing the position.


Instruction:
Write a Python 3 function for
reading up to size bytes from the underlying raw stream and returns up to size bytes.

Response:
def read(self, size=-1):
        """
        Read and return up to size bytes,
        with at most one call to the underlying raw stream’s.

        Use at most one call to the underlying raw stream’s read method.

        Args:
            size (int): Number of bytes to read. -1 to read the
                stream until end.

        Returns:
            bytes: Object content
        """
        if not self._readable:
            raise UnsupportedOperation('read')

        # Checks if EOF
        if self._seek == self._size:
            return b''

        # Returns existing buffer with no copy
        if size == self._buffer_size:
            queue_index = self._seek

            # Starts initial preloading on first call
            if queue_index == 0:
                self._preload_range()

            # Get buffer from future
            with handle_os_exceptions():
                buffer = self._read_queue.pop(queue_index).result()

            # Append another buffer preload at end of queue
            buffer_size = self._buffer_size
            index = queue_index + buffer_size * self._max_buffers
            if index < self._size:
                self._read_queue[index] = self._workers.submit(
                    self._read_range, index, index + buffer_size)

            # Update seek
                self._seek += buffer_size
            else:
                self._seek = self._size

            return buffer

        # Uses a prealocated buffer
        if size != -1:
            buffer = bytearray(size)

        # Uses a mutable buffer
        else:
            buffer = bytearray()

        read_size = self.readinto(buffer)
        return memoryview(buffer)[:read_size].tobytes()


Instruction:
Can you tell what is the following Python 3 function doing
def readinto(self, b):
        if not self._readable:
            raise UnsupportedOperation('read')

        with self._seek_lock:
            # Gets seek
            seek = self._seek

            # Initializes queue
            queue = self._read_queue
            if seek == 0:
                # Starts initial preloading on first call
                self._preload_range()

            # Initializes read data buffer
            size = len(b)
            if size:
                # Preallocated buffer:
                # Use memory view to avoid copies
                b_view = memoryview(b)
                size_left = size
            else:
                # Dynamic buffer:
                # Can't avoid copy, read until EOF
                b_view = b
                size_left = -1
            b_end = 0

            # Starts reading
            buffer_size = self._buffer_size
            while size_left > 0 or size_left == -1:

                # Finds buffer position in queue and buffer seek
                start = seek % buffer_size
                queue_index = seek - start

                # Gets preloaded buffer
                try:
                    buffer = queue[queue_index]
                except KeyError:
                    # EOF
                    break

                # Get buffer from future
                with handle_os_exceptions():
                    try:
                        queue[queue_index] = buffer = buffer.result()

                    # Already evaluated
                    except AttributeError:
                        pass
                buffer_view = memoryview(buffer)
                data_size = len(buffer)

                # Checks if end of file reached
                if not data_size:
                    break

                # Gets theoretical range to copy
                if size_left != -1:
                    end = start + size_left
                else:
                    end = data_size - start

                # Checks for end of buffer
                if end >= data_size:
                    # Adjusts range to copy
                    end = data_size

                    # Removes consumed buffer from queue
                    del queue[queue_index]

                    # Append another buffer preload at end of queue
                    index = queue_index + buffer_size * self._max_buffers
                    if index < self._size:
                        queue[index] = self._workers.submit(
                            self._read_range, index, index + buffer_size)

                # Gets read size, updates seek and updates size left
                read_size = end - start
                if size_left != -1:
                    size_left -= read_size
                seek += read_size

                # Defines read buffer range
                b_start = b_end
                b_end = b_start + read_size

                # Copy data from preload buffer to read buffer
                b_view[b_start:b_end] = buffer_view[start:end]

            # Updates seek and sync raw
            self._seek = seek
            self._raw.seek(seek)

        # Returns read size
        return b_end

Response: Reads bytes into a pre - allocated writable bytes - like object b and returns the number of bytes read.


Instruction:
Here you have a function in Python 3, explain what it does
def seek(self, offset, whence=SEEK_SET):
        if not self._seekable:
            raise UnsupportedOperation('seek')

        # Only read mode is seekable
        with self._seek_lock:
            # Set seek using raw method and
            # sync buffered seek with raw seek
            self.raw.seek(offset, whence)
            self._seek = seek = self.raw._seek

            # Preload starting from current seek
            self._preload_range()

        return seek

Response: Change the stream position to the given byte offset.


Instruction:
Write a Python 3 function for
writing the given bytes - like object b to the underlying raw stream and returns the number of bytes written.

Response:
def write(self, b):
        """
        Write the given bytes-like object, b, to the underlying raw stream,
        and return the number of bytes written.

        Args:
            b (bytes-like object): Bytes to write.

        Returns:
            int: The number of bytes written.
        """
        if not self._writable:
            raise UnsupportedOperation('write')

        size = len(b)
        b_view = memoryview(b)
        size_left = size
        buffer_size = self._buffer_size
        max_buffers = self._max_buffers

        with self._seek_lock:
            end = self._buffer_seek
            buffer_view = memoryview(self._write_buffer)

            while size_left > 0:
                # Get range to copy
                start = end
                end = start + size_left

                if end > buffer_size:
                    # End of buffer, need flush after copy
                    end = buffer_size
                    flush = True
                else:
                    flush = False

                buffer_range = end - start

                # Update not remaining data size
                b_start = size - size_left
                size_left -= buffer_range

                # Copy data
                buffer_view[start:end] = b_view[b_start: b_start + buffer_range]

                # Flush buffer if needed
                if flush:
                    # Update buffer seek
                    # Needed to write the good amount of data
                    self._buffer_seek = end

                    # Update global seek, this is the number
                    # of buffer flushed
                    self._seek += 1

                    # Block flush based on maximum number of
                    # buffers in flush progress
                    if max_buffers:
                        futures = self._write_futures
                        flush_wait = self._FLUSH_WAIT
                        while sum(1 for future in futures
                                  if not future.done()) >= max_buffers:
                            sleep(flush_wait)

                    # Flush
                    with handle_os_exceptions():
                        self._flush()

                    # Clear buffer
                    self._write_buffer = bytearray(buffer_size)
                    buffer_view = memoryview(self._write_buffer)
                    end = 0

            # Update buffer seek
            self._buffer_seek = end
            return size


Instruction:
Explain what the following Python 3 code does
def create(content, error='H', version=None, mode=None, encoding=None):
    return QRCode(content, error, version, mode, encoding)

Response: This function creates a new QR code.


Instruction:
Here you have a function in Python 3, explain what it does
def show(self, wait=1.2, scale=10, module_color=(0, 0, 0, 255),
            background=(255, 255, 255, 255), quiet_zone=4):
        """Displays this QR code.

        This method is mainly intended for debugging purposes.

        This method saves the output of the :py:meth:`png` method (with a default
        scaling factor of 10) to a temporary file and opens it with the
        standard PNG viewer application or within the standard webbrowser. The
        temporary file is deleted afterwards.

        If this method does not show any result, try to increase the `wait`
        parameter. This parameter specifies the time in seconds to wait till
        the temporary file is deleted. Note, that this method does not return
        until the provided amount of seconds (default: 1.2) has passed.

        The other parameters are simply passed on to the `png` method.
        """
        import os
        import time
        import tempfile
        import webbrowser

        try:  # Python 2
            from urlparse import urljoin
            from urllib import pathname2url
        except ImportError:  # Python 3
            from urllib.parse import urljoin
            from urllib.request import pathname2url

        f = tempfile.NamedTemporaryFile('wb', suffix='.png', delete=False)
        self.png(f, scale=scale, module_color=module_color,
                 background=background, quiet_zone=quiet_zone)
        f.close()
        webbrowser.open_new_tab(urljoin('file:', pathname2url(f.name)))
        time.sleep(wait)
        os.unlink(f.name)

Response: Displays this QR code.


Instruction:
Make a summary of the following Python 3 code
def png_as_base64_str(self, scale=1, module_color=(0, 0, 0, 255),
                          background=(255, 255, 255, 255), quiet_zone=4):
        """This method uses the png render and returns the PNG image encoded as
        base64 string. This can be useful for creating dynamic PNG images for
        web development, since no file needs to be created.

        Example:
            >>> code = pyqrcode.create('Are you suggesting coconuts migrate?')
            >>> image_as_str = code.png_as_base64_str(scale=5)
            >>> html_img = '<img src="data:image/png;base64,{}">'.format(image_as_str)

        The parameters are passed directly to the :py:meth:`png` method. Refer
        to that method's documentation for the meaning behind the parameters.

        .. note::
            This method depends on the pypng module to actually create the
            PNG image.

        """
        import io
        import base64

        with io.BytesIO() as virtual_file:
            self.png(file=virtual_file, scale=scale, module_color=module_color,
                     background=background, quiet_zone=quiet_zone)
            image_as_str = base64.b64encode(virtual_file.getvalue()).decode("ascii")
        return image_as_str

Response: This method uses the png render and returns the PNG image encoded as base64 string.


Instruction:
How would you code a function in Python 3 to
return a string representing an XBM image of the QR code.

Response:
def xbm(self, scale=1, quiet_zone=4):
        """Returns a string representing an XBM image of the QR code.
        The XBM format is a black and white image format that looks like a
        C header file.

        Because displaying QR codes in Tkinter is the
        primary use case for this renderer, this method does not take a file
        parameter. Instead it retuns the rendered QR code data as a string.

        Example of using this renderer with Tkinter:
            >>> import pyqrcode
            >>> import tkinter
            >>> code = pyqrcode.create('Knights who say ni!')
            >>> code_xbm = code.xbm(scale=5)
            >>>
            >>> top = tkinter.Tk()
            >>> code_bmp = tkinter.BitmapImage(data=code_xbm)
            >>> code_bmp.config(foreground="black")
            >>> code_bmp.config(background="white")
            >>> label = tkinter.Label(image=code_bmp)
            >>> label.pack()

        The *scale* parameter sets how large to draw a single module. By
        default one pixel is used to draw a single module. This may make the
        code too small to be read efficiently. Increasing the scale will make
        the code larger. Only integer scales are usable. This method will
        attempt to coerce the parameter into an integer (e.g. 2.5 will become 2,
        and '3' will become 3). You can use the :py:meth:`get_png_size` method
        to calculate the actual pixel size of this image when displayed.

        The *quiet_zone* parameter sets how wide the quiet zone around the code
        should be. According to the standard this should be 4 modules. It is
        left settable because such a wide quiet zone is unnecessary in many
        applications where the QR code is not being printed.
        """
        return builder._xbm(self.code, scale, quiet_zone)


Instruction:
Make a summary of the following Python 3 code
def _text(code, quiet_zone=4):
    buf = io.StringIO()

    border_row = '0' * (len(code[0]) + (quiet_zone*2))

    #Every QR code start with a quiet zone at the top
    for b in range(quiet_zone):
        buf.write(border_row)
        buf.write('\n')

    for row in code:
        #Draw the starting quiet zone
        for b in range(quiet_zone):
            buf.write('0')

        #Actually draw the QR code
        for bit in row:
            if bit == 1:
                buf.write('1')
            elif bit == 0:
                buf.write('0')
            #This is for debugging unfinished QR codes,
            #unset pixels will be spaces.
            else:
                buf.write(' ')

        #Draw the ending quiet zone
        for b in range(quiet_zone):
            buf.write('0')
        buf.write('\n')

    #Every QR code ends with a quiet zone at the bottom
    for b in range(quiet_zone):
        buf.write(border_row)
        buf.write('\n')

    return buf.getvalue()

Response: This method returns a text based representation of the QR code.


Instruction:
Explain what the following Python 3 code does
def _xbm(code, scale=1, quiet_zone=4):
    try:
        str = unicode  # Python 2
    except NameError:
        str = __builtins__['str']

    buf = io.StringIO()

    # Calculate the width in pixels
    pixel_width = (len(code[0]) + quiet_zone * 2) * scale

    # Add the size information and open the pixel data section
    buf.write('#define im_width ')
    buf.write(str(pixel_width))
    buf.write('\n')
    buf.write('#define im_height ')
    buf.write(str(pixel_width))
    buf.write('\n')
    buf.write('static char im_bits[] = {\n')

    # Calculate the number of bytes per row
    byte_width = int(math.ceil(pixel_width / 8.0))

    # Add the top quiet zone
    buf.write(('0x00,' * byte_width + '\n') * quiet_zone * scale)
    for row in code:
        # Add the left quiet zone
        row_bits = '0' * quiet_zone * scale
        # Add the actual QR code
        for pixel in row:
            row_bits += str(pixel) * scale
        # Add the right quiet zone
        row_bits += '0' * quiet_zone * scale
        # Format the row
        formated_row = ''
        for b in range(byte_width):
            formated_row += '0x{0:02x},'.format(int(row_bits[:8][::-1], 2))
            row_bits = row_bits[8:]
        formated_row += '\n'
        # Add the formatted row
        buf.write(formated_row * scale)
    # Add the bottom quiet zone and close the pixel data section
    buf.write(('0x00,' * byte_width + '\n') * quiet_zone * scale)
    buf.write('};')

    return buf.getvalue()

Response: This function will format the QR code as a X BitMap.


Instruction:
Can you tell what is the following Python 3 function doing
def _svg(code, version, file, scale=1, module_color='#000', background=None,
         quiet_zone=4, xmldecl=True, svgns=True, title=None, svgclass='pyqrcode',
         lineclass='pyqrline', omithw=False, debug=False):
    """This function writes the QR code out as an SVG document. The
    code is drawn by drawing only the modules corresponding to a 1. They
    are drawn using a line, such that contiguous modules in a row
    are drawn with a single line. The file parameter is used to
    specify where to write the document to. It can either be a writable (binary)
    stream or a file path. The scale parameter is sets how large to draw
    a single module. By default one pixel is used to draw a single
    module. This may make the code to small to be read efficiently.
    Increasing the scale will make the code larger. This method will accept
    fractional scales (e.g. 2.5).

    :param module_color: Color of the QR code (default: ``#000`` (black))
    :param background: Optional background color.
            (default: ``None`` (no background))
    :param quiet_zone: Border around the QR code (also known as  quiet zone)
            (default: ``4``). Set to zero (``0``) if the code shouldn't
            have a border.
    :param xmldecl: Inidcates if the XML declaration header should be written
            (default: ``True``)
    :param svgns: Indicates if the SVG namespace should be written
            (default: ``True``)
    :param title: Optional title of the generated SVG document.
    :param svgclass: The CSS class of the SVG document
            (if set to ``None``, the SVG element won't have a class).
    :param lineclass: The CSS class of the path element
            (if set to ``None``, the path won't have a class).
    :param omithw: Indicates if width and height attributes should be
            omitted (default: ``False``). If these attributes are omitted,
            a ``viewBox`` attribute will be added to the document.
    :param debug: Inidicates if errors in the QR code should be added to the
            output (default: ``False``).
    """
    from functools import partial
    from xml.sax.saxutils import quoteattr

    def write_unicode(write_meth, unicode_str):
        """\
        Encodes the provided string into UTF-8 and writes the result using
        the `write_meth`.
        """
        write_meth(unicode_str.encode('utf-8'))

    def line(x, y, length, relative):
        """Returns coordinates to draw a line with the provided length.
        """
        return '{0}{1} {2}h{3}'.format(('m' if relative else 'M'), x, y, length)

    def errline(col_number, row_number):
        """Returns the coordinates to draw an error bit.
        """
        # Debug path uses always absolute coordinates
        # .5 == stroke / 2
        return line(col_number + quiet_zone, row_number + quiet_zone + .5, 1, False)

    f, autoclose = _get_writable(file, 'wb')
    write = partial(write_unicode, f.write)
    write_bytes = f.write
    # Write the document header
    if xmldecl:
        write_bytes(b'<?xml version="1.0" encoding="UTF-8"?>\n')
    write_bytes(b'<svg')
    if svgns:
        write_bytes(b' xmlns="http://www.w3.org/2000/svg"')
    size = tables.version_size[version] * scale + (2 * quiet_zone * scale)
    if not omithw:
        write(' height="{0}" width="{0}"'.format(size))
    else:
        write(' viewBox="0 0 {0} {0}"'.format(size))
    if svgclass is not None:
        write_bytes(b' class=')
        write(quoteattr(svgclass))
    write_bytes(b'>')
    if title is not None:
        write('<title>{0}</title>'.format(title))

    # Draw a background rectangle if necessary
    if background is not None:
        write('<path fill="{1}" d="M0 0h{0}v{0}h-{0}z"/>'
                .format(size, background))
    write_bytes(b'<path')
    if scale != 1:
        write(' transform="scale({0})"'.format(scale))
    if module_color is not None:
        write_bytes(b' stroke=')
        write(quoteattr(module_color))
    if lineclass is not None:
        write_bytes(b' class=')
        write(quoteattr(lineclass))
    write_bytes(b' d="')
    # Used to keep track of unknown/error coordinates.
    debug_path = ''
    # Current pen pointer position
    x, y = -quiet_zone, quiet_zone - .5  # .5 == stroke-width / 2
    wrote_bit = False
    # Loop through each row of the code
    for rnumber, row in enumerate(code):
        start_column = 0  # Reset the starting column number
        coord = ''  # Reset row coordinates
        y += 1  # Pen position on y-axis
        length = 0  # Reset line length
        # Examine every bit in the row
        for colnumber, bit in enumerate(row):
            if bit == 1:
                length += 1
            else:
                if length:
                    x = start_column - x
                    coord += line(x, y, length, relative=wrote_bit)
                    x = start_column + length
                    y = 0  # y-axis won't change unless the row changes
                    length = 0
                    wrote_bit = True
                start_column = colnumber + 1
                if debug and bit != 0:
                    debug_path += errline(colnumber, rnumber)
        if length:
            x = start_column - x
            coord += line(x, y, length, relative=wrote_bit)
            x = start_column + length
            wrote_bit = True
        write(coord)
    # Close path
    write_bytes(b'"/>')
    if debug and debug_path:
        write_bytes(b'<path')
        if scale != 1:
            write(' transform="scale({0})"'.format(scale))
        write(' class="pyqrerr" stroke="red" d="{0}"/>'.format(debug_path))
    # Close document
    write_bytes(b'</svg>\n')
    if autoclose:
        f.close()

Response: This function writes the QR code out as an SVG document.


Instruction:
Given the following Python 3 function, write the documentation
def _hex_to_rgb(color):
    if color[0] == '#':
        color = color[1:]
    if len(color) == 3:
        color = color[0] * 2 + color[1] * 2 + color[2] * 2
    if len(color) != 6:
        raise ValueError('Input #{0} is not in #RRGGBB format'.format(color))
    return [int(n, 16) for n in (color[:2], color[2:4], color[4:])]

Response:
Helper function to convert a color provided in hexadecimal format
    as RGB triple.


Instruction:
Make a summary of the following Python 3 code
def grouper(self, n, iterable, fillvalue=None):
        args = [iter(iterable)] * n
        if hasattr(itertools, 'zip_longest'):
            return itertools.zip_longest(*args, fillvalue=fillvalue)
        return itertools.izip_longest(*args, fillvalue=fillvalue)

Response: This generator yields a set of tuples where the iterable is evenly sized and fillvalue will be appended to the last tuple.


Instruction:
How would you explain what the following Python 3 function does
def encode(self):
        if self.mode == tables.modes['alphanumeric']:
            encoded = self.encode_alphanumeric()
        elif self.mode == tables.modes['numeric']:
            encoded = self.encode_numeric()
        elif self.mode == tables.modes['binary']:
            encoded = self.encode_bytes()
        elif self.mode == tables.modes['kanji']:
            encoded = self.encode_kanji()
        return encoded

Response: This method encodes the data into a binary string using the appropriate algorithm specified by the mode.


Instruction:
Here you have a function in Python 3, explain what it does
def encode_numeric(self):
        with io.StringIO() as buf:
            #Break the number into groups of three digits
            for triplet in self.grouper(3, self.data):
                number = ''
                for digit in triplet:
                    if isinstance(digit, int):
                        digit = chr(digit)

                    #Only build the string if digit is not None
                    if digit:
                        number = ''.join([number, digit])
                    else:
                        break

                #If the number is one digits, make a 4 bit field
                if len(number) == 1:
                    bin = self.binary_string(number, 4)

                #If the number is two digits, make a 7 bit field
                elif len(number) == 2:
                    bin = self.binary_string(number, 7)

                #Three digit numbers use a 10 bit field
                else:
                    bin = self.binary_string(number, 10)

                buf.write(bin)
            return buf.getvalue()

Response:
This method encodes the QR code s data if its mode is
        numeric. It returns the data encoded as a binary string.


Instruction:
Given the following Python 3 function, write the documentation
def encode_kanji(self):
        def two_bytes(data):
            """Output two byte character code as a single integer."""
            def next_byte(b):
                """Make sure that character code is an int. Python 2 and
                3 compatibility.
                """
                if not isinstance(b, int):
                    return ord(b)
                else:
                    return b

            #Go through the data by looping to every other character
            for i in range(0, len(data), 2):
                yield (next_byte(data[i]) << 8) | next_byte(data[i+1])

        #Force the data into Kanji encoded bytes
        if isinstance(self.data, bytes):
            data = self.data.decode('shiftjis').encode('shiftjis')
        else:
            data = self.data.encode('shiftjis')

        #Now perform the algorithm that will make the kanji into 13 bit fields
        with io.StringIO() as buf:
            for asint in two_bytes(data):
                #Shift the two byte value as indicated by the standard
                if 0x8140 <= asint <= 0x9FFC:
                    difference = asint - 0x8140
                elif 0xE040 <= asint <= 0xEBBF:
                    difference = asint - 0xC140

                #Split the new value into most and least significant bytes
                msb = (difference >> 8)
                lsb = (difference & 0x00FF)

                #Calculate the actual 13 bit binary value
                buf.write('{0:013b}'.format((msb * 0xC0) + lsb))
            #Return the binary string
            return buf.getvalue()

Response: This method encodes the QR code s data into a kanji encoded binary string. It returns the data encoded as a binary string.


Instruction:
Can you tell what is the following Python 3 function doing
def add_data(self):
        #Encode the data into a QR code
        self.buffer.write(self.binary_string(self.mode, 4))
        self.buffer.write(self.get_data_length())
        self.buffer.write(self.encode())

        #Converts the buffer into "code word" integers.
        #The online debugger outputs them this way, makes
        #for easier comparisons.
        #s = self.buffer.getvalue()
        #for i in range(0, len(s), 8):
        #    print(int(s[i:i+8], 2), end=',')
        #print()

        #Fix for issue #3: https://github.com/mnooner256/pyqrcode/issues/3#
        #I was performing the terminate_bits() part in the encoding.
        #As per the standard, terminating bits are only supposed to
        #be added after the bit stream is complete. I took that to
        #mean after the encoding, but actually it is after the entire
        #bit stream has been constructed.
        bits = self.terminate_bits(self.buffer.getvalue())
        if bits is not None:
            self.buffer.write(bits)

        #delimit_words and add_words can return None
        add_bits = self.delimit_words()
        if add_bits:
            self.buffer.write(add_bits)

        fill_bytes = self.add_words()
        if fill_bytes:
            self.buffer.write(fill_bytes)

        #Get a numeric representation of the data
        data = [int(''.join(x),2)
                    for x in self.grouper(8, self.buffer.getvalue())]

        #This is the error information for the code
        error_info = tables.eccwbi[self.version][self.error]

        #This will hold our data blocks
        data_blocks = []

        #This will hold our error blocks
        error_blocks = []

        #Some codes have the data sliced into two different sized blocks
        #for example, first two 14 word sized blocks, then four 15 word
        #sized blocks. This means that slicing size can change over time.
        data_block_sizes = [error_info[2]] * error_info[1]
        if error_info[3] != 0:
            data_block_sizes.extend([error_info[4]] * error_info[3])

        #For every block of data, slice the data into the appropriate
        #sized block
        current_byte = 0
        for n_data_blocks in data_block_sizes:
            data_blocks.append(data[current_byte:current_byte+n_data_blocks])
            current_byte += n_data_blocks

        #I am not sure about the test after the "and". This was added to
        #fix a bug where after delimit_words padded the bit stream, a zero
        #byte ends up being added. After checking around, it seems this extra
        #byte is supposed to be chopped off, but I cannot find that in the
        #standard! I am adding it to solve the bug, I believe it is correct.
        if current_byte < len(data):
            raise ValueError('Too much data for this code version.')

        #DEBUG CODE!!!!
        #Print out the data blocks
        #print('Data Blocks:\n{0}'.format(data_blocks))

        #Calculate the error blocks
        for n, block in enumerate(data_blocks):
            error_blocks.append(self.make_error_block(block, n))

        #DEBUG CODE!!!!
        #Print out the error blocks
        #print('Error Blocks:\n{0}'.format(error_blocks))

        #Buffer we will write our data blocks into
        data_buffer = io.StringIO()

        #Add the data blocks
        #Write the buffer such that: block 1 byte 1, block 2 byte 1, etc.
        largest_block = max(error_info[2], error_info[4])+error_info[0]
        for i in range(largest_block):
            for block in data_blocks:
                if i < len(block):
                    data_buffer.write(self.binary_string(block[i], 8))

        #Add the error code blocks.
        #Write the buffer such that: block 1 byte 1, block 2 byte 2, etc.
        for i in range(error_info[0]):
            for block in error_blocks:
                data_buffer.write(self.binary_string(block[i], 8))

        self.buffer = data_buffer

Response: This function properly constructs a QR code s data string. It takes the data and returns the data string.


Instruction:
Write a Python 3 script for
adding words to the data block of the QR code.

Response:
def add_words(self):
        """The data block must fill the entire data capacity of the QR code.
        If we fall short, then we must add bytes to the end of the encoded
        data field. The value of these bytes are specified in the standard.
        """

        data_blocks = len(self.buffer.getvalue()) // 8
        total_blocks = tables.data_capacity[self.version][self.error][0] // 8
        needed_blocks = total_blocks - data_blocks

        if needed_blocks == 0:
            return None

        #This will return item1, item2, item1, item2, etc.
        block = itertools.cycle(['11101100', '00010001'])

        #Create a string of the needed blocks
        return ''.join([next(block) for x in range(needed_blocks)])


Instruction:
How would you explain what the following Python 3 function does
def add_detection_pattern(self, m):

        #Draw outer black box
        for i in range(7):
            inv = -(i+1)
            for j in [0,6,-1,-7]:
                m[j][i] = 1
                m[i][j] = 1
                m[inv][j] = 1
                m[j][inv] = 1

        #Draw inner white box
        for i in range(1, 6):
            inv = -(i+1)
            for j in [1, 5, -2, -6]:
                m[j][i] = 0
                m[i][j] = 0
                m[inv][j] = 0
                m[j][inv] = 0

        #Draw inner black box
        for i in range(2, 5):
            for j in range(2, 5):
                inv = -(i+1)
                m[i][j] = 1
                m[inv][j] = 1
                m[j][inv] = 1

        #Draw white border
        for i in range(8):
            inv = -(i+1)
            for j in [7, -8]:
                m[i][j] = 0
                m[j][i] = 0
                m[inv][j] = 0
                m[j][inv] = 0

        #To keep the code short, it draws an extra box
        #in the lower right corner, this removes it.
        for i in range(-8, 0):
            for j in range(-8, 0):
                m[i][j] = ' '

        #Add the timing pattern
        bit = itertools.cycle([1,0])
        for i in range(8, (len(m)-8)):
            b = next(bit)
            m[i][6] = b
            m[6][i] = b

        #Add the extra black pixel
        m[-8][8] = 1

Response: This method adds the detection patterns to the QR code.


Instruction:
Can you tell what is the following Python 3 function doing
def add_position_pattern(self, m):
        #Version 1 does not have a position adjustment pattern
        if self.version == 1:
            return

        #Get the coordinates for where to place the boxes
        coordinates = tables.position_adjustment[self.version]

        #Get the max and min coordinates to handle special cases
        min_coord = coordinates[0]
        max_coord = coordinates[-1]

        #Draw a box at each intersection of the coordinates
        for i in coordinates:
            for j in coordinates:
                #Do not draw these boxes because they would
                #interfere with the detection pattern
                if (i == min_coord and j == min_coord) or \
                   (i == min_coord and j == max_coord) or \
                   (i == max_coord and j == min_coord):
                    continue

                #Center black pixel
                m[i][j] = 1

                #Surround the pixel with a white box
                for x in [-1,1]:
                    m[i+x][j+x] = 0
                    m[i+x][j] = 0
                    m[i][j+x] = 0
                    m[i-x][j+x] = 0
                    m[i+x][j-x] = 0

                #Surround the white box with a black box
                for x in [-2,2]:
                    for y in [0,-1,1]:
                        m[i+x][j+x] = 1
                        m[i+x][j+y] = 1
                        m[i+y][j+x] = 1
                        m[i-x][j+x] = 1
                        m[i+x][j-x] = 1

Response: This method draws the position adjustment patterns onto the QR code code.


Instruction:
How would you code a function in Python 3 to
add the version pattern to the internal table m.

Response:
def add_version_pattern(self, m):
        """For QR codes with a version 7 or higher, a special pattern
        specifying the code's version is required.

        For further information see:
        http://www.thonky.com/qr-code-tutorial/format-version-information/#example-of-version-7-information-string
        """
        if self.version < 7:
            return

        #Get the bit fields for this code's version
        #We will iterate across the string, the bit string
        #needs the least significant digit in the zero-th position
        field = iter(tables.version_pattern[self.version][::-1])

        #Where to start placing the pattern
        start = len(m)-11

        #The version pattern is pretty odd looking
        for i in range(6):
            #The pattern is three modules wide
            for j in range(start, start+3):
                bit = int(next(field))

                #Bottom Left
                m[i][j] = bit

                #Upper right
                m[j][i] = bit


Instruction:
Can you generate the documentation for the following Python 3 function
def split_pulls(all_issues, project="arokem/python-matlab-bridge"):
    pulls = []
    issues = []
    for i in all_issues:
        if is_pull_request(i):
            pull = get_pull_request(project, i['number'], auth=True)
            pulls.append(pull)
        else:
            issues.append(i)
    return issues, pulls

Response: split a list of closed issues into non - PR Issues and Pull Requests


Instruction:
How would you explain what the following Python 3 function does
def issues_closed_since(period=timedelta(days=365), project="arokem/python-matlab-bridge", pulls=False):

    which = 'pulls' if pulls else 'issues'

    if isinstance(period, timedelta):
        since = round_hour(datetime.utcnow() - period)
    else:
        since = period
    url = "https://api.github.com/repos/%s/%s?state=closed&sort=updated&since=%s&per_page=%i" % (project, which, since.strftime(ISO8601), PER_PAGE)
    allclosed = get_paged_request(url, headers=make_auth_header())

    filtered = [ i for i in allclosed if _parse_datetime(i['closed_at']) > since ]
    if pulls:
        filtered = [ i for i in filtered if _parse_datetime(i['merged_at']) > since ]
        # filter out PRs not against master (backports)
        filtered = [ i for i in filtered if i['base']['ref'] == 'master' ]
    else:
        filtered = [ i for i in filtered if not is_pull_request(i) ]

    return filtered

Response: Get all issues closed since a particular point in time.


Instruction:
Explain what the following Python 3 code does
def sorted_by_field(issues, field='closed_at', reverse=False):
    return sorted(issues, key = lambda i:i[field], reverse=reverse)

Response: Return a list of issues sorted by field.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def report(issues, show_urls=False):
    # titles may have unicode in them, so we must encode everything below
    if show_urls:
        for i in issues:
            print(u'#%d: %s' % (i['number'],
                                      i['title'].replace(u'`', u'``')))
    else:
        for i in issues:
            print(u'* %d: %s' % (i['number'], i['title'].replace(u'`', u'``')))

Response: Summary report about a list of issues printing number and title.


Instruction:
Implement a Python 3 function for
writing a numpy array and its shape to base64 buffers

Response:
def encode_ndarray(obj):
    """Write a numpy array and its shape to base64 buffers"""
    shape = obj.shape
    if len(shape) == 1:
        shape = (1, obj.shape[0])
    if obj.flags.c_contiguous:
        obj = obj.T
    elif not obj.flags.f_contiguous:
        obj = asfortranarray(obj.T)
    else:
        obj = obj.T
    try:
        data = obj.astype(float64).tobytes()
    except AttributeError:
        data = obj.astype(float64).tostring()

    data = base64.b64encode(data).decode('utf-8')
    return data, shape


Instruction:
Write a Python 3 script for
extracting a numpy array from a base64 buffer

Response:
def decode_arr(data):
    """Extract a numpy array from a base64 buffer"""
    data = data.encode('utf-8')
    return frombuffer(base64.b64decode(data), float64)


Instruction:
How would you explain what the following Python 3 function does
def run_func(self, func_path, *func_args, **kwargs):
        if not self.started:
            raise ValueError('Session not started, use start()')

        nargout = kwargs.pop('nargout', 1)
        func_args += tuple(item for pair in zip(kwargs.keys(), kwargs.values())
                           for item in pair)
        dname = os.path.dirname(func_path)
        fname = os.path.basename(func_path)
        func_name, ext = os.path.splitext(fname)
        if ext and not ext == '.m':
            raise TypeError('Need to give path to .m file')
        return self._json_response(cmd='eval',
                                   func_name=func_name,
                                   func_args=func_args or '',
                                   dname=dname,
                                   nargout=nargout)

Response: Run a function in Matlab and return the result.


Instruction:
Can you write a function in Python 3 where it
binds a Matlab function to the instance of the named attribute.

Response:
def _bind_method(self, name, unconditionally=False):
        """Generate a Matlab function and bind it to the instance

        This is where the magic happens. When an unknown attribute of the
        Matlab class is requested, it is assumed to be a call to a
        Matlab function, and is generated and bound to the instance.

        This works because getattr() falls back to __getattr__ only if no
        attributes of the requested name can be found through normal
        routes (__getattribute__, __dict__, class tree).

        bind_method first checks whether the requested name is a callable
        Matlab function before generating a binding.

        Parameters
        ----------
        name : str
            The name of the Matlab function to call
                e.g. 'sqrt', 'sum', 'svd', etc
        unconditionally : bool, optional
            Bind the method without performing
                checks. Used to bootstrap methods that are required and
                know to exist

        Returns
        -------
        MatlabFunction
            A reference to a newly bound MatlabFunction instance if the
                requested name is determined to be a callable function

        Raises
        ------
        AttributeError: if the requested name is not a callable
                Matlab function

        """
        # TODO: This does not work if the function is a mex function inside a folder of the same name
        exists = self.run_func('exist', name)['result'] in [2, 3, 5]
        if not unconditionally and not exists:
            raise AttributeError("'Matlab' object has no attribute '%s'" % name)

        # create a new method instance
        method_instance = MatlabFunction(weakref.ref(self), name)
        method_instance.__name__ = name

        # bind to the Matlab instance with a weakref (to avoid circular references)
        if sys.version.startswith('3'):
            method = types.MethodType(method_instance, weakref.ref(self))
        else:
            method = types.MethodType(method_instance, weakref.ref(self),
                                      _Session)
        setattr(self, name, method)
        return getattr(self, name)


Instruction:
Can you write a function in Python 3 where it
formats a line of Matlab into a markdown line or a code line.

Response:
def format_line(line):
    """
    Format a line of Matlab into either a markdown line or a code line.

    Parameters
    ----------
    line : str
        The line of code to be formatted. Formatting occurs according to the
        following rules:

        - If the line starts with (at least) two %% signs, a new cell will be
          started.

        - If the line doesn't start with a '%' sign, it is assumed to be legit
          matlab code. We will continue to add to the same cell until reaching
          the next comment line
    """
    if line.startswith('%%'):
        md = True
        new_cell = True
        source = line.split('%%')[1] + '\n'  # line-breaks in md require a line
                                             # gap!

    elif line.startswith('%'):
        md = True
        new_cell = False
        source = line.split('%')[1] + '\n'

    else:
        md = False
        new_cell = False
        source = line

    return new_cell, md, source


Instruction:
Create a Python 3 function to
convert the lines of an m file into an IPython notebooknode.

Response:
def lines_to_notebook(lines, name=None):
    """
    Convert the lines of an m file into an IPython notebook

    Parameters
    ----------
    lines : list
        A list of strings. Each element is a line in the m file

    Returns
    -------
    notebook : an IPython NotebookNode class instance, containing the
    information required to create a file

    """
    source = []
    md = np.empty(len(lines), dtype=object)
    new_cell = np.empty(len(lines), dtype=object)
    for idx, l in enumerate(lines):
        new_cell[idx], md[idx], this_source = format_line(l)
        # Transitions between markdown and code and vice-versa merit a new
        # cell, even if no newline, or "%%" is found. Make sure not to do this
        # check for the very first line!
        if idx>1 and not new_cell[idx]:
            if md[idx] != md[idx-1]:
                new_cell[idx] = True

        source.append(this_source)
    # This defines the breaking points between cells:
    new_cell_idx = np.hstack([np.where(new_cell)[0], -1])

    # Listify the sources:
    cell_source = [source[new_cell_idx[i]:new_cell_idx[i+1]]
                   for i in range(len(new_cell_idx)-1)]
    cell_md = [md[new_cell_idx[i]] for i in range(len(new_cell_idx)-1)]
    cells = []

    # Append the notebook with loading matlab magic extension
    notebook_head = "import pymatbridge as pymat\n" + "ip = get_ipython()\n" \
                    + "pymat.load_ipython_extension(ip)"
    cells.append(nbformat.new_code_cell(notebook_head))#, language='python'))

    for cell_idx, cell_s in enumerate(cell_source):
        if cell_md[cell_idx]:
            cells.append(nbformat.new_markdown_cell(cell_s))
        else:
            cell_s.insert(0, '%%matlab\n')
            cells.append(nbformat.new_code_cell(cell_s))#, language='matlab'))

    #ws = nbformat.new_worksheet(cells=cells)
    notebook = nbformat.new_notebook(cells=cells)
    return notebook


Instruction:
Can you generate the documentation for the following Python 3 function
def convert_mfile(mfile, outfile=None):
    lines = mfile_to_lines(mfile)
    nb = lines_to_notebook(lines)
    if outfile is None:
        outfile = mfile.split('.m')[0] + '.ipynb'
    with open(outfile, 'w') as fid:
        nbwrite(nb, fid)

Response: Convert a Matlab m - file into a Matlab notebook in ipynb format


Instruction:
Explain what the following Python 3 code does
def load_ipython_extension(ip, **kwargs):
    global _loaded
    if not _loaded:
        ip.register_magics(MatlabMagics(ip, **kwargs))
        _loaded = True

Response: Load the extension in IPython.


Instruction:
How would you explain what the following Python 3 function does
def post_gist(content, description='', filename='file', auth=False):
    post_data = json.dumps({
      "description": description,
      "public": True,
      "files": {
        filename: {
          "content": content
        }
      }
    }).encode('utf-8')

    headers = make_auth_header() if auth else {}
    response = requests.post("https://api.github.com/gists", data=post_data, headers=headers)
    response.raise_for_status()
    response_data = json.loads(response.text)
    return response_data['html_url']

Response: Post some text to a Gist and return the URL.


Instruction:
Write a Python 3 script for
getting pull request info by number

Response:
def get_pull_request(project, num, auth=False):
    """get pull request info  by number
    """
    url = "https://api.github.com/repos/{project}/pulls/{num}".format(project=project, num=num)
    if auth:
        header = make_auth_header()
    else:
        header = None
    response = requests.get(url, headers=header)
    response.raise_for_status()
    return json.loads(response.text, object_hook=Obj)


Instruction:
Given the following Python 3 function, write the documentation
def get_pull_request_files(project, num, auth=False):
    url = "https://api.github.com/repos/{project}/pulls/{num}/files".format(project=project, num=num)
    if auth:
        header = make_auth_header()
    else:
        header = None
    return get_paged_request(url, headers=header)

Response: get list of files in a pull request


Instruction:
Write a Python 3 function that can
get a full list handling APIv3 s paging

Response:
def get_paged_request(url, headers=None, **params):
    """get a full list, handling APIv3's paging"""
    results = []
    params.setdefault("per_page", 100)
    while True:
        if '?' in url:
            params = None
            print("fetching %s" % url, file=sys.stderr)
        else:
            print("fetching %s with %s" % (url, params), file=sys.stderr)
        response = requests.get(url, headers=headers, params=params)
        response.raise_for_status()
        results.extend(response.json())
        if 'next' in response.links:
            url = response.links['next']['url']
        else:
            break
    return results


Instruction:
Can you write a function in Python 3 where it
gets pull request list

Response:
def get_pulls_list(project, auth=False, **params):
    """get pull request list"""
    params.setdefault("state", "closed")
    url = "https://api.github.com/repos/{project}/pulls".format(project=project)
    if auth:
        headers = make_auth_header()
    else:
        headers = None
    pages = get_paged_request(url, headers=headers, **params)
    return pages


Instruction:
Here you have a function in Python 3, explain what it does
def encode_multipart_formdata(fields, boundary=None):
    # copy requests imports in here:
    from io import BytesIO
    from requests.packages.urllib3.filepost import (
        choose_boundary, six, writer, b, get_content_type
    )
    body = BytesIO()
    if boundary is None:
        boundary = choose_boundary()

    for fieldname, value in iter_fields(fields):
        body.write(b('--%s\r\n' % (boundary)))

        if isinstance(value, tuple):
            filename, data = value
            writer(body).write('Content-Disposition: form-data; name="%s"; '
                               'filename="%s"\r\n' % (fieldname, filename))
            body.write(b('Content-Type: %s\r\n\r\n' %
                       (get_content_type(filename))))
        else:
            data = value
            writer(body).write('Content-Disposition: form-data; name="%s"\r\n'
                               % (fieldname))
            body.write(b'Content-Type: text/plain\r\n\r\n')

        if isinstance(data, int):
            data = str(data)  # Backwards compatibility
        if isinstance(data, six.text_type):
            writer(body).write(data)
        else:
            body.write(data)

        body.write(b'\r\n')

    body.write(b('--%s--\r\n' % (boundary)))

    content_type = b('multipart/form-data; boundary=%s' % boundary)

    return body.getvalue(), content_type

Response: Encode a dictionary of fields using the multipart - form - data mime format.


Instruction:
Implement a Python 3 function for
uploading a file to the GitHub downloads area

Response:
def post_download(project, filename, name=None, description=""):
    """Upload a file to the GitHub downloads area"""
    if name is None:
        name = os.path.basename(filename)
    with open(filename, 'rb') as f:
        filedata = f.read()

    url = "https://api.github.com/repos/{project}/downloads".format(project=project)

    payload = json.dumps(dict(name=name, size=len(filedata),
                    description=description))
    response = requests.post(url, data=payload, headers=make_auth_header())
    response.raise_for_status()
    reply = json.loads(response.content)
    s3_url = reply['s3_url']

    fields = dict(
        key=reply['path'],
        acl=reply['acl'],
        success_action_status=201,
        Filename=reply['name'],
        AWSAccessKeyId=reply['accesskeyid'],
        Policy=reply['policy'],
        Signature=reply['signature'],
        file=(reply['name'], filedata),
    )
    fields['Content-Type'] = reply['mime_type']
    data, content_type = encode_multipart_formdata(fields)
    s3r = requests.post(s3_url, data=data, headers={'Content-Type': content_type})
    return s3r


Instruction:
Write a Python 3 script for
checking that the given path is an executable regular file or a symlink to a file.

Response:
def is_executable_file(path):
    """Checks that path is an executable regular file (or a symlink to a file).

    This is roughly ``os.path isfile(path) and os.access(path, os.X_OK)``, but
    on some platforms :func:`os.access` gives us the wrong answer, so this
    checks permission bits directly.

    Note
    ----
    This function is taken from the pexpect module, see module doc-string for
    license.
    """
    # follow symlinks,
    fpath = os.path.realpath(path)

    # return False for non-files (directories, fifo, etc.)
    if not os.path.isfile(fpath):
        return False

    # On Solaris, etc., "If the process has appropriate privileges, an
    # implementation may indicate success for X_OK even if none of the
    # execute file permission bits are set."
    #
    # For this reason, it is necessary to explicitly check st_mode

    # get file mode using os.stat, and check if `other',
    # that is anybody, may read and execute.
    mode = os.stat(fpath).st_mode
    if mode & stat.S_IROTH and mode & stat.S_IXOTH:
        return True

    # get current user's group ids, and check if `group',
    # when matching ours, may read and execute.
    user_gids = os.getgroups() + [os.getgid()]
    if (os.stat(fpath).st_gid in user_gids and
            mode & stat.S_IRGRP and mode & stat.S_IXGRP):
        return True

    # finally, if file owner matches our effective userid,
    # check if `user', may read and execute.
    user_gids = os.getgroups() + [os.getgid()]
    if (os.stat(fpath).st_uid == os.geteuid() and
            mode & stat.S_IRUSR and mode & stat.S_IXUSR):
        return True

    return False


Instruction:
Write a Python 3 function for
building the messenger mex for MATLAB

Response:
def build_matlab(static=False):
    """build the messenger mex for MATLAB

    static : bool
        Determines if the zmq library has been statically linked.
        If so, it will append the command line option -DZMQ_STATIC
        when compiling the mex so it matches libzmq.
    """
    cfg = get_config()
    # To deal with spaces, remove quotes now, and add
    # to the full commands themselves.
    if 'matlab_bin' in cfg and cfg['matlab_bin'] != '.':
        matlab_bin = cfg['matlab_bin'].strip('"')
    else:  # attempt to autodetect MATLAB filepath
        matlab_bin = which_matlab()
        if matlab_bin is None:
            raise ValueError("specify 'matlab_bin' in cfg file")
    # Get the extension
    extcmd = esc(os.path.join(matlab_bin, "mexext"))
    extension = subprocess.check_output(extcmd, shell=use_shell)
    extension = extension.decode('utf-8').rstrip('\r\n')

    # Build the mex file
    mex = esc(os.path.join(matlab_bin, "mex"))
    paths = "-L%(zmq_lib)s -I%(zmq_inc)s" % cfg
    make_cmd = '%s -O %s -lzmq ./src/messenger.c' % (mex, paths)
    if static:
        make_cmd += ' -DZMQ_STATIC'
    do_build(make_cmd, 'messenger.%s' % extension)


Instruction:
Can you implement a function in Python 3 that
adds an existing function to a class as a method.

Response:
def add_method(function, klass, name=None):
    '''Add an existing function to a class as a method.

    Note: Consider using the extend decorator as a more readable alternative
        to using this function directly.

    Args:
        function: The function to be added to the class klass.

        klass: The class to which the new method will be added.

        name: An optional name for the new method.  If omitted or None the
            original name of the function is used.

    Returns:
        The function argument unmodified.

    Raises:
        ValueError: If klass already has an attribute with the same name as the
            extension method.
    '''
    # Should we be using functools.update_wrapper in here?
    if name is None:
        name = function_name(function)
    if hasattr(klass, name):
        raise ValueError("Cannot replace existing attribute with method "
                         "'{name}'".format(name=name))
    setattr(klass, name, function)
    return function


Instruction:
Here you have a function in Python 3, explain what it does
def extend(klass, name=None):
    '''A function decorator for extending an existing class.

    Use as a decorator for functions to add to an existing class.

    Args:
        klass: The class to be decorated.

        name: The name the new method is to be given in the klass class.

    Returns:
        A decorator function which accepts a single function as its only
        argument.  The decorated function will be added to class klass.

    Raises:
        ValueError: If klass already has an attribute with the same name as the
            extension method.
    '''
    def decorator(f):
        return add_method(f, klass, name)

    return decorator

Response: A function decorator for extending an existing class. Use as a decorator for functions to add to an existing class.


Instruction:
Can you write a function in Python 3 where it
transforms each element of a sequence into a new form.

Response:
def select(
            self,
            selector):
        '''Transforms each element of a sequence into a new form.

        Each element of the source is transformed through a selector function
        to produce a corresponding element in teh result sequence.

        If the selector is identity the method will return self.

        Note: This method uses deferred execution.

        Args:
            selector: A unary function mapping a value in the source sequence
                to the corresponding value in the generated generated sequence.
                The single positional argument to the selector function is the
                element value.  The return value of the selector function
                should be the corresponding element of the result sequence.

        Returns:
            A Queryable over generated sequence whose elements are the result
            of invoking the selector function on each element of the source
            sequence.

        Raises:
            ValueError: If this Queryable has been closed.
            TypeError: If selector is not callable.
        '''
        if self.closed():
            raise ValueError("Attempt to call select() on a closed Queryable.")

        try:
            selector = make_selector(selector)
        except ValueError:
            raise TypeError("select() parameter selector={selector} cannot be"
                            "converted into a callable "
                            "selector".format(selector=repr(selector)))

        if selector is identity:
            return self

        return self._create(imap(selector, self))


Instruction:
Can you generate the documentation for the following Python 3 function
def select_with_index(
            self,
            selector=IndexedElement,
            transform=identity):
        '''Transforms each element of a sequence into a new form, incorporating
        the index of the element.

        Each element is transformed through a selector function which accepts
        the element value and its zero-based index in the source sequence. The
        generated sequence is lazily evaluated.

        Note: This method uses deferred execution.

        Args:
            selector: A binary function mapping the index of a value in
                the source sequence and the element value itself to the
                corresponding value in the generated sequence. The two
                positional arguments of the selector function are the zero-
                based index of the current element and the value of the current
                element. The return value should be the corresponding value in
                the result sequence. The default selector produces an IndexedElement
                containing the index and the element giving this function
                similar behaviour to the built-in enumerate().

        Returns:
            A Queryable whose elements are the result of invoking the selector
            function on each element of the source sequence

        Raises:
            ValueError: If this Queryable has been closed.
            TypeError: If selector is not callable.
        '''
        if self.closed():
            raise ValueError("Attempt to call select_with_index() on a "
                             "closed Queryable.")

        if not is_callable(selector):
            raise TypeError("select_with_index() parameter selector={0} is "
                            "not callable".format(repr(selector)))

        if not is_callable(transform):
            raise TypeError("select_with_index() parameter item_selector={0} is "
                            "not callable".format(repr(selector)))

        return self._create(itertools.starmap(selector, enumerate(imap(transform, iter(self)))))

Response:
Returns a new sequence where each element of a source sequence is transformed into a new form incorporating
        the index of the element.


Instruction:
Can you create a Python 3 function that
applies a callable to each element in an input sequence generating a new sequence of 2 - tuples where the first element is the input value and the second is the transformed input value.

Response:
def select_with_correspondence(
            self,
            selector,
            result_selector=KeyedElement):
        '''Apply a callable to each element in an input sequence, generating a new
        sequence of 2-tuples where the first element is the input value and the
        second is the transformed input value.

        The generated sequence is lazily evaluated.

        Note: This method uses deferred execution.

        Args:
            selector: A unary function mapping a value in the source sequence
                to the second argument of the result selector.

            result_selector: A binary callable mapping the of a value in
                the source sequence and the transformed value to the
                corresponding value in the generated sequence. The two
                positional arguments of the selector function are the original
                source element and the transformed value. The return value
                should be the corresponding value in the result sequence. The
                default selector produces a KeyedElement containing the index
                and the element giving this function similar behaviour to the
                built-in enumerate().

        Returns:
            When using the default selector, a Queryable whose elements are
            KeyedElements where the first element is from the input sequence
            and the second is the result of invoking the transform function on
            the first value.

        Raises:
            ValueError: If this Queryable has been closed.
            TypeError: If transform is not callable.

        '''
        if self.closed():
            raise ValueError("Attempt to call select_with_correspondence() on a "
                             "closed Queryable.")

        if not is_callable(selector):
            raise TypeError("select_with_correspondence() parameter selector={0} is "
                            "not callable".format(repr(selector)))

        if not is_callable(result_selector):
            raise TypeError("select_with_correspondence() parameter result_selector={0} is "
                            "not callable".format(repr(result_selector)))

        return self._create(result_selector(elem, selector(elem)) for elem in iter(self))


Instruction:
Can you create a Python 3 function that
returns a new Queryable over the elements of the source sequence that are contained in the result sequence.

Response:
def select_many(
            self,
            collection_selector=identity,
            result_selector=identity):
        '''Projects each element of a sequence to an intermediate new sequence,
        flattens the resulting sequences into one sequence and optionally
        transforms the flattened sequence using a selector function.

        Note: This method uses deferred execution.

        Args:
            collection_selector: A unary function mapping each element of the
                source iterable into an intermediate sequence. The single
                argument of the collection_selector is the value of an element
                from the source sequence. The return value should be an
                iterable derived from that element value. The default
                collection_selector, which is the identity function, assumes
                that each element of the source sequence is itself iterable.

            result_selector: An optional unary function mapping the elements in
                the flattened intermediate sequence to corresponding elements
                of the result sequence. The single argument of the
                result_selector is the value of an element from the flattened
                intermediate sequence. The return value should be the
                corresponding value in the result sequence. The default
                result_selector is the identity function.

        Returns:
            A Queryable over a generated sequence whose elements are the result
            of applying the one-to-many collection_selector to each element of
            the source sequence, concatenating the results into an intermediate
            sequence, and then mapping each of those elements through the
            result_selector into the result sequence.

        Raises:
            ValueError: If this Queryable has been closed.
            TypeError: If either collection_selector or result_selector are not
                callable.
        '''
        if self.closed():
            raise ValueError("Attempt to call select_many() on a closed "
                             "Queryable.")

        if not is_callable(collection_selector):
            raise TypeError("select_many() parameter projector={0} is not "
                            "callable".format(repr(collection_selector)))

        if not is_callable(result_selector):
            raise TypeError("select_many() parameter selector={selector} is "
                        " not callable".format(selector=repr(result_selector)))

        sequences = self.select(collection_selector)
        chained_sequence = itertools.chain.from_iterable(sequences)
        return self._create(chained_sequence).select(result_selector)


Instruction:
Can you create a Python 3 function that
projects each element of a sequence to an intermediate new sequence and concatenates the resulting sequence into one sequence and optionally transforms the flattened intermediate sequence into one sequence.

Response:
def select_many_with_index(
            self,
            collection_selector=IndexedElement,
            result_selector=lambda source_element,
                                   collection_element: collection_element):
        '''Projects each element of a sequence to an intermediate new sequence,
        incorporating the index of the element, flattens the resulting sequence
        into one sequence and optionally transforms the flattened sequence
        using a selector function.

        Note: This method uses deferred execution.

        Args:
            collection_selector: A binary function mapping each element of the
                source sequence into an intermediate sequence, by incorporating
                its index in the source sequence. The two positional arguments
                to the function are the zero-based index of the source element
                and the value of the element.  The result of the function
                should be an iterable derived from the index and element value.
                If no collection_selector is provided, the elements of the
                intermediate  sequence will consist of tuples of (index,
                element) from the source sequence.

            result_selector:
                An optional binary function mapping the elements in the
                flattened intermediate sequence together with their
                corresponding source elements to elements of the result
                sequence. The two positional arguments of the result_selector
                are, first the source element corresponding to an element from
                the intermediate sequence, and second the actual element from
                the intermediate sequence. The return value should be the
                corresponding value in the result sequence. If no
                result_selector function is provided, the elements of the
                flattened intermediate sequence are returned untransformed.

        Returns:
            A Queryable over a generated sequence whose elements are the result
            of applying the one-to-many collection_selector to each element of
            the source sequence which incorporates both the index and value of
            the source element, concatenating the results into an intermediate
            sequence, and then mapping each of those elements through the
            result_selector into the result sequence.

        Raises:
            ValueError: If this Queryable has been closed.
            TypeError: If projector [and selector] are not callable.
        '''
        if self.closed():
            raise ValueError("Attempt to call select_many_with_index() on a "
                             "closed Queryable.")

        if not is_callable(collection_selector):
            raise TypeError("select_many_with_index() parameter "
             "projector={0} is not callable".format(repr(collection_selector)))

        if not is_callable(result_selector):
            raise TypeError("select_many_with_index() parameter "
                "selector={0} is not callable".format(repr(result_selector)))

        return self._create(
                self._generate_select_many_with_index(collection_selector,
                                                      result_selector))


Instruction:
Write a Python 3 function that can
project each element of a sequence to an intermediate new sequence and flattens the resulting sequence into one sequence and uses a selector function to incorporate each corresponding source for each item in the result sequence.

Response:
def select_many_with_correspondence(
            self,
            collection_selector=identity,
            result_selector=KeyedElement):
        '''Projects each element of a sequence to an intermediate new sequence,
        and flattens the resulting sequence, into one sequence and uses a
        selector function to incorporate the corresponding source for each item
        in the result sequence.

        Note: This method uses deferred execution.

        Args:
            collection_selector: A unary function mapping each element of the
                source iterable into an intermediate sequence. The single
                argument of the collection_selector is the value of an element
                from the source sequence. The return value should be an
                iterable derived from that element value. The default
                collection_selector, which is the identity function, assumes
                that each element of the source sequence is itself iterable.

            result_selector:
                An optional binary function mapping the elements in the
                flattened intermediate sequence together with their
                corresponding source elements to elements of the result
                sequence. The two positional arguments of the result_selector
                are, first the source element corresponding to an element from
                the intermediate sequence, and second the actual element from
                the intermediate sequence. The return value should be the
                corresponding value in the result sequence. If no
                result_selector function is provided, the elements of the
                result sequence are KeyedElement namedtuples.

        Returns:
            A Queryable over a generated sequence whose elements are the result
            of applying the one-to-many collection_selector to each element of
            the source sequence, concatenating the results into an intermediate
            sequence, and then mapping each of those elements through the
            result_selector which incorporates the corresponding source element
            into the result sequence.

        Raises:
            ValueError: If this Queryable has been closed.
            TypeError: If projector or selector are not callable.
        '''

        if self.closed():
            raise ValueError("Attempt to call "
                "select_many_with_correspondence() on a closed Queryable.")

        if not is_callable(collection_selector):
            raise TypeError("select_many_with_correspondence() parameter "
             "projector={0} is not callable".format(repr(collection_selector)))

        if not is_callable(result_selector):
            raise TypeError("select_many_with_correspondence() parameter "
                "selector={0} is not callable".format(repr(result_selector)))

        return self._create(
            self._generate_select_many_with_correspondence(collection_selector,
                                                           result_selector))