Instruction:
Create a Python 3 function to
print a string through a pager and return a new object.

Response:
def page(strng, start=0, screen_lines=0, pager_cmd=None,
         html=None, auto_html=False):
    """Print a string, piping through a pager.

    This version ignores the screen_lines and pager_cmd arguments and uses
    IPython's payload system instead.

    Parameters
    ----------
    strng : str
      Text to page.

    start : int
      Starting line at which to place the display.

    html : str, optional
      If given, an html string to send as well.

    auto_html : bool, optional
      If true, the input string is assumed to be valid reStructuredText and is
      converted to HTML with docutils.  Note that if docutils is not found,
      this option is silently ignored.

    Note
    ----

    Only one of the ``html`` and ``auto_html`` options can be given, not
    both.
    """

    # Some routines may auto-compute start offsets incorrectly and pass a
    # negative value.  Offset to 0 for robustness.
    start = max(0, start)
    shell = InteractiveShell.instance()

    if auto_html:
        try:
            # These defaults ensure user configuration variables for docutils
            # are not loaded, only our config is used here.
            defaults = {'file_insertion_enabled': 0,
                        'raw_enabled': 0,
                        '_disable_config': 1}
            html = publish_string(strng, writer_name='html',
                                  settings_overrides=defaults)
        except:
            pass

    payload = dict(
        source='IPython.zmq.page.page',
        text=strng,
        html=html,
        start_line_number=start
        )
    shell.payload_manager.write_payload(payload)


Instruction:
How would you explain what the following Python 3 function does
def from_line(cls, name, comes_from=None, isolated=False):
        from pip.index import Link

        url = None
        if is_url(name):
            marker_sep = '; '
        else:
            marker_sep = ';'
        if marker_sep in name:
            name, markers = name.split(marker_sep, 1)
            markers = markers.strip()
            if not markers:
                markers = None
        else:
            markers = None
        name = name.strip()
        req = None
        path = os.path.normpath(os.path.abspath(name))
        link = None

        if is_url(name):
            link = Link(name)
        elif (os.path.isdir(path)
                and (os.path.sep in name or name.startswith('.'))):
            if not is_installable_dir(path):
                raise InstallationError(
                    "Directory %r is not installable. File 'setup.py' not "
                    "found." % name
                )
            link = Link(path_to_url(name))
        elif is_archive_file(path):
            if not os.path.isfile(path):
                logger.warning(
                    'Requirement %r looks like a filename, but the file does '
                    'not exist',
                    name
                )
            link = Link(path_to_url(name))

        # it's a local file, dir, or url
        if link:

            url = link.url
            # Handle relative file URLs
            if link.scheme == 'file' and re.search(r'\.\./', url):
                url = path_to_url(os.path.normpath(os.path.abspath(link.path)))

            # wheel file
            if link.ext == wheel_ext:
                wheel = Wheel(link.filename)  # can raise InvalidWheelFilename
                if not wheel.supported():
                    raise UnsupportedWheel(
                        "%s is not a supported wheel on this platform." %
                        wheel.filename
                    )
                req = "%s==%s" % (wheel.name, wheel.version)
            else:
                # set the req to the egg fragment.  when it's not there, this
                # will become an 'unnamed' requirement
                req = link.egg_fragment

        # a requirement specifier
        else:
            req = name

        return cls(req, comes_from, url=url, markers=markers,
                   isolated=isolated)

Response: Creates an InstallRequirement object from a line of text.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def load_pyconfig_files(config_files, path):
    config = Config()
    for cf in config_files:
        loader = PyFileConfigLoader(cf, path=path)
        try:
            next_config = loader.load_config()
        except ConfigFileNotFound:
            pass
        except:
            raise
        else:
            config._merge(next_config)
    return config

Response: Load multiple Python config files into a single config object.


Instruction:
Write a Python 3 function that can
load the config from a file and return it as a Struct.

Response:
def load_config(self):
        """Load the config from a file and return it as a Struct."""
        self.clear()
        try:
            self._find_file()
        except IOError as e:
            raise ConfigFileNotFound(str(e))
        self._read_file_as_dict()
        self._convert_to_config()
        return self.config


Instruction:
Can you create a Python 3 function that
loads the config file into self. config with recursive loading.

Response:
def _read_file_as_dict(self):
        """Load the config file into self.config, with recursive loading."""
        # This closure is made available in the namespace that is used
        # to exec the config file.  It allows users to call
        # load_subconfig('myconfig.py') to load config files recursively.
        # It needs to be a closure because it has references to self.path
        # and self.config.  The sub-config is loaded with the same path
        # as the parent, but it uses an empty config which is then merged
        # with the parents.

        # If a profile is specified, the config file will be loaded
        # from that profile

        def load_subconfig(fname, profile=None):
            # import here to prevent circular imports
            from IPython.core.profiledir import ProfileDir, ProfileDirError
            if profile is not None:
                try:
                    profile_dir = ProfileDir.find_profile_dir_by_name(
                            get_ipython_dir(),
                            profile,
                    )
                except ProfileDirError:
                    return
                path = profile_dir.location
            else:
                path = self.path
            loader = PyFileConfigLoader(fname, path)
            try:
                sub_config = loader.load_config()
            except ConfigFileNotFound:
                # Pass silently if the sub config is not there. This happens
                # when a user s using a profile, but not the default config.
                pass
            else:
                self.config._merge(sub_config)

        # Again, this needs to be a closure and should be used in config
        # files to get the config being loaded.
        def get_config():
            return self.config

        namespace = dict(load_subconfig=load_subconfig, get_config=get_config)
        fs_encoding = sys.getfilesystemencoding() or 'ascii'
        conf_filename = self.full_filename.encode(fs_encoding)
        py3compat.execfile(conf_filename, namespace)


Instruction:
Create a Python 3 function to
execute the config string.

Response:
def _exec_config_str(self, lhs, rhs):
        """execute self.config.<lhs> = <rhs>

        * expands ~ with expanduser
        * tries to assign with raw eval, otherwise assigns with just the string,
          allowing `--C.a=foobar` and `--C.a="foobar"` to be equivalent.  *Not*
          equivalent are `--C.a=4` and `--C.a='4'`.
        """
        rhs = os.path.expanduser(rhs)
        try:
            # Try to see if regular Python syntax will work. This
            # won't handle strings as the quote marks are removed
            # by the system shell.
            value = eval(rhs)
        except (NameError, SyntaxError):
            # This case happens if the rhs is a string.
            value = rhs

        exec u'self.config.%s = value' % lhs


Instruction:
Can you write a function in Python 3 where it
updates self. config from a flag which can be a dict or Config

Response:
def _load_flag(self, cfg):
        """update self.config from a flag, which can be a dict or Config"""
        if isinstance(cfg, (dict, Config)):
            # don't clobber whole config sections, update
            # each section from config:
            for sec,c in cfg.iteritems():
                self.config[sec].update(c)
        else:
            raise TypeError("Invalid flag: %r" % cfg)


Instruction:
How would you implement a function in Python 3 that
decodes argv if bytes using stin. encoding falling back on default enc

Response:
def _decode_argv(self, argv, enc=None):
        """decode argv if bytes, using stin.encoding, falling back on default enc"""
        uargv = []
        if enc is None:
            enc = DEFAULT_ENCODING
        for arg in argv:
            if not isinstance(arg, unicode):
                # only decode if not already decoded
                arg = arg.decode(enc)
            uargv.append(arg)
        return uargv


Instruction:
Here you have a function in Python 3, explain what it does
def load_config(self, argv=None, aliases=None, flags=None):
        from IPython.config.configurable import Configurable

        self.clear()
        if argv is None:
            argv = self.argv
        if aliases is None:
            aliases = self.aliases
        if flags is None:
            flags = self.flags

        # ensure argv is a list of unicode strings:
        uargv = self._decode_argv(argv)
        for idx,raw in enumerate(uargv):
            # strip leading '-'
            item = raw.lstrip('-')

            if raw == '--':
                # don't parse arguments after '--'
                # this is useful for relaying arguments to scripts, e.g.
                # ipython -i foo.py --pylab=qt -- args after '--' go-to-foo.py
                self.extra_args.extend(uargv[idx+1:])
                break

            if kv_pattern.match(raw):
                lhs,rhs = item.split('=',1)
                # Substitute longnames for aliases.
                if lhs in aliases:
                    lhs = aliases[lhs]
                if '.' not in lhs:
                    # probably a mistyped alias, but not technically illegal
                    warn.warn("Unrecognized alias: '%s', it will probably have no effect."%lhs)
                try:
                    self._exec_config_str(lhs, rhs)
                except Exception:
                    raise ArgumentError("Invalid argument: '%s'" % raw)

            elif flag_pattern.match(raw):
                if item in flags:
                    cfg,help = flags[item]
                    self._load_flag(cfg)
                else:
                    raise ArgumentError("Unrecognized flag: '%s'"%raw)
            elif raw.startswith('-'):
                kv = '--'+item
                if kv_pattern.match(kv):
                    raise ArgumentError("Invalid argument: '%s', did you mean '%s'?"%(raw, kv))
                else:
                    raise ArgumentError("Invalid argument: '%s'"%raw)
            else:
                # keep all args that aren't valid in a list,
                # in case our parent knows what to do with them.
                self.extra_args.append(item)
        return self.config

Response: Parse the configuration and generate a Config object.


Instruction:
Explain what the following Python 3 code does
def load_config(self, argv=None, aliases=None, flags=None):
        self.clear()
        if argv is None:
            argv = self.argv
        if aliases is None:
            aliases = self.aliases
        if flags is None:
            flags = self.flags
        self._create_parser(aliases, flags)
        self._parse_args(argv)
        self._convert_to_config()
        return self.config

Response: Parse command line arguments and return as a Config object.


Instruction:
Write a Python 3 function for
parsing command - line arguments.

Response:
def _parse_args(self, args):
        """self.parser->self.parsed_data"""
        # decode sys.argv to support unicode command-line options
        enc = DEFAULT_ENCODING
        uargs = [py3compat.cast_unicode(a, enc) for a in args]
        self.parsed_data, self.extra_args = self.parser.parse_known_args(uargs)


Instruction:
Can you generate the documentation for the following Python 3 function
def _convert_to_config(self):
        for k, v in vars(self.parsed_data).iteritems():
            exec "self.config.%s = v"%k in locals(), globals()

