Instruction:
Implement a function in Python 3 to
convert a value to a string for display purpose

Response:
def value_to_display(value, minmax=False, level=0):
    """Convert value for display purpose"""
    # To save current Numpy printoptions
    np_printoptions = FakeObject

    try:
        numeric_numpy_types = (int64, int32, int16, int8,
                               uint64, uint32, uint16, uint8,
                               float64, float32, float16,
                               complex128, complex64, bool_)
        if ndarray is not FakeObject:
            # Save printoptions
            np_printoptions = get_printoptions()
            # Set max number of elements to show for Numpy arrays
            # in our display
            set_printoptions(threshold=10)
        if isinstance(value, recarray):
            if level == 0:
                fields = value.names
                display = 'Field names: ' + ', '.join(fields)
            else:
                display = 'Recarray'
        elif isinstance(value, MaskedArray):
            display = 'Masked array'
        elif isinstance(value, ndarray):
            if level == 0:
                if minmax:
                    try:
                        display = 'Min: %r\nMax: %r' % (value.min(), value.max())
                    except (TypeError, ValueError):
                        if value.dtype.type in numeric_numpy_types:
                            display = str(value)
                        else:
                            display = default_display(value)
                elif value.dtype.type in numeric_numpy_types:
                    display = str(value)
                else:
                    display = default_display(value)
            else:
                display = 'Numpy array'
        elif any([type(value) == t for t in [list, set, tuple, dict]]):
            display = collections_display(value, level+1)
        elif isinstance(value, Image):
            if level == 0:
                display = '%s  Mode: %s' % (address(value), value.mode)
            else:
                display = 'Image'
        elif isinstance(value, DataFrame):
            if level == 0:
                cols = value.columns
                if PY2 and len(cols) > 0:
                    # Get rid of possible BOM utf-8 data present at the
                    # beginning of a file, which gets attached to the first
                    # column header when headers are present in the first
                    # row.
                    # Fixes Issue 2514
                    try:
                        ini_col = to_text_string(cols[0], encoding='utf-8-sig')
                    except:
                        ini_col = to_text_string(cols[0])
                    cols = [ini_col] + [to_text_string(c) for c in cols[1:]]
                else:
                    cols = [to_text_string(c) for c in cols]
                display = 'Column names: ' + ', '.join(list(cols))
            else:
                display = 'Dataframe'
        elif isinstance(value, NavigableString):
            # Fixes Issue 2448
            display = to_text_string(value)
            if level > 0:
                display = u"'" + display + u"'"
        elif isinstance(value, Index):
            if level == 0:
                try:
                    display = value._summary()
                except AttributeError:
                    display = value.summary()
            else:
                display = 'Index'
        elif is_binary_string(value):
            # We don't apply this to classes that extend string types
            # See issue 5636
            if is_type_text_string(value):
                try:
                    display = to_text_string(value, 'utf8')
                    if level > 0:
                        display = u"'" + display + u"'"
                except:
                    display = value
                    if level > 0:
                        display = b"'" + display + b"'"
            else:
                display = default_display(value)
        elif is_text_string(value):
            # We don't apply this to classes that extend string types
            # See issue 5636
            if is_type_text_string(value):
                display = value
                if level > 0:
                    display = u"'" + display + u"'"
            else:
                display = default_display(value)
        elif (isinstance(value, datetime.date) or
              isinstance(value, datetime.timedelta)):
            display = str(value)
        elif (isinstance(value, NUMERIC_TYPES) or
              isinstance(value, bool) or
              isinstance(value, numeric_numpy_types)):
            display = repr(value)
        else:
            if level == 0:
                display = default_display(value)
            else:
                display = default_display(value, with_module=False)
    except:
        display = default_display(value)

    # Truncate display at 70 chars to avoid freezing Spyder
    # because of large displays
    if len(display) > 70:
        if is_binary_string(display):
            ellipses = b' ...'
        else:
            ellipses = u' ...'
        display = display[:70].rstrip() + ellipses

    # Restore Numpy printoptions
    if np_printoptions is not FakeObject:
        set_printoptions(**np_printoptions)

    return display


Instruction:
How would you implement a function in Python 3 that
converts value to value

Response:
def display_to_value(value, default_value, ignore_errors=True):
    """Convert back to value"""
    from qtpy.compat import from_qvariant
    value = from_qvariant(value, to_text_string)
    try:
        np_dtype = get_numpy_dtype(default_value)
        if isinstance(default_value, bool):
            # We must test for boolean before NumPy data types
            # because `bool` class derives from `int` class
            try:
                value = bool(float(value))
            except ValueError:
                value = value.lower() == "true"
        elif np_dtype is not None:
            if 'complex' in str(type(default_value)):
                value = np_dtype(complex(value))
            else:
                value = np_dtype(value)
        elif is_binary_string(default_value):
            value = to_binary_string(value, 'utf8')
        elif is_text_string(default_value):
            value = to_text_string(value)
        elif isinstance(default_value, complex):
            value = complex(value)
        elif isinstance(default_value, float):
            value = float(value)
        elif isinstance(default_value, int):
            try:
                value = int(value)
            except ValueError:
                value = float(value)
        elif isinstance(default_value, datetime.datetime):
            value = datestr_to_datetime(value)
        elif isinstance(default_value, datetime.date):
            value = datestr_to_datetime(value).date()
        elif isinstance(default_value, datetime.timedelta):
            value = str_to_timedelta(value)
        elif ignore_errors:
            value = try_to_eval(value)
        else:
            value = eval(value)
    except (ValueError, SyntaxError):
        if ignore_errors:
            value = try_to_eval(value)
        else:
            return default_value
    return value


Instruction:
Implement a function in Python 3 to
return type string of an object.

Response:
def get_type_string(item):
    """Return type string of an object."""
    if isinstance(item, DataFrame):
        return "DataFrame"
    if isinstance(item, Index):
        return type(item).__name__
    if isinstance(item, Series):
        return "Series"
    found = re.findall(r"<(?:type|class) '(\S*)'>",
                       to_text_string(type(item)))
    if found:
        return found[0]


Instruction:
Create a Python 3 function for
returning human - readable type string of an item

Response:
def get_human_readable_type(item):
    """Return human-readable type string of an item"""
    if isinstance(item, (ndarray, MaskedArray)):
        return item.dtype.name
    elif isinstance(item, Image):
        return "Image"
    else:
        text = get_type_string(item)
        if text is None:
            text = to_text_string('unknown')
        else:
            return text[text.find('.')+1:]


Instruction:
Create a Python 3 function for
returning True if the value is supported False otherwise

Response:
def is_supported(value, check_all=False, filters=None, iterate=False):
    """Return True if the value is supported, False otherwise"""
    assert filters is not None
    if value is None:
        return True
    if not is_editable_type(value):
        return False
    elif not isinstance(value, filters):
        return False
    elif iterate:
        if isinstance(value, (list, tuple, set)):
            valid_count = 0
            for val in value:
                if is_supported(val, filters=filters, iterate=check_all):
                    valid_count += 1
                if not check_all:
                    break
            return valid_count > 0
        elif isinstance(value, dict):
            for key, val in list(value.items()):
                if not is_supported(key, filters=filters, iterate=check_all) \
                   or not is_supported(val, filters=filters,
                                       iterate=check_all):
                    return False
                if not check_all:
                    break
    return True


Instruction:
Write a Python 3 function for
keeping only objects that can be pickled

Response:
def globalsfilter(input_dict, check_all=False, filters=None,
                  exclude_private=None, exclude_capitalized=None,
                  exclude_uppercase=None, exclude_unsupported=None,
                  excluded_names=None):
    """Keep only objects that can be pickled"""
    output_dict = {}
    for key, value in list(input_dict.items()):
        excluded = (exclude_private and key.startswith('_')) or \
                   (exclude_capitalized and key[0].isupper()) or \
                   (exclude_uppercase and key.isupper()
                    and len(key) > 1 and not key[1:].isdigit()) or \
                   (key in excluded_names) or \
                   (exclude_unsupported and \
                    not is_supported(value, check_all=check_all,
                                     filters=filters))
        if not excluded:
            output_dict[key] = value
    return output_dict


Instruction:
How would you explain what the following Python 3 function does
def get_supported_types():
    from datetime import date, timedelta
    editable_types = [int, float, complex, list, set, dict, tuple, date,
                      timedelta] + list(TEXT_TYPES) + list(INT_TYPES)
    try:
        from numpy import ndarray, matrix, generic
        editable_types += [ndarray, matrix, generic]
    except:
        pass
    try:
        from pandas import DataFrame, Series, DatetimeIndex
        editable_types += [DataFrame, Series, Index]
    except:
        pass
    picklable_types = editable_types[:]
    try:
        from spyder.pil_patch import Image
        editable_types.append(Image.Image)
    except:
        pass
    return dict(picklable=picklable_types, editable=editable_types)

Response:
Returns a dictionnary containing types lists supported by the
    namespace browser.


Instruction:
Create a Python 3 function for
getting the data from the remote data file.

Response:
def get_remote_data(data, settings, mode, more_excluded_names=None):
    """
    Return globals according to filter described in *settings*:
        * data: data to be filtered (dictionary)
        * settings: variable explorer settings (dictionary)
        * mode (string): 'editable' or 'picklable'
        * more_excluded_names: additional excluded names (list)
    """
    supported_types = get_supported_types()
    assert mode in list(supported_types.keys())
    excluded_names = settings['excluded_names']
    if more_excluded_names is not None:
        excluded_names += more_excluded_names
    return globalsfilter(data, check_all=settings['check_all'],
                         filters=tuple(supported_types[mode]),
                         exclude_private=settings['exclude_private'],
                         exclude_uppercase=settings['exclude_uppercase'],
                         exclude_capitalized=settings['exclude_capitalized'],
                         exclude_unsupported=settings['exclude_unsupported'],
                         excluded_names=excluded_names)


Instruction:
How would you code a function in Python 3 to
make a remote view of dictionary data

Response:
def make_remote_view(data, settings, more_excluded_names=None):
    """
    Make a remote view of dictionary *data*
    -> globals explorer
    """
    data = get_remote_data(data, settings, mode='editable',
                           more_excluded_names=more_excluded_names)
    remote = {}
    for key, value in list(data.items()):
        view = value_to_display(value, minmax=settings['minmax'])
        remote[key] = {'type':  get_human_readable_type(value),
                       'size':  get_size(value),
                       'color': get_color_name(value),
                       'view':  view}
    return remote


Instruction:
Can you generate the documentation for the following Python 3 function
def _pdb_frame(self):
        if self._pdb_obj is not None and self._pdb_obj.curframe is not None:
            return self._pdb_obj.curframe

Response: Return the current Pdb frame if there is any


Instruction:
Implement a function in Python 3 to
return the namespace view

