Instruction:
How would you explain what the following Python 3 function does
def _upload_file_to_gdoc(
            self, file_path,
            content_type='application/x-vnd.oasis.opendocument.spreadsheet'):
        """
        Uploads file to GDocs spreadsheet.
        Content type can be provided as argument, default is ods.
        """
        try:
            entry = self.gd_client.GetResourceById(self.key)
            media = gdata.data.MediaSource(
                file_path=file_path, content_type=content_type)
            self.gd_client.UpdateResource(
                entry, media=media, update_metadata=True)
        except (RequestError, IOError) as e:
            raise PODocsError(e)

Response: Uploads file to GDocs spreadsheet.


Instruction:
Write a Python 3 function that can
download csv from GDoc and update resource if worksheets are present.

Response:
def _merge_local_and_gdoc(self, entry, local_trans_csv,
                              local_meta_csv, gdocs_trans_csv, gdocs_meta_csv):
        """
        Download csv from GDoc.
        :return: returns resource if worksheets are present
        :except: raises PODocsError with info if communication with GDocs
                 lead to any errors
        """
        try:
            new_translations = po_to_csv_merge(
                self.languages, self.locale_root, self.po_files_path,
                local_trans_csv, local_meta_csv,
                gdocs_trans_csv, gdocs_meta_csv)
            if new_translations:
                local_ods = os.path.join(self.temp_path, LOCAL_ODS)
                csv_to_ods(local_trans_csv, local_meta_csv, local_ods)
                media = gdata.data.MediaSource(
                    file_path=local_ods, content_type=
                    'application/x-vnd.oasis.opendocument.spreadsheet'
                )
                self.gd_client.UpdateResource(entry, media=media,
                                              update_metadata=True)
        except (IOError, OSError, RequestError) as e:
            raise PODocsError(e)


Instruction:
How would you explain what the following Python 3 function does
def synchronize(self):
        gdocs_trans_csv = os.path.join(self.temp_path, GDOCS_TRANS_CSV)
        gdocs_meta_csv = os.path.join(self.temp_path, GDOCS_META_CSV)
        local_trans_csv = os.path.join(self.temp_path, LOCAL_TRANS_CSV)
        local_meta_csv = os.path.join(self.temp_path, LOCAL_META_CSV)

        try:
            entry = self._download_csv_from_gdocs(gdocs_trans_csv,
                                                  gdocs_meta_csv)
        except PODocsError as e:
            if 'Sheet 1 not found' in str(e) \
                    or 'Conversion failed unexpectedly' in str(e):
                self.upload()
            else:
                raise PODocsError(e)
        else:
            self._merge_local_and_gdoc(entry, local_trans_csv, local_meta_csv,
                                       gdocs_trans_csv, gdocs_meta_csv)

            try:
                csv_to_po(local_trans_csv, local_meta_csv,
                          self.locale_root, self.po_files_path, self.header)
            except IOError as e:
                raise PODocsError(e)

        self._clear_temp()

Response: Synchronize local po files with GDocs Spreadsheet.


Instruction:
Can you implement a function in Python 3 that
downloads csv files from GDocs and convert them into PO files structure.

Response:
def download(self):
        """
        Download csv files from GDocs and convert them into po files structure.
        """
        trans_csv_path = os.path.realpath(
            os.path.join(self.temp_path, GDOCS_TRANS_CSV))
        meta_csv_path = os.path.realpath(
            os.path.join(self.temp_path, GDOCS_META_CSV))

        self._download_csv_from_gdocs(trans_csv_path, meta_csv_path)

        try:
            csv_to_po(trans_csv_path, meta_csv_path,
                      self.locale_root, self.po_files_path, header=self.header)
        except IOError as e:
            raise PODocsError(e)

        self._clear_temp()


Instruction:
Can you write a function in Python 3 where it
uploads all po files to GDocs ignoring conflicts. This method looks for all msgids in po_files and sends them as ods to GDocs Spreadsheet.

Response:
def upload(self):
        """
        Upload all po files to GDocs ignoring conflicts.
        This method looks for all msgids in po_files and sends them
        as ods to GDocs Spreadsheet.
        """
        local_ods_path = os.path.join(self.temp_path, LOCAL_ODS)
        try:
            po_to_ods(self.languages, self.locale_root,
                      self.po_files_path, local_ods_path)
        except (IOError, OSError) as e:
            raise PODocsError(e)

        self._upload_file_to_gdoc(local_ods_path)

        self._clear_temp()


Instruction:
Given the following Python 3 function, write the documentation
def clear(self):
        empty_file_path = os.path.join(self.temp_path, 'empty.csv')
        try:
            empty_file = open(empty_file_path, 'w')
            empty_file.write(',')
            empty_file.close()
        except IOError as e:
            raise PODocsError(e)

        self._upload_file_to_gdoc(empty_file_path, content_type='text/csv')

        os.remove(empty_file_path)

Response: Clear the GDoc Spreadsheet by sending empty. csv file to the temp directory.


Instruction:
Write a Python 3 script for
starting a new qtconsole connected to our kernel

Response:
def new_qt_console(self, evt=None):
        """start a new qtconsole connected to our kernel"""
        return connect_qtconsole(self.ipkernel.connection_file, profile=self.ipkernel.profile)


Instruction:
Write a Python 3 function that can
check whether the URL is accessible and returns HTTP 200 OK or raises ValidationError

Response:
def check_url_accessibility(url, timeout=10):
    '''
    Check whether the URL accessible and returns HTTP 200 OK or not
    if not raises ValidationError
    '''
    if(url=='localhost'):
        url = 'http://127.0.0.1'
    try:
        req = urllib2.urlopen(url, timeout=timeout)
        if (req.getcode()==200):
            return True
    except Exception:
        pass
    fail("URL '%s' is not accessible from this machine" % url)


Instruction:
Can you generate the documentation for the following Python 3 function
def url_has_contents(url, contents, case_sensitive=False, timeout=10):
    '''
    Check whether the HTML page contains the content or not and return boolean
    '''
    try:
        req = urllib2.urlopen(url, timeout=timeout)
    except Exception, _:
        False
    else:
        rep = req.read()
        if (not case_sensitive and rep.lower().find(contents.lower()) >= 0) or (case_sensitive and rep.find(contents) >= 0):
            return True
        else:
            return False

Response: Check whether the URL contains the contents or not and return boolean


Instruction:
Write a Python 3 script to
get the HTTP response code of the given URL.

Response:
def get_response_code(url, timeout=10):
    '''
    Visit the URL and return the HTTP response code in 'int'
    '''
    try:
        req = urllib2.urlopen(url, timeout=timeout)
    except HTTPError, e:
        return e.getcode()
    except Exception, _:
        fail("Couldn't reach the URL '%s'" % url)
    else:
        return req.getcode()


Instruction:
How would you implement a function in Python 3 that
compares the content type header of url param with content_type param and returns boolean

Response:
def compare_content_type(url, content_type):
    '''
    Compare the content type header of url param with content_type param and returns boolean
    @param url -> string e.g. http://127.0.0.1/index
    @param content_type -> string e.g. text/html
    '''
    try:
        response = urllib2.urlopen(url)
    except:
        return False

    return response.headers.type == content_type


Instruction:
Implement a Python 3 function for
comparing the response code of url param with code param and returns boolean

Response:
def compare_response_code(url, code):
    '''
    Compare the response code of url param with code param and returns boolean
    @param url -> string e.g. http://127.0.0.1/index
    @param content_type -> int e.g. 404, 500, 400 ..etc
    '''
    try:
        response = urllib2.urlopen(url)

    except HTTPError as e:
        return e.code == code

    except:
        return False

    return response.code == code


Instruction:
Write a Python 3 script to
publish data and metadata to all frontends.

Response:
def publish_display_data(source, data, metadata=None):
    """Publish data and metadata to all frontends.

    See the ``display_data`` message in the messaging documentation for
    more details about this message type.

    The following MIME types are currently implemented:

    * text/plain
    * text/html
    * text/latex
    * application/json
    * application/javascript
    * image/png
    * image/jpeg
    * image/svg+xml

    Parameters
    ----------
    source : str
        A string that give the function or method that created the data,
        such as 'IPython.core.page'.
    data : dict
        A dictionary having keys that are valid MIME types (like
        'text/plain' or 'image/svg+xml') and values that are the data for
        that MIME type. The data itself must be a JSON'able data
        structure. Minimally all data should have the 'text/plain' data,
        which can be displayed by all frontends. If more than the plain
        text is given, it is up to the frontend to decide which
        representation to use.
    metadata : dict
        A dictionary for metadata related to the data. This can contain
        arbitrary key, value pairs that frontends can use to interpret
        the data.
    """
    from IPython.core.interactiveshell import InteractiveShell
    InteractiveShell.instance().display_pub.publish(
        source,
        data,
        metadata
    )


Instruction:
Can you generate the documentation for the following Python 3 function
def _validate_data(self, source, data, metadata=None):

        if not isinstance(source, basestring):
            raise TypeError('source must be a str, got: %r' % source)
        if not isinstance(data, dict):
            raise TypeError('data must be a dict, got: %r' % data)
        if metadata is not None:
            if not isinstance(metadata, dict):
                raise TypeError('metadata must be a dict, got: %r' % data)

Response: Validate the display data.