Response: Convert the parsed data to config.


Instruction:
Here you have a function in Python 3, explain what it does
def _convert_to_config(self):
        # remove subconfigs list from namespace before transforming the Namespace
        if '_flags' in self.parsed_data:
            subcs = self.parsed_data._flags
            del self.parsed_data._flags
        else:
            subcs = []

        for k, v in vars(self.parsed_data).iteritems():
            if v is None:
                # it was a flag that shares the name of an alias
                subcs.append(self.alias_flags[k])
            else:
                # eval the KV assignment
                self._exec_config_str(k, v)

        for subc in subcs:
            self._load_flag(subc)

        if self.extra_args:
            sub_parser = KeyValueConfigLoader()
            sub_parser.load_config(self.extra_args)
            self.config._merge(sub_parser.config)
            self.extra_args = sub_parser.extra_args

Response: Convert the parsed_data to the config.


Instruction:
Can you create a Python 3 function that
finds a module in sys. path and return the full path of the module.

Response:
def find_module(name, path=None):
    """imp.find_module variant that only return path of module.

    The `imp.find_module` returns a filehandle that we are not interested in.
    Also we ignore any bytecode files that `imp.find_module` finds.

    Parameters
    ----------
    name : str
        name of module to locate
    path : list of str
        list of paths to search for `name`. If path=None then search sys.path

    Returns
    -------
    filename : str
        Return full path of module or None if module is missing or does not have
        .py or .pyw extension
    """
    if name is None:
        return None
    try:
        file, filename, _ = imp.find_module(name, path)
    except ImportError:
        return None
    if file is None:
        return filename
    else:
        file.close()
    if os.path.splitext(filename)[1] in [".py", "pyc"]:
        return filename
    else:
        return None


Instruction:
Here you have a function in Python 3, explain what it does
def get_init(dirname):
    fbase =  os.path.join(dirname, "__init__")
    for ext in [".py", ".pyw"]:
        fname = fbase + ext
        if os.path.isfile(fname):
            return fname

Response: Get the path to the __init__ file for the specified module directory.


Instruction:
Can you generate the documentation for the following Python 3 function
def find_mod(module_name):
    parts = module_name.split(".")
    basepath = find_module(parts[0])
    for submodname in parts[1:]:
        basepath = find_module(submodname, [basepath])
    if basepath and os.path.isdir(basepath):
        basepath = get_init(basepath)
    return basepath

Response:
Find module module_name on sys. path

 Return the path to module or None if module is missing or does not have a. py or. pyw
 extension.


Instruction:
Write a Python 3 script for
registering a callback to be called with this Launcher s stop_data when the process actually finishes.

Response:
def on_stop(self, f):
        """Register a callback to be called with this Launcher's stop_data
        when the process actually finishes.
        """
        if self.state=='after':
            return f(self.stop_data)
        else:
            self.stop_callbacks.append(f)


Instruction:
Can you tell what is the following Python 3 function doing
def notify_start(self, data):

        self.log.debug('Process %r started: %r', self.args[0], data)
        self.start_data = data
        self.state = 'running'
        return data

Response: Called by the daemon when the process starts.


Instruction:
Write a Python 3 function for
calling this to trigger process stop actions.

Response:
def notify_stop(self, data):
        """Call this to trigger process stop actions.

        This logs the process stopping and sets the state to 'after'. Call
        this to trigger callbacks registered via :meth:`on_stop`."""

        self.log.debug('Process %r stopped: %r', self.args[0], data)
        self.stop_data = data
        self.state = 'after'
        for i in range(len(self.stop_callbacks)):
            d = self.stop_callbacks.pop()
            d(data)
        return data


Instruction:
Write a Python 3 script to
send INT wait a delay and then send KILL.

Response:
def interrupt_then_kill(self, delay=2.0):
        """Send INT, wait a delay and then send KILL."""
        try:
            self.signal(SIGINT)
        except Exception:
            self.log.debug("interrupt failed")
            pass
        self.killer  = ioloop.DelayedCallback(lambda : self.signal(SIGKILL), delay*1000, self.loop)
        self.killer.start()


Instruction:
Implement a Python 3 function for
starting n engines by profile or profile_dir.

Response:
def start(self, n):
        """Start n engines by profile or profile_dir."""
        dlist = []
        for i in range(n):
            if i > 0:
                time.sleep(self.delay)
            el = self.launcher_class(work_dir=self.work_dir, config=self.config, log=self.log,
                                    profile_dir=self.profile_dir, cluster_id=self.cluster_id,
            )

            # Copy the engine args over to each engine launcher.
            el.engine_cmd = copy.deepcopy(self.engine_cmd)
            el.engine_args = copy.deepcopy(self.engine_args)
            el.on_stop(self._notice_engine_stopped)
            d = el.start()
            self.launchers[i] = el
            dlist.append(d)
        self.notify_start(dlist)
        return dlist


Instruction:
Can you generate the documentation for the following Python 3 function
def find_args(self):
        return self.mpi_cmd + ['-n', str(self.n)] + self.mpi_args + \
               self.program + self.program_args

Response: Build self. args using all the fields.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def start(self, n):
        self.n = n
        return super(MPILauncher, self).start()

Response: Start n instances of the program using mpiexec.


Instruction:
Can you tell what is the following Python 3 function doing
def start(self, n):
        self.n = n
        return super(MPIEngineSetLauncher, self).start(n)

Response: Start n engines by profile or profile_dir.


Instruction:
Implement a function in Python 3 to
send a single file to the remote host

Response:
def _send_file(self, local, remote):
        """send a single file"""
        remote = "%s:%s" % (self.location, remote)
        for i in range(10):
            if not os.path.exists(local):
                self.log.debug("waiting for %s" % local)
                time.sleep(1)
            else:
                break
        self.log.info("sending %s to %s", local, remote)
        check_output(self.scp_cmd + [local, remote])


Instruction:
Here you have a function in Python 3, explain what it does
def send_files(self):
        if not self.to_send:
            return
        for local_file, remote_file in self.to_send:
            self._send_file(local_file, remote_file)

Response: send our files (called before start)


Instruction:
Can you create a Python 3 function that
fetches a single file from a remote host

Response:
def _fetch_file(self, remote, local):
        """fetch a single file"""
        full_remote = "%s:%s" % (self.location, remote)
        self.log.info("fetching %s from %s", local, full_remote)
        for i in range(10):
            # wait up to 10s for remote file to exist
            check = check_output(self.ssh_cmd + self.ssh_args + \
                [self.location, 'test -e', remote, "&& echo 'yes' || echo 'no'"])
            check = check.strip()
            if check == 'no':
                time.sleep(1)
            elif check == 'yes':
                break
        check_output(self.scp_cmd + [full_remote, local])