Response:
def get_namespace_view(self):
        """
        Return the namespace view

        This is a dictionary with the following structure

        {'a': {'color': '#800000', 'size': 1, 'type': 'str', 'view': '1'}}

        Here:
        * 'a' is the variable name
        * 'color' is the color used to show it
        * 'size' and 'type' are self-evident
        * and'view' is its value or the text shown in the last column
        """
        from spyder_kernels.utils.nsview import make_remote_view

        settings = self.namespace_view_settings
        if settings:
            ns = self._get_current_namespace()
            view = repr(make_remote_view(ns, settings, EXCLUDED_NAMES))
            return view
        else:
            return repr(None)


Instruction:
Explain what the following Python 3 code does
def get_var_properties(self):
        from spyder_kernels.utils.nsview import get_remote_data

        settings = self.namespace_view_settings
        if settings:
            ns = self._get_current_namespace()
            data = get_remote_data(ns, settings, mode='editable',
                                   more_excluded_names=EXCLUDED_NAMES)

            properties = {}
            for name, value in list(data.items()):
                properties[name] = {
                    'is_list':  isinstance(value, (tuple, list)),
                    'is_dict':  isinstance(value, dict),
                    'is_set': isinstance(value, set),
                    'len': self._get_len(value),
                    'is_array': self._is_array(value),
                    'is_image': self._is_image(value),
                    'is_data_frame': self._is_data_frame(value),
                    'is_series': self._is_series(value),
                    'array_shape': self._get_array_shape(value),
                    'array_ndim': self._get_array_ndim(value)
                }

            return repr(properties)
        else:
            return repr(None)

Response: Get some properties of the variables in the current namespace


Instruction:
Here you have a function in Python 3, explain what it does
def send_spyder_msg(self, spyder_msg_type, content=None, data=None):
        import cloudpickle

        if content is None:
            content = {}
        content['spyder_msg_type'] = spyder_msg_type
        msg = self.session.send(
            self.iopub_socket,
            'spyder_msg',
            content=content,
            buffers=[cloudpickle.dumps(data, protocol=PICKLE_PROTOCOL)],
            parent=self._parent_header,
        )
        self.log.debug(msg)

Response: Send a custom message to the Spyder frontend.


Instruction:
Can you implement a function in Python 3 that
gets the value of a variable

Response:
def get_value(self, name):
        """Get the value of a variable"""
        ns = self._get_current_namespace()
        value = ns[name]
        try:
            self.send_spyder_msg('data', data=value)
        except:
            # * There is no need to inform users about
            #   these errors.
            # * value = None makes Spyder to ignore
            #   petitions to display a value
            self.send_spyder_msg('data', data=None)
        self._do_publish_pdb_state = False


Instruction:
Write a Python 3 function that can
set the value of a variable in the namespace

Response:
def set_value(self, name, value, PY2_frontend):
        """Set the value of a variable"""
        import cloudpickle
        ns = self._get_reference_namespace(name)

        # We send serialized values in a list of one element
        # from Spyder to the kernel, to be able to send them
        # at all in Python 2
        svalue = value[0]

        # We need to convert svalue to bytes if the frontend
        # runs in Python 2 and the kernel runs in Python 3
        if PY2_frontend and not PY2:
            svalue = bytes(svalue, 'latin-1')

        # Deserialize and set value in namespace
        dvalue = cloudpickle.loads(svalue)
        ns[name] = dvalue
        self.log.debug(ns)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def remove_value(self, name):
        ns = self._get_reference_namespace(name)
        ns.pop(name)

Response: Remove a variable from the reference namespace


Instruction:
Explain what the following Python 3 code does
def copy_value(self, orig_name, new_name):
        ns = self._get_reference_namespace(orig_name)
        ns[new_name] = ns[orig_name]

Response: Copy a variable from the reference namespace to the new name.


Instruction:
Here you have a function in Python 3, explain what it does
def load_data(self, filename, ext):
        from spyder_kernels.utils.iofuncs import iofunctions
        from spyder_kernels.utils.misc import fix_reference_name

        glbs = self._mglobals()

        load_func = iofunctions.load_funcs[ext]
        data, error_message = load_func(filename)

        if error_message:
            return error_message

        for key in list(data.keys()):
            new_key = fix_reference_name(key, blacklist=list(glbs.keys()))
            if new_key != key:
                data[new_key] = data.pop(key)

        try:
            glbs.update(data)
        except Exception as error:
            return str(error)

        return None

Response: Load data from filename and update the local cache


Instruction:
Write a Python 3 function for
saving the current namespace into filename

Response:
def save_namespace(self, filename):
        """Save namespace into filename"""
        from spyder_kernels.utils.nsview import get_remote_data
        from spyder_kernels.utils.iofuncs import iofunctions

        ns = self._get_current_namespace()
        settings = self.namespace_view_settings
        data = get_remote_data(ns, settings, mode='picklable',
                               more_excluded_names=EXCLUDED_NAMES).copy()
        return iofunctions.save(data, filename)


Instruction:
Write a Python 3 script for
publishing Variable Explorer state and Pdb step through send_spyder_msg.

Response:
def publish_pdb_state(self):
        """
        Publish Variable Explorer state and Pdb step through
        send_spyder_msg.
        """
        if self._pdb_obj and self._do_publish_pdb_state:
            state = dict(namespace_view = self.get_namespace_view(),
                         var_properties = self.get_var_properties(),
                         step = self._pdb_step)
            self.send_spyder_msg('pdb_state', content={'pdb_state': state})
        self._do_publish_pdb_state = True


Instruction:
Given the following Python 3 function, write the documentation
def is_defined(self, obj, force_import=False):
        from spyder_kernels.utils.dochelpers import isdefined

        ns = self._get_current_namespace(with_magics=True)
        return isdefined(obj, force_import=force_import, namespace=ns)

Response: Return True if object is defined in current namespace


Instruction:
Given the following Python 3 function, write the documentation
def get_doc(self, objtxt):
        try:
            import matplotlib
            matplotlib.rcParams['docstring.hardcopy'] = True
        except:
            pass
        from spyder_kernels.utils.dochelpers import getdoc

        obj, valid = self._eval(objtxt)
        if valid:
            return getdoc(obj)

Response: Get object documentation dictionary


Instruction:
How would you implement a function in Python 3 that
gets object source from object text

Response:
def get_source(self, objtxt):
        """Get object source"""
        from spyder_kernels.utils.dochelpers import getsource

        obj, valid = self._eval(objtxt)
        if valid:
            return getsource(obj)


Instruction:
How would you implement a function in Python 3 that
returns the current namespace

Response:
def _get_current_namespace(self, with_magics=False):
        """
        Return current namespace

        This is globals() if not debugging, or a dictionary containing
        both locals() and globals() for current frame when debugging
        """
        ns = {}
        glbs = self._mglobals()

        if self._pdb_frame is None:
            ns.update(glbs)
        else:
            ns.update(glbs)
            ns.update(self._pdb_locals)

        # Add magics to ns so we can show help about them on the Help
        # plugin
        if with_magics:
            line_magics = self.shell.magics_manager.magics['line']
            cell_magics = self.shell.magics_manager.magics['cell']
            ns.update(line_magics)
            ns.update(cell_magics)

        return ns


Instruction:
Write a Python 3 script for
returning the namespace where the reference name is defined.

Response:
def _get_reference_namespace(self, name):
        """
        Return namespace where reference name is defined

        It returns the globals() if reference has not yet been defined
        """
        glbs = self._mglobals()
        if self._pdb_frame is None:
            return glbs
        else:
            lcls = self._pdb_locals
            if name in lcls:
                return lcls
            else:
                return glbs


Instruction:
Write a Python 3 script to
return current globals -- handles Pdb frames

Response:
def _mglobals(self):
        """Return current globals -- handles Pdb frames"""
        if self._pdb_frame is not None:
            return self._pdb_frame.f_globals
        else:
            return self.shell.user_ns


Instruction:
How would you explain what the following Python 3 function does
def _is_image(self, var):
        try:
            from PIL import Image
            return isinstance(var, Image.Image)
        except:
            return False

Response: Return True if variable is a PIL. Image image


Instruction:
Can you create a Python 3 function that
sets all Spyder breakpoints in an active pdb session

Response:
def _set_spyder_breakpoints(self, breakpoints):
        """Set all Spyder breakpoints in an active pdb session"""
        if not self._pdb_obj:
            return

        # Breakpoints come serialized from Spyder. We send them
        # in a list of one element to be able to send them at all
        # in Python 2
        serialized_breakpoints = breakpoints[0]
        breakpoints = pickle.loads(serialized_breakpoints)

        self._pdb_obj.set_spyder_breakpoints(breakpoints)


Instruction:
Explain what the following Python 3 code does
def _eval(self, text):
        from spyder_kernels.py3compat import is_text_string

        assert is_text_string(text)
        ns = self._get_current_namespace(with_magics=True)
        try:
            return eval(text, ns), True
        except:
            return None, False

Response: Evaluate text and return obj valid


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _set_mpl_backend(self, backend, pylab=False):
        import traceback
        from IPython.core.getipython import get_ipython

        generic_error = (
            "\n" + "="*73 + "\n"
            "NOTE: The following error appeared when setting "
            "your Matplotlib backend!!\n" + "="*73 + "\n\n"
            "{0}"
        )

        magic = 'pylab' if pylab else 'matplotlib'

        error = None
        try:
            get_ipython().run_line_magic(magic, backend)
        except RuntimeError as err:
            # This catches errors generated by ipykernel when
            # trying to set a backend. See issue 5541
            if "GUI eventloops" in str(err):
                import matplotlib
                previous_backend = matplotlib.get_backend()
                if not backend in previous_backend.lower():
                    # Only inform about an error if the user selected backend
                    # and the one set by Matplotlib are different. Else this
                    # message is very confusing.
                    error = (
                        "\n"
                        "NOTE: Spyder *can't* set your selected Matplotlib "
                        "backend because there is a previous backend already "
                        "in use.\n\n"
                        "Your backend will be {0}".format(previous_backend)
                    )
                del matplotlib
            # This covers other RuntimeError's
            else:
                error = generic_error.format(traceback.format_exc())
        except Exception:
            error = generic_error.format(traceback.format_exc())

        self._mpl_backend_error = error

Response: Set a Matplotlib backend for Matplotlib.


Instruction:
Implement a Python 3 function for
importing our customizations into the kernel.

Response:
def import_spydercustomize():
    """Import our customizations into the kernel."""
    here = osp.dirname(__file__)
    parent = osp.dirname(here)
    customize_dir = osp.join(parent, 'customize')

    # Remove current directory from sys.path to prevent kernel
    # crashes when people name Python files or modules with
    # the same name as standard library modules.
    # See spyder-ide/spyder#8007
    while '' in sys.path:
        sys.path.remove('')

    # Import our customizations
    site.addsitedir(customize_dir)
    import spydercustomize

    # Remove our customize path from sys.path
    try:
        sys.path.remove(customize_dir)
    except ValueError:
        pass