Instruction:
Given the following Python 3 function, write the documentation
def publish(self, source, data, metadata=None):

        # The default is to simply write the plain text data using io.stdout.
        if data.has_key('text/plain'):
            print(data['text/plain'], file=io.stdout)

Response: Publish data and metadata to all frontends.


Instruction:
Explain what the following Python 3 code does
def clear_output(self, stdout=True, stderr=True, other=True):
        if stdout:
            print('\033[2K\r', file=io.stdout, end='')
            io.stdout.flush()
        if stderr:
            print('\033[2K\r', file=io.stderr, end='')
            io.stderr.flush()

Response: Clear the output of the cell receiving output.


Instruction:
Write a Python 3 script to
return numerical gradient function of given input function f

Response:
def grad(f, delta=DELTA):
    """
    Returns numerical gradient function of given input function
    Input: f, scalar function of one or two variables
           delta(optional), finite difference step
    Output: gradient function object
    """
    def grad_f(*args, **kwargs):
        if len(args) == 1:
            x, = args
            gradf_x = (
                f(x+delta/2) - f(x-delta/2)
                )/delta
            return gradf_x
        elif len(args) == 2:
            x, y = args
            if type(x) in [float, int] and type(y) in [float, int]:
                gradf_x = (f(x + delta/2, y) - f(x - delta/2, y))/delta
                gradf_y = (f(x, y + delta/2) - f(x, y - delta/2))/delta
                return gradf_x, gradf_y
    return grad_f


Instruction:
Can you implement a function in Python 3 that
returns numerical hessian of a given function f on the object

Response:
def hessian(f, delta=DELTA):
    """
    Returns numerical hessian function of given input function
    Input: f, scalar function of one or two variables
           delta(optional), finite difference step
    Output: hessian function object
    """
    def hessian_f(*args, **kwargs):
        if len(args) == 1:
            x, = args
            hessianf_x = (
                f(x+delta) + f(x-delta) - 2*f(x)
                )/delta**2
            return hessianf_x
        elif len(args) == 2:
            x, y = args
            if type(x) in [float, int] and type(y) in [float, int]:
                hess_xx = (
                    f(x + delta, y) + f(x - delta, y) - 2*f(x, y)
                    )/delta**2
                hess_yy = (
                    f(x, y + delta) + f(x, y - delta) - 2*f(x, y)
                    )/delta**2
                hess_xy = (
                    + f(x+delta/2, y+delta/2)
                    + f(x-delta/2, y-delta/2)
                    - f(x+delta/2, y-delta/2)
                    - f(x-delta/2, y+delta/2)
                    )/delta**2
                return hess_xx, hess_xy, hess_yy
    return hessian_f


Instruction:
How would you implement a function in Python 3 that
returns numerical gradient function of given input function f delta

Response:
def ndgrad(f, delta=DELTA):
    """
    Returns numerical gradient function of given input function
    Input: f, scalar function of an numpy array object
           delta(optional), finite difference step
    Output: gradient function object
    """
    def grad_f(*args, **kwargs):
        x = args[0]
        grad_val = numpy.zeros(x.shape)
        it = numpy.nditer(x, op_flags=['readwrite'], flags=['multi_index'])
        for xi in it:
            i = it.multi_index
            xi += delta/2
            fp = f(*args, **kwargs)
            xi -= delta
            fm = f(*args, **kwargs)
            xi += delta/2
            grad_val[i] = (fp - fm)/delta
        return grad_val
    return grad_f


Instruction:
Can you tell what is the following Python 3 function doing
def ndhess(f, delta=DELTA):
    def hess_f(*args, **kwargs):
        x = args[0]
        hess_val = numpy.zeros(x.shape + x.shape)
        it = numpy.nditer(x, op_flags=['readwrite'], flags=['multi_index'])
        for xi in it:
            i = it.multi_index
            jt = numpy.nditer(x, op_flags=['readwrite'], flags=['multi_index'])
            for xj in jt:
                j = jt.multi_index
                xi += delta/2
                xj += delta/2
                fpp = f(x)
                xj -= delta
                fpm = f(x)
                xi -= delta
                fmm = f(x)
                xj += delta
                fmp = f(x)
                xi += delta/2
                xj -= delta/2
                hess_val[i + j] = (fpp + fmm - fpm - fmp)/delta**2
        return hess_val
    return hess_f

Response: Returns numerical hessian of given input function f


Instruction:
Can you write a function in Python 3 where it
returns numerical gradient function of given class method and attribute with respect to a class attribute.

Response:
def clgrad(obj, exe, arg, delta=DELTA):
    """
    Returns numerical gradient function of given class method
    with respect to a class attribute
    Input: obj, general object
           exe (str), name of object method
           arg (str), name of object atribute
           delta(float, optional), finite difference step
    Output: gradient function object
    """
    f, x = get_method_and_copy_of_attribute(obj, exe, arg)
    def grad_f(*args, **kwargs):
        grad_val = numpy.zeros(x.shape)
        it = numpy.nditer(x, op_flags=['readwrite'], flags=['multi_index'])
        for xi in it:
            i = it.multi_index
            xi += delta/2
            fp = f(*args, **kwargs)
            xi -= delta
            fm = f(*args, **kwargs)
            xi += delta/2
            grad_val[i] = (fp - fm)/delta
        return grad_val
    return grad_f


Instruction:
Can you write a function in Python 3 where it
returns numerical mixed Hessian function of given class method and two class attributes.

Response:
def clmixhess(obj, exe, arg1, arg2, delta=DELTA):
    """
    Returns numerical mixed Hessian function of given class method
    with respect to two class attributes
    Input: obj, general object
           exe (str), name of object method
           arg1(str), name of object attribute
           arg2(str), name of object attribute
           delta(float, optional), finite difference step
    Output: Hessian function object
    """
    f, x = get_method_and_copy_of_attribute(obj, exe, arg1)
    _, y = get_method_and_copy_of_attribute(obj, exe, arg2)
    def hess_f(*args, **kwargs):
        hess_val = numpy.zeros(x.shape + y.shape)
        it = numpy.nditer(x, op_flags=['readwrite'], flags=['multi_index'])
        for xi in it:
            i = it.multi_index
            jt = numpy.nditer(y, op_flags=['readwrite'], flags=['multi_index'])
            for yj in jt:
                j = jt.multi_index
                xi += delta/2
                yj += delta/2
                fpp = f(*args, **kwargs)
                yj -= delta
                fpm = f(*args, **kwargs)
                xi -= delta
                fmm = f(*args, **kwargs)
                yj += delta
                fmp = f(*args, **kwargs)
                xi += delta/2
                yj -= delta/2
                hess_val[i + j] = (fpp + fmm - fpm - fmp)/delta**2
        return hess_val
    return hess_f


Instruction:
Here you have a function in Python 3, explain what it does
def find_cmd(cmd):
    if cmd == 'python':
        return os.path.abspath(sys.executable)
    try:
        path = _find_cmd(cmd).rstrip()
    except OSError:
        raise FindCmdError('command could not be found: %s' % cmd)
    # which returns empty if not found
    if path == '':
        raise FindCmdError('command could not be found: %s' % cmd)
    return os.path.abspath(path)

Response: Find absolute path to executable cmd in a cross platform manner.


Instruction:
Given the following Python 3 function, write the documentation
def pycmd2argv(cmd):
    ext = os.path.splitext(cmd)[1]
    if ext in ['.exe', '.com', '.bat']:
        return [cmd]
    else:
        return [sys.executable, cmd]

Response: Take the path of a python command and return a list of argv - style strings.


Instruction:
Implement a function in Python 3 to
return abbreviated version of cwd e. g. d mydir

Response:
def abbrev_cwd():
    """ Return abbreviated version of cwd, e.g. d:mydir """
    cwd = os.getcwdu().replace('\\','/')
    drivepart = ''
    tail = cwd
    if sys.platform == 'win32':
        if len(cwd) < 4:
            return cwd
        drivepart,tail = os.path.splitdrive(cwd)

    parts = tail.split('/')
    if len(parts) > 2:
        tail = '/'.join(parts[-2:])

    return (drivepart + (
        cwd == '/' and '/' or tail))


Instruction:
How would you explain what the following Python 3 function does
def code_unit_factory(morfs, file_locator):
    # Be sure we have a list.
    if not isinstance(morfs, (list, tuple)):
        morfs = [morfs]

    # On Windows, the shell doesn't expand wildcards.  Do it here.
    globbed = []
    for morf in morfs:
        if isinstance(morf, string_class) and ('?' in morf or '*' in morf):
            globbed.extend(glob.glob(morf))
        else:
            globbed.append(morf)
    morfs = globbed

    code_units = [CodeUnit(morf, file_locator) for morf in morfs]

    return code_units

Response: Construct a list of CodeUnits from a list of polymorphic inputs.


Instruction:
Given the following Python 3 function, write the documentation
def source_file(self):
        if os.path.exists(self.filename):
            # A regular text file: open it.
            return open_source(self.filename)

        # Maybe it's in a zip file?
        source = self.file_locator.get_zip_data(self.filename)
        if source is not None:
            return StringIO(source)

        # Couldn't find source.
        raise CoverageException(
            "No source for code '%s'." % self.filename
            )

Response: Return an open file for reading the source of the code unit.


Instruction:
Can you generate the documentation for the following Python 3 function
def should_be_python(self):
        # Get the file extension.
        _, ext = os.path.splitext(self.filename)

        # Anything named *.py* should be Python.
        if ext.startswith('.py'):
            return True
        # A file with no extension should be Python.
        if not ext:
            return True
        # Everything else is probably not Python.
        return False