Instruction:
Write a Python 3 script to
fetch remote files ( called after start

Response:
def fetch_files(self):
        """fetch remote files (called after start)"""
        if not self.to_fetch:
            return
        for remote_file, local_file in self.to_fetch:
            self._fetch_file(remote_file, local_file)


Instruction:
Can you tell what is the following Python 3 function doing
def _remote_profile_dir_default(self):
        home = get_home_dir()
        if not home.endswith('/'):
            home = home+'/'

        if self.profile_dir.startswith(home):
            return self.profile_dir[len(home):]
        else:
            return self.profile_dir

Response: turns self. profile_dir into. ipython


Instruction:
Implement a Python 3 function for
determining engine count from engines dict

Response:
def engine_count(self):
        """determine engine count from `engines` dict"""
        count = 0
        for n in self.engines.itervalues():
            if isinstance(n, (tuple,list)):
                n,args = n
            count += n
        return count


Instruction:
Make a summary of the following Python 3 code
def start(self, n):

        dlist = []
        for host, n in self.engines.iteritems():
            if isinstance(n, (tuple, list)):
                n, args = n
            else:
                args = copy.deepcopy(self.engine_args)

            if '@' in host:
                user,host = host.split('@',1)
            else:
                user=None
            for i in range(n):
                if i > 0:
                    time.sleep(self.delay)
                el = self.launcher_class(work_dir=self.work_dir, config=self.config, log=self.log,
                                        profile_dir=self.profile_dir, cluster_id=self.cluster_id,
                )
                if i > 0:
                    # only send files for the first engine on each host
                    el.to_send = []

                # Copy the engine args over to each engine launcher.
                el.engine_cmd = self.engine_cmd
                el.engine_args = args
                el.on_stop(self._notice_engine_stopped)
                d = el.start(user=user, hostname=host)
                self.launchers[ "%s/%i" % (host,i) ] = el
                dlist.append(d)
        self.notify_start(dlist)
        return dlist

Response: Start engines by profile or profile_dir.


Instruction:
Can you implement a function in Python 3 that
starts n copies of the process using the Win HPC job scheduler.

Response:
def start(self, n):
        """Start n copies of the process using the Win HPC job scheduler."""
        self.write_job_file(n)
        args = [
            'submit',
            '/jobfile:%s' % self.job_file,
            '/scheduler:%s' % self.scheduler
        ]
        self.log.debug("Starting Win HPC Job: %s" % (self.job_cmd + ' ' + ' '.join(args),))

        output = check_output([self.job_cmd]+args,
            env=os.environ,
            cwd=self.work_dir,
            stderr=STDOUT
        )
        job_id = self.parse_job_id(output)
        self.notify_start(job_id)
        return job_id


Instruction:
Implement a Python 3 function for
loading the default context with the default values for the basic keys

Response:
def _context_default(self):
        """load the default context with the default values for the basic keys

        because the _trait_changed methods only load the context if they
        are set to something other than the default value.
        """
        return dict(n=1, queue=u'', profile_dir=u'', cluster_id=u'')


Instruction:
Implement a Python 3 function for
taking the output of the submit command and return the job id.

Response:
def parse_job_id(self, output):
        """Take the output of the submit command and return the job id."""
        m = self.job_id_regexp.search(output)
        if m is not None:
            job_id = m.group()
        else:
            raise LauncherError("Job id couldn't be determined: %s" % output)
        self.job_id = job_id
        self.log.info('Job submitted with job id: %r', job_id)
        return job_id


Instruction:
Write a Python 3 function for
starting n copies of the process using a batch system.

Response:
def start(self, n):
        """Start n copies of the process using a batch system."""
        self.log.debug("Starting %s: %r", self.__class__.__name__, self.args)
        # Here we save profile_dir in the context so they
        # can be used in the batch script template as {profile_dir}
        self.write_batch_script(n)
        output = check_output(self.args, env=os.environ)

        job_id = self.parse_job_id(output)
        self.notify_start(job_id)
        return job_id


Instruction:
Can you create a Python 3 function that
starts n copies of the process using LSF batch system.

Response:
def start(self, n):
        """Start n copies of the process using LSF batch system.
        This cant inherit from the base class because bsub expects
        to be piped a shell script in order to honor the #BSUB directives :
        bsub < script
        """
        # Here we save profile_dir in the context so they
        # can be used in the batch script template as {profile_dir}
        self.write_batch_script(n)
        #output = check_output(self.args, env=os.environ)
        piped_cmd = self.args[0]+'<\"'+self.args[1]+'\"'
        self.log.debug("Starting %s: %s", self.__class__.__name__, piped_cmd)
        p = Popen(piped_cmd, shell=True,env=os.environ,stdout=PIPE)
        output,err = p.communicate()
        job_id = self.parse_job_id(output)
        self.notify_start(job_id)
        return job_id


Instruction:
Here you have a function in Python 3, explain what it does
def check_filemode(filepath, mode):

    filemode = stat.S_IMODE(os.stat(filepath).st_mode)
    return (oct(filemode) == mode)

Response: Check if file is in octal mode.


Instruction:
Here you have a function in Python 3, explain what it does
def _context_menu_make(self, pos):
        format = self._control.cursorForPosition(pos).charFormat()
        name = format.stringProperty(QtGui.QTextFormat.ImageName)
        if name:
            menu = QtGui.QMenu()

            menu.addAction('Copy Image', lambda: self._copy_image(name))
            menu.addAction('Save Image As...', lambda: self._save_image(name))
            menu.addSeparator()

            svg = self._name_to_svg_map.get(name, None)
            if svg is not None:
                menu.addSeparator()
                menu.addAction('Copy SVG', lambda: svg_to_clipboard(svg))
                menu.addAction('Save SVG As...',
                               lambda: save_svg(svg, self._control))
        else:
            menu = super(RichIPythonWidget, self)._context_menu_make(pos)
        return menu

Response: Reimplemented to return a custom context menu for images.


Instruction:
Write a Python 3 script to
append the prompt and make the output nicer

Response:
def _pre_image_append(self, msg, prompt_number):
        """ Append the Out[] prompt  and make the output nicer

        Shared code for some the following if statement
        """
        self.log.debug("pyout: %s", msg.get('content', ''))
        self._append_plain_text(self.output_sep, True)
        self._append_html(self._make_out_prompt(prompt_number), True)
        self._append_plain_text('\n', True)


Instruction:
Can you tell what is the following Python 3 function doing
def _handle_pyout(self, msg):
        if not self._hidden and self._is_from_this_session(msg):
            content = msg['content']
            prompt_number = content.get('execution_count', 0)
            data = content['data']
            if data.has_key('image/svg+xml'):
                self._pre_image_append(msg, prompt_number)
                self._append_svg(data['image/svg+xml'], True)
                self._append_html(self.output_sep2, True)
            elif data.has_key('image/png'):
                self._pre_image_append(msg, prompt_number)
                self._append_png(decodestring(data['image/png'].encode('ascii')), True)
                self._append_html(self.output_sep2, True)
            elif data.has_key('image/jpeg') and self._jpg_supported:
                self._pre_image_append(msg, prompt_number)
                self._append_jpg(decodestring(data['image/jpeg'].encode('ascii')), True)
                self._append_html(self.output_sep2, True)
            else:
                # Default back to the plain text representation.
                return super(RichIPythonWidget, self)._handle_pyout(msg)

Response: Override to handle rich data types like SVG and JPEG.


Instruction:
How would you explain what the following Python 3 function does
def _handle_display_data(self, msg):
        if not self._hidden and self._is_from_this_session(msg):
            source = msg['content']['source']
            data = msg['content']['data']
            metadata = msg['content']['metadata']
            # Try to use the svg or html representations.
            # FIXME: Is this the right ordering of things to try?
            if data.has_key('image/svg+xml'):
                self.log.debug("display: %s", msg.get('content', ''))
                svg = data['image/svg+xml']
                self._append_svg(svg, True)
            elif data.has_key('image/png'):
                self.log.debug("display: %s", msg.get('content', ''))
                # PNG data is base64 encoded as it passes over the network
                # in a JSON structure so we decode it.
                png = decodestring(data['image/png'].encode('ascii'))
                self._append_png(png, True)
            elif data.has_key('image/jpeg') and self._jpg_supported:
                self.log.debug("display: %s", msg.get('content', ''))
                jpg = decodestring(data['image/jpeg'].encode('ascii'))
                self._append_jpg(jpg, True)
            else:
                # Default back to the plain text representation.
                return super(RichIPythonWidget, self)._handle_display_data(msg)

Response: Override to handle rich data types like SVG and HTML.


Instruction:
Explain what the following Python 3 code does
def _append_jpg(self, jpg, before_prompt=False):
        self._append_custom(self._insert_jpg, jpg, before_prompt)

Response: Append raw JPG data to the widget.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _append_png(self, png, before_prompt=False):
        self._append_custom(self._insert_png, png, before_prompt)

Response: Append raw PNG data to the widget.


Instruction:
Can you write a function in Python 3 where it
appends raw SVG data to the widget.

Response:
def _append_svg(self, svg, before_prompt=False):
        """ Append raw SVG data to the widget.
        """
        self._append_custom(self._insert_svg, svg, before_prompt)


Instruction:
Can you write a function in Python 3 where it
adds the specified QImage to the document and returns a QTextImageFormat that references it.

Response:
def _add_image(self, image):
        """ Adds the specified QImage to the document and returns a
            QTextImageFormat that references it.
        """
        document = self._control.document()
        name = str(image.cacheKey())
        document.addResource(QtGui.QTextDocument.ImageResource,
                             QtCore.QUrl(name), image)
        format = QtGui.QTextImageFormat()
        format.setName(name)
        return format


Instruction:
Write a Python 3 script for
copying the ImageResource with name to the clipboard.

Response:
def _copy_image(self, name):
        """ Copies the ImageResource with 'name' to the clipboard.
        """
        image = self._get_image(name)
        QtGui.QApplication.clipboard().setImage(image)


Instruction:
How would you code a function in Python 3 to
return the QImage stored as the ImageResource with name.

Response:
def _get_image(self, name):
        """ Returns the QImage stored as the ImageResource with 'name'.
        """
        document = self._control.document()
        image = document.resource(QtGui.QTextDocument.ImageResource,
                                  QtCore.QUrl(name))
        return image


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _get_image_tag(self, match, path = None, format = "png"):
        if format in ("png","jpg"):
            try:
                image = self._get_image(match.group("name"))
            except KeyError:
                return "Couldn't find image %s" % match.group("name")

            if path is not None:
                if not os.path.exists(path):
                    os.mkdir(path)
                relpath = os.path.basename(path)
                if image.save("%s/qt_img%s.%s" % (path, match.group("name"), format),
                              "PNG"):
                    return '' % (relpath,
                                                            match.group("name"),format)
                else:
                    return "Couldn't save image!"
            else:
                ba = QtCore.QByteArray()
                buffer_ = QtCore.QBuffer(ba)
                buffer_.open(QtCore.QIODevice.WriteOnly)
                image.save(buffer_, format.upper())
                buffer_.close()
                return '' % (
                    format,re.sub(r'(.{60})',r'\1\n',str(ba.toBase64())))

        elif format == "svg":
            try:
                svg = str(self._name_to_svg_map[match.group("name")])
            except KeyError:
                if not self._svg_warning_displayed:
                    QtGui.QMessageBox.warning(self, 'Error converting PNG to SVG.',
                        'Cannot convert a PNG to SVG.  To fix this, add this '
                        'to your ipython config:\n\n'
                        '\tc.InlineBackendConfig.figure_format = \'svg\'\n\n'
                        'And regenerate the figures.',
                                              QtGui.QMessageBox.Ok)
                    self._svg_warning_displayed = True
                return ("Cannot convert a PNG to SVG.  "
                        "To fix this, add this to your config: "
                        "c.InlineBackendConfig.figure_format = 'svg' "
                        "and regenerate the figures.")

            # Not currently checking path, because it's tricky to find a
            # cross-browser way to embed external SVG images (e.g., via
            # object or embed tags).

            # Chop stand-alone header from matplotlib SVG
            offset = svg.find("<svg")
            assert(offset > -1)

            return svg[offset:]

        else:
            return 'Unrecognized image format'

Response: Returns an HTML image tag given by a match.


Instruction:
How would you explain what the following Python 3 function does
def _insert_img(self, cursor, img, fmt):
        try:
            image = QtGui.QImage()
            image.loadFromData(img, fmt.upper())
        except ValueError:
            self._insert_plain_text(cursor, 'Received invalid %s data.'%fmt)
        else:
            format = self._add_image(image)
            cursor.insertBlock()
            cursor.insertImage(format)
            cursor.insertBlock()

Response: insert a raw image jpg or png


Instruction:
Can you tell what is the following Python 3 function doing
def _insert_svg(self, cursor, svg):
        try:
            image = svg_to_image(svg)
        except ValueError:
            self._insert_plain_text(cursor, 'Received invalid SVG data.')
        else:
            format = self._add_image(image)
            self._name_to_svg_map[format.name()] = svg
            cursor.insertBlock()
            cursor.insertImage(format)
            cursor.insertBlock()

Response: Insert SVG data into the widet.


Instruction:
Here you have a function in Python 3, explain what it does
def _save_image(self, name, format='PNG'):
        dialog = QtGui.QFileDialog(self._control, 'Save Image')
        dialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
        dialog.setDefaultSuffix(format.lower())
        dialog.setNameFilter('%s file (*.%s)' % (format, format.lower()))
        if dialog.exec_():
            filename = dialog.selectedFiles()[0]
            image = self._get_image(name)
            image.save(filename, format)

Response: Shows a save dialog for the ImageResource with name.


Instruction:
Here you have a function in Python 3, explain what it does
def safe_unicode(e):
    try:
        return unicode(e)
    except UnicodeError:
        pass

    try:
        return py3compat.str_to_unicode(str(e))
    except UnicodeError:
        pass

    try:
        return py3compat.str_to_unicode(repr(e))
    except UnicodeError:
        pass

    return u'Unrecoverably corrupt evalue'

Response: Unicode function that can be used to call unicode on a node.


Instruction:
Implement a function in Python 3 to
stop eventloop when exit_now fires

Response:
def _exit_now_changed(self, name, old, new):
        """stop eventloop when exit_now fires"""
        if new:
            loop = ioloop.IOLoop.instance()
            loop.add_timeout(time.time()+0.1, loop.stop)


Instruction:
Can you generate the documentation for the following Python 3 function
def init_environment(self):
        env = os.environ
        # These two ensure 'ls' produces nice coloring on BSD-derived systems
        env['TERM'] = 'xterm-color'
        env['CLICOLOR'] = '1'
        # Since normal pagers don't work at all (over pexpect we don't have
        # single-key control of the subprocess), try to disable paging in
        # subprocesses as much as possible.
        env['PAGER'] = 'cat'
        env['GIT_PAGER'] = 'cat'

        # And install the payload version of page.
        install_payload_page()

Response: Configure the user s environment.


Instruction:
Make a summary of the following Python 3 code
def auto_rewrite_input(self, cmd):
        new = self.prompt_manager.render('rewrite') + cmd
        payload = dict(
            source='IPython.zmq.zmqshell.ZMQInteractiveShell.auto_rewrite_input',
            transformed_input=new,
            )
        self.payload_manager.write_payload(payload)

Response: Called to show the auto - rewritten input for autocall and friends.


Instruction:
Write a Python 3 function for
engaging the exit actions.

Response:
def ask_exit(self):
        """Engage the exit actions."""
        self.exit_now = True
        payload = dict(
            source='IPython.zmq.zmqshell.ZMQInteractiveShell.ask_exit',
            exit=True,
            keepkernel=self.keepkernel_on_exit,
            )
        self.payload_manager.write_payload(payload)


Instruction:
Make a summary of the following Python 3 code
def set_next_input(self, text):
        payload = dict(
            source='IPython.zmq.zmqshell.ZMQInteractiveShell.set_next_input',
            text=text
        )
        self.payload_manager.write_payload(payload)

Response:
Send the specified text to the frontend to be presented at the next
            input cell.


Instruction:
Write a Python 3 script for
checking if process is running

Response:
def check_running(process_name="apache2"):
    '''
        CHECK if process (default=apache2) is not running
    '''
    if not gurumate.base.get_pid_list(process_name):
        fail("Apache process '%s' doesn't seem to be working" % process_name)
        return False #unreachable
    return True


Instruction:
Explain what the following Python 3 code does
def get_listening_ports(process_name="apache2"):
    '''
        returns a list of listening ports for running process (default=apache2)
    '''
    ports = set()
    for _, address_info in gurumate.base.get_listening_ports(process_name):
        ports.add(address_info[1])
    return list(ports)

Response: get_listening_ports returns a list of listening ports for running process


Instruction:
Can you create a Python 3 function that
reads a filename as UTF - 8 configuration data.

Response:
def read(self, filename):
        """Read a filename as UTF-8 configuration data."""
        kwargs = {}
        if sys.version_info >= (3, 2):
            kwargs['encoding'] = "utf-8"
        return configparser.RawConfigParser.read(self, filename, **kwargs)


Instruction:
How would you explain what the following Python 3 function does
def getlist(self, section, option):
        value_list = self.get(section, option)
        values = []
        for value_line in value_list.split('\n'):
            for value in value_line.split(','):
                value = value.strip()
                if value:
                    values.append(value)
        return values

Response: Read a list of strings.


Instruction:
Explain what the following Python 3 code does
def getlinelist(self, section, option):
        value_list = self.get(section, option)
        return list(filter(None, value_list.split('\n')))

Response: Read a list of full - line strings.


Instruction:
Can you implement a function in Python 3 that
reads configuration from the env_var environment variable.

Response:
def from_environment(self, env_var):
        """Read configuration from the `env_var` environment variable."""
        # Timidity: for nose users, read an environment variable.  This is a
        # cheap hack, since the rest of the command line arguments aren't
        # recognized, but it solves some users' problems.
        env = os.environ.get(env_var, '')
        if env:
            self.timid = ('--timid' in env)


Instruction:
Make a summary of the following Python 3 code
def from_args(self, **kwargs):
        for k, v in iitems(kwargs):
            if v is not None:
                if k in self.MUST_BE_LIST and isinstance(v, string_class):
                    v = [v]
                setattr(self, k, v)

Response: Read config values from kwargs.


Instruction:
Write a Python 3 script for
reading configuration from a. rc file.

Response:
def from_file(self, filename):
        """Read configuration from a .rc file.

        `filename` is a file name to read.

        """
        self.attempted_config_files.append(filename)

        cp = HandyConfigParser()
        files_read = cp.read(filename)
        if files_read is not None:  # return value changed in 2.4
            self.config_files.extend(files_read)

        for option_spec in self.CONFIG_FILE_OPTIONS:
            self.set_attr_from_config_option(cp, *option_spec)

        # [paths] is special
        if cp.has_section('paths'):
            for option in cp.options('paths'):
                self.paths[option] = cp.getlist('paths', option)


Instruction:
Write a Python 3 function that can
set an attribute on self if it exists in the ConfigParser.

Response:
def set_attr_from_config_option(self, cp, attr, where, type_=''):
        """Set an attribute on self if it exists in the ConfigParser."""
        section, option = where.split(":")
        if cp.has_option(section, option):
            method = getattr(cp, 'get'+type_)
            setattr(self, attr, method(section, option))


Instruction:
How would you explain what the following Python 3 function does
def expand_user(path):
    # Default values
    tilde_expand = False
    tilde_val = ''
    newpath = path

    if path.startswith('~'):
        tilde_expand = True
        rest = len(path)-1
        newpath = os.path.expanduser(path)
        if rest:
            tilde_val = newpath[:-rest]
        else:
            tilde_val = newpath

    return newpath, tilde_expand, tilde_val

Response: Expand ~ - style usernames in strings.


Instruction:
Create a Python 3 function for
setting the delimiters for line splitting.

Response:
def delims(self, delims):
        """Set the delimiters for line splitting."""
        expr = '[' + ''.join('\\'+ c for c in delims) + ']'
        self._delim_re = re.compile(expr)
        self._delims = delims
        self._delim_expr = expr


Instruction:
Create a Python 3 function to
split a line of text with a cursor at the given position.

Response:
def split_line(self, line, cursor_pos=None):
        """Split a line of text with a cursor at the given position.
        """
        l = line if cursor_pos is None else line[:cursor_pos]
        return self._delim_re.split(l)[-1]


Instruction:
Create a Python 3 function to
compute matches when text is a simple name.

Response:
def global_matches(self, text):
        """Compute matches when text is a simple name.

        Return a list of all keywords, built-in functions and names currently
        defined in self.namespace or self.global_namespace that match.

        """
        #print 'Completer->global_matches, txt=%r' % text # dbg
        matches = []
        match_append = matches.append
        n = len(text)
        for lst in [keyword.kwlist,
                    __builtin__.__dict__.keys(),
                    self.namespace.keys(),
                    self.global_namespace.keys()]:
            for word in lst:
                if word[:n] == text and word != "__builtins__":
                    match_append(word)
        return matches


Instruction:
Make a summary of the following Python 3 code
def attr_matches(self, text):

        #io.rprint('Completer->attr_matches, txt=%r' % text) # dbg
        # Another option, seems to work great. Catches things like ''.<tab>
        m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)

        if m:
            expr, attr = m.group(1, 3)
        elif self.greedy:
            m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
            if not m2:
                return []
            expr, attr = m2.group(1,2)
        else:
            return []

        try:
            obj = eval(expr, self.namespace)
        except:
            try:
                obj = eval(expr, self.global_namespace)
            except:
                return []

        if self.limit_to__all__ and hasattr(obj, '__all__'):
            words = get__all__entries(obj)
        else:
            words = dir2(obj)

        try:
            words = generics.complete_object(obj, words)
        except TryNext:
            pass
        except Exception:
            # Silence errors from completion function
            #raise # dbg
            pass
        # Build match list to return
        n = len(attr)
        res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
        return res