Instruction:
Can you tell what is the following Python 3 function doing
def kernel_config():
    import ipykernel
    from IPython.core.application import get_ipython_dir
    from traitlets.config.loader import Config, load_pyconfig_files

    # ---- IPython config ----
    try:
        profile_path = osp.join(get_ipython_dir(), 'profile_default')
        cfg = load_pyconfig_files(['ipython_config.py',
                                   'ipython_kernel_config.py'],
                                  profile_path)
    except:
        cfg = Config()

    # ---- Spyder config ----
    spy_cfg = Config()

    # Enable/disable certain features for testing
    testing = os.environ.get('SPY_TESTING') == 'True'
    if testing:
        # Don't load nor save history in our IPython consoles.
        spy_cfg.HistoryAccessor.enabled = False

    # Until we implement Issue 1052
    spy_cfg.InteractiveShell.xmode = 'Plain'

    # Jedi completer. It's only available in Python 3
    jedi_o = os.environ.get('SPY_JEDI_O') == 'True'
    if not PY2:
        spy_cfg.IPCompleter.use_jedi = jedi_o

    # Run lines of code at startup
    run_lines_o = os.environ.get('SPY_RUN_LINES_O')
    if run_lines_o is not None:
        spy_cfg.IPKernelApp.exec_lines = [x.strip() for x in run_lines_o.split(';')]
    else:
        spy_cfg.IPKernelApp.exec_lines = []

    # Clean terminal arguments input
    clear_argv = "import sys;sys.argv = [''];del sys"
    spy_cfg.IPKernelApp.exec_lines.append(clear_argv)

    # Load %autoreload magic
    spy_cfg.IPKernelApp.exec_lines.append(
        "get_ipython().kernel._load_autoreload_magic()")

    # Load wurlitzer extension
    spy_cfg.IPKernelApp.exec_lines.append(
        "get_ipython().kernel._load_wurlitzer()")

    # Default inline backend configuration
    # This is useful to have when people doesn't
    # use our config system to configure the
    # inline backend but want to use
    # '%matplotlib inline' at runtime
    if LooseVersion(ipykernel.__version__) < LooseVersion('4.5'):
        dpi_option = 'savefig.dpi'
    else:
        dpi_option = 'figure.dpi'

    spy_cfg.InlineBackend.rc = {'figure.figsize': (6.0, 4.0),
                                dpi_option: 72,
                                'font.size': 10,
                                'figure.subplot.bottom': .125,
                                'figure.facecolor': 'white',
                                'figure.edgecolor': 'white'}

    # Pylab configuration
    mpl_backend = None
    if is_module_installed('matplotlib'):
        # Set Matplotlib backend with Spyder options
        pylab_o = os.environ.get('SPY_PYLAB_O')
        backend_o = os.environ.get('SPY_BACKEND_O')
        if pylab_o == 'True' and backend_o is not None:
            # Select the automatic backend
            if backend_o == '1':
                if is_module_installed('PyQt5'):
                    auto_backend = 'qt5'
                elif is_module_installed('PyQt4'):
                    auto_backend = 'qt4'
                elif is_module_installed('_tkinter'):
                    auto_backend = 'tk'
                else:
                    auto_backend = 'inline'
            else:
                auto_backend = ''

            # Mapping of Spyder options to backends
            backends = {'0': 'inline',
                        '1': auto_backend,
                        '2': 'qt5',
                        '3': 'qt4',
                        '4': 'osx',
                        '5': 'gtk3',
                        '6': 'gtk',
                        '7': 'wx',
                        '8': 'tk'}

            # Select backend
            mpl_backend = backends[backend_o]

            # Inline backend configuration
            if mpl_backend == 'inline':
                # Figure format
                format_o = os.environ.get('SPY_FORMAT_O')
                formats = {'0': 'png',
                           '1': 'svg'}
                if format_o is not None:
                    spy_cfg.InlineBackend.figure_format = formats[format_o]

                # Resolution
                resolution_o = os.environ.get('SPY_RESOLUTION_O')
                if resolution_o is not None:
                    spy_cfg.InlineBackend.rc[dpi_option] = float(resolution_o)

                # Figure size
                width_o = float(os.environ.get('SPY_WIDTH_O'))
                height_o = float(os.environ.get('SPY_HEIGHT_O'))
                if width_o is not None and height_o is not None:
                    spy_cfg.InlineBackend.rc['figure.figsize'] = (width_o,
                                                                  height_o)

                # Print figure kwargs
                bbox_inches_o = os.environ.get('SPY_BBOX_INCHES_O')
                bbox_inches = 'tight' if bbox_inches_o == 'True' else None
                spy_cfg.InlineBackend.print_figure_kwargs.update(
                    {'bbox_inches': bbox_inches})
        else:
            # Set Matplotlib backend to inline for external kernels.
            # Fixes issue 108
            mpl_backend = 'inline'

        # Automatically load Pylab and Numpy, or only set Matplotlib
        # backend
        autoload_pylab_o = os.environ.get('SPY_AUTOLOAD_PYLAB_O') == 'True'
        command = "get_ipython().kernel._set_mpl_backend('{0}', {1})"
        spy_cfg.IPKernelApp.exec_lines.append(
            command.format(mpl_backend, autoload_pylab_o))

    # Enable Cython magic
    run_cython = os.environ.get('SPY_RUN_CYTHON') == 'True'
    if run_cython and is_module_installed('Cython'):
        spy_cfg.IPKernelApp.exec_lines.append('%reload_ext Cython')

    # Run a file at startup
    use_file_o = os.environ.get('SPY_USE_FILE_O')
    run_file_o = os.environ.get('SPY_RUN_FILE_O')
    if use_file_o == 'True' and run_file_o is not None:
        spy_cfg.IPKernelApp.file_to_run = run_file_o

    # Autocall
    autocall_o = os.environ.get('SPY_AUTOCALL_O')
    if autocall_o is not None:
        spy_cfg.ZMQInteractiveShell.autocall = int(autocall_o)

    # To handle the banner by ourselves in IPython 3+
    spy_cfg.ZMQInteractiveShell.banner1 = ''

    # Greedy completer
    greedy_o = os.environ.get('SPY_GREEDY_O') == 'True'
    spy_cfg.IPCompleter.greedy = greedy_o

    # Sympy loading
    sympy_o = os.environ.get('SPY_SYMPY_O') == 'True'
    if sympy_o and is_module_installed('sympy'):
        lines = sympy_config(mpl_backend)
        spy_cfg.IPKernelApp.exec_lines.append(lines)

    # Merge IPython and Spyder configs. Spyder prefs will have prevalence
    # over IPython ones
    cfg._merge(spy_cfg)
    return cfg

Response: Create a config object with IPython kernel options.


Instruction:
Here you have a function in Python 3, explain what it does
def fix_reference_name(name, blacklist=None):
    name = "".join(re.split(r'[^0-9a-zA-Z_]', name))
    while name and not re.match(r'([a-zA-Z]+[0-9a-zA-Z_]*)$', name):
        if not re.match(r'[a-zA-Z]', name[0]):
            name = name[1:]
            continue
    name = str(name)
    if not name:
        name = "data"
    if blacklist is not None and name in blacklist:
        get_new_name = lambda index: name+('%03d' % index)
        index = 0
        while get_new_name(index) in blacklist:
            index += 1
        name = get_new_name(index)
    return name

Response: Return a syntax - valid Python reference name from an arbitrary name


Instruction:
Can you generate a brief explanation for the following Python 3 code
def user_return(self, frame, return_value):
    # This is useful when debugging in an active interpreter (otherwise,
    # the debugger will stop before reaching the target file)
    if self._wait_for_mainpyfile:
        if (self.mainpyfile != self.canonic(frame.f_code.co_filename)
            or frame.f_lineno<= 0):
            return
        self._wait_for_mainpyfile = 0
    self._old_Pdb_user_return(frame, return_value)

Response: This function is called when a user return trap is set here.


Instruction:
Can you tell what is the following Python 3 function doing
def set_post_mortem():
    def ipython_post_mortem_debug(shell, etype, evalue, tb,
               tb_offset=None):
        post_mortem_excepthook(etype, evalue, tb)
    ipython_shell = get_ipython()
    ipython_shell.set_custom_exc((Exception,), ipython_post_mortem_debug)

Response: Enable the post mortem debugging excepthook.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def runfile(filename, args=None, wdir=None, namespace=None, post_mortem=False):
    try:
        filename = filename.decode('utf-8')
    except (UnicodeError, TypeError, AttributeError):
        # UnicodeError, TypeError --> eventually raised in Python 2
        # AttributeError --> systematically raised in Python 3
        pass
    if __umr__.enabled:
        __umr__.run()
    if args is not None and not isinstance(args, basestring):
        raise TypeError("expected a character buffer object")
    if namespace is None:
        namespace = _get_globals()
    namespace['__file__'] = filename
    sys.argv = [filename]
    if args is not None:
        for arg in shlex.split(args):
            sys.argv.append(arg)
    if wdir is not None:
        try:
            wdir = wdir.decode('utf-8')
        except (UnicodeError, TypeError, AttributeError):
            # UnicodeError, TypeError --> eventually raised in Python 2
            # AttributeError --> systematically raised in Python 3
            pass
        os.chdir(wdir)
    if post_mortem:
        set_post_mortem()
    if __umr__.has_cython:
        # Cython files
        with io.open(filename, encoding='utf-8') as f:
            ipython_shell = get_ipython()
            ipython_shell.run_cell_magic('cython', '', f.read())
    else:
        execfile(filename, namespace)

    clear_post_mortem()
    sys.argv = ['']

    # Avoid error when running `%reset -f` programmatically
    # See issue spyder-ide/spyder-kernels#91
    try:
        namespace.pop('__file__')
    except KeyError:
        pass

Response: Run a file and return a dictionary of the result.


Instruction:
Can you tell what is the following Python 3 function doing
def runcell(cellname, filename):
    try:
        filename = filename.decode('utf-8')
    except (UnicodeError, TypeError, AttributeError):
        # UnicodeError, TypeError --> eventually raised in Python 2
        # AttributeError --> systematically raised in Python 3
        pass
    ipython_shell = get_ipython()
    namespace = _get_globals()
    namespace['__file__'] = filename
    try:
        cell_code = ipython_shell.cell_code
    except AttributeError:
        _print("--Run Cell Error--\n"
               "Please use only through Spyder's Editor; "
               "shouldn't be called manually from the console")
        return

    # Trigger `post_execute` to exit the additional pre-execution.
    # See Spyder PR #7310.
    ipython_shell.events.trigger('post_execute')

    ipython_shell.run_cell(cell_code)
    namespace.pop('__file__')
    del ipython_shell.cell_code