Response: Does this file seem like this file contains Python?


Instruction:
Can you implement a function in Python 3 that
calls the method f with the current object as its first argument.

Response:
def check_ready(f, self, *args, **kwargs):
    """Call spin() to sync state prior to calling the method."""
    self.wait(0)
    if not self._ready:
        raise error.TimeoutError("result not ready")
    return f(self, *args, **kwargs)


Instruction:
How would you code a function in Python 3 to
return the result when it arrives within timeout seconds.

Response:
def get(self, timeout=-1):
        """Return the result when it arrives.

        If `timeout` is not ``None`` and the result does not arrive within
        `timeout` seconds then ``TimeoutError`` is raised. If the
        remote call raised an exception then that exception will be reraised
        by get() inside a `RemoteError`.
        """
        if not self.ready():
            self.wait(timeout)

        if self._ready:
            if self._success:
                return self._result
            else:
                raise self._exception
        else:
            raise error.TimeoutError("Result not ready.")


Instruction:
How would you explain what the following Python 3 function does
def wait(self, timeout=-1):
        if self._ready:
            return
        self._ready = self._client.wait(self.msg_ids, timeout)
        if self._ready:
            try:
                results = map(self._client.results.get, self.msg_ids)
                self._result = results
                if self._single_result:
                    r = results[0]
                    if isinstance(r, Exception):
                        raise r
                else:
                    results = error.collect_exceptions(results, self._fname)
                self._result = self._reconstruct_result(results)
            except Exception, e:
                self._exception = e
                self._success = False
            else:
                self._success = True
            finally:
                self._metadata = map(self._client.metadata.get, self.msg_ids)
                self._wait_for_outputs(10)

Response: Wait until the result is available or until timeout seconds pass.


Instruction:
Create a Python 3 function for
getting the results as a dict keyed by engine_id.

Response:
def get_dict(self, timeout=-1):
        """Get the results as a dict, keyed by engine_id.

        timeout behavior is described in `get()`.
        """

        results = self.get(timeout)
        engine_ids = [ md['engine_id'] for md in self._metadata ]
        bycount = sorted(engine_ids, key=lambda k: engine_ids.count(k))
        maxcount = bycount.count(bycount[-1])
        if maxcount > 1:
            raise ValueError("Cannot build dict, %i jobs ran on engine #%i"%(
                    maxcount, bycount[-1]))

        return dict(zip(engine_ids,results))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def timedelta(self, start, end, start_key=min, end_key=max):
        if not isinstance(start, datetime):
            # handle single_result AsyncResults, where ar.stamp is single object,
            # not a list
            start = start_key(start)
        if not isinstance(end, datetime):
            # handle single_result AsyncResults, where ar.stamp is single object,
            # not a list
            end = end_key(end)
        return _total_seconds(end - start)

Response: compute the difference between two set of timestamps


Instruction:
Can you generate the documentation for the following Python 3 function
def progress(self):
        self.wait(0)
        return len(self) - len(set(self.msg_ids).intersection(self._client.outstanding))

Response: Returns the number of tasks which have been completed at this point.


Instruction:
Explain what the following Python 3 code does
def elapsed(self):
        if self.ready():
            return self.wall_time

        now = submitted = datetime.now()
        for msg_id in self.msg_ids:
            if msg_id in self._client.metadata:
                stamp = self._client.metadata[msg_id]['submitted']
                if stamp and stamp < submitted:
                    submitted = stamp
        return _total_seconds(now-submitted)

Response: elapsed time since initial submission


Instruction:
Explain what the following Python 3 code does
def serial_time(self):
        t = 0
        for md in self._metadata:
            t += _total_seconds(md['completed'] - md['started'])
        return t

Response:
Computes the total serial time of a parallel calculation

        Computed as the sum of completed - started of each task


Instruction:
How would you explain what the following Python 3 function does
def wait_interactive(self, interval=1., timeout=None):
        N = len(self)
        tic = time.time()
        while not self.ready() and (timeout is None or time.time() - tic <= timeout):
            self.wait(interval)
            clear_output()
            print("%4i/%i tasks finished after %4i s" % (self.progress, N, self.elapsed), end="")
            sys.stdout.flush()
        print()
        print("done")

Response: wait for all tasks to finish


Instruction:
Can you write a function in Python 3 where it
republishes individual displaypub content dicts

Response:
def _republish_displaypub(self, content, eid):
        """republish individual displaypub content dicts"""
        try:
            ip = get_ipython()
        except NameError:
            # displaypub is meaningless outside IPython
            return
        md = content['metadata'] or {}
        md['engine'] = eid
        ip.display_pub.publish(content['source'], content['data'], md)


Instruction:
Can you write a function in Python 3 where it
waits for all outputs to be ready

Response:
def _wait_for_outputs(self, timeout=-1):
        """wait for the 'status=idle' message that indicates we have all outputs
        """
        if not self._success:
            # don't wait on errors
            return
        tic = time.time()
        while not all(md['outputs_ready'] for md in self._metadata):
            time.sleep(0.01)
            self._client._flush_iopub(self._client._iopub_socket)
            if timeout >= 0 and time.time() > tic + timeout:
                break


Instruction:
Here you have a function in Python 3, explain what it does
def display_outputs(self, groupby="type"):
        if self._single_result:
            self._display_single_result()
            return

        stdouts = self.stdout
        stderrs = self.stderr
        pyouts  = self.pyout
        output_lists = self.outputs
        results = self.get()

        targets = self.engine_id

        if groupby == "engine":
            for eid,stdout,stderr,outputs,r,pyout in zip(
                    targets, stdouts, stderrs, output_lists, results, pyouts
                ):
                self._display_stream(stdout, '[stdout:%i] ' % eid)
                self._display_stream(stderr, '[stderr:%i] ' % eid, file=sys.stderr)

                try:
                    get_ipython()
                except NameError:
                    # displaypub is meaningless outside IPython
                    return

                if outputs or pyout is not None:
                    _raw_text('[output:%i]' % eid)

                for output in outputs:
                    self._republish_displaypub(output, eid)

                if pyout is not None:
                    display(r)

        elif groupby in ('type', 'order'):
            # republish stdout:
            for eid,stdout in zip(targets, stdouts):
                self._display_stream(stdout, '[stdout:%i] ' % eid)

            # republish stderr:
            for eid,stderr in zip(targets, stderrs):
                self._display_stream(stderr, '[stderr:%i] ' % eid, file=sys.stderr)

            try:
                get_ipython()
            except NameError:
                # displaypub is meaningless outside IPython
                return

            if groupby == 'order':
                output_dict = dict((eid, outputs) for eid,outputs in zip(targets, output_lists))
                N = max(len(outputs) for outputs in output_lists)
                for i in range(N):
                    for eid in targets:
                        outputs = output_dict[eid]
                        if len(outputs) >= N:
                            _raw_text('[output:%i]' % eid)
                            self._republish_displaypub(outputs[i], eid)
            else:
                # republish displaypub output
                for eid,outputs in zip(targets, output_lists):
                    if outputs:
                        _raw_text('[output:%i]' % eid)
                    for output in outputs:
                        self._republish_displaypub(output, eid)

            # finally, add pyout:
            for eid,r,pyout in zip(targets, results, pyouts):
                if pyout is not None:
                    display(r)

        else:
            raise ValueError("groupby must be one of 'type', 'engine', 'collate', not %r" % groupby)

Response: display the outputs of the computation


Instruction:
Here you have a function in Python 3, explain what it does
def _unordered_iter(self):
        try:
            rlist = self.get(0)
        except error.TimeoutError:
            pending = set(self.msg_ids)
            while pending:
                try:
                    self._client.wait(pending, 1e-3)
                except error.TimeoutError:
                    # ignore timeout error, because that only means
                    # *some* jobs are outstanding
                    pass
                # update ready set with those no longer outstanding:
                ready = pending.difference(self._client.outstanding)
                # update pending to exclude those that are finished
                pending = pending.difference(ready)
                while ready:
                    msg_id = ready.pop()
                    ar = AsyncResult(self._client, msg_id, self._fname)
                    rlist = ar.get()
                    try:
                        for r in rlist:
                            yield r
                    except TypeError:
                        # flattened, not a list
                        # this could get broken by flattened data that returns iterables
                        # but most calls to map do not expose the `flatten` argument
                        yield rlist
        else:
            # already done
            for r in rlist:
                yield r

Response: iterator for results as they arrive on FCFS basis ignoring submission order.


Instruction:
Here you have a function in Python 3, explain what it does
def wait(self, timeout=-1):
        start = time.time()
        if self._ready:
            return
        local_ids = filter(lambda msg_id: msg_id in self._client.outstanding, self.msg_ids)
        local_ready = self._client.wait(local_ids, timeout)
        if local_ready:
            remote_ids = filter(lambda msg_id: msg_id not in self._client.results, self.msg_ids)
            if not remote_ids:
                self._ready = True
            else:
                rdict = self._client.result_status(remote_ids, status_only=False)
                pending = rdict['pending']
                while pending and (timeout < 0 or time.time() < start+timeout):
                    rdict = self._client.result_status(remote_ids, status_only=False)
                    pending = rdict['pending']
                    if pending:
                        time.sleep(0.1)
                if not pending:
                    self._ready = True
        if self._ready:
            try:
                results = map(self._client.results.get, self.msg_ids)
                self._result = results
                if self._single_result:
                    r = results[0]
                    if isinstance(r, Exception):
                        raise r
                else:
                    results = error.collect_exceptions(results, self._fname)
                self._result = self._reconstruct_result(results)
            except Exception, e:
                self._exception = e
                self._success = False
            else:
                self._success = True
            finally:
                self._metadata = map(self._client.metadata.get, self.msg_ids)