Response: Compute matches when text contains a dot.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _greedy_changed(self, name, old, new):
        if new:
            self.splitter.delims = GREEDY_DELIMS
        else:
            self.splitter.delims = DELIMS

        if self.readline:
            self.readline.set_completer_delims(self.splitter.delims)

Response: update the splitter and readline delims when greedy is changed


Instruction:
Given the following Python 3 function, write the documentation
def file_matches(self, text):

        #io.rprint('Completer->file_matches: <%r>' % text) # dbg

        # chars that require escaping with backslash - i.e. chars
        # that readline treats incorrectly as delimiters, but we
        # don't want to treat as delimiters in filename matching
        # when escaped with backslash
        if text.startswith('!'):
            text = text[1:]
            text_prefix = '!'
        else:
            text_prefix = ''

        text_until_cursor = self.text_until_cursor
        # track strings with open quotes
        open_quotes = has_open_quotes(text_until_cursor)

        if '(' in text_until_cursor or '[' in text_until_cursor:
            lsplit = text
        else:
            try:
                # arg_split ~ shlex.split, but with unicode bugs fixed by us
                lsplit = arg_split(text_until_cursor)[-1]
            except ValueError:
                # typically an unmatched ", or backslash without escaped char.
                if open_quotes:
                    lsplit = text_until_cursor.split(open_quotes)[-1]
                else:
                    return []
            except IndexError:
                # tab pressed on empty line
                lsplit = ""

        if not open_quotes and lsplit != protect_filename(lsplit):
            # if protectables are found, do matching on the whole escaped name
            has_protectables = True
            text0,text = text,lsplit
        else:
            has_protectables = False
            text = os.path.expanduser(text)

        if text == "":
            return [text_prefix + protect_filename(f) for f in self.glob("*")]

        # Compute the matches from the filesystem
        m0 = self.clean_glob(text.replace('\\',''))

        if has_protectables:
            # If we had protectables, we need to revert our changes to the
            # beginning of filename so that we don't double-write the part
            # of the filename we have so far
            len_lsplit = len(lsplit)
            matches = [text_prefix + text0 +
                       protect_filename(f[len_lsplit:]) for f in m0]
        else:
            if open_quotes:
                # if we have a string with an open quote, we don't need to
                # protect the names at all (and we _shouldn't_, as it
                # would cause bugs when the filesystem call is made).
                matches = m0
            else:
                matches = [text_prefix +
                           protect_filename(f) for f in m0]

        #io.rprint('mm', matches)  # dbg

        # Mark directories in input list by appending '/' to their names.
        matches = [x+'/' if os.path.isdir(x) else x for x in matches]
        return matches