Response: Runs a code cell from an editor as a file.


Instruction:
Write a Python 3 function for
debugging filename args: command line arguments (string) wdir: working directory post_mortem: boolean, included for compatiblity with runfile

Response:
def debugfile(filename, args=None, wdir=None, post_mortem=False):
    """
    Debug filename
    args: command line arguments (string)
    wdir: working directory
    post_mortem: boolean, included for compatiblity with runfile
    """
    debugger = pdb.Pdb()
    filename = debugger.canonic(filename)
    debugger._wait_for_mainpyfile = 1
    debugger.mainpyfile = filename
    debugger._user_requested_quit = 0
    if os.name == 'nt':
        filename = filename.replace('\\', '/')
    debugger.run("runfile(%r, args=%r, wdir=%r)" % (filename, args, wdir))


Instruction:
Can you create a Python 3 function that
creates a list of paths to be skipped from module AttributeNames reloading.

Response:
def create_pathlist(self, initial_pathlist):
        """
        Add to pathlist Python library paths to be skipped from module
        reloading.
        """
        # Get standard installation paths
        try:
            paths = sysconfig.get_paths()
            standard_paths = [paths['stdlib'],
                              paths['purelib'],
                              paths['scripts'],
                              paths['data']]
        except Exception:
            standard_paths = []

        # Get user installation path
        # See Spyder issue 8776
        try:
            import site
            if getattr(site, 'getusersitepackages', False):
                # Virtualenvs don't have this function but
                # conda envs do
                user_path = [site.getusersitepackages()]
            elif getattr(site, 'USER_SITE', False):
                # However, it seems virtualenvs have this
                # constant
                user_path = [site.USER_SITE]
            else:
                user_path = []
        except Exception:
            user_path = []

        return initial_pathlist + standard_paths + user_path


Instruction:
Can you implement a function in Python 3 that
decides if a module is reloadable or not.

Response:
def is_module_reloadable(self, module, modname):
        """Decide if a module is reloadable or not."""
        if self.has_cython:
            # Don't return cached inline compiled .PYX files
            return False
        else:
            if (self.is_module_in_pathlist(module) or
                    self.is_module_in_namelist(modname)):
                return False
            else:
                return True


Instruction:
Create a Python 3 function for
deciding if a module can be reloaded or not according to its pathlist.

Response:
def is_module_in_pathlist(self, module):
        """Decide if a module can be reloaded or not according to its path."""
        modpath = getattr(module, '__file__', None)

        # Skip module according to different criteria
        if modpath is None:
            # *module* is a C module that is statically linked into the
            # interpreter. There is no way to know its path, so we
            # choose to ignore it.
            return True
        elif any([p in modpath for p in self.pathlist]):
            # We don't want to reload modules that belong to the
            # standard library or installed to site-packages,
            # just modules created by the user.
            return True
        elif not os.name == 'nt':
            # Module paths containing the strings below can be ihherited
            # from the default Linux installation, Homebrew or the user
            # site-packages in a virtualenv.
            patterns = [r'^/usr/lib.*',
                        r'^/usr/local/lib.*',
                        r'^/usr/.*/dist-packages/.*',
                        r'^/home/.*/.local/lib.*',
                        r'^/Library/.*',
                        r'^/Users/.*/Library/.*',
                        r'^/Users/.*/.local/.*',
            ]

            if [p for p in patterns if re.search(p, modpath)]:
                return True
            else:
                return False
        else:
            return False


Instruction:
Create a Python 3 function for
activating Cython support. We need to run this here because if the support is active, we don't to run the UMR at all.

Response:
def activate_cython(self):
        """
        Activate Cython support.

        We need to run this here because if the support is
        active, we don't to run the UMR at all.
        """
        run_cython = os.environ.get("SPY_RUN_CYTHON") == "True"

        if run_cython:
            try:
                __import__('Cython')
                self.has_cython = True
            except Exception:
                pass

            if self.has_cython:
                # Import pyximport to enable Cython files support for
                # import statement
                import pyximport
                pyx_setup_args = {}

                # Add Numpy include dir to pyximport/distutils
                try:
                    import numpy
                    pyx_setup_args['include_dirs'] = numpy.get_include()
                except Exception:
                    pass

                # Setup pyximport and enable Cython files reload
                pyximport.install(setup_args=pyx_setup_args,
                                  reload_support=True)


Instruction:
Can you implement a function in Python 3 that
deletes user modules which are not considered as system modules and which are not in previous_modules and if they are reloadable then reload them.

Response:
def run(self):
        """
        Delete user modules to force Python to deeply reload them

        Do not del modules which are considered as system modules, i.e.
        modules installed in subdirectories of Python interpreter's binary
        Do not del C modules
        """
        self.modnames_to_reload = []
        for modname, module in list(sys.modules.items()):
            if modname not in self.previous_modules:
                # Decide if a module can be reloaded or not
                if self.is_module_reloadable(module, modname):
                    self.modnames_to_reload.append(modname)
                    del sys.modules[modname]
                else:
                    continue

        # Report reloaded modules
        if self.verbose and self.modnames_to_reload:
            modnames = self.modnames_to_reload
            _print("\x1b[4;33m%s\x1b[24m%s\x1b[0m"\
                   % ("Reloaded modules", ": "+", ".join(modnames)))


Instruction:
Implement a function in Python 3 to
extract a value from a Matlab file and return it as a list of dicts.

Response:
def get_matlab_value(val):
    """
    Extract a value from a Matlab file

    From the oct2py project, see
    https://pythonhosted.org/oct2py/conversions.html
    """
    import numpy as np

    # Extract each item of a list.
    if isinstance(val, list):
        return [get_matlab_value(v) for v in val]

    # Ignore leaf objects.
    if not isinstance(val, np.ndarray):
        return val

    # Convert user defined classes.
    if hasattr(val, 'classname'):
        out = dict()
        for name in val.dtype.names:
            out[name] = get_matlab_value(val[name].squeeze().tolist())
        cls = type(val.classname, (object,), out)
        return cls()

    # Extract struct data.
    elif val.dtype.names:
        out = MatlabStruct()
        for name in val.dtype.names:
            out[name] = get_matlab_value(val[name].squeeze().tolist())
        val = out

    # Extract cells.
    elif val.dtype.kind == 'O':
        val = val.squeeze().tolist()
        if not isinstance(val, list):
            val = [val]
        val = get_matlab_value(val)

    # Compress singleton values.
    elif val.size == 1:
        val = val.item()

    # Compress empty values.
    elif val.size == 0:
        if val.dtype.kind in 'US':
            val = ''
        else:
            val = []

    return val


Instruction:
Write a Python 3 function that can
load a pickle file as a dictionary

Response:
def load_pickle(filename):
    """Load a pickle file as a dictionary"""
    try:
        if pd:
            return pd.read_pickle(filename), None
        else:
            with open(filename, 'rb') as fid:
                data = pickle.load(fid)
            return data, None
    except Exception as err:
        return None, str(err)


Instruction:
Write a Python 3 script for
loading a json file as a dictionary

Response:
def load_json(filename):
    """Load a json file as a dictionary"""
    try:
        if PY2:
            args = 'rb'
        else:
            args = 'r'
        with open(filename, args) as fid:
            data = json.load(fid)
        return data, None
    except Exception as err:
        return None, str(err)


Instruction:
Can you tell what is the following Python 3 function doing
def save_dictionary(data, filename):
    filename = osp.abspath(filename)
    old_cwd = getcwd()
    os.chdir(osp.dirname(filename))
    error_message = None
    skipped_keys = []
    data_copy = {}

    try:
        # Copy dictionary before modifying it to fix #6689
        for obj_name, obj_value in data.items():
            # Skip modules, since they can't be pickled, users virtually never
            # would want them to be and so they don't show up in the skip list.
            # Skip callables, since they are only pickled by reference and thus
            # must already be present in the user's environment anyway.
            if not (callable(obj_value) or isinstance(obj_value,
                                                      types.ModuleType)):
                # If an object cannot be deepcopied, then it cannot be pickled.
                # Ergo, we skip it and list it later.
                try:
                    data_copy[obj_name] = copy.deepcopy(obj_value)
                except Exception:
                    skipped_keys.append(obj_name)
        data = data_copy
        if not data:
            raise RuntimeError('No supported objects to save')

        saved_arrays = {}
        if load_array is not None:
            # Saving numpy arrays with np.save
            arr_fname = osp.splitext(filename)[0]
            for name in list(data.keys()):
                try:
                    if isinstance(data[name],
                                  np.ndarray) and data[name].size > 0:
                        # Save arrays at data root
                        fname = __save_array(data[name], arr_fname,
                                             len(saved_arrays))
                        saved_arrays[(name, None)] = osp.basename(fname)
                        data.pop(name)
                    elif isinstance(data[name], (list, dict)):
                        # Save arrays nested in lists or dictionaries
                        if isinstance(data[name], list):
                            iterator = enumerate(data[name])
                        else:
                            iterator = iter(list(data[name].items()))
                        to_remove = []
                        for index, value in iterator:
                            if isinstance(value,
                                          np.ndarray) and value.size > 0:
                                fname = __save_array(value, arr_fname,
                                                     len(saved_arrays))
                                saved_arrays[(name, index)] = (
                                    osp.basename(fname))
                                to_remove.append(index)
                        for index in sorted(to_remove, reverse=True):
                            data[name].pop(index)
                except (RuntimeError, pickle.PicklingError, TypeError,
                        AttributeError, IndexError):
                    # If an array can't be saved with numpy for some reason,
                    # leave the object intact and try to save it normally.
                    pass
            if saved_arrays:
                data['__saved_arrays__'] = saved_arrays

        pickle_filename = osp.splitext(filename)[0] + '.pickle'
        # Attempt to pickle everything.
        # If pickling fails, iterate through to eliminate problem objs & retry.
        with open(pickle_filename, 'w+b') as fdesc:
            try:
                pickle.dump(data, fdesc, protocol=2)
            except (pickle.PicklingError, AttributeError, TypeError,
                    ImportError, IndexError, RuntimeError):
                data_filtered = {}
                for obj_name, obj_value in data.items():
                    try:
                        pickle.dumps(obj_value, protocol=2)
                    except Exception:
                        skipped_keys.append(obj_name)
                    else:
                        data_filtered[obj_name] = obj_value
                if not data_filtered:
                    raise RuntimeError('No supported objects to save')
                pickle.dump(data_filtered, fdesc, protocol=2)

        # Use PAX (POSIX.1-2001) format instead of default GNU.
        # This improves interoperability and UTF-8/long variable name support.
        with tarfile.open(filename, "w", format=tarfile.PAX_FORMAT) as tar:
            for fname in ([pickle_filename]
                          + [fn for fn in list(saved_arrays.values())]):
                tar.add(osp.basename(fname))
                os.remove(fname)
    except (RuntimeError, pickle.PicklingError, TypeError) as error:
        error_message = to_text_string(error)
    else:
        if skipped_keys:
            skipped_keys.sort()
            error_message = ('Some objects could not be saved: '
                             + ', '.join(skipped_keys))
    finally:
        os.chdir(old_cwd)
    return error_message