Response: wait for result to complete.


Instruction:
How would you implement a function in Python 3 that
returns the absolute normalized form of filename.

Response:
def abs_file(filename):
    """Return the absolute normalized form of `filename`."""
    path = os.path.expandvars(os.path.expanduser(filename))
    path = os.path.abspath(os.path.realpath(path))
    path = actual_path(path)
    return path


Instruction:
Write a Python 3 function for
preparing the file patterns for use in a FnmatchMatcher.

Response:
def prep_patterns(patterns):
    """Prepare the file patterns for use in a `FnmatchMatcher`.

    If a pattern starts with a wildcard, it is used as a pattern
    as-is.  If it does not start with a wildcard, then it is made
    absolute with the current directory.

    If `patterns` is None, an empty list is returned.

    """
    prepped = []
    for p in patterns or []:
        if p.startswith("*") or p.startswith("?"):
            prepped.append(p)
        else:
            prepped.append(abs_file(p))
    return prepped


Instruction:
Here you have a function in Python 3, explain what it does
def sep(s):
    sep_match = re.search(r"[\\/]", s)
    if sep_match:
        the_sep = sep_match.group(0)
    else:
        the_sep = os.sep
    return the_sep

Response: Find the path separator used in this string or os. sep if none is found.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def find_python_files(dirname):
    for i, (dirpath, dirnames, filenames) in enumerate(os.walk(dirname)):
        if i > 0 and '__init__.py' not in filenames:
            # If a directory doesn't have __init__.py, then it isn't
            # importable and neither are its files
            del dirnames[:]
            continue
        for filename in filenames:
            # We're only interested in files that look like reasonable Python
            # files: Must end with .py or .pyw, and must not have certain funny
            # characters that probably mean they are editor junk.
            if re.match(r"^[^.#~!$@%^&*()+=,]+\.pyw?$", filename):
                yield os.path.join(dirpath, filename)

Response: Yield all of the importable Python files in a directory recursively.


Instruction:
How would you explain what the following Python 3 function does
def relative_filename(self, filename):
        fnorm = os.path.normcase(filename)
        if fnorm.startswith(self.relative_dir):
            filename = filename[len(self.relative_dir):]
        return filename

Response: Return the relative form of filename.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def canonical_filename(self, filename):
        if filename not in self.canonical_filename_cache:
            if not os.path.isabs(filename):
                for path in [os.curdir] + sys.path:
                    if path is None:
                        continue
                    f = os.path.join(path, filename)
                    if os.path.exists(f):
                        filename = f
                        break
            cf = abs_file(filename)
            self.canonical_filename_cache[filename] = cf
        return self.canonical_filename_cache[filename]

Response: Return a canonical filename for filename.


Instruction:
How would you explain what the following Python 3 function does
def get_zip_data(self, filename):
        import zipimport
        markers = ['.zip'+os.sep, '.egg'+os.sep]
        for marker in markers:
            if marker in filename:
                parts = filename.split(marker)
                try:
                    zi = zipimport.zipimporter(parts[0]+marker[:-1])
                except zipimport.ZipImportError:
                    continue
                try:
                    data = zi.get_data(parts[1])
                except IOError:
                    continue
                return to_string(data)
        return None

Response: Get data from filename if it is a zip file path.


Instruction:
How would you code a function in Python 3 to
do fpath indicate a file in one of our trees?

Response:
def match(self, fpath):
        """Does `fpath` indicate a file in one of our trees?"""
        for d in self.dirs:
            if fpath.startswith(d):
                if fpath == d:
                    # This is the same file!
                    return True
                if fpath[len(d)] == os.sep:
                    # This is a file in the directory
                    return True
        return False


Instruction:
Write a Python 3 function for
doing fpath match one of our filename patterns?

Response:
def match(self, fpath):
        """Does `fpath` match one of our filename patterns?"""
        for pat in self.pats:
            if fnmatch.fnmatch(fpath, pat):
                return True
        return False


Instruction:
Can you tell what is the following Python 3 function doing
def add(self, pattern, result):
        # The pattern can't end with a wildcard component.
        pattern = pattern.rstrip(r"\/")
        if pattern.endswith("*"):
            raise CoverageException("Pattern must not end with wildcards.")
        pattern_sep = sep(pattern)

        # The pattern is meant to match a filepath.  Let's make it absolute
        # unless it already is, or is meant to match any prefix.
        if not pattern.startswith('*') and not isabs_anywhere(pattern):
            pattern = abs_file(pattern)
        pattern += pattern_sep

        # Make a regex from the pattern.  fnmatch always adds a \Z or $ to
        # match the whole string, which we don't want.
        regex_pat = fnmatch.translate(pattern).replace(r'\Z(', '(')
        if regex_pat.endswith("$"):
            regex_pat = regex_pat[:-1]
        # We want */a/b.py to match on Windows too, so change slash to match
        # either separator.
        regex_pat = regex_pat.replace(r"\/", r"[\\/]")
        # We want case-insensitive matching, so add that flag.
        regex = re.compile(r"(?i)" + regex_pat)

        # Normalize the result: it must end with a path separator.
        result_sep = sep(result)
        result = result.rstrip(r"\/") + result_sep
        self.aliases.append((regex, result, pattern_sep, result_sep))

Response: Add the pattern and result pair to the list of aliases.


Instruction:
Make a summary of the following Python 3 code
def map(self, path):
        for regex, result, pattern_sep, result_sep in self.aliases:
            m = regex.match(path)
            if m:
                new = path.replace(m.group(0), result)
                if pattern_sep != result_sep:
                    new = new.replace(pattern_sep, result_sep)
                if self.locator:
                    new = self.locator.canonical_filename(new)
                return new
        return path

Response: Map a path through the aliases.


Instruction:
Given the following Python 3 function, write the documentation
def loop_qt4(kernel):

    from IPython.external.qt_for_kernel import QtCore
    from IPython.lib.guisupport import get_app_qt4, start_event_loop_qt4

    kernel.app = get_app_qt4([" "])
    kernel.app.setQuitOnLastWindowClosed(False)
    kernel.timer = QtCore.QTimer()
    kernel.timer.timeout.connect(kernel.do_one_iteration)
    # Units for the timer are in milliseconds
    kernel.timer.start(1000*kernel._poll_interval)
    start_event_loop_qt4(kernel.app)

Response: Start a kernel with PyQt4 event loop integration.


Instruction:
Write a Python 3 script for
starting a kernel with wx event loop support.

Response:
def loop_wx(kernel):
    """Start a kernel with wx event loop support."""

    import wx
    from IPython.lib.guisupport import start_event_loop_wx

    doi = kernel.do_one_iteration
     # Wx uses milliseconds
    poll_interval = int(1000*kernel._poll_interval)

    # We have to put the wx.Timer in a wx.Frame for it to fire properly.
    # We make the Frame hidden when we create it in the main app below.
    class TimerFrame(wx.Frame):
        def __init__(self, func):
            wx.Frame.__init__(self, None, -1)
            self.timer = wx.Timer(self)
            # Units for the timer are in milliseconds
            self.timer.Start(poll_interval)
            self.Bind(wx.EVT_TIMER, self.on_timer)
            self.func = func

        def on_timer(self, event):
            self.func()

    # We need a custom wx.App to create our Frame subclass that has the
    # wx.Timer to drive the ZMQ event loop.
    class IPWxApp(wx.App):
        def OnInit(self):
            self.frame = TimerFrame(doi)
            self.frame.Show(False)
            return True

    # The redirect=False here makes sure that wx doesn't replace
    # sys.stdout/stderr with its own classes.
    kernel.app = IPWxApp(redirect=False)

    # The import of wx on Linux sets the handler for signal.SIGINT
    # to 0.  This is a bug in wx or gtk.  We fix by just setting it
    # back to the Python default.
    import signal
    if not callable(signal.getsignal(signal.SIGINT)):
        signal.signal(signal.SIGINT, signal.default_int_handler)

    start_event_loop_wx(kernel.app)


Instruction:
How would you implement a function in Python 3 that
starts a kernel with the Tk event loop.

Response:
def loop_tk(kernel):
    """Start a kernel with the Tk event loop."""

    import Tkinter
    doi = kernel.do_one_iteration
    # Tk uses milliseconds
    poll_interval = int(1000*kernel._poll_interval)
    # For Tkinter, we create a Tk object and call its withdraw method.
    class Timer(object):
        def __init__(self, func):
            self.app = Tkinter.Tk()
            self.app.withdraw()
            self.func = func

        def on_timer(self):
            self.func()
            self.app.after(poll_interval, self.on_timer)

        def start(self):
            self.on_timer()  # Call it once to get things going.
            self.app.mainloop()

    kernel.timer = Timer(doi)
    kernel.timer.start()


Instruction:
Explain what the following Python 3 code does
def loop_gtk(kernel):
    from .gui.gtkembed import GTKEmbed

    gtk_kernel = GTKEmbed(kernel)
    gtk_kernel.start()