Response: Match filenames expanding ~USER type strings.


Instruction:
Here you have a function in Python 3, explain what it does
def magic_matches(self, text):
        #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg
        # Get all shell magics now rather than statically, so magics loaded at
        # runtime show up too.
        lsm = self.shell.magics_manager.lsmagic()
        line_magics = lsm['line']
        cell_magics = lsm['cell']
        pre = self.magic_escape
        pre2 = pre+pre

        # Completion logic:
        # - user gives %%: only do cell magics
        # - user gives %: do both line and cell magics
        # - no prefix: do both
        # In other words, line magics are skipped if the user gives %% explicitly
        bare_text = text.lstrip(pre)
        comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)]
        if not text.startswith(pre2):
            comp += [ pre+m for m in line_magics if m.startswith(bare_text)]
        return comp

Response: Match magics in text.


Instruction:
Can you generate the documentation for the following Python 3 function
def alias_matches(self, text):
        #print 'Completer->alias_matches:',text,'lb',self.text_until_cursor # dbg

        # if we are not in the first 'item', alias matching
        # doesn't make sense - unless we are starting with 'sudo' command.
        main_text = self.text_until_cursor.lstrip()
        if ' ' in main_text and not main_text.startswith('sudo'):
            return []
        text = os.path.expanduser(text)
        aliases =  self.alias_table.keys()
        if text == '':
            return aliases
        else:
            return [a for a in aliases if a.startswith(text)]

Response: Match internal system aliases


Instruction:
Can you generate the documentation for the following Python 3 function
def python_matches(self,text):

        #io.rprint('Completer->python_matches, txt=%r' % text) # dbg
        if "." in text:
            try:
                matches = self.attr_matches(text)
                if text.endswith('.') and self.omit__names:
                    if self.omit__names == 1:
                        # true if txt is _not_ a __ name, false otherwise:
                        no__name = (lambda txt:
                                    re.match(r'.*\.__.*?__',txt) is None)
                    else:
                        # true if txt is _not_ a _ name, false otherwise:
                        no__name = (lambda txt:
                                    re.match(r'.*\._.*?',txt) is None)
                    matches = filter(no__name, matches)
            except NameError:
                # catches <undefined attributes>.<tab>
                matches = []
        else:
            matches = self.global_matches(text)

        return matches

Response: Match attributes or global python names.


Instruction:
Can you write a function in Python 3 where it
returns the list of default arguments of obj if it is callable or empty list otherwise.

Response:
def _default_arguments(self, obj):
        """Return the list of default arguments of obj if it is callable,
        or empty list otherwise."""

        if not (inspect.isfunction(obj) or inspect.ismethod(obj)):
            # for classes, check for __init__,__new__
            if inspect.isclass(obj):
                obj = (getattr(obj,'__init__',None) or
                       getattr(obj,'__new__',None))
            # for all others, check if they are __call__able
            elif hasattr(obj, '__call__'):
                obj = obj.__call__
            # XXX: is there a way to handle the builtins ?
        try:
            args,_,_1,defaults = inspect.getargspec(obj)
            if defaults:
                return args[-len(defaults):]
        except TypeError: pass
        return []


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

        if "." in text: # a parameter cannot be dotted
            return []
        try: regexp = self.__funcParamsRegex
        except AttributeError:
            regexp = self.__funcParamsRegex = re.compile(r'''
                '.*?(?<!\\)' |    # single quoted strings or
                ".*?(?<!\\)" |    # double quoted strings or
                \w+          |    # identifier
                \S                # other characters
                ''', re.VERBOSE | re.DOTALL)
        # 1. find the nearest identifier that comes before an unclosed
        # parenthesis before the cursor
        # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo"
        tokens = regexp.findall(self.text_until_cursor)
        tokens.reverse()
        iterTokens = iter(tokens); openPar = 0
        for token in iterTokens:
            if token == ')':
                openPar -= 1
            elif token == '(':
                openPar += 1
                if openPar > 0:
                    # found the last unclosed parenthesis
                    break
        else:
            return []
        # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
        ids = []
        isId = re.compile(r'\w+$').match
        while True:
            try:
                ids.append(iterTokens.next())
                if not isId(ids[-1]):
                    ids.pop(); break
                if not iterTokens.next() == '.':
                    break
            except StopIteration:
                break
        # lookup the candidate callable matches either using global_matches
        # or attr_matches for dotted names
        if len(ids) == 1:
            callableMatches = self.global_matches(ids[0])
        else:
            callableMatches = self.attr_matches('.'.join(ids[::-1]))
        argMatches = []
        for callableMatch in callableMatches:
            try:
                namedArgs = self._default_arguments(eval(callableMatch,
                                                         self.namespace))
            except:
                continue
            for namedArg in namedArgs:
                if namedArg.startswith(text):
                    argMatches.append("%s=" %namedArg)
        return argMatches

Response: Match named parameters of the last open function.


Instruction:
Create a Python 3 function to
find completions for the given text and line context.

Response:
def complete(self, text=None, line_buffer=None, cursor_pos=None):
        """Find completions for the given text and line context.

        This is called successively with state == 0, 1, 2, ... until it
        returns None.  The completion should begin with 'text'.

        Note that both the text and the line_buffer are optional, but at least
        one of them must be given.

        Parameters
        ----------
          text : string, optional
            Text to perform the completion on.  If not given, the line buffer
            is split using the instance's CompletionSplitter object.

          line_buffer : string, optional
            If not given, the completer attempts to obtain the current line
            buffer via readline.  This keyword allows clients which are
            requesting for text completions in non-readline contexts to inform
            the completer of the entire text.

          cursor_pos : int, optional
            Index of the cursor in the full line buffer.  Should be provided by
            remote frontends where kernel has no access to frontend state.

        Returns
        -------
        text : str
          Text that was actually used in the completion.

        matches : list
          A list of completion matches.
        """
        #io.rprint('\nCOMP1 %r %r %r' % (text, line_buffer, cursor_pos))  # dbg

        # if the cursor position isn't given, the only sane assumption we can
        # make is that it's at the end of the line (the common case)
        if cursor_pos is None:
            cursor_pos = len(line_buffer) if text is None else len(text)

        # if text is either None or an empty string, rely on the line buffer
        if not text:
            text = self.splitter.split_line(line_buffer, cursor_pos)

        # If no line buffer is given, assume the input text is all there was
        if line_buffer is None:
            line_buffer = text

        self.line_buffer = line_buffer
        self.text_until_cursor = self.line_buffer[:cursor_pos]
        #io.rprint('COMP2 %r %r %r' % (text, line_buffer, cursor_pos))  # dbg

        # Start with a clean slate of completions
        self.matches[:] = []
        custom_res = self.dispatch_custom_completer(text)
        if custom_res is not None:
            # did custom completers produce something?
            self.matches = custom_res
        else:
            # Extend the list of completions with the results of each
            # matcher, so we return results to the user from all
            # namespaces.
            if self.merge_completions:
                self.matches = []
                for matcher in self.matchers:
                    try:
                        self.matches.extend(matcher(text))
                    except:
                        # Show the ugly traceback if the matcher causes an
                        # exception, but do NOT crash the kernel!
                        sys.excepthook(*sys.exc_info())
            else:
                for matcher in self.matchers:
                    self.matches = matcher(text)
                    if self.matches:
                        break
        # FIXME: we should extend our api to return a dict with completions for
        # different types of objects.  The rlcomplete() method could then
        # simply collapse the dict into a list for readline, but we'd have
        # richer completion semantics in other evironments.
        self.matches = sorted(set(self.matches))
        #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg
        return text, self.matches


Instruction:
Implement a function in Python 3 to
return the state - th possible completion for text.

Response:
def rlcomplete(self, text, state):
        """Return the state-th possible completion for 'text'.

        This is called successively with state == 0, 1, 2, ... until it
        returns None.  The completion should begin with 'text'.

        Parameters
        ----------
          text : string
            Text to perform the completion on.

          state : int
            Counter used by readline.
        """
        if state==0:

            self.line_buffer = line_buffer = self.readline.get_line_buffer()
            cursor_pos = self.readline.get_endidx()

            #io.rprint("\nRLCOMPLETE: %r %r %r" %
            #          (text, line_buffer, cursor_pos) ) # dbg

            # if there is only a tab on a line with only whitespace, instead of
            # the mostly useless 'do you want to see all million completions'
            # message, just do the right thing and give the user his tab!
            # Incidentally, this enables pasting of tabbed text from an editor
            # (as long as autoindent is off).

            # It should be noted that at least pyreadline still shows file
            # completions - is there a way around it?

            # don't apply this on 'dumb' terminals, such as emacs buffers, so
            # we don't interfere with their own tab-completion mechanism.
            if not (self.dumb_terminal or line_buffer.strip()):
                self.readline.insert_text('\t')
                sys.stdout.flush()
                return None

            # Note: debugging exceptions that may occur in completion is very
            # tricky, because readline unconditionally silences them.  So if
            # during development you suspect a bug in the completion code, turn
            # this flag on temporarily by uncommenting the second form (don't
            # flip the value in the first line, as the '# dbg' marker can be
            # automatically detected and is used elsewhere).
            DEBUG = False
            #DEBUG = True # dbg
            if DEBUG:
                try:
                    self.complete(text, line_buffer, cursor_pos)
                except:
                    import traceback; traceback.print_exc()
            else:
                # The normal production version is here

                # This method computes the self.matches array
                self.complete(text, line_buffer, cursor_pos)

        try:
            return self.matches[state]
        except IndexError:
            return None


Instruction:
Write a Python 3 function for
checking if a specific record matches tests.

Response:
def _match_one(self, rec, tests):
        """Check if a specific record matches tests."""
        for key,test in tests.iteritems():
            if not test(rec.get(key, None)):
                return False
        return True


Instruction:
Make a summary of the following Python 3 code
def _match(self, check):
        matches = []
        tests = {}
        for k,v in check.iteritems():
            if isinstance(v, dict):
                tests[k] = CompositeFilter(v)
            else:
                tests[k] = lambda o: o==v

        for rec in self._records.itervalues():
            if self._match_one(rec, tests):
                matches.append(copy(rec))
        return matches