Response: Save dictionary to a single file.


Instruction:
Explain what the following Python 3 code does
def load_dictionary(filename):
    filename = osp.abspath(filename)
    old_cwd = getcwd()
    tmp_folder = tempfile.mkdtemp()
    os.chdir(tmp_folder)
    data = None
    error_message = None
    try:
        with tarfile.open(filename, "r") as tar:
            tar.extractall()
        pickle_filename = glob.glob('*.pickle')[0]
        # 'New' format (Spyder >=2.2 for Python 2 and Python 3)
        with open(pickle_filename, 'rb') as fdesc:
            data = pickle.loads(fdesc.read())
        saved_arrays = {}
        if load_array is not None:
            # Loading numpy arrays saved with np.save
            try:
                saved_arrays = data.pop('__saved_arrays__')
                for (name, index), fname in list(saved_arrays.items()):
                    arr = np.load( osp.join(tmp_folder, fname) )
                    if index is None:
                        data[name] = arr
                    elif isinstance(data[name], dict):
                        data[name][index] = arr
                    else:
                        data[name].insert(index, arr)
            except KeyError:
                pass
    # Except AttributeError from e.g. trying to load function no longer present
    except (AttributeError, EOFError, ValueError) as error:
        error_message = to_text_string(error)
    # To ensure working dir gets changed back and temp dir wiped no matter what
    finally:
        os.chdir(old_cwd)
        try:
            shutil.rmtree(tmp_folder)
        except OSError as error:
            error_message = to_text_string(error)
    return data, error_message

Response: Load a dictionary from a. spydata file


Instruction:
Explain what the following Python 3 code does
def getobj(txt, last=False):
    txt_end = ""
    for startchar, endchar in ["[]", "()"]:
        if txt.endswith(endchar):
            pos = txt.rfind(startchar)
            if pos:
                txt_end = txt[pos:]
                txt = txt[:pos]
    tokens = re.split(SYMBOLS, txt)
    token = None
    try:
        while token is None or re.match(SYMBOLS, token):
            token = tokens.pop()
        if token.endswith('.'):
            token = token[:-1]
        if token.startswith('.'):
            # Invalid object name
            return None
        if last:
            #XXX: remove this statement as well as the "last" argument
            token += txt[ txt.rfind(token) + len(token) ]
        token += txt_end
        if token:
            return token
    except IndexError:
        return None

Response: Return the last valid object name in string


Instruction:
Can you implement a function in Python 3 that
gets the text documentation from an object.

Response:
def getdoc(obj):
    """
    Return text documentation from an object. This comes in a form of
    dictionary with four keys:

    name:
      The name of the inspected object
    argspec:
      It's argspec
    note:
      A phrase describing the type of object (function or method) we are
      inspecting, and the module it belongs to.
    docstring:
      It's docstring
    """

    docstring = inspect.getdoc(obj) or inspect.getcomments(obj) or ''

    # Most of the time doc will only contain ascii characters, but there are
    # some docstrings that contain non-ascii characters. Not all source files
    # declare their encoding in the first line, so querying for that might not
    # yield anything, either. So assume the most commonly used
    # multi-byte file encoding (which also covers ascii).
    try:
        docstring = to_text_string(docstring)
    except:
        pass

    # Doc dict keys
    doc = {'name': '',
           'argspec': '',
           'note': '',
           'docstring': docstring}

    if callable(obj):
        try:
            name = obj.__name__
        except AttributeError:
            doc['docstring'] = docstring
            return doc
        if inspect.ismethod(obj):
            imclass = get_meth_class(obj)
            if get_meth_class_inst(obj) is not None:
                doc['note'] = 'Method of %s instance' \
                              % get_meth_class_inst(obj).__class__.__name__
            else:
                doc['note'] = 'Unbound %s method' % imclass.__name__
            obj = get_meth_func(obj)
        elif hasattr(obj, '__module__'):
            doc['note'] = 'Function of %s module' % obj.__module__
        else:
            doc['note'] = 'Function'
        doc['name'] = obj.__name__
        if inspect.isfunction(obj):
            if PY2:
                args, varargs, varkw, defaults = inspect.getargspec(obj)
                doc['argspec'] = inspect.formatargspec(
                    args, varargs, varkw, defaults,
                    formatvalue=lambda o:'='+repr(o))
            else:
                (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults,
                 annotations) = inspect.getfullargspec(obj)
                doc['argspec'] = inspect.formatargspec(
                    args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults,
                    annotations, formatvalue=lambda o:'='+repr(o))
            if name == '<lambda>':
                doc['name'] = name + ' lambda '
                doc['argspec'] = doc['argspec'][1:-1] # remove parentheses
        else:
            argspec = getargspecfromtext(doc['docstring'])
            if argspec:
                doc['argspec'] = argspec
                # Many scipy and numpy docstrings begin with a function
                # signature on the first line. This ends up begin redundant
                # when we are using title and argspec to create the
                # rich text "Definition:" field. We'll carefully remove this
                # redundancy but only under a strict set of conditions:
                # Remove the starting charaters of the 'doc' portion *iff*
                # the non-whitespace characters on the first line
                # match *exactly* the combined function title
                # and argspec we determined above.
                signature = doc['name'] + doc['argspec']
                docstring_blocks = doc['docstring'].split("\n\n")
                first_block = docstring_blocks[0].strip()
                if first_block == signature:
                    doc['docstring'] = doc['docstring'].replace(
                                                     signature, '', 1).lstrip()
            else:
                doc['argspec'] = '(...)'

        # Remove self from argspec
        argspec = doc['argspec']
        doc['argspec'] = argspec.replace('(self)', '()').replace('(self, ', '(')

    return doc


Instruction:
Can you generate the documentation for the following Python 3 function
def getsource(obj):
    try:
        try:
            src = to_text_string(inspect.getsource(obj))
        except TypeError:
            if hasattr(obj, '__class__'):
                src = to_text_string(inspect.getsource(obj.__class__))
            else:
                # Bindings like VTK or ITK require this case
                src = getdoc(obj)
        return src
    except (TypeError, IOError):
        return

Response: Wrapper around inspect. getSource


Instruction:
Can you tell what is the following Python 3 function doing
def getsignaturefromtext(text, objname):
    if isinstance(text, dict):
        text = text.get('docstring', '')
    # Regexps
    oneline_re = objname + r'\([^\)].+?(?<=[\w\]\}\'"])\)(?!,)'
    multiline_re = objname + r'\([^\)]+(?<=[\w\]\}\'"])\)(?!,)'
    multiline_end_parenleft_re = r'(%s\([^\)]+(\),\n.+)+(?<=[\w\]\}\'"])\))'
    # Grabbing signatures
    if not text:
        text = ''
    sigs_1 = re.findall(oneline_re + '|' + multiline_re, text)
    sigs_2 = [g[0] for g in re.findall(multiline_end_parenleft_re % objname, text)]
    all_sigs = sigs_1 + sigs_2
    # The most relevant signature is usually the first one. There could be
    # others in doctests but those are not so important
    if all_sigs:
        return all_sigs[0]
    else:
        return ''

Response: Get object signatures from text


Instruction:
How would you code a function in Python 3 to
try to get the formatted argspec of a callable from the first block of the text.

Response:
def getargspecfromtext(text):
    """
    Try to get the formatted argspec of a callable from the first block of its
    docstring

    This will return something like
    '(foo, bar, k=1)'
    """
    blocks = text.split("\n\n")
    first_block = blocks[0].strip()
    return getsignaturefromtext(first_block, '')


Instruction:
Can you tell what is the following Python 3 function doing
def getargsfromtext(text, objname):
    signature = getsignaturefromtext(text, objname)
    if signature:
        argtxt = signature[signature.find('(')+1:-1]
        return argtxt.split(',')

Response: Get arguments from text.


Instruction:
How would you explain what the following Python 3 function does
def getargs(obj):
    if inspect.isfunction(obj) or inspect.isbuiltin(obj):
        func_obj = obj
    elif inspect.ismethod(obj):
        func_obj = get_meth_func(obj)
    elif inspect.isclass(obj) and hasattr(obj, '__init__'):
        func_obj = getattr(obj, '__init__')
    else:
        return []
    if not hasattr(func_obj, 'func_code'):
        # Builtin: try to extract info from doc
        args = getargsfromdoc(func_obj)
        if args is not None:
            return args
        else:
            # Example: PyQt5
            return getargsfromdoc(obj)
    args, _, _ = inspect.getargs(func_obj.func_code)
    if not args:
        return getargsfromdoc(obj)

    # Supporting tuple arguments in def statement:
    for i_arg, arg in enumerate(args):
        if isinstance(arg, list):
            args[i_arg] = "(%s)" % ", ".join(arg)

    defaults = get_func_defaults(func_obj)
    if defaults is not None:
        for index, default in enumerate(defaults):
            args[index+len(args)-len(defaults)] += '='+repr(default)
    if inspect.isclass(obj) or inspect.ismethod(obj):
        if len(args) == 1:
            return None
        if 'self' in args:
            args.remove('self')
    return args

Response: Get the names and default values of a function s arguments


Instruction:
Can you generate the documentation for the following Python 3 function
def getargtxt(obj, one_arg_per_line=True):
    args = getargs(obj)
    if args:
        sep = ', '
        textlist = None
        for i_arg, arg in enumerate(args):
            if textlist is None:
                textlist = ['']
            textlist[-1] += arg
            if i_arg < len(args)-1:
                textlist[-1] += sep
                if len(textlist[-1]) >= 32 or one_arg_per_line:
                    textlist.append('')
        if inspect.isclass(obj) or inspect.ismethod(obj):
            if len(textlist) == 1:
                return None
            if 'self'+sep in textlist:
                textlist.remove('self'+sep)
        return textlist