Response: Start the kernel coordinating with the GTK event loop


Instruction:
Implement a Python 3 function for
starting the kernel and return a CFRunLoop event loop for the Cocoa event loop.

Response:
def loop_cocoa(kernel):
    """Start the kernel, coordinating with the Cocoa CFRunLoop event loop
    via the matplotlib MacOSX backend.
    """
    import matplotlib
    if matplotlib.__version__ < '1.1.0':
        kernel.log.warn(
        "MacOSX backend in matplotlib %s doesn't have a Timer, "
        "falling back on Tk for CFRunLoop integration.  Note that "
        "even this won't work if Tk is linked against X11 instead of "
        "Cocoa (e.g. EPD).  To use the MacOSX backend in the kernel, "
        "you must use matplotlib >= 1.1.0, or a native libtk."
        )
        return loop_tk(kernel)

    from matplotlib.backends.backend_macosx import TimerMac, show

    # scale interval for sec->ms
    poll_interval = int(1000*kernel._poll_interval)

    real_excepthook = sys.excepthook
    def handle_int(etype, value, tb):
        """don't let KeyboardInterrupts look like crashes"""
        if etype is KeyboardInterrupt:
            io.raw_print("KeyboardInterrupt caught in CFRunLoop")
        else:
            real_excepthook(etype, value, tb)

    # add doi() as a Timer to the CFRunLoop
    def doi():
        # restore excepthook during IPython code
        sys.excepthook = real_excepthook
        kernel.do_one_iteration()
        # and back:
        sys.excepthook = handle_int

    t = TimerMac(poll_interval)
    t.add_callback(doi)
    t.start()

    # but still need a Poller for when there are no active windows,
    # during which time mainloop() returns immediately
    poller = zmq.Poller()
    if kernel.control_stream:
        poller.register(kernel.control_stream.socket, zmq.POLLIN)
    for stream in kernel.shell_streams:
        poller.register(stream.socket, zmq.POLLIN)

    while True:
        try:
            # double nested try/except, to properly catch KeyboardInterrupt
            # due to pyzmq Issue #130
            try:
                # don't let interrupts during mainloop invoke crash_handler:
                sys.excepthook = handle_int
                show.mainloop()
                sys.excepthook = real_excepthook
                # use poller if mainloop returned (no windows)
                # scale by extra factor of 10, since it's a real poll
                poller.poll(10*poll_interval)
                kernel.do_one_iteration()
            except:
                raise
        except KeyboardInterrupt:
            # Ctrl-C shouldn't crash the kernel
            io.raw_print("KeyboardInterrupt caught in kernel")
        finally:
            # ensure excepthook is restored
            sys.excepthook = real_excepthook


Instruction:
Can you generate the documentation for the following Python 3 function
def enable_gui(gui, kernel=None):
    if gui not in loop_map:
        raise ValueError("GUI %r not supported" % gui)
    if kernel is None:
        if Application.initialized():
            kernel = getattr(Application.instance(), 'kernel', None)
        if kernel is None:
            raise RuntimeError("You didn't specify a kernel,"
                " and no IPython Application with a kernel appears to be running."
            )
    loop = loop_map[gui]
    if kernel.eventloop is not None and kernel.eventloop is not loop:
        raise RuntimeError("Cannot activate multiple GUI eventloops")
    kernel.eventloop = loop

Response: Enable integration with a given GUI


Instruction:
Can you tell what is the following Python 3 function doing
def GOE(N):
    m = ra.standard_normal((N,N))
    m += m.T
    return m/2

Response: Creates an NxN element of the Gaussian Orthogonal Ensemble


Instruction:
Make a summary of the following Python 3 code
def center_eigenvalue_diff(mat):
    N = len(mat)
    evals = np.sort(la.eigvals(mat))
    diff = np.abs(evals[N/2] - evals[N/2-1])
    return diff

Response: Compute the eigvals of mat and then find the center eigval difference.


Instruction:
Can you tell what is the following Python 3 function doing
def ensemble_diffs(num, N):
    diffs = np.empty(num)
    for i in xrange(num):
        mat = GOE(N)
        diffs[i] = center_eigenvalue_diff(mat)
    return diffs

Response: Return num eigenvalue diffs for the NxN GOE ensemble.


Instruction:
Write a Python 3 script for
initializing the item. This calls the class constructor with the appropriate arguments and returns the initialized object.

Response:
def init(self, ctxt, step_addr):
        """
        Initialize the item.  This calls the class constructor with the
        appropriate arguments and returns the initialized object.

        :param ctxt: The context object.
        :param step_addr: The address of the step in the test
                          configuration.
        """

        return self.cls(ctxt, self.name, self.conf, step_addr)


Instruction:
Make a summary of the following Python 3 code
def parse_file(cls, ctxt, fname, key=None, step_addr=None):

        # Load the YAML file
        try:
            with open(fname) as f:
                step_data = yaml.load(f)
        except Exception as exc:
            raise ConfigError(
                'Failed to read file "%s": %s' % (fname, exc),
                step_addr,
            )

        # Do we have a key?
        if key is not None:
            if (not isinstance(step_data, collections.Mapping) or
                    key not in step_data):
                raise ConfigError(
                    'Bad step configuration file "%s": expecting dictionary '
                    'with key "%s"' % (fname, key),
                    step_addr,
                )

            # Extract just the step data
            step_data = step_data[key]

        # Validate that it's a sequence
        if not isinstance(step_data, collections.Sequence):
            addr = ('%s[%s]' % (fname, key)) if key is not None else fname
            raise ConfigError(
                'Bad step configuration sequence at %s: expecting list, '
                'not "%s"' % (addr, step_data.__class__.__name__),
                step_addr,
            )

        # OK, assemble the step list and return it
        steps = []
        for idx, step_conf in enumerate(step_data):
            steps.extend(cls.parse_step(
                ctxt, StepAddress(fname, idx, key), step_conf))

        return steps

Response: Parse a YAML file containing test steps.


Instruction:
Create a Python 3 function to
parse a step dictionary into a list of steps.

Response:
def parse_step(cls, ctxt, step_addr, step_conf):
        """
        Parse a step dictionary.

        :param ctxt: The context object.
        :param step_addr: The address of the step in the test
                          configuration.
        :param step_conf: The description of the step.  This may be a
                          scalar string or a dictionary.

        :returns: A list of steps.
        """

        # Make sure the step makes sense
        if isinstance(step_conf, six.string_types):
            # Convert string to a dict for uniformity of processing
            step_conf = {step_conf: None}
        elif not isinstance(step_conf, collections.Mapping):
            raise ConfigError(
                'Unable to parse step configuration: expecting string or '
                'dictionary, not "%s"' % step_conf.__class__.__name__,
                step_addr,
            )

        # Parse the configuration into the action and modifier classes
        # and the configuration to apply to each
        action_item = None
        mod_items = {}
        kwargs = {}  # extra args for Step.__init__()
        for key, key_conf in step_conf.items():
            # Handle special keys first
            if key in cls.schemas:
                # Validate the key
                utils.schema_validate(key_conf, cls.schemas[key], ConfigError,
                                      key, step_addr=step_addr)

                # Save the value
                kwargs[key] = key_conf

            # Is it an action?
            elif key in entry.points[NAMESPACE_ACTION]:
                if action_item is not None:
                    raise ConfigError(
                        'Bad step configuration: action "%s" specified, '
                        'but action "%s" already processed' %
                        (key, action_item.name),
                        step_addr,
                    )

                action_item = StepItem(
                    entry.points[NAMESPACE_ACTION][key], key, key_conf)

            # OK, is it a modifier?
            elif key in entry.points[NAMESPACE_MODIFIER]:
                mod_class = entry.points[NAMESPACE_MODIFIER][key]

                # Store it in priority order
                mod_items.setdefault(mod_class.priority, [])
                mod_items[mod_class.priority].append(StepItem(
                    mod_class, key, key_conf))

            # Couldn't resolve it
            else:
                raise ConfigError(
                    'Bad step configuration: unable to resolve action '
                    '"%s"' % key,
                    step_addr,
                )

        # Make sure we have an action
        if action_item is None:
            raise ConfigError(
                'Bad step configuration: no action specified',
                step_addr,
            )

        # What is the action type?
        action_type = (Modifier.STEP if action_item.cls.step_action
                       else Modifier.NORMAL)

        # OK, build our modifiers list and preprocess the action
        # configuration
        modifiers = []
        for mod_item in utils.iter_prio_dict(mod_items):
            # Verify that the modifier is compatible with the
            # action
            if mod_item.cls.restriction & action_type == 0:
                raise ConfigError(
                    'Bad step configuration: modifier "%s" is '
                    'incompatible with the action "%s"' %
                    (mod_item.name, action_item.name),
                    step_addr,
                )

            # Initialize the modifier
            modifier = mod_item.init(ctxt, step_addr)

            # Add it to the list of modifiers
            modifiers.append(modifier)

            # Apply the modifier's configuration processing
            action_item.conf = modifier.action_conf(
                ctxt, action_item.cls, action_item.name, action_item.conf,
                step_addr)

        # Now we can initialize the action
        action = action_item.init(ctxt, step_addr)

        # Create the step
        step = cls(step_addr, action, modifiers, **kwargs)

        # If the final_action is a StepAction, invoke it now and
        # return the list of steps.  We do this after creating the
        # Step object so that we can take advantage of its handling of
        # modifiers.
        if action_item.cls.step_action:
            return step(ctxt)

        # Not a step action, return the step as a list of one element
        return [step]