Response: Find all the matches for a check dict.


Instruction:
Write a Python 3 function that can
extract subdict of keys

Response:
def _extract_subdict(self, rec, keys):
        """extract subdict of keys"""
        d = {}
        d['msg_id'] = rec['msg_id']
        for key in keys:
            d[key] = rec[key]
        return copy(d)


Instruction:
Can you implement a function in Python 3 that
adds a new Task Record by msg_id.

Response:
def add_record(self, msg_id, rec):
        """Add a new Task Record, by msg_id."""
        if self._records.has_key(msg_id):
            raise KeyError("Already have msg_id %r"%(msg_id))
        self._records[msg_id] = rec


Instruction:
How would you implement a function in Python 3 that
gets a specific Task Record by msg_id.

Response:
def get_record(self, msg_id):
        """Get a specific Task Record, by msg_id."""
        if not msg_id in self._records:
            raise KeyError("No such msg_id %r"%(msg_id))
        return copy(self._records[msg_id])


Instruction:
Make a summary of the following Python 3 code
def drop_matching_records(self, check):
        matches = self._match(check)
        for m in matches:
            del self._records[m['msg_id']]

Response: Remove a record from the DB.


Instruction:
Explain what the following Python 3 code does
def find_records(self, check, keys=None):
        matches = self._match(check)
        if keys:
            return [ self._extract_subdict(rec, keys) for rec in matches ]
        else:
            return matches

Response: Find records matching a query dict optionally extracting subset of keys.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_history(self):
        msg_ids = self._records.keys()
        return sorted(msg_ids, key=lambda m: self._records[m]['submitted'])

Response: get all msg_ids ordered by time submitted.


Instruction:
Make a summary of the following Python 3 code
def quiet(self):
        # do not print output if input ends in ';'
        try:
            cell = self.shell.history_manager.input_hist_parsed[self.prompt_count]
            if cell.rstrip().endswith(';'):
                return True
        except IndexError:
            # some uses of ipshellembed may fail here
            pass
        return False

Response: Should we silence the display hook because of ';'?


Instruction:
Can you generate a brief explanation for the following Python 3 code
def write_output_prompt(self):
        # Use write, not print which adds an extra space.
        io.stdout.write(self.shell.separate_out)
        outprompt = self.shell.prompt_manager.render('out')
        if self.do_full_cache:
            io.stdout.write(outprompt)

Response: Write the output prompt.


Instruction:
Write a Python 3 script for
writing the format data dict to the frontend.

Response:
def write_format_data(self, format_dict):
        """Write the format data dict to the frontend.

        This default version of this method simply writes the plain text
        representation of the object to ``io.stdout``. Subclasses should
        override this method to send the entire `format_dict` to the
        frontends.

        Parameters
        ----------
        format_dict : dict
            The format dict for the object passed to `sys.displayhook`.
        """
        # We want to print because we want to always make sure we have a
        # newline, even if all the prompt separators are ''. This is the
        # standard IPython behavior.
        result_repr = format_dict['text/plain']
        if '\n' in result_repr:
            # So that multi-line strings line up with the left column of
            # the screen, instead of having the output prompt mess up
            # their first line.
            # We use the prompt template instead of the expanded prompt
            # because the expansion may add ANSI escapes that will interfere
            # with our ability to determine whether or not we should add
            # a newline.
            prompt_template = self.shell.prompt_manager.out_template
            if prompt_template and not prompt_template.endswith('\n'):
                # But avoid extraneous empty lines.
                result_repr = '\n' + result_repr

        print >>io.stdout, result_repr


Instruction:
Can you create a Python 3 function that
updates user_ns with the current result.