Response: Get the names and default values of a function s arguments Return list with separators ( ', ) formatted for calltips


Instruction:
Write a Python 3 function for
returning True if object is defined in namespace

Response:
def isdefined(obj, force_import=False, namespace=None):
    """Return True if object is defined in namespace
    If namespace is None --> namespace = locals()"""
    if namespace is None:
        namespace = locals()
    attr_list = obj.split('.')
    base = attr_list.pop(0)
    if len(base) == 0:
        return False
    if base not in builtins.__dict__ and base not in namespace:
        if force_import:
            try:
                module = __import__(base, globals(), namespace)
                if base not in globals():
                    globals()[base] = module
                namespace[base] = module
            except Exception:
                return False
        else:
            return False
    for attr in attr_list:
        try:
            attr_not_found = not hasattr(eval(base, namespace), attr)
        except (SyntaxError, AttributeError):
            return False
        if attr_not_found:
            if force_import:
                try:
                    __import__(base+'.'+attr, globals(), namespace)
                except (ImportError, SyntaxError):
                    return False
            else:
                return False
        base += '.'+attr
    return True


Instruction:
Can you tell what is the following Python 3 function doing
def read(self, size):
        return_val = self.handle.read(size)
        if not return_val:
            raise IOError
        else:
            return return_val

Response: Reads the n bytes from the file and returns it.


Instruction:
Given the following Python 3 function, write the documentation
def escape_tex(value):
  newval = value
  for pattern, replacement in LATEX_SUBS:
    newval = pattern.sub(replacement, newval)
  return newval

Response: Escape a text tex safe value.


Instruction:
How would you explain what the following Python 3 function does
def packagenameify(s):
  return ''.join(w if w in ACRONYMS else w.title() for w in s.split('.')[-1:])

Response: Makes a package name from a string.


Instruction:
Can you write a function in Python 3 where it
handles the fields of the class.

Response:
def handle_fields(definitions, fields, prefix, offset, multiplier):
  """
  Helper for handling naming and sizing of fields. It's terrible.
  """
  items = []
  for f in fields:
    if f.type_id == "array" and f.options['fill'].value in CONSTRUCT_CODE:
      prefix_name = '.'.join([prefix, f.identifier]) if prefix else f.identifier
      n_with_values = f.options['n_with_values'].value
      bitfields = f.options['fields'].value if n_with_values > 0 else None
      if 'size' in f.options:
        name = "%s[%s]" % (f.options['fill'].value, str(f.options['size'].value))
        size = field_sizes[f.options['fill'].value] * f.options['size'].value
        item = FieldItem(prefix_name, name, offset, size,
                         str(f.units), f.desc, n_with_values, bitfields)
        items.append(item)
        offset += size
      else:
        name = "%s[%s]" % (f.options['fill'].value, "N")
        multiplier = field_sizes[f.options['fill'].value]
        size = field_sizes[f.options['fill'].value] * 1
        item = FieldItem(prefix_name, name, offset, "N",
                         str(f.units), f.desc, n_with_values, bitfields)
        items.append(item)
        offset += size
    elif f.type_id == "string":
      prefix_name = '.'.join([prefix, f.identifier]) if prefix else f.identifier
      n_with_values = f.options['n_with_values'].value
      bitfields = f.options['fields'].value if n_with_values > 0 else None
      if 'size' in f.options:
        name = "string"
        size = field_sizes['u8'] * f.options['size'].value
        item = FieldItem(prefix_name, name, offset, size,
                         str(f.units), f.desc, n_with_values, bitfields)
        items.append(item)
        offset += size
      else:
        name = "string"
        size = field_sizes['u8']
        multiplier = 1
        item = FieldItem(prefix_name, name, offset, "N",
                         str(f.units), f.desc, n_with_values, bitfields)
        items.append(item)
        offset += size
    elif f.type_id == "array":
      name = f.options['fill'].value
      definition = next(d for d in definitions if name == d.identifier)
      prefix_name = '.'.join([prefix, f.identifier]) if prefix else f.identifier
      (new_items, new_offset, new_multiplier) \
        = handle_fields(definitions,
                        definition.fields,
                        prefix_name + "[N]",
                        offset,
                        multiplier)
      multiplier = new_offset - offset
      (newer_items, newer_offset, newer_multiplier) \
        = handle_fields(definitions,
                        definition.fields,
                        prefix_name + "[N]", offset,
                        multiplier)
      items += newer_items
      offset = newer_offset
    elif f.type_id not in CONSTRUCT_CODE:
      name = f.type_id
      definition = next(d for d in definitions if name == d.identifier)
      prefix_name = '.'.join([prefix, f.identifier]) if prefix else f.identifier
      (new_items, new_offset, new_multiplier) \
        = handle_fields(definitions,
                        definition.fields,
                        prefix_name,
                        offset,
                        multiplier)
      items += new_items
      offset = new_offset
      multiplier = new_multiplier
    else:
      size = field_sizes[f.type_id]
      name = f.type_id
      adj_offset = "%dN+%d" % (multiplier, offset) if multiplier else offset
      prefix_name = '.'.join([prefix, f.identifier]) if prefix else f.identifier
      n_with_values = f.options['n_with_values'].value
      bitfields = f.options['fields'].value if n_with_values > 0 else None
      item = FieldItem(prefix_name, name, adj_offset, size, str(f.units), f.desc, n_with_values, bitfields)
      items.append(item)
      offset += size
  return (items, offset, multiplier)


Instruction:
Can you generate the documentation for the following Python 3 function
def render_source(output_dir, package_specs, version):
  destination_filename = "%s/sbp_out.tex" % output_dir
  py_template = JENV.get_template(TEMPLATE_NAME)
  stable_msgs = []
  unstable_msgs = []
  prims = []
  for p in sorted(package_specs, key=attrgetter('identifier')):
    pkg_name = p.identifier
    stable = p.stable

    # build list of required definitions (this package plus includes)
    # TODO: recursively include files
    definitions = p.definitions
    for inc in p.includes:
      inc_basename = inc.split(".")[0]
      for pkg in package_specs:
        if pkg.identifier.endswith(inc_basename):
          definitions += pkg.definitions

    if pkg_name == "swiftnav.sbp.types":
      prims = p.definitions
    for d in p.definitions:
      if d.public and d.static and d.sbp_id:
        items, size, multiplier \
          = handle_fields(definitions, d.fields, "", 0, None)
        adj_size = ""
        if multiplier == 1:
          adj_size = "N+%d" % (size - 1) if size > 1 else "N"
        elif multiplier:
           if multiplier == size:
             adj_size = "%dN" % multiplier
           else:
             adj_size = "%dN+%d" % (multiplier, size - multiplier)
        else:
          adj_size = "%d" % size
        ti = TableItem(pkg_name, d.identifier, d.sbp_id,
                       d.short_desc, d.desc, adj_size, items, p.stable,
                       p.description)
        pkg_name = ""
        if stable:
          stable_msgs.append(ti)
        else:
          unstable_msgs.append(ti)
  with open(destination_filename, 'w') as f:
    f.write(py_template.render(msgs=stable_msgs,
                               umsgs=unstable_msgs,
                               prims=prims,
                               version=version))
  import subprocess
  import os
  os.chdir(output_dir)
  subprocess.call(["pdflatex", "--enable-write18",
                   "-shell-escape", "sbp_out.tex"])
  subprocess.call(["mv", "sbp_out.pdf", "../docs/sbp.pdf"])

Response: Render and output the source file for the given package_specs and version.


Instruction:
Can you create a Python 3 function that
gets and parse arguments.

Response:
def get_args():
    """
    Get and parse arguments.
    """
    import argparse
    parser = argparse.ArgumentParser(
        description="Swift Navigation SBP Example.")
    parser.add_argument(
        "-s",
        "--serial-port",
        default=[DEFAULT_SERIAL_PORT],
        nargs=1,
        help="specify the serial port to use.")
    parser.add_argument(
        "-b",
        "--baud",
        default=[DEFAULT_SERIAL_BAUD],
        nargs=1,
        help="specify the baud rate to use.")
    parser.add_argument(
        "-a",
        "--address",
        default=[DEFAULT_UDP_ADDRESS],
        nargs=1,
        help="specify the serial port to use.")
    parser.add_argument(
        "-p",
        "--udp-port",
        default=[DEFAULT_UDP_PORT],
        nargs=1,
        help="specify the baud rate to use.")
    return parser.parse_args()


Instruction:
Implement a Python 3 function for
converting a value to a C language appropriate identifier format.

Response:
def convert(value):
  """Converts to a C language appropriate identifier format.

  """
  s0 = "Sbp" + value if value in COLLISIONS else value
  s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', s0)
  return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower() + "_t"


Instruction:
How would you code a function in Python 3 to
build an identifier from a field.

Response:
def mk_id(field):
  """Builds an identifier from a field.
  """
  name = field.type_id
  if name == "string":
    return "%s" % ("char")
  elif name == "array" and field.size:
    if field.options['fill'].value not in CONSTRUCT_CODE:
      return "%s" % convert(field.options['fill'].value)
    else:
      return "%s" % field.options['fill'].value
  elif name == "array":
    return "%s" % convert(field.options['fill'].value)
  elif name not in CONSTRUCT_CODE:
    return convert(name)
  else:
    return name


Instruction:
Given the following Python 3 function, write the documentation
def mk_size(field):
  name = field.type_id
  if name == "string" and field.options.get('size', None):
    return "%s[%d];" % (field.identifier, field.options.get('size').value)
  elif name == "string":
    return "%s[0];" % field.identifier
  elif name == "array" and field.options.get('size', None):
    return "%s[%d];" % (field.identifier, field.options.get('size').value)
  elif name == "array":
    return "%s[0];" % field.identifier
  else:
    return '%s;' % field.identifier

Response: Builds an identifier for a container type.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def render_source(output_dir, package_spec):
  path, name = package_spec.filepath
  destination_filename = "%s/%s.h" % (output_dir, name)
  py_template = JENV.get_template(MESSAGES_TEMPLATE_NAME)
  with open(destination_filename, 'w') as f:
    f.write(py_template.render(msgs=package_spec.definitions,
                               pkg_name=name,
                               filepath="/".join(package_spec.filepath) + ".yaml",
                               max_msgid_len=package_spec.max_msgid_len,
                               description=package_spec.description,
                               timestamp=package_spec.creation_timestamp,
                               include=extensions(package_spec.includes)))

Response: Render and output a source file given a package specification.


Instruction:
Make a summary of the following Python 3 code
def crc16jit(buf, offset, crc, length):
  for index in range(offset, offset + length):
    data = buf[index]
    lookup = crc16_tab[((nb.u2(crc) >> 8) & nb.u2(0xFF)) ^ (data & nb.u2(0xFF))]
    crc = ((nb.u2(crc) << nb.u2(8)) & nb.u2(0xFFFF)) ^ lookup
    crc = nb.u2(crc) & nb.u2(0xFFFF)
  return crc

Response: CRC16 implementation acording to CCITT standards.


Instruction:
Can you tell what is the following Python 3 function doing
def crc16_nojit(s, crc=0):
  for ch in bytearray(s):  # bytearray's elements are integers in both python 2 and 3
    crc = ((crc << 8) & 0xFFFF) ^ _crc16_tab[((crc >> 8) & 0xFF) ^ (ch & 0xFF)]
    crc &= 0xFFFF
  return crc

Response: CRC16 implementation acording to CCITT standards.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _get_framed(self, buf, offset, insert_payload):
    header_offset = offset + self._header_len
    self.length = insert_payload(buf, header_offset, self.payload)
    struct.pack_into(self._header_fmt,
                     buf,
                     offset,
                     self.preamble,
                     self.msg_type,
                     self.sender,
                     self.length)
    crc_offset = header_offset + self.length
    preamble_bytes = 1
    crc_over_len = self._header_len + self.length - preamble_bytes
    self.crc = crc16jit(buf, offset+1, 0, crc_over_len)
    struct.pack_into(self._crc_fmt, buf, crc_offset, self.crc)
    length = preamble_bytes + crc_over_len + self._crc_len
    return length

Response: Returns the framed message and updates the CRC.


Instruction:
Given the following Python 3 function, write the documentation
def pack(self):
    buf = np.zeros(512, dtype=np.uint8)
    packed_len = self._get_framed(buf, 0, self._copy_payload)
    d = buf[:packed_len]
    return d.tobytes()

Response: Pack to framed binary message.


Instruction:
Here you have a function in Python 3, explain what it does
def pack_into(self, buf, offset, write_payload):
    return self._get_framed(buf, offset, write_payload)

Response: Pack the binary message into the buffer.


Instruction:
How would you code a function in Python 3 to
unpack and return a framed binary message.

Response:
def unpack(d):
    """Unpack and return a framed binary message.

    """
    p = SBP._parser.parse(d)
    assert p.preamble == SBP_PREAMBLE, "Invalid preamble 0x%x." % p.preamble
    return SBP(p.msg_type, p.sender, p.length, p.payload, p.crc)


Instruction:
Write a Python 3 function that can
produce a JSON - encoded SBP message.

Response:
def to_json(self, sort_keys=False):
    """Produce a JSON-encoded SBP message.

    """
    d = self.to_json_dict()
    return json.dumps(d, sort_keys=sort_keys)


Instruction:
Create a Python 3 function for
giving a JSON - encoded message build an object.

Response:
def from_json(s):
    """Given a JSON-encoded message, build an object.

    """
    d = json.loads(s)
    sbp = SBP.from_json_dict(d)
    return sbp


Instruction:
Write a Python 3 function for
reading a set of bytes from the underlying file and returns them as a string.

Response:
def read(self, size):
        """
        Read wrapper.

        Parameters
        ----------
        size : int
          Number of bytes to read.
        """
        try:
            return self.handle.read(size)
        except (OSError, serial.SerialException):
            print()
            print("Piksi disconnected")
            print()
            self.handle.close()
            raise IOError


Instruction:
Write a Python 3 script to
wrap for serial. write.

Response:
def write(self, s):
        """
        Write wrapper.

        Parameters
        ----------
        s : bytes
          Bytes to write
        """
        try:
            return self.handle.write(s)
        except (OSError, serial.SerialException,
                serial.writeTimeoutError) as e:
            if e == serial.writeTimeoutError:
                print("sbp pyserial_driver: writeTimeoutError")
                return 0
            else:
                print()
                print("Piksi disconnected")
                print()
                self.handle.close()
                raise IOError


Instruction:
Can you generate a brief explanation for the following Python 3 code
def parse_type(field):
  if field.type_id == 'string':
    if 'size' in field.options:
      return "parser.getString(%d)" % field.options['size'].value
    else:
      return "parser.getString()"
  elif field.type_id in JAVA_TYPE_MAP:
    # Primitive java types have extractor methods in SBPMessage.Parser
    return "parser.get" + field.type_id.capitalize() + "()"
  if field.type_id == 'array':
    # Call function to build array
    t = field.options['fill'].value
    if t in JAVA_TYPE_MAP:
      if 'size' in field.options:
        return "parser.getArrayof%s(%d)" % (t.capitalize(), field.options['size'].value)
      else:
        return "parser.getArrayof%s()" % t.capitalize()
    else:
      if 'size' in field.options:
        return "parser.getArray(%s.class, %d)" % (t, field.options['size'].value)
      else:
        return "parser.getArray(%s.class)" % t
  else:
    # This is an inner class, call default constructor
    return "new %s().parse(parser)" % field.type_id

Response: Function to pull a type from the binary payload.


Instruction:
Make a summary of the following Python 3 code
def build_type(field):
  if field.type_id == 'string':
    if 'size' in field.options:
      return "builder.putString(%s, %d)" % (field.identifier, field.options['size'].value)
    else:
      return "builder.putString(%s)" % field.identifier
  elif field.type_id in JAVA_TYPE_MAP:
    # Primitive java types have extractor methods in SBPMessage.Builder
    return "builder.put%s(%s)" % (field.type_id.capitalize(), field.identifier)
  if field.type_id == 'array':
    # Call function to build array
    t = field.options['fill'].value
    if t in JAVA_TYPE_MAP:
      if 'size' in field.options:
        return "builder.putArrayof%s(%s, %d)" % (t.capitalize(),
                                                 field.identifier,
                                                 field.options['size'].value)
      else:
        return "builder.putArrayof%s(%s)" % (t.capitalize(), field.identifier)
    else:
      if 'size' in field.options:
        return "builder.putArray(%s, %d)" % (field.identifier, field.options['size'].value)
      else:
        return "builder.putArray(%s)" % field.identifier
  else:
    return "%s.build(builder)" % field.identifier

Response: Function to build a type into the binary payload.


Instruction:
How would you explain what the following Python 3 function does
def render_source(output_dir, package_spec, jenv=JENV):
  path, module_name = package_spec.filepath
  java_template = jenv.get_template(TEMPLATE_NAME)
  module_path = "com." + package_spec.identifier
  yaml_filepath = "/".join(package_spec.filepath) + ".yaml"
  includes = [".".join(i.split(".")[:-1]) for i in package_spec.includes]
  includes = [i for i in includes if i != "types"]
  for msg in package_spec.definitions:
    msg_name = classnameify(msg.identifier) if msg.sbp_id else msg.identifier
    l = "/".join(package_spec.filepath)
    destination_filename = "%s/com/%s/%s.java" % (output_dir, l , msg_name)

    # Create the output directory if it doesn't exist
    if not os.path.exists(os.path.dirname(destination_filename)):
      os.mkdir(os.path.dirname(destination_filename))

    with open(destination_filename, 'w+') as f:
        print(destination_filename)
        f.write(java_template.render(m=msg,
                                     filepath=yaml_filepath,
                                     module_path=module_path,
                                     include=includes,
                                     description=package_spec.description))

Response: Render and output the source code for the package_spec.


Instruction:
Write a Python 3 function for
rendering and output dispatch table

Response:
def render_table(output_dir, packages, jenv=JENV):
  """
  Render and output dispatch table
  """
  destination_filename = output_dir + "/com/swiftnav/sbp/client/MessageTable.java"
  with open(destination_filename, 'w+') as f:
      print(destination_filename)
      f.write(jenv.get_template(TEMPLATE_TABLE_NAME).render(packages=packages))


Instruction:
Here you have a function in Python 3, explain what it does
def construct_format(f, type_map=CONSTRUCT_CODE):
  formatted = ""
  if type_map.get(f.type_id, None):
    return "%s('%s')" % (type_map.get(f.type_id), f.identifier)
  elif f.type_id == 'string' and f.options.get('size', None):
    return "string('%s', { length: %d })" % (f.identifier, f.options['size'].value)
  elif f.type_id == 'string':
    return "string('%s', { greedy: true })" % (f.identifier)
  elif f.type_id == 'array':
    fill = f.options['fill'].value
    f_ = copy.copy(f)
    f_.type_id = fill
    size = f.options.get('size', None)
    size_fn = f.options.get('size_fn', None)
    field_type = type_map.get(f_.type_id, None)
    if field_type is None:
      field_type = "%s.prototype.parser" % f_.type_id
    else:
      field_type = "'%s'" % field_type
    if size is not None:
      d = { "'uint16'" : "'uint16le'", "'uint32'" : "'uint32le'", "'uint64'" : "'uint16le'",
            "'int16'" : "'int16le'", "'int32'" : "'int32le'", "'int64'" : "'int16le'" }
      field_type_arr = d.get(field_type, field_type)
      return "array('%s', { length: %d, type: %s })" % (f.identifier, size.value, field_type_arr)
    elif f.options.get('size_fn') is not None:
      return "array('%s', { type: %s, length: '%s' })" % (f_.identifier, field_type, size_fn.value)
    else:
      return "array('%s', { type: %s, readUntil: 'eof' })" % (f_.identifier, field_type)
  else:
    return "nest('%s', { type: %s.prototype.parser })" % (f.identifier, f.type_id)
  return formatted

Response: Constructs a format string for binary - parser library.


Instruction:
Write a Python 3 function that can
give a binary payload d update the appropriate payload fields of the message.

Response:
def from_binary(self, d):
    """Given a binary payload d, update the appropriate payload fields of
    the message.

    """
    p = MsgEphemerisGPSDepF._parser.parse(d)
    for n in self.__class__.__slots__:
      setattr(self, n, getattr(p, n))


Instruction:
Can you generate the documentation for the following Python 3 function
def to_binary(self):
    c = containerize(exclude_fields(self))
    self.payload = MsgEphemerisGPSDepF._parser.build(c)
    return self.pack()

Response: Produce a framed or packed SBP message.


Instruction:
Implement a Python 3 function for
producing a framed SBP message into the provided buffer and offset.

Response:
def into_buffer(self, buf, offset):
    """Produce a framed/packed SBP message into the provided buffer and offset.

    """
    self.payload = containerize(exclude_fields(self))
    self.parser = MsgEphemerisGPSDepF._parser
    self.stream_payload.reset(buf, offset)
    return self.pack_into(buf, offset, self._build_payload)


Instruction:
Explain what the following Python 3 code does
def _readall(self, size):
        data = b""
        while len(data) < size:
            d = self._read(size - len(data))
            if self._broken:
                raise StopIteration
            if not d:
                # NOTE (Buro/jgross): Force a yield here to another thread. In
                # case the stream fails midstream, the spinning here causes
                # the UI thread to lock up without yielding.
                time.sleep(0)
                continue
            data += d
        return data

Response: Reads all the available bytes from the stream.


Instruction:
How would you implement a function in Python 3 that
reads and build a new SBP message.

Response:
def _receive(self):
        """
        Read and build SBP message.
        """
        # preamble - not readall(1) to allow breaking before messages,
        # empty input
        preamble = self._read(1)
        if not preamble:
            return None
        elif ord(preamble) != SBP_PREAMBLE:
            if self._verbose:
                print("Host Side Unhandled byte: 0x%02x" % ord(preamble))
            return None
        # hdr
        hdr = self._readall(5)
        msg_crc = crc16(hdr)
        msg_type, sender, msg_len = struct.unpack("<HHB", hdr)
        # data
        data = self._readall(msg_len)
        msg_crc = crc16(data, msg_crc)
        # crc
        crc = self._readall(2)
        crc, = struct.unpack("<H", crc)
        if crc != msg_crc:
            if self._verbose:
                print("crc mismatch: 0x%04X 0x%04X" % (msg_crc, crc))
            return None
        msg = SBP(msg_type, sender, msg_len, data, crc)
        try:
            msg = self._dispatch(msg)
        except Exception as exc:
            warnings.warn("SBP dispatch error: %s" % (exc,))
        return msg


Instruction:
Here you have a function in Python 3, explain what it does
def dispatch(msg, table=_SBP_TABLE):
  try:
    return table[msg.msg_type](msg)
  except KeyError:
    warn = "No message found for msg_type id %d for msg %s." \
           % (msg.msg_type, msg)
    warnings.warn(warn, RuntimeWarning)
    return msg
  except FormatFieldError:
    warnings.warn("SBP payload deserialization error! 0x%x" % msg.msg_type,
                  RuntimeWarning)
    return msg

Response:
Dispatch an SBP message based on its msg_type and parse its
 payload.


Instruction:
Can you generate the documentation for the following Python 3 function
def _recv_thread(self):
        for msg, metadata in self._source:
            if msg.msg_type:
                self._call(msg, **metadata)
        # Break any upstream iterators
        for sink in self._sinks:
            i = sink()
            if i is not None:
                i.breakiter()
        self._dead = True

Response: Internal thread to iterate over source messages and dispatch callbacks.


Instruction:
How would you explain what the following Python 3 function does
def filter(self, msg_type=None, maxsize=0):
        if self._dead:
            return iter(())
        iterator = Handler._SBPQueueIterator(maxsize)
        # We use a weakref so that the iterator may be garbage collected if it's
        # consumer no longer has a reference.
        ref = weakref.ref(iterator)
        self._sinks.append(ref)

        def feediter(msg, **metadata):
            i = ref()
            if i is not None:
                i(msg, **metadata)
            else:
                raise Handler._DeadCallbackException

        self.add_callback(feediter, msg_type)
        return iterator

Response:
Get a filtered iterator of messages for synchronous blocking use in
        another thread.


Instruction:
Can you tell what is the following Python 3 function doing
def add_callback(self, callback, msg_type=None):
        cb_keys = self._to_iter(msg_type)
        if cb_keys is not None:
            for msg_type_ in cb_keys:
                self._callbacks[msg_type_].add(callback)
        else:
            self._callbacks[msg_type].add(callback)

Response: Adds a callback to the internal list of all the active message types.


Instruction:
Given the following Python 3 function, write the documentation
def remove_callback(self, callback, msg_type=None):
        if msg_type is None:
            msg_type = self._callbacks.keys()
        cb_keys = self._to_iter(msg_type)
        if cb_keys is not None:
            for msg_type_ in cb_keys:
                try:
                    self._callbacks[msg_type_].remove(callback)
                except KeyError:
                    pass
        else:
            self._callbacks[msg_type].remove(callback)

Response: Removes a callback from the list of callbacks.


Instruction:
How would you explain what the following Python 3 function does
def _gc_dead_sinks(self):
        deadsinks = []
        for i in self._sinks:
            if i() is None:
                deadsinks.append(i)
        for i in deadsinks:
            self._sinks.remove(i)

Response: Remove any dead weakrefs.


Instruction:
How would you explain what the following Python 3 function does
def _call(self, msg, **metadata):
        if msg.msg_type:
            for callback in self._get_callbacks(msg.msg_type):
                try:
                    callback(msg, **metadata)
                except Handler._DeadCallbackException:
                    # The callback was an upstream iterator that has been garbage
                    # collected.  Remove it from our internal structures.
                    self.remove_callback(callback)
                    self._gc_dead_sinks()
                except SystemExit:
                    raise
                except:
                    import traceback
                    traceback.print_exc()

Response: Process a message with all callbacks.


Instruction:
Write a Python 3 script for
waiting for a SBP message.

Response:
def wait(self, msg_type, timeout=1.0):
        """
        Wait for a SBP message.

        Parameters
        ----------
        msg_type : int
          SBP message type.
        timeout : float
          Waiting period
        """
        event = threading.Event()
        payload = {'data': None}

        def cb(sbp_msg, **metadata):
            payload['data'] = sbp_msg
            event.set()

        self.add_callback(cb, msg_type)
        event.wait(timeout)
        self.remove_callback(cb, msg_type)
        return payload['data']


Instruction:
Can you write a function in Python 3 where it
waits for a message with a callback.

Response:
def wait_callback(self, callback, msg_type=None, timeout=1.0):
        """
        Wait for a SBP message with a callback.

        Parameters
        ----------
        callback : fn
          Callback function
        msg_type : int | iterable
          Message type to register callback against. Default `None` means global callback.
          Iterable type adds the callback to all the message types.
        timeout : float
          Waiting period
        """
        event = threading.Event()

        def cb(msg, **metadata):
            callback(msg, **metadata)
            event.set()

        self.add_callback(cb, msg_type)
        event.wait(timeout)
        self.remove_callback(cb, msg_type)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def read(self, size):
        try:
            return_val = self.handle.read(size)
            if return_val == '':
                print()
                print("Piksi disconnected")
                print()
                raise IOError
            return return_val
        except OSError:
            print()
            print("Piksi disconnected")
            print()
            raise IOError

Response: Reads a set of bytes from the file and returns it.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def write(self, s):
        try:
            return self.handle.write(s)
        except OSError:
            print()
            print("Piksi disconnected")
            print()
            raise IOError

Response: Write a bytes object to the log.


Instruction:
How would you implement a function in Python 3 that
returns pretty printed string representation of an object.

Response:
def fmt_repr(obj):
  """
  Return pretty printed string representation of an object.

  """
  items = {k: v for k, v in list(obj.__dict__.items())}
  return "<%s: {%s}>" % (obj.__class__.__name__, pprint.pformat(items, width=1))


Instruction:
Can you tell what is the following Python 3 function doing
def capture_setting(self, sbp_msg, **metadata):
        if sbp_msg.msg_type == SBP_MSG_SETTINGS_READ_RESP:
            section, setting, value = sbp_msg.payload.split(b'\0')[:3]
            self.settings.append((section, setting, value))

Response: Callback to extract and store setting values from SBP_MSG_SETTINGS_READ_RESP and store them in self. settings.


Instruction:
Can you generate the documentation for the following Python 3 function
def wait_for_setting_value(self, section, setting, value, wait_time=5.0):
        expire = time.time() + wait_time
        ok = False
        while not ok and time.time() < expire:
            settings = [x for x in self.settings if (x[0], x[1]) == (section, setting)]
            # Check to see if the last setting has the value we want
            if len(settings) > 0:
                ok = settings[-1][2] == value

            time.sleep(0.1)
        return ok

Response: Function to wait until a user - specified value is set in the specified section.


Instruction:
Can you generate the documentation for the following Python 3 function
def clear(self, section=None, setting=None, value=None):
        match = [all((section is None or x_y_z[0] == section,
                      setting is None or x_y_z[1] == setting,
                      value is None or x_y_z[2] == value)) for x_y_z in self.settings]

        keep = [setting_remove for setting_remove in zip(self.settings,match) if not setting_remove[1]]

        self.settings[:] = [x[0] for x in keep]

Response: Clear the settings of the current session.


Instruction:
How would you code a function in Python 3 to
render and output a source file from a package specification.

Response:
def render_source(output_dir, package_spec, jenv=JENV):
  """
  Render and output
  """
  path, name = package_spec.filepath
  directory = output_dir
  destination_filename = "%s/%s.py" % (directory, name)
  py_template = jenv.get_template(TEMPLATE_NAME)
  module_path = ".".join(package_spec.identifier.split(".")[1:-1])
  includes = [".".join(i.split(".")[:-1]) for i in package_spec.includes]
  includes = [i for i in includes if i != "types"]
  print(destination_filename, includes)
  with open(destination_filename, 'w') as f:
    f.write(py_template.render(msgs=package_spec.definitions,
                               filepath="/".join(package_spec.filepath) + ".yaml",
                               module_path=module_path,
                               include=includes,
                               timestamp=package_spec.creation_timestamp,
                               description=package_spec.description))


Instruction:
Explain what the following Python 3 code does
def read(self, size):
        data = None
        while True:
            try:
                data = self.handle.recv(size)
            except socket.timeout as socket_error:
                self._reconnect(socket_error)
            except socket.error as socket_error:
                # this is fine, just retry
                if socket_error.errno == errno.EINTR:
                    continue
                self._reconnect(IOError)
            if not data:
                self._reconnect(IOError)
            break
        return data

Response: Reads from the socket and returns the data.


Instruction:
How would you code a function in Python 3 to
write a bytes to the log.

Response:
def write(self, s):
        """
        Write wrapper.

        Parameters
        ----------
        s : bytes
          Bytes to write
        """
        try:
            self._write_lock.acquire()
            self.handle.sendall(s)
        except socket.timeout:
            self._connect()
        except socket.error:
            raise IOError
        finally:
            self._write_lock.release()


Instruction:
Here you have a function in Python 3, explain what it does
def exclude_fields(obj, exclude=EXCLUDE):
  return dict([(k, getattr(obj, k)) for k in obj.__slots__ if k not in exclude])

Response: Return dict of object without parent attrs.


Instruction:
Explain what the following Python 3 code does
def walk_json_dict(coll):
  if isinstance(coll, dict):
    return dict((k, walk_json_dict(v)) for (k, v) in iter(coll.items()))
  elif isinstance(coll, bytes):
    return coll.decode('ascii')
  elif hasattr(coll, '__iter__') and not isinstance(coll, str):
    return [walk_json_dict(seq) for seq in coll]
  else:
    return coll

Response:
Flatten a parsed SBP object into dicts and lists which are
  compatible for JSON output.


Instruction:
How would you explain what the following Python 3 function does
def containerize(coll):
  if isinstance(coll, Container):
    [setattr(coll, k, containerize(v)) for (k, v) in coll.items()]
    return coll
  elif isinstance(coll, dict):
    return containerize(Container(**coll))
  elif isinstance(coll, list):
    for j, i in enumerate(coll):
      if isinstance(i, dict):
        coll[j] = containerize(Container(**i))
    return coll
  else:
    return coll

Response:
Walk attribute fields passed from an SBP message and convert them to a Container where appropriate. Used for Construct proper
  serialization.