Instruction:
Can you generate a brief explanation for the following Python 3 code
def init_crash_handler(self):
        self.crash_handler = self.crash_handler_class(self)
        sys.excepthook = self.excepthook
        def unset_crashhandler():
            sys.excepthook = sys.__excepthook__
        atexit.register(unset_crashhandler)

Response: Create a crash handler typically setting sys. excepthook to it.


Instruction:
Write a Python 3 function for
loading the config file.

Response:
def load_config_file(self, suppress_errors=True):
        """Load the config file.

        By default, errors in loading config are handled, and a warning
        printed on screen. For testing, the suppress_errors option is set
        to False, so errors will make tests fail.
        """
        self.log.debug("Searching path %s for config files", self.config_file_paths)
        base_config = 'ipython_config.py'
        self.log.debug("Attempting to load config file: %s" %
                       base_config)
        try:
            Application.load_config_file(
                self,
                base_config,
                path=self.config_file_paths
            )
        except ConfigFileNotFound:
            # ignore errors loading parent
            self.log.debug("Config file %s not found", base_config)
            pass
        if self.config_file_name == base_config:
            # don't load secondary config
            return
        self.log.debug("Attempting to load config file: %s" %
                       self.config_file_name)
        try:
            Application.load_config_file(
                self,
                self.config_file_name,
                path=self.config_file_paths
            )
        except ConfigFileNotFound:
            # Only warn if the default config file was NOT being used.
            if self.config_file_specified:
                msg = self.log.warn
            else:
                msg = self.log.debug
            msg("Config file not found, skipping: %s", self.config_file_name)
        except:
            # For testing purposes.
            if not suppress_errors:
                raise
            self.log.warn("Error loading config file: %s" %
                          self.config_file_name, exc_info=True)


Instruction:
Can you tell what is the following Python 3 function doing
def init_profile_dir(self):
        try:
            # location explicitly specified:
            location = self.config.ProfileDir.location
        except AttributeError:
            # location not specified, find by profile name
            try:
                p = ProfileDir.find_profile_dir_by_name(self.ipython_dir, self.profile, self.config)
            except ProfileDirError:
                # not found, maybe create it (always create default profile)
                if self.auto_create or self.profile=='default':
                    try:
                        p = ProfileDir.create_profile_dir_by_name(self.ipython_dir, self.profile, self.config)
                    except ProfileDirError:
                        self.log.fatal("Could not create profile: %r"%self.profile)
                        self.exit(1)
                    else:
                        self.log.info("Created profile dir: %r"%p.location)
                else:
                    self.log.fatal("Profile %r not found."%self.profile)
                    self.exit(1)
            else:
                self.log.info("Using existing profile dir: %r"%p.location)
        else:
            # location is fully specified
            try:
                p = ProfileDir.find_profile_dir(location, self.config)
            except ProfileDirError:
                # not found, maybe create it
                if self.auto_create:
                    try:
                        p = ProfileDir.create_profile_dir(location, self.config)
                    except ProfileDirError:
                        self.log.fatal("Could not create profile directory: %r"%location)
                        self.exit(1)
                    else:
                        self.log.info("Creating new profile dir: %r"%location)
                else:
                    self.log.fatal("Profile directory %r not found."%location)
                    self.exit(1)
            else:
                self.log.info("Using existing profile dir: %r"%location)

        self.profile_dir = p
        self.config_file_paths.append(p.location)

Response: initialize the profile dir


Instruction:
Can you generate the documentation for the following Python 3 function
def init_config_files(self):
        # copy config files
        path = self.builtin_profile_dir
        if self.copy_config_files:
            src = self.profile

            cfg = self.config_file_name
            if path and os.path.exists(os.path.join(path, cfg)):
                self.log.warn("Staging %r from %s into %r [overwrite=%s]"%(
                        cfg, src, self.profile_dir.location, self.overwrite)
                )
                self.profile_dir.copy_config_file(cfg, path=path, overwrite=self.overwrite)
            else:
                self.stage_default_config_file()
        else:
            # Still stage *bundled* config files, but not generated ones
            # This is necessary for `ipython profile=sympy` to load the profile
            # on the first go
            files = glob.glob(os.path.join(path, '*.py'))
            for fullpath in files:
                cfg = os.path.basename(fullpath)
                if self.profile_dir.copy_config_file(cfg, path=path, overwrite=False):
                    # file was copied
                    self.log.warn("Staging bundled %s from %s into %r"%(
                            cfg, self.profile, self.profile_dir.location)
                    )

Response: Copy default config files into profile dir.


Instruction:
How would you implement a function in Python 3 that
reads coverage data from the coverage data file.

Response:
def read(self):
        """Read coverage data from the coverage data file (if it exists)."""
        if self.use_file:
            self.lines, self.arcs = self._read_file(self.filename)
        else:
            self.lines, self.arcs = {}, {}


Instruction:
Write a Python 3 script to
write the collected coverage data to a file.

Response:
def write(self, suffix=None):
        """Write the collected coverage data to a file.

        `suffix` is a suffix to append to the base file name. This can be used
        for multiple or parallel execution, so that many coverage data files
        can exist simultaneously.  A dot will be used to join the base name and
        the suffix.

        """
        if self.use_file:
            filename = self.filename
            if suffix:
                filename += "." + suffix
            self.write_file(filename)


Instruction:
Make a summary of the following Python 3 code
def erase(self):
        if self.use_file:
            if self.filename:
                file_be_gone(self.filename)
        self.lines = {}
        self.arcs = {}

Response: Erase the data both in this object and from its file storage.


Instruction:
Here you have a function in Python 3, explain what it does
def line_data(self):
        return dict(
            [(f, sorted(lmap.keys())) for f, lmap in iitems(self.lines)]
            )

Response: Return the map from filenames to lists of line numbers executed.


Instruction:
Implement a Python 3 function for
returning the map from filenames to lists of line number pairs.

Response:
def arc_data(self):
        """Return the map from filenames to lists of line number pairs."""
        return dict(
            [(f, sorted(amap.keys())) for f, amap in iitems(self.arcs)]
            )


Instruction:
Write a Python 3 function that can
write the coverage data to filename.

Response:
def write_file(self, filename):
        """Write the coverage data to `filename`."""

        # Create the file data.
        data = {}

        data['lines'] = self.line_data()
        arcs = self.arc_data()
        if arcs:
            data['arcs'] = arcs

        if self.collector:
            data['collector'] = self.collector

        if self.debug and self.debug.should('dataio'):
            self.debug.write("Writing data to %r" % (filename,))

        # Write the pickle to the file.
        fdata = open(filename, 'wb')
        try:
            pickle.dump(data, fdata, 2)
        finally:
            fdata.close()


Instruction:
Implement a function in Python 3 to
read the coverage data from filename.

Response:
def read_file(self, filename):
        """Read the coverage data from `filename`."""
        self.lines, self.arcs = self._read_file(filename)


Instruction:
Can you create a Python 3 function that
returns the raw pickled data from filename.

Response:
def raw_data(self, filename):
        """Return the raw pickled data from `filename`."""
        if self.debug and self.debug.should('dataio'):
            self.debug.write("Reading data from %r" % (filename,))
        fdata = open(filename, 'rb')
        try:
            data = pickle.load(fdata)
        finally:
            fdata.close()
        return data


Instruction:
Here you have a function in Python 3, explain what it does
def _read_file(self, filename):
        lines = {}
        arcs = {}
        try:
            data = self.raw_data(filename)
            if isinstance(data, dict):
                # Unpack the 'lines' item.
                lines = dict([
                    (f, dict.fromkeys(linenos, None))
                        for f, linenos in iitems(data.get('lines', {}))
                    ])
                # Unpack the 'arcs' item.
                arcs = dict([
                    (f, dict.fromkeys(arcpairs, None))
                        for f, arcpairs in iitems(data.get('arcs', {}))
                    ])
        except Exception:
            pass
        return lines, arcs

Response: Read the stored coverage data from the given file.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def combine_parallel_data(self, aliases=None):
        aliases = aliases or PathAliases()
        data_dir, local = os.path.split(self.filename)
        localdot = local + '.'
        for f in os.listdir(data_dir or '.'):
            if f.startswith(localdot):
                full_path = os.path.join(data_dir, f)
                new_lines, new_arcs = self._read_file(full_path)
                for filename, file_data in iitems(new_lines):
                    filename = aliases.map(filename)
                    self.lines.setdefault(filename, {}).update(file_data)
                for filename, file_data in iitems(new_arcs):
                    filename = aliases.map(filename)
                    self.arcs.setdefault(filename, {}).update(file_data)
                if f != local:
                    os.remove(full_path)

Response: Combine a number of data files together.


Instruction:
Make a summary of the following Python 3 code
def add_line_data(self, line_data):
        for filename, linenos in iitems(line_data):
            self.lines.setdefault(filename, {}).update(linenos)

Response: Add executed line data.


Instruction:
Given the following Python 3 function, write the documentation
def add_arc_data(self, arc_data):
        for filename, arcs in iitems(arc_data):
            self.arcs.setdefault(filename, {}).update(arcs)

Response: Add measured arc data. Arc data is a dictionary of filename - > arcs.


Instruction:
Write a Python 3 function for
contributing filename s data to the Md5Hash hasher.