Response:
def update_user_ns(self, result):
        """Update user_ns with various things like _, __, _1, etc."""

        # Avoid recursive reference when displaying _oh/Out
        if result is not self.shell.user_ns['_oh']:
            if len(self.shell.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
                warn('Output cache limit (currently '+
                      `self.cache_size`+' entries) hit.\n'
                     'Flushing cache and resetting history counter...\n'
                     'The only history variables available will be _,__,___ and _1\n'
                     'with the current result.')

                self.flush()
            # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
            # we cause buggy behavior for things like gettext).

            if '_' not in __builtin__.__dict__:
                self.___ = self.__
                self.__ = self._
                self._ = result
                self.shell.push({'_':self._,
                                 '__':self.__,
                                '___':self.___}, interactive=False)

            # hackish access to top-level  namespace to create _1,_2... dynamically
            to_main = {}
            if self.do_full_cache:
                new_result = '_'+`self.prompt_count`
                to_main[new_result] = result
                self.shell.push(to_main, interactive=False)
                self.shell.user_ns['_oh'][self.prompt_count] = result


Instruction:
How would you implement a function in Python 3 that
logs the output of the current process.

Response:
def log_output(self, format_dict):
        """Log the output."""
        if self.shell.logger.log_output:
            self.shell.logger.log_write(format_dict['text/plain'], 'output')
        self.shell.history_manager.output_hist_reprs[self.prompt_count] = \
                                                    format_dict['text/plain']


Instruction:
Can you tell what is the following Python 3 function doing
def finish_displayhook(self):
        io.stdout.write(self.shell.separate_out2)
        io.stdout.flush()

Response: Finish up all displayhook activities.


Instruction:
Can you create a Python 3 function that
loads the extension in IPython.

Response:
def load_ipython_extension(ip):
    """Load the extension in IPython."""
    global _loaded
    if not _loaded:
        plugin = StoreMagic(shell=ip, config=ip.config)
        ip.plugin_manager.register_plugin('storemagic', plugin)
        _loaded = True


Instruction:
Explain what the following Python 3 code does
def raise_if_freezed(self):
        '''raise `InvalidOperationException` if is freezed.'''
        if self.is_freezed:
            name = type(self).__name__
            raise InvalidOperationException('obj {name} is freezed.'.format(name=name))

Response: raise InvalidOperationException if is freezed.


Instruction:
Implement a function in Python 3 to
convert a MySQL TIMESTAMP to a Timestamp object.

Response:
def mysql_timestamp_converter(s):
    """Convert a MySQL TIMESTAMP to a Timestamp object."""
    # MySQL>4.1 returns TIMESTAMP in the same format as DATETIME
    if s[4] == '-': return DateTime_or_None(s)
    s = s + "0"*(14-len(s)) # padding
    parts = map(int, filter(None, (s[:4],s[4:6],s[6:8],
                                   s[8:10],s[10:12],s[12:14])))
    try:
        return Timestamp(*parts)
    except (SystemExit, KeyboardInterrupt):
        raise
    except:
        return None


Instruction:
Can you create a Python 3 function that
creates a SOAP envelope for the current object.

Response:
def make_envelope(self, body_elements=None):
        """
        body_elements: <list> of etree.Elements or <None>
        """
        soap = self.get_soap_el_factory()
        body_elements = body_elements or []
        body = soap.Body(*body_elements)
        header = self.get_soap_header()
        if header is not None:
            elements = [header, body]
        else:
            elements = [body]
        return soap.Envelope(
            #{
            #    '{%s}encodingStyle' % namespaces.SOAP_1_2: (
            #        'http://www.w3.org/2001/12/soap-encoding')
            #},
            *elements
        )


Instruction:
Given the following Python 3 function, write the documentation
def embed_kernel(module=None, local_ns=None, **kwargs):
    # get the app if it exists, or set it up if it doesn't
    if IPKernelApp.initialized():
        app = IPKernelApp.instance()
    else:
        app = IPKernelApp.instance(**kwargs)
        app.initialize([])
        # Undo unnecessary sys module mangling from init_sys_modules.
        # This would not be necessary if we could prevent it
        # in the first place by using a different InteractiveShell
        # subclass, as in the regular embed case.
        main = app.kernel.shell._orig_sys_modules_main_mod
        if main is not None:
            sys.modules[app.kernel.shell._orig_sys_modules_main_name] = main

    # load the calling scope if not given
    (caller_module, caller_locals) = extract_module_locals(1)
    if module is None:
        module = caller_module
    if local_ns is None:
        local_ns = caller_locals

    app.kernel.user_module = module
    app.kernel.user_ns = local_ns
    app.shell.set_completer_frame()
    app.start()

Response: Embed and start an IPython kernel in a given scope.


Instruction:
How would you explain what the following Python 3 function does
def _eventloop_changed(self, name, old, new):
        loop = ioloop.IOLoop.instance()
        loop.add_timeout(time.time()+0.1, self.enter_eventloop)

Response: schedule call to eventloop from IOLoop


Instruction:
Create a Python 3 function for
registering dispatchers for streams

Response:
def start(self):
        """register dispatchers for streams"""
        self.shell.exit_now = False
        if self.control_stream:
            self.control_stream.on_recv(self.dispatch_control, copy=False)

        def make_dispatcher(stream):
            def dispatcher(msg):
                return self.dispatch_shell(stream, msg)
            return dispatcher

        for s in self.shell_streams:
            s.on_recv(make_dispatcher(s), copy=False)


Instruction:
How would you code a function in Python 3 to
step eventloop just once

Response:
def do_one_iteration(self):
        """step eventloop just once"""
        if self.control_stream:
            self.control_stream.flush()
        for stream in self.shell_streams:
            # handle at most one request per iteration
            stream.flush(zmq.POLLIN, 1)
            stream.flush(zmq.POLLOUT)


Instruction:
Can you tell what is the following Python 3 function doing
def _publish_pyin(self, code, parent, execution_count):

        self.session.send(self.iopub_socket, u'pyin',
                            {u'code':code, u'execution_count': execution_count},
                            parent=parent, ident=self._topic('pyin')
        )

Response: Publish the code request on the pyin stream.


Instruction:
Implement a function in Python 3 to
send status on IOPub

Response:
def _publish_status(self, status, parent=None):
        """send status (busy/idle) on IOPub"""
        self.session.send(self.iopub_socket,
                          u'status',
                          {u'execution_state': status},
                          parent=parent,
                          ident=self._topic('status'),
                          )


Instruction:
Given the following Python 3 function, write the documentation
def abort_request(self, stream, ident, parent):
        msg_ids = parent['content'].get('msg_ids', None)
        if isinstance(msg_ids, basestring):
            msg_ids = [msg_ids]
        if not msg_ids:
            self.abort_queues()
        for mid in msg_ids:
            self.aborted.add(str(mid))

        content = dict(status='ok')
        reply_msg = self.session.send(stream, 'abort_reply', content=content,
                parent=parent, ident=ident)
        self.log.debug("%s", reply_msg)

Response: abort a specifig msg by id


Instruction:
Write a Python 3 script for
prefixing topic for IOPub messages

Response:
def _topic(self, topic):
        """prefixed topic for IOPub messages"""
        if self.int_id >= 0:
            base = "engine.%i" % self.int_id
        else:
            base = "kernel.%s" % self.ident

        return py3compat.cast_bytes("%s.%s" % (base, topic))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _at_shutdown(self):
        # io.rprint("Kernel at_shutdown") # dbg
        if self._shutdown_message is not None:
            self.session.send(self.iopub_socket, self._shutdown_message, ident=self._topic('shutdown'))
            self.log.debug("%s", self._shutdown_message)
        [ s.flush(zmq.POLLOUT) for s in self.shell_streams ]

Response: Called by the kernel when the kernel is at shutdown.


Instruction:
Write a Python 3 script to
enable GUI event loop integration taking pylab into account.

Response:
def init_gui_pylab(self):
        """Enable GUI event loop integration, taking pylab into account."""

        # Provide a wrapper for :meth:`InteractiveShellApp.init_gui_pylab`
        # to ensure that any exception is printed straight to stderr.
        # Normally _showtraceback associates the reply with an execution,
        # which means frontends will never draw it, as this exception
        # is not associated with any execute request.

        shell = self.shell
        _showtraceback = shell._showtraceback
        try:
            # replace pyerr-sending traceback with stderr
            def print_tb(etype, evalue, stb):
                print ("GUI event loop or pylab initialization failed",
                       file=io.stderr)
                print (shell.InteractiveTB.stb2text(stb), file=io.stderr)
            shell._showtraceback = print_tb
            InteractiveShellApp.init_gui_pylab(self)
        finally:
            shell._showtraceback = _showtraceback


Instruction:
Here you have a function in Python 3, explain what it does
def slices(self, size, n=3):
        '''
        Create equal sized slices of the file. The last slice may be larger than
        the others.

        args:
            * size (int): The full size to be sliced.
            * n (int): The number of slices to return.
        returns:
            A list of `FileSlice` objects of length (n).
        '''
        if n <= 0:
            raise ValueError('n must be greater than 0')
        if size < n:
            raise ValueError('size argument cannot be less than n argument')
        slice_size = size // n
        last_slice_size = size - (n-1) * slice_size
        t = [self(c, slice_size) for c in range(0, (n-1)*slice_size, slice_size)]
        t.append(self((n-1)*slice_size, last_slice_size))
        return t

Response: Create equal sized slices of the file.


Instruction:
Implement a function in Python 3 to
return a new array of size from the current position.

Response:
def read(self, size=-1):
        '''
        Same as `file.read()` but for the slice. Does not read beyond
        `self.end`.
        '''
        if self.closed:
            raise ValueError('I/O operation on closed file.')

        if size == -1 or size > self.size - self.pos:
            size = self.size - self.pos

        with self.lock:
            self.f.seek(self.start + self.pos)
            result = self.f.read(size)
            self.seek(self.pos + size)

        return result


Instruction:
Can you write a function in Python 3 where it
writes a list of lines to the file.

Response:
def writelines(self, lines):
        '''
        Same as `file.writelines()` but for the slice.

        raises:
            EOFError if the new seek position is > `self.size`.
        '''
        lines = b''.join(lines)
        self.write(lines)


Instruction:
Explain what the following Python 3 code does
def configure(self, options, conf):
        Plugin.configure(self, options, conf)
        self._mod_stack = []

Response: Configure the object with the given options and conf.


Instruction:
How would you explain what the following Python 3 function does
def beforeContext(self):
        mods = sys.modules.copy()
        self._mod_stack.append(mods)

Response: Copy sys. modules onto my mod stack and add them to the stack


Instruction:
How would you explain what the following Python 3 function does
def afterContext(self):
        mods = self._mod_stack.pop()
        to_del = [ m for m in sys.modules.keys() if m not in mods ]
        if to_del:
            log.debug('removing sys modules entries: %s', to_del)
            for mod in to_del:
                del sys.modules[mod]
        sys.modules.update(mods)

Response:
Called after the context has been pushed and restore sys. modules to the state
        it was in.


Instruction:
Make a summary of the following Python 3 code
def absdir(path):
    if not os.path.isabs(path):
        path = os.path.normpath(os.path.abspath(os.path.join(os.getcwd(),
                                                             path)))
    if path is None or not os.path.isdir(path):
        return None
    return path

Response: Return absolute normalized path to directory if it exists ; None otherwise.


Instruction:
Can you tell what is the following Python 3 function doing
def absfile(path, where=None):
    orig = path
    if where is None:
        where = os.getcwd()
    if isinstance(where, list) or isinstance(where, tuple):
        for maybe_path in where:
            maybe_abs = absfile(path, maybe_path)
            if maybe_abs is not None:
                return maybe_abs
        return None
    if not os.path.isabs(path):
        path = os.path.normpath(os.path.abspath(os.path.join(where, path)))
    if path is None or not os.path.exists(path):
        if where != os.getcwd():
            # try the cwd instead
            path = os.path.normpath(os.path.abspath(os.path.join(os.getcwd(),
                                                                 orig)))
    if path is None or not os.path.exists(path):
        return None
    if os.path.isdir(path):
        # might want an __init__.py from pacakge
        init = os.path.join(path,'__init__.py')
        if os.path.isfile(init):
            return init
    elif os.path.isfile(path):
        return path
    return None

Response:
Return absolute path to file optionally in directory
    where or None if the file can t be found either in directory
    or the current directory
    where.


Instruction:
Make a summary of the following Python 3 code
def file_like(name):
    return (os.path.exists(name)
            or os.path.dirname(name)
            or name.endswith('.py')
            or not ident_re.match(os.path.splitext(name)[0]))

Response:
A name is file - like if it exists a directory part of a
    or a file - like identifier.


Instruction:
Create a Python 3 function for
being obj a class? Inspect s isclass is too liberal and returns True for objects that can t be subclasses of anything.

Response:
def isclass(obj):
    """Is obj a class? Inspect's isclass is too liberal and returns True
    for objects that can't be subclasses of anything.
    """
    obj_type = type(obj)
    return obj_type in class_types or issubclass(obj_type, type)


Instruction:
Make a summary of the following Python 3 code
def ispackage(path):
    if os.path.isdir(path):
        # at least the end of the path must be a legal python identifier
        # and __init__.py[co] must exist
        end = os.path.basename(path)
        if ident_re.match(end):
            for init in ('__init__.py', '__init__.pyc', '__init__.pyo'):
                if os.path.isfile(os.path.join(path, init)):
                    return True
            if sys.platform.startswith('java') and \
                    os.path.isfile(os.path.join(path, '__init__$py.class')):
                return True
    return False

Response: Check if path is a package directory.


Instruction:
Make a summary of the following Python 3 code
def getfilename(package, relativeTo=None):
    if relativeTo is None:
        relativeTo = os.getcwd()
    path = os.path.join(relativeTo, os.sep.join(package.split('.')))
    suffixes = ('/__init__.py', '.py')
    for suffix in suffixes:
        filename = path + suffix
        if os.path.exists(filename):
            return filename
    return None

Response: Find the python source file for a package relative to the current working directory.


Instruction:
Given the following Python 3 function, write the documentation
def getpackage(filename):
    src_file = src(filename)
    if not src_file.endswith('.py') and not ispackage(src_file):
        return None
    base, ext = os.path.splitext(os.path.basename(src_file))
    if base == '__init__':
        mod_parts = []
    else:
        mod_parts = [base]
    path, part = os.path.split(os.path.split(src_file)[0])
    while part:
        if ispackage(os.path.join(path, part)):
            mod_parts.append(part)
        else:
            break
        path, part = os.path.split(path)
    mod_parts.reverse()
    return '.'.join(mod_parts)

Response: Find the full dotted package name for a given python source file. Returns None if the file is not a python source file.


Instruction:
Given the following Python 3 function, write the documentation
def ln(label):
    label_len = len(label) + 2
    chunk = (70 - label_len) // 2
    out = '%s %s %s' % ('-' * chunk, label, '-' * chunk)
    pad = 70 - len(out)
    if pad > 0:
        out = out + ('-' * pad)
    return out

Response: Draw a 70 - char - wide divider with label in the middle.


Instruction:
Create a Python 3 function for
trying to run a list of possible method names with the object obj.

Response:
def try_run(obj, names):
    """Given a list of possible method names, try to run them with the
    provided object. Keep going until something works. Used to run
    setup/teardown methods for module, package, and function tests.
    """
    for name in names:
        func = getattr(obj, name, None)
        if func is not None:
            if type(obj) == types.ModuleType:
                # py.test compatibility
                try:
                    args, varargs, varkw, defaults = inspect.getargspec(func)
                except TypeError:
                    # Not a function. If it's callable, call it anyway
                    if hasattr(func, '__call__'):
                        func = func.__call__
                    try:
                        args, varargs, varkw, defaults = \
                            inspect.getargspec(func)
                        args.pop(0) # pop the self off
                    except TypeError:
                        raise TypeError("Attribute %s of %r is not a python "
                                        "function. Only functions or callables"
                                        " may be used as fixtures." %
                                        (name, obj))
                if len(args):
                    log.debug("call fixture %s.%s(%s)", obj, name, obj)
                    return func(obj)
            log.debug("call fixture %s.%s", obj, name)
            return func()


Instruction:
Can you generate the documentation for the following Python 3 function
def regex_last_key(regex):
    def k(obj):
        if regex.search(obj):
            return (1, obj)
        return (0, obj)
    return k

Response:
Sort list of items that match a
    regular expression last.


Instruction:
Can you write a function in Python 3 where it
converts a value that may be a list or a ( possibly comma - separated ) string into a list. The exception is returned as None not [ None ] not [ None ]

Response:
def tolist(val):
    """Convert a value that may be a list or a (possibly comma-separated)
    string into a list. The exception: None is returned as None, not [None].

    >>> tolist(["one", "two"])
    ['one', 'two']
    >>> tolist("hello")
    ['hello']
    >>> tolist("separate,values, with, commas,  spaces , are    ,ok")
    ['separate', 'values', 'with', 'commas', 'spaces', 'are', 'ok']
    """
    if val is None:
        return None
    try:
        # might already be a list
        val.extend([])
        return val
    except AttributeError:
        pass
    # might be a string
    try:
        return re.split(r'\s*,\s*', val)
    except TypeError:
        # who knows...
        return list(val)


Instruction:
How would you code a function in Python 3 to
transplant a function in module A to module B.

Response:
def transplant_func(func, module):
    """
    Make a function imported from module A appear as if it is located
    in module B.

    >>> from pprint import pprint
    >>> pprint.__module__
    'pprint'
    >>> pp = transplant_func(pprint, __name__)
    >>> pp.__module__
    'nose.util'

    The original function is not modified.

    >>> pprint.__module__
    'pprint'

    Calling the transplanted function calls the original.

    >>> pp([1, 2])
    [1, 2]
    >>> pprint([1,2])
    [1, 2]

    """
    from nose.tools import make_decorator
    def newfunc(*arg, **kw):
        return func(*arg, **kw)

    newfunc = make_decorator(func)(newfunc)
    newfunc.__module__ = module
    return newfunc


Instruction:
Explain what the following Python 3 code does
def transplant_class(cls, module):
    class C(cls):
        pass
    C.__module__ = module
    C.__name__ = cls.__name__
    return C

Response:
Create a class that resides in module rather than the module in which
    it is actually defined.


Instruction:
Can you create a Python 3 function that
returns the current system memory usage as a tuple.

Response:
def swap_memory():
    """Swap system memory as a (total, used, free, sin, sout) tuple."""
    total, used, free, sin, sout = _psutil_osx.get_swap_mem()
    percent = usage_percent(used, total, _round=1)
    return nt_swapmeminfo(total, used, free, percent, sin, sout)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_system_cpu_times():
    user, nice, system, idle = _psutil_osx.get_system_cpu_times()
    return _cputimes_ntuple(user, nice, system, idle)

Response: Return system CPU times as a namedtuple.


Instruction:
Implement a function in Python 3 to
return system CPU times as a named tuple

Response:
def get_system_per_cpu_times():
    """Return system CPU times as a named tuple"""
    ret = []
    for cpu_t in _psutil_osx.get_system_per_cpu_times():
        user, nice, system, idle = cpu_t
        item = _cputimes_ntuple(user, nice, system, idle)
        ret.append(item)
    return ret


Instruction:
Write a Python 3 script to
return process cmdline as a list of arguments.

Response:
def get_process_cmdline(self):
        """Return process cmdline as a list of arguments."""
        if not pid_exists(self.pid):
            raise NoSuchProcess(self.pid, self._process_name)
        return _psutil_osx.get_process_cmdline(self.pid)


Instruction:
Write a Python 3 function for
returning a tuple with the process RSS and VMS size.

Response:
def get_memory_info(self):
        """Return a tuple with the process' RSS and VMS size."""
        rss, vms = _psutil_osx.get_process_memory_info(self.pid)[:2]
        return nt_meminfo(rss, vms)


Instruction:
Create a Python 3 function to
return a tuple with the process RSS VMS size and the process s memory size.

Response:
def get_ext_memory_info(self):
        """Return a tuple with the process' RSS and VMS size."""
        rss, vms, pfaults, pageins = _psutil_osx.get_process_memory_info(self.pid)
        return self._nt_ext_mem(rss, vms,
                                pfaults * _PAGESIZE,
                                pageins * _PAGESIZE)


Instruction:
Write a Python 3 script for
returning list of open files.

Response:
def get_open_files(self):
        """Return files opened by process."""
        if self.pid == 0:
            return []
        files = []
        rawlist = _psutil_osx.get_process_open_files(self.pid)
        for path, fd in rawlist:
            if isfile_strict(path):
                ntuple = nt_openfile(path, fd)
                files.append(ntuple)
        return files


Instruction:
Implement a Python 3 function for
returning a list of etwork connections opened by a process as a list of namedtuples.

Response:
def get_connections(self, kind='inet'):
        """Return etwork connections opened by a process as a list of
        namedtuples.
        """
        if kind not in conn_tmap:
            raise ValueError("invalid %r kind argument; choose between %s"
                             % (kind, ', '.join([repr(x) for x in conn_tmap])))
        families, types = conn_tmap[kind]
        ret = _psutil_osx.get_process_connections(self.pid, families, types)
        return [nt_connection(*conn) for conn in ret]


Instruction:
Make a summary of the following Python 3 code
def user_has_group(user, group, superuser_skip=True):

    if user.is_superuser and superuser_skip:
        return True

    return user.groups.filter(name=group).exists()

Response: Check if a user is in a certaing group.


Instruction:
Can you generate the documentation for the following Python 3 function
def resolve_class(class_path):

    modulepath, classname = class_path.rsplit('.', 1)
    module = __import__(modulepath, fromlist=[classname])
    return getattr(module, classname)

Response: Load a class by a fully qualified class_path eg. myapp. models. ModelName


Instruction:
Write a Python 3 script to
calculate percentage usage of used against total.

Response:
def usage_percent(used, total, _round=None):
    """Calculate percentage usage of 'used' against 'total'."""
    try:
        ret = (used / total) * 100
    except ZeroDivisionError:
        ret = 0
    if _round is not None:
        return round(ret, _round)
    else:
        return ret


Instruction:
Can you tell what is the following Python 3 function doing
def deprecated(replacement=None):
    def outer(fun):
        msg = "psutil.%s is deprecated" % fun.__name__
        if replacement is not None:
            msg += "; use %s instead" % replacement
        if fun.__doc__ is None:
            fun.__doc__ = msg

        @wraps(fun)
        def inner(*args, **kwargs):
            warnings.warn(msg, category=DeprecationWarning, stacklevel=2)
            return fun(*args, **kwargs)

        return inner
    return outer

Response: A decorator which can be used to mark functions as deprecated.


Instruction:
Create a Python 3 function to
push specified directory to git remote

Response:
def git_push(git_message=None, git_repository=None, git_branch=None,
             locale_root=None):
    """
    Pushes specified directory to git remote
    :param git_message: commit message
    :param git_repository: repository address
    :param git_branch: git branch
    :param locale_root: path to locale root folder containing directories
                        with languages
    :return: tuple stdout, stderr of completed command
    """
    if git_message is None:
        git_message = settings.GIT_MESSAGE
    if git_repository is None:
        git_repository = settings.GIT_REPOSITORY
    if git_branch is None:
        git_branch = settings.GIT_BRANCH
    if locale_root is None:
        locale_root = settings.LOCALE_ROOT

    try:
        subprocess.check_call(['git', 'checkout', git_branch])
    except subprocess.CalledProcessError:
        try:
            subprocess.check_call(['git', 'checkout', '-b', git_branch])
        except subprocess.CalledProcessError as e:
            raise PODocsError(e)

    try:
        subprocess.check_call(['git', 'ls-remote', git_repository])
    except subprocess.CalledProcessError:
        try:
            subprocess.check_call(['git', 'remote', 'add', 'po_translator',
                                   git_repository])
        except subprocess.CalledProcessError as e:
            raise PODocsError(e)

    commands = 'git add ' + locale_root + \
               ' && git commit -m "' + git_message + '"' + \
               ' && git push po_translator ' + git_branch + ':' + git_branch
    proc = Popen(commands, shell=True, stdout=PIPE, stderr=PIPE)
    stdout, stderr = proc.communicate()

    return stdout, stderr


Instruction:
Make a summary of the following Python 3 code
def git_checkout(git_branch=None, locale_root=None):
    if git_branch is None:
        git_branch = settings.GIT_BRANCH
    if locale_root is None:
        locale_root = settings.LOCALE_ROOT

    proc = Popen('git checkout ' + git_branch + ' -- ' + locale_root,
                 shell=True, stdout=PIPE, stderr=PIPE)
    stdout, stderr = proc.communicate()

    return stdout, stderr

Response: Checkout the last commit of a node in a git repository


Instruction:
Given the following Python 3 function, write the documentation
def _login(self):
        try:
            self.gd_client = gdata.docs.client.DocsClient()
            self.gd_client.ClientLogin(self.email, self.password, self.source)
        except RequestError as e:
            raise PODocsError(e)

Response: Login into Google Docs with user authentication info.


Instruction:
Implement a function in Python 3 to
parse GDocs key from Spreadsheet url.

Response:
def _get_gdocs_key(self):
        """
        Parse GDocs key from Spreadsheet url.
        """
        try:
            args = urlparse.parse_qs(urlparse.urlparse(self.url).query)
            self.key = args['key'][0]
        except KeyError as e:
            raise PODocsError(e)


Instruction:
Can you write a function in Python 3 where it
ensures that temp directory exists and create one if it does not.

Response:
def _ensure_temp_path_exists(self):
        """
        Make sure temp directory exists and create one if it does not.
        """
        try:
            if not os.path.exists(self.temp_path):
                os.mkdir(self.temp_path)
        except OSError as e:
            raise PODocsError(e)


Instruction:
Given the following Python 3 function, write the documentation
def _clear_temp(self):
        temp_files = [LOCAL_ODS, GDOCS_TRANS_CSV, GDOCS_META_CSV,
                      LOCAL_TRANS_CSV, LOCAL_META_CSV]
        for temp_file in temp_files:
            file_path = os.path.join(self.temp_path, temp_file)
            if os.path.exists(file_path):
                os.remove(file_path)

Response: Clear temp directory from created csv and ods files during communicator operations.


Instruction:
How would you code a function in Python 3 to
download csv from GDoc.

Response:
def _download_csv_from_gdocs(self, trans_csv_path, meta_csv_path):
        """
        Download csv from GDoc.
        :return: returns resource if worksheets are present
        :except: raises PODocsError with info if communication
                 with GDocs lead to any errors
        """
        try:
            entry = self.gd_client.GetResourceById(self.key)
            self.gd_client.DownloadResource(
                entry, trans_csv_path,
                extra_params={'gid': 0, 'exportFormat': 'csv'}
            )
            self.gd_client.DownloadResource(
                entry, meta_csv_path,
                extra_params={'gid': 1, 'exportFormat': 'csv'}
            )
        except (RequestError, IOError) as e:
            raise PODocsError(e)
        return entry