Response:
def add_to_hash(self, filename, hasher):
        """Contribute `filename`'s data to the Md5Hash `hasher`."""
        hasher.update(self.executed_lines(filename))
        hasher.update(self.executed_arcs(filename))


Instruction:
Can you tell what is the following Python 3 function doing
def summary(self, fullpath=False):
        summ = {}
        if fullpath:
            filename_fn = lambda f: f
        else:
            filename_fn = os.path.basename
        for filename, lines in iitems(self.lines):
            summ[filename_fn(filename)] = len(lines)
        return summ

Response: Return a dict summarizing the coverage data.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_pasted_lines(sentinel, l_input=py3compat.input):
    print "Pasting code; enter '%s' alone on the line to stop or use Ctrl-D." \
          % sentinel
    while True:
        try:
            l = l_input(':')
            if l == sentinel:
                return
            else:
                yield l
        except EOFError:
            print '<EOF>'
            return

Response: Yield pasted lines until the user enters the given sentinel value.


Instruction:
Create a Python 3 function for
starting the mainloop. If an optional banner argument is given, it will override the internally created default banner.

Response:
def mainloop(self, display_banner=None):
        """Start the mainloop.

        If an optional banner argument is given, it will override the
        internally created default banner.
        """

        with nested(self.builtin_trap, self.display_trap):

            while 1:
                try:
                    self.interact(display_banner=display_banner)
                    #self.interact_with_readline()
                    # XXX for testing of a readline-decoupled repl loop, call
                    # interact_with_readline above
                    break
                except KeyboardInterrupt:
                    # this should not be necessary, but KeyboardInterrupt
                    # handling seems rather unpredictable...
                    self.write("\nKeyboardInterrupt in interact()\n")


Instruction:
How would you explain what the following Python 3 function does
def _replace_rlhist_multiline(self, source_raw, hlen_before_cell):

        # do nothing without readline or disabled multiline
        if not self.has_readline or not self.multiline_history:
            return hlen_before_cell

        # windows rl has no remove_history_item
        if not hasattr(self.readline, "remove_history_item"):
            return hlen_before_cell

        # skip empty cells
        if not source_raw.rstrip():
            return hlen_before_cell

        # nothing changed do nothing, e.g. when rl removes consecutive dups
        hlen = self.readline.get_current_history_length()
        if hlen == hlen_before_cell:
            return hlen_before_cell

        for i in range(hlen - hlen_before_cell):
            self.readline.remove_history_item(hlen - i - 1)
        stdin_encoding = get_stream_enc(sys.stdin, 'utf-8')
        self.readline.add_history(py3compat.unicode_to_str(source_raw.rstrip(),
                                    stdin_encoding))
        return self.readline.get_current_history_length()

Response: Replace the history of a single line with a single entry in history


Instruction:
Can you tell what is the following Python 3 function doing
def raw_input(self, prompt=''):
        # Code run by the user may have modified the readline completer state.
        # We must ensure that our completer is back in place.

        if self.has_readline:
            self.set_readline_completer()

        # raw_input expects str, but we pass it unicode sometimes
        prompt = py3compat.cast_bytes_py2(prompt)

        try:
            line = py3compat.str_to_unicode(self.raw_input_original(prompt))
        except ValueError:
            warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
                 " or sys.stdout.close()!\nExiting IPython!\n")
            self.ask_exit()
            return ""

        # Try to be reasonably smart about not re-indenting pasted input more
        # than necessary.  We do this by trimming out the auto-indent initial
        # spaces, if the user's actual input started itself with whitespace.
        if self.autoindent:
            if num_ini_spaces(line) > self.indent_current_nsp:
                line = line[self.indent_current_nsp:]
                self.indent_current_nsp = 0

        return line

Response: Write a prompt and read a line.


Instruction:
Here you have a function in Python 3, explain what it does
def _should_recompile(self,e):

        if e.filename in ('<ipython console>','<input>','<string>',
                          '<console>','<BackgroundJob compilation>',
                          None):

            return False
        try:
            if (self.autoedit_syntax and
                not self.ask_yes_no('Return to editor to correct syntax error? '
                              '[Y/n] ','y')):
                return False
        except EOFError:
            return False

        def int0(x):
            try:
                return int(x)
            except TypeError:
                return 0
        # always pass integer line and offset values to editor hook
        try:
            self.hooks.fix_error_editor(e.filename,
                int0(e.lineno),int0(e.offset),e.msg)
        except TryNext:
            warn('Could not open editor')
            return False
        return True

Response: Utility routine for edit_syntax_error


Instruction:
Make a summary of the following Python 3 code
def exit(self):
        if self.confirm_exit:
            if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
                self.ask_exit()
        else:
            self.ask_exit()

Response:
Handle interactive exit.
            This method calls the ask_exit callback.


Instruction:
Can you implement a function in Python 3 that
returns the correct repository URL and revision by parsing the given repository URL

Response:
def get_url_rev(self):
        """
        Returns the correct repository URL and revision by parsing the given
        repository URL
        """
        error_message= (
           "Sorry, '%s' is a malformed VCS url. "
           "Ihe format is <vcs>+<protocol>://<url>, "
           "e.g. svn+http://myrepo/svn/MyApp#egg=MyApp")
        assert '+' in self.url, error_message % self.url
        url = self.url.split('+', 1)[1]
        scheme, netloc, path, query, frag = urlparse.urlsplit(url)
        rev = None
        if '@' in path:
            path, rev = path.rsplit('@', 1)
        url = urlparse.urlunsplit((scheme, netloc, path, query, ''))
        return url, rev


Instruction:
Can you write a function in Python 3 where it
disposes of the exitable object.

Response:
def dispose_at_exit(exitable):
    '''
    register `exitable.__exit__()` into `atexit` module.

    return the `exitable` itself.
    '''
    @atexit.register
    def callback():
        exitable.__exit__(*sys.exc_info())
    return exitable


Instruction:
Can you create a Python 3 function that
creates and return a new frontend master.

Response:
def new_frontend_master(self):
        """ Create and return new frontend attached to new kernel, launched on localhost.
        """
        ip = self.ip if self.ip in LOCAL_IPS else LOCALHOST
        kernel_manager = self.kernel_manager_class(
                                ip=ip,
                                connection_file=self._new_connection_file(),
                                config=self.config,
        )
        # start the kernel
        kwargs = dict()
        kwargs['extra_arguments'] = self.kernel_argv
        kernel_manager.start_kernel(**kwargs)
        kernel_manager.start_channels()
        widget = self.widget_factory(config=self.config,
                                   local_kernel=True)
        self.init_colors(widget)
        widget.kernel_manager = kernel_manager
        widget._existing = False
        widget._may_close = True
        widget._confirm_exit = self.confirm_exit
        return widget


Instruction:
Explain what the following Python 3 code does
def new_frontend_slave(self, current_widget):
        kernel_manager = self.kernel_manager_class(
                                connection_file=current_widget.kernel_manager.connection_file,
                                config = self.config,
        )
        kernel_manager.load_connection_file()
        kernel_manager.start_channels()
        widget = self.widget_factory(config=self.config,
                                local_kernel=False)
        self.init_colors(widget)
        widget._existing = True
        widget._may_close = False
        widget._confirm_exit = False
        widget.kernel_manager = kernel_manager
        return widget

Response: Create and return a new frontend attached to an existing kernel.


Instruction:
Create a Python 3 function for
configuring the coloring of the given widget.

Response:
def init_colors(self, widget):
        """Configure the coloring of the widget"""
        # Note: This will be dramatically simplified when colors
        # are removed from the backend.

        # parse the colors arg down to current known labels
        try:
            colors = self.config.ZMQInteractiveShell.colors
        except AttributeError:
            colors = None
        try:
            style = self.config.IPythonWidget.syntax_style
        except AttributeError:
            style = None
        try:
            sheet = self.config.IPythonWidget.style_sheet
        except AttributeError:
            sheet = None

        # find the value for colors:
        if colors:
            colors=colors.lower()
            if colors in ('lightbg', 'light'):
                colors='lightbg'
            elif colors in ('dark', 'linux'):
                colors='linux'
            else:
                colors='nocolor'
        elif style:
            if style=='bw':
                colors='nocolor'
            elif styles.dark_style(style):
                colors='linux'
            else:
                colors='lightbg'
        else:
            colors=None

        # Configure the style
        if style:
            widget.style_sheet = styles.sheet_from_template(style, colors)
            widget.syntax_style = style
            widget._syntax_style_changed()
            widget._style_sheet_changed()
        elif colors:
            # use a default dark/light/bw style
            widget.set_default_style(colors=colors)

        if self.stylesheet:
            # we got an explicit stylesheet
            if os.path.isfile(self.stylesheet):
                with open(self.stylesheet) as f:
                    sheet = f.read()
            else:
                raise IOError("Stylesheet %r not found." % self.stylesheet)
        if sheet:
            widget.style_sheet = sheet
            widget._style_sheet_changed()


Instruction:
Can you tell what is the following Python 3 function doing
def info(self):
        return (self.identity, self.url, self.pub_url, self.location)

Response: return the connection info for this object s sockets.


Instruction:
Explain what the following Python 3 code does
def connect(self, peers):
        for peer, (ident, url, pub_url, location) in peers.items():
            self.peers[peer] = ident
            if ident != self.identity:
                self.sub.connect(disambiguate_url(pub_url, location))
            if ident > self.identity:
                # prevent duplicate xrep, by only connecting
                # engines to engines with higher IDENTITY
                # a doubly-connected pair will crash
                self.socket.connect(disambiguate_url(url, location))

Response:
connect to peers.  `peers` will be a dict of 4-tuples, keyed by name.
        {peer : (ident, addr, pub_addr, location)}
        where peer is the name, ident is the XREP identity, addr,pub_addr are the


Instruction:
Write a Python 3 function for
converting an object in R s namespace to one suitable for ipython s namespace.

Response:
def Rconverter(Robj, dataframe=False):
    """
    Convert an object in R's namespace to one suitable
    for ipython's namespace.

    For a data.frame, it tries to return a structured array.
    It first checks for colnames, then names.
    If all are NULL, it returns np.asarray(Robj), else
    it tries to construct a recarray

    Parameters
    ----------

    Robj: an R object returned from rpy2
    """
    is_data_frame = ro.r('is.data.frame')
    colnames = ro.r('colnames')
    rownames = ro.r('rownames') # with pandas, these could be used for the index
    names = ro.r('names')

    if dataframe:
        as_data_frame = ro.r('as.data.frame')
        cols = colnames(Robj)
        _names = names(Robj)
        if cols != ri.NULL:
            Robj = as_data_frame(Robj)
            names = tuple(np.array(cols))
        elif _names != ri.NULL:
            names = tuple(np.array(_names))
        else: # failed to find names
            return np.asarray(Robj)
        Robj = np.rec.fromarrays(Robj, names = names)
    return np.asarray(Robj)


Instruction:
How would you explain what the following Python 3 function does
def findsource(object):

    file = getsourcefile(object) or getfile(object)
    # If the object is a frame, then trying to get the globals dict from its
    # module won't work. Instead, the frame object itself has the globals
    # dictionary.
    globals_dict = None
    if inspect.isframe(object):
        # XXX: can this ever be false?
        globals_dict = object.f_globals
    else:
        module = getmodule(object, file)
        if module:
            globals_dict = module.__dict__
    lines = linecache.getlines(file, globals_dict)
    if not lines:
        raise IOError('could not get source code')

    if ismodule(object):
        return lines, 0

    if isclass(object):
        name = object.__name__
        pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
        # make some effort to find the best matching class definition:
        # use the one with the least indentation, which is the one
        # that's most probably not inside a function definition.
        candidates = []
        for i in range(len(lines)):
            match = pat.match(lines[i])
            if match:
                # if it's at toplevel, it's already the best one
                if lines[i][0] == 'c':
                    return lines, i
                # else add whitespace to candidate list
                candidates.append((match.group(1), i))
        if candidates:
            # this will sort by whitespace, and by line number,
            # less whitespace first
            candidates.sort()
            return lines, candidates[0][1]
        else:
            raise IOError('could not find class definition')

    if ismethod(object):
        object = object.im_func
    if isfunction(object):
        object = object.func_code
    if istraceback(object):
        object = object.tb_frame
    if isframe(object):
        object = object.f_code
    if iscode(object):
        if not hasattr(object, 'co_firstlineno'):
            raise IOError('could not find function definition')
        pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
        pmatch = pat.match
        # fperez - fix: sometimes, co_firstlineno can give a number larger than
        # the length of lines, which causes an error.  Safeguard against that.
        lnum = min(object.co_firstlineno,len(lines))-1
        while lnum > 0:
            if pmatch(lines[lnum]): break
            lnum -= 1

        return lines, lnum
    raise IOError('could not find code object')

Response: Return the entire source file and starting line number for an object.


Instruction:
Implement a Python 3 function for
trying to fix the filenames in each record from inspect. getinnerframes.

Response:
def fix_frame_records_filenames(records):
    """Try to fix the filenames in each record from inspect.getinnerframes().

    Particularly, modules loaded from within zip files have useless filenames
    attached to their code object, and inspect.getinnerframes() just uses it.
    """
    fixed_records = []
    for frame, filename, line_no, func_name, lines, index in records:
        # Look inside the frame's globals dictionary for __file__, which should
        # be better.
        better_fn = frame.f_globals.get('__file__', None)
        if isinstance(better_fn, str):
            # Check the type just in case someone did something weird with
            # __file__. It might also be None if the error occurred during
            # import.
            filename = better_fn
        fixed_records.append((frame, filename, line_no, func_name, lines, index))
    return fixed_records


Instruction:
Can you generate a brief explanation for the following Python 3 code
def color_toggle(self):

        if self.color_scheme_table.active_scheme_name == 'NoColor':
            self.color_scheme_table.set_active_scheme(self.old_scheme)
            self.Colors = self.color_scheme_table.active_colors
        else:
            self.old_scheme = self.color_scheme_table.active_scheme_name
            self.color_scheme_table.set_active_scheme('NoColor')
            self.Colors = self.color_scheme_table.active_colors

Response: Toggle between the currently active color scheme and NoColor.


Instruction:
Can you generate the documentation for the following Python 3 function
def text(self, etype, value, tb, tb_offset=None, context=5):
        tb_list = self.structured_traceback(etype, value, tb,
                                            tb_offset, context)
        return self.stb2text(tb_list)

Response:
Return formatted traceback.

        Subclasses may override this if they add extra arguments.


Instruction:
Can you write a function in Python 3 where it
returns a color formatted string with the traceback info.

Response:
def structured_traceback(self, etype, value, elist, tb_offset=None,
                             context=5):
        """Return a color formatted string with the traceback info.

        Parameters
        ----------
        etype : exception type
          Type of the exception raised.

        value : object
          Data stored in the exception

        elist : list
          List of frames, see class docstring for details.

        tb_offset : int, optional
          Number of frames in the traceback to skip.  If not given, the
          instance value is used (set in constructor).

        context : int, optional
          Number of lines of context information to print.

        Returns
        -------
        String with formatted exception.
        """
        tb_offset = self.tb_offset if tb_offset is None else tb_offset
        Colors = self.Colors
        out_list = []
        if elist:

            if tb_offset and len(elist) > tb_offset:
                elist = elist[tb_offset:]

            out_list.append('Traceback %s(most recent call last)%s:' %
                                (Colors.normalEm, Colors.Normal) + '\n')
            out_list.extend(self._format_list(elist))
        # The exception info should be a single entry in the list.
        lines = ''.join(self._format_exception_only(etype, value))
        out_list.append(lines)

        # Note: this code originally read:

        ## for line in lines[:-1]:
        ##     out_list.append(" "+line)
        ## out_list.append(lines[-1])

        # This means it was indenting everything but the last line by a little
        # bit.  I've disabled this for now, but if we see ugliness somewhre we
        # can restore it.

        return out_list


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _format_list(self, extracted_list):

        Colors = self.Colors
        list = []
        for filename, lineno, name, line in extracted_list[:-1]:
            item = '  File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
                    (Colors.filename, filename, Colors.Normal,
                     Colors.lineno, lineno, Colors.Normal,
                     Colors.name, name, Colors.Normal)
            if line:
                item += '    %s\n' % line.strip()
            list.append(item)
        # Emphasize the last entry
        filename, lineno, name, line = extracted_list[-1]
        item = '%s  File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
                (Colors.normalEm,
                 Colors.filenameEm, filename, Colors.normalEm,
                 Colors.linenoEm, lineno, Colors.normalEm,
                 Colors.nameEm, name, Colors.normalEm,
                 Colors.Normal)
        if line:
            item += '%s    %s%s\n' % (Colors.line, line.strip(),
                                            Colors.Normal)
        list.append(item)
        #from pprint import pformat; print 'LISTTB', pformat(list) # dbg
        return list

Response: Format a list of traceback entries for printing.


Instruction:
Can you tell what is the following Python 3 function doing
def _format_exception_only(self, etype, value):

        have_filedata = False
        Colors = self.Colors
        list = []
        stype = Colors.excName + etype.__name__ + Colors.Normal
        if value is None:
            # Not sure if this can still happen in Python 2.6 and above
            list.append( str(stype) + '\n')
        else:
            if etype is SyntaxError:
                have_filedata = True
                #print 'filename is',filename  # dbg
                if not value.filename: value.filename = "<string>"
                list.append('%s  File %s"%s"%s, line %s%d%s\n' % \
                        (Colors.normalEm,
                         Colors.filenameEm, value.filename, Colors.normalEm,
                         Colors.linenoEm, value.lineno, Colors.Normal  ))
                if value.text is not None:
                    i = 0
                    while i < len(value.text) and value.text[i].isspace():
                        i += 1
                    list.append('%s    %s%s\n' % (Colors.line,
                                                  value.text.strip(),
                                                  Colors.Normal))
                    if value.offset is not None:
                        s = '    '
                        for c in value.text[i:value.offset-1]:
                            if c.isspace():
                                s += c
                            else:
                                s += ' '
                        list.append('%s%s^%s\n' % (Colors.caret, s,
                                                   Colors.Normal) )

            try:
                s = value.msg
            except Exception:
                s = self._some_str(value)
            if s:
                list.append('%s%s:%s %s\n' % (str(stype), Colors.excName,
                                              Colors.Normal, s))
            else:
                list.append('%s\n' % str(stype))

        # sync with user hooks
        if have_filedata:
            ipinst = ipapi.get()
            if ipinst is not None:
                ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0)

        return list

Response: Format the exception part of a traceback.