Instruction:
Explain what the following Python 3 code does
def get_tokens_unprocessed(self, text, stack=('root',)):
    pos = 0
    tokendefs = self._tokens
    if hasattr(self, '_saved_state_stack'):
        statestack = list(self._saved_state_stack)
    else:
        statestack = list(stack)
    statetokens = tokendefs[statestack[-1]]
    while 1:
        for rexmatch, action, new_state in statetokens:
            m = rexmatch(text, pos)
            if m:
                if type(action) is _TokenType:
                    yield pos, action, m.group()
                else:
                    for item in action(self, m):
                        yield item
                pos = m.end()
                if new_state is not None:
                    # state transition
                    if isinstance(new_state, tuple):
                        for state in new_state:
                            if state == '#pop':
                                statestack.pop()
                            elif state == '#push':
                                statestack.append(statestack[-1])
                            else:
                                statestack.append(state)
                    elif isinstance(new_state, int):
                        # pop
                        del statestack[new_state:]
                    elif new_state == '#push':
                        statestack.append(statestack[-1])
                    else:
                        assert False, "wrong state def: %r" % new_state
                    statetokens = tokendefs[statestack[-1]]
                break
        else:
            try:
                if text[pos] == '\n':
                    # at EOL, reset state to "root"
                    pos += 1
                    statestack = ['root']
                    statetokens = tokendefs['root']
                    yield pos, Text, u'\n'
                    continue
                yield pos, Error, text[pos]
                pos += 1
            except IndexError:
                break
    self._saved_state_stack = list(statestack)

Response: Yields a list of tokens that are not in the stack.


Instruction:
Can you create a Python 3 function that
sets the style of the log entry.

Response:
def set_style(self, style):
        """ Sets the style to the specified Pygments style.
        """
        if isinstance(style, basestring):
            style = get_style_by_name(style)
        self._style = style
        self._clear_caches()


Instruction:
Can you create a Python 3 function that
returns a QTextCharFormat for token or None.

Response:
def _get_format(self, token):
        """ Returns a QTextCharFormat for token or None.
        """
        if token in self._formats:
            return self._formats[token]

        if self._style is None:
            result = self._get_format_from_document(token, self._document)
        else:
            result = self._get_format_from_style(token, self._style)

        self._formats[token] = result
        return result


Instruction:
Implement a function in Python 3 to
return a QTextCharFormat for the given token by the given document.

Response:
def _get_format_from_document(self, token, document):
        """ Returns a QTextCharFormat for token by
        """
        code, html = self._formatter._format_lines([(token, u'dummy')]).next()
        self._document.setHtml(html)
        return QtGui.QTextCursor(self._document).charFormat()


Instruction:
Here you have a function in Python 3, explain what it does
def _get_format_from_style(self, token, style):
        result = QtGui.QTextCharFormat()
        for key, value in style.style_for_token(token).items():
            if value:
                if key == 'color':
                    result.setForeground(self._get_brush(value))
                elif key == 'bgcolor':
                    result.setBackground(self._get_brush(value))
                elif key == 'bold':
                    result.setFontWeight(QtGui.QFont.Bold)
                elif key == 'italic':
                    result.setFontItalic(True)
                elif key == 'underline':
                    result.setUnderlineStyle(
                        QtGui.QTextCharFormat.SingleUnderline)
                elif key == 'sans':
                    result.setFontStyleHint(QtGui.QFont.SansSerif)
                elif key == 'roman':
                    result.setFontStyleHint(QtGui.QFont.Times)
                elif key == 'mono':
                    result.setFontStyleHint(QtGui.QFont.TypeWriter)
        return result

Response: Returns a QTextCharFormat for the given token by reading a Pygments style.


Instruction:
Write a Python 3 function that can
search the PATH for the given command and returns its path

Response:
def find_command(cmd, paths=None, pathext=None):
    """Searches the PATH for the given command and returns its path"""
    if paths is None:
        paths = os.environ.get('PATH', '').split(os.pathsep)
    if isinstance(paths, six.string_types):
        paths = [paths]
    # check if there are funny path extensions for executables, e.g. Windows
    if pathext is None:
        pathext = get_pathext()
    pathext = [ext for ext in pathext.lower().split(os.pathsep) if len(ext)]
    # don't use extensions if the command ends with one of them
    if os.path.splitext(cmd)[1].lower() in pathext:
        pathext = ['']
    # check if we find the command on PATH
    for path in paths:
        # try without extension first
        cmd_path = os.path.join(path, cmd)
        for ext in pathext:
            # then including the extension
            cmd_path_ext = cmd_path + ext
            if os.path.isfile(cmd_path_ext):
                return cmd_path_ext
        if os.path.isfile(cmd_path):
            return cmd_path
    raise BadCommand('Cannot find command %r' % cmd)


Instruction:
How would you explain what the following Python 3 function does
def normalize_path(path):
    return os.path.normcase(os.path.realpath(os.path.expanduser(path)))

Response: Convert a path to its canonical case - normalized absolute version.


Instruction:
How would you code a function in Python 3 to
verify that namespace packages are valid.

Response:
def check_nsp(dist, attr, value):
    """Verify that namespace packages are valid"""
    assert_string_list(dist,attr,value)
    for nsp in value:
        if not dist.has_contents_for(nsp):
            raise DistutilsSetupError(
                "Distribution contains no modules or packages for " +
                "namespace package %r" % nsp
            )
        if '.' in nsp:
            parent = '.'.join(nsp.split('.')[:-1])
            if parent not in value:
                distutils.log.warn(
                    "%r is declared as a package namespace, but %r is not:"
                    " please correct this in setup.py", nsp, parent
                )


Instruction:
How would you implement a function in Python 3 that
verifies that extras_require mapping is valid.

Response:
def check_extras(dist, attr, value):
    """Verify that extras_require mapping is valid"""
    try:
        for k,v in value.items():
            list(pkg_resources.parse_requirements(v))
    except (TypeError,ValueError,AttributeError):
        raise DistutilsSetupError(
            "'extras_require' must be a dictionary whose values are "
            "strings or lists of strings containing valid project/version "
            "requirement specifiers."
        )


Instruction:
Can you generate the documentation for the following Python 3 function
def check_entry_points(dist, attr, value):
    try:
        pkg_resources.EntryPoint.parse_map(value)
    except ValueError, e:
        raise DistutilsSetupError(e)

Response: Verify that entry_points map is parseable


Instruction:
Can you generate the documentation for the following Python 3 function
def handle_display_options(self, option_order):
        import sys

        if sys.version_info < (3,) or self.help_commands:
            return _Distribution.handle_display_options(self, option_order)

        # Stdout may be StringIO (e.g. in tests)
        import io
        if not isinstance(sys.stdout, io.TextIOWrapper):
            return _Distribution.handle_display_options(self, option_order)

        # Don't wrap stdout if utf-8 is already the encoding. Provides
        #  workaround for #334.
        if sys.stdout.encoding.lower() in ('utf-8', 'utf8'):
            return _Distribution.handle_display_options(self, option_order)

        # Print metadata in UTF-8 no matter the platform
        encoding = sys.stdout.encoding
        errors = sys.stdout.errors
        newline = sys.platform != 'win32' and '\n' or None
        line_buffering = sys.stdout.line_buffering

        sys.stdout = io.TextIOWrapper(
            sys.stdout.detach(), 'utf-8', errors, newline, line_buffering)
        try:
            return _Distribution.handle_display_options(self, option_order)
        finally:
            sys.stdout = io.TextIOWrapper(
                sys.stdout.detach(), encoding, errors, newline, line_buffering)

Response: Handles display - only options on the command - line and returns true if any display - only options are present on the command - line and return false otherwise.


Instruction:
Can you write a function in Python 3 where it
determines if the input source ends in a blank.

Response:
def last_blank(src):
    """Determine if the input source ends in a blank.

    A blank is either a newline or a line consisting of whitespace.

    Parameters
    ----------
    src : string
      A single or multiline string.
    """
    if not src: return False
    ll  = src.splitlines()[-1]
    return (ll == '') or ll.isspace()


Instruction:
Make a summary of the following Python 3 code
def last_two_blanks(src):
    if not src: return False
    # The logic here is tricky: I couldn't get a regexp to work and pass all
    # the tests, so I took a different approach: split the source by lines,
    # grab the last two and prepend '###\n' as a stand-in for whatever was in
    # the body before the last two lines.  Then, with that structure, it's
    # possible to analyze with two regexps.  Not the most elegant solution, but
    # it works.  If anyone tries to change this logic, make sure to validate
    # the whole test suite first!
    new_src = '\n'.join(['###\n'] + src.splitlines()[-2:])
    return (bool(last_two_blanks_re.match(new_src)) or
            bool(last_two_blanks_re2.match(new_src)) )

Response: Determine if the input source ends in two blanks.


Instruction:
Implement a function in Python 3 to
indicate whether an input line ends in a comment.

Response:
def has_comment(src):
    """Indicate whether an input line has (i.e. ends in, or is) a comment.

    This uses tokenize, so it can distinguish comments from # inside strings.

    Parameters
    ----------
    src : string
      A single line input string.

    Returns
    -------
    Boolean: True if source has a comment.
    """
    readline = StringIO(src).readline
    toktypes = set()
    try:
        for t in tokenize.generate_tokens(readline):
            toktypes.add(t[0])
    except tokenize.TokenError:
        pass
    return(tokenize.COMMENT in toktypes)


Instruction:
Can you create a Python 3 function that
handles the files =!ls syntax.

Response:
def transform_assign_system(line):
    """Handle the `files = !ls` syntax."""
    m = _assign_system_re.match(line)
    if m is not None:
        cmd = m.group('cmd')
        lhs = m.group('lhs')
        new_line = '%s = get_ipython().getoutput(%r)' % (lhs, cmd)
        return new_line
    return line


Instruction:
Here you have a function in Python 3, explain what it does
def transform_assign_magic(line):
    m = _assign_magic_re.match(line)
    if m is not None:
        cmd = m.group('cmd')
        lhs = m.group('lhs')
        new_line = '%s = get_ipython().magic(%r)' % (lhs, cmd)
        return new_line
    return line

Response: Handle the a = %who syntax.


Instruction:
Implement a function in Python 3 to
handle inputs that start with >>> syntax.

Response:
def transform_classic_prompt(line):
    """Handle inputs that start with '>>> ' syntax."""

    if not line or line.isspace():
        return line
    m = _classic_prompt_re.match(line)
    if m:
        return line[len(m.group(0)):]
    else:
        return line


Instruction:
Can you tell what is the following Python 3 function doing
def transform_ipy_prompt(line):

    if not line or line.isspace():
        return line
    #print 'LINE:  %r' % line # dbg
    m = _ipy_prompt_re.match(line)
    if m:
        #print 'MATCH! %r -> %r' % (line, line[len(m.group(0)):]) # dbg
        return line[len(m.group(0)):]
    else:
        return line

Response: Handle inputs that start classic IPython prompt syntax.


Instruction:
Write a Python 3 script for
preparing a help call from a target name and the escape

Response:
def _make_help_call(target, esc, lspace, next_input=None):
    """Prepares a pinfo(2)/psearch call from a target name and the escape
    (i.e. ? or ??)"""
    method  = 'pinfo2' if esc == '??' \
                else 'psearch' if '*' in target \
                else 'pinfo'
    arg = " ".join([method, target])
    if next_input is None:
        return '%sget_ipython().magic(%r)' % (lspace, arg)
    else:
        return '%sget_ipython().set_next_input(%r);get_ipython().magic(%r)' % \
           (lspace, next_input, arg)


Instruction:
How would you implement a function in Python 3 that
translates lines with? or? at the end of help line

Response:
def transform_help_end(line):
    """Translate lines with ?/?? at the end"""
    m = _help_end_re.search(line)
    if m is None or has_comment(line):
        return line
    target = m.group(1)
    esc = m.group(3)
    lspace = _initial_space_re.match(line).group(0)

    # If we're mid-command, put it back on the next prompt for the user.
    next_input = line.rstrip('?') if line.strip() != m.group(0) else None

    return _make_help_call(target, esc, lspace, next_input)


Instruction:
Make a summary of the following Python 3 code
def reset(self):
        self.indent_spaces = 0
        self._buffer[:] = []
        self.source = ''
        self.code = None
        self._is_complete = False
        self._full_dedent = False

Response: Reset the input buffer and associated state.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def push(self, lines):
        if self.input_mode == 'cell':
            self.reset()

        self._store(lines)
        source = self.source

        # Before calling _compile(), reset the code object to None so that if an
        # exception is raised in compilation, we don't mislead by having
        # inconsistent code/source attributes.
        self.code, self._is_complete = None, None

        # Honor termination lines properly
        if source.rstrip().endswith('\\'):
            return False

        self._update_indent(lines)
        try:
            self.code = self._compile(source, symbol="exec")
        # Invalid syntax can produce any of a number of different errors from
        # inside the compiler, so we have to catch them all.  Syntax errors
        # immediately produce a 'ready' block, so the invalid Python can be
        # sent to the kernel for evaluation with possible ipython
        # special-syntax conversion.
        except (SyntaxError, OverflowError, ValueError, TypeError,
                MemoryError):
            self._is_complete = True
        else:
            # Compilation didn't produce any exceptions (though it may not have
            # given a complete code object)
            self._is_complete = self.code is not None

        return self._is_complete

Response: Push one or more lines of input into the internal code object and returns a status code indicating whether the code forms a complete Python execution block or not.


Instruction:
Explain what the following Python 3 code does
def push_accepts_more(self):

        # With incomplete input, unconditionally accept more
        if not self._is_complete:
            return True

        # If we already have complete input and we're flush left, the answer
        # depends.  In line mode, if there hasn't been any indentation,
        # that's it. If we've come back from some indentation, we need
        # the blank final line to finish.
        # In cell mode, we need to check how many blocks the input so far
        # compiles into, because if there's already more than one full
        # independent block of input, then the client has entered full
        # 'cell' mode and is feeding lines that each is complete.  In this
        # case we should then keep accepting. The Qt terminal-like console
        # does precisely this, to provide the convenience of terminal-like
        # input of single expressions, but allowing the user (with a
        # separate keystroke) to switch to 'cell' mode and type multiple
        # expressions in one shot.
        if self.indent_spaces==0:
            if self.input_mode=='line':
                if not self._full_dedent:
                    return False
            else:
                try:
                    code_ast = ast.parse(u''.join(self._buffer))
                except Exception:
                    return False
                else:
                    if len(code_ast.body) == 1:
                        return False

        # When input is complete, then termination is marked by an extra blank
        # line at the end.
        last_line = self.source.splitlines()[-1]
        return bool(last_line and not last_line.isspace())

Response: Return whether a block of interactive input can accept more input.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _find_indent(self, line):
        indent_spaces = self.indent_spaces
        full_dedent = self._full_dedent

        inisp = num_ini_spaces(line)
        if inisp < indent_spaces:
            indent_spaces = inisp
            if indent_spaces <= 0:
                #print 'Full dedent in text',self.source # dbg
                full_dedent = True

        if line.rstrip()[-1] == ':':
            indent_spaces += 4
        elif dedent_re.match(line):
            indent_spaces -= 4
            if indent_spaces <= 0:
                full_dedent = True

        # Safety
        if indent_spaces < 0:
            indent_spaces = 0
            #print 'safety' # dbg

        return indent_spaces, full_dedent

Response: Compute the new indentation level for a single line.


Instruction:
Can you generate the documentation for the following Python 3 function
def _store(self, lines, buffer=None, store='source'):

        if buffer is None:
            buffer = self._buffer

        if lines.endswith('\n'):
            buffer.append(lines)
        else:
            buffer.append(lines+'\n')
        setattr(self, store, self._set_source(buffer))

Response: Store one or more lines of input.


Instruction:
Make a summary of the following Python 3 code
def _tr_system(line_info):
        "Translate lines escaped with: !"
        cmd = line_info.line.lstrip().lstrip(ESC_SHELL)
        return '%sget_ipython().system(%r)' % (line_info.pre, cmd)

Response: Translate lines escaped with :!"


Instruction:
How would you explain what the following Python 3 function does
def _tr_help(line_info):
        "Translate lines escaped with: ?/??"
        # A naked help line should just fire the intro help screen
        if not line_info.line[1:]:
            return 'get_ipython().show_usage()'

        return _make_help_call(line_info.ifun, line_info.esc, line_info.pre)

Response: Translate lines escaped with?/???


Instruction:
Here you have a function in Python 3, explain what it does
def _tr_magic(line_info):
        "Translate lines escaped with: %"
        tpl = '%sget_ipython().magic(%r)'
        cmd = ' '.join([line_info.ifun, line_info.the_rest]).strip()
        return tpl % (line_info.pre, cmd)

Response: Translate lines escaped with : %


Instruction:
Write a Python 3 function for
translating lines escaped with :,

Response:
def _tr_quote(line_info):
        "Translate lines escaped with: ,"
        return '%s%s("%s")' % (line_info.pre, line_info.ifun,
                             '", "'.join(line_info.the_rest.split()) )


Instruction:
How would you implement a function in Python 3 that
translates lines escaped with : /

Response:
def _tr_paren(line_info):
        "Translate lines escaped with: /"
        return '%s%s(%s)' % (line_info.pre, line_info.ifun,
                             ", ".join(line_info.the_rest.split()))


Instruction:
Write a Python 3 script for
resetting the input buffer and associated state.

Response:
def reset(self):
        """Reset the input buffer and associated state."""
        super(IPythonInputSplitter, self).reset()
        self._buffer_raw[:] = []
        self.source_raw = ''
        self.cell_magic_parts = []
        self.processing_cell_magic = False


Instruction:
Implement a function in Python 3 to
return input and raw source and perform a full reset.

Response:
def source_raw_reset(self):
        """Return input and raw source and perform a full reset.
        """
        out = self.source
        out_r = self.source_raw
        self.reset()
        return out, out_r


Instruction:
Given the following Python 3 function, write the documentation
def _handle_cell_magic(self, lines):
        self.processing_cell_magic = True
        first, _, body = lines.partition('\n')
        magic_name, _, line = first.partition(' ')
        magic_name = magic_name.lstrip(ESC_MAGIC)
        # We store the body of the cell and create a call to a method that
        # will use this stored value. This is ugly, but it's a first cut to
        # get it all working, as right now changing the return API of our
        # methods would require major refactoring.
        self.cell_magic_parts = [body]
        tpl = 'get_ipython()._run_cached_cell_magic(%r, %r)'
        tlines = tpl % (magic_name, line)
        self._store(tlines)
        self._store(lines, self._buffer_raw, 'source_raw')
        # We can actually choose whether to allow for single blank lines here
        # during input for clients that use cell mode to decide when to stop
        # pushing input (currently only the Qt console).
        # My first implementation did that, and then I realized it wasn't
        # consistent with the terminal behavior, so I've reverted it to one
        # line.  But I'm leaving it here so we can easily test both behaviors,
        # I kind of liked having full blank lines allowed in the cell magics...
        #self._is_complete = last_two_blanks(lines)
        self._is_complete = last_blank(lines)
        return self._is_complete

Response: Process lines when they start with %%, which marks cell magics.


Instruction:
Can you implement a function in Python 3 that
appends new content to the cell magic pieces.

Response:
def _line_mode_cell_append(self, lines):
        """Append new content for a cell magic in line mode.
        """
        # Only store the raw input.  Lines beyond the first one are only only
        # stored for history purposes; for execution the caller will grab the
        # magic pieces from cell_magic_parts and will assemble the cell body
        self._store(lines, self._buffer_raw, 'source_raw')
        self.cell_magic_parts.append(lines)
        # Find out if the last stored block has a whitespace line as its
        # last line and also this line is whitespace, case in which we're
        # done (two contiguous blank lines signal termination).  Note that
        # the storage logic *enforces* that every stored block is
        # newline-terminated, so we grab everything but the last character
        # so we can have the body of the block alone.
        last_block = self.cell_magic_parts[-1]
        self._is_complete = last_blank(last_block) and lines.isspace()
        return self._is_complete


Instruction:
Can you generate a brief explanation for the following Python 3 code
def transform_cell(self, cell):
        self.reset()
        self.push(cell)
        return self.source_reset()

Response: Process and translate a cell of input.


Instruction:
Write a Python 3 script to
push one or more lines of IPython input into the internal state.

Response:
def push(self, lines):
        """Push one or more lines of IPython input.

        This stores the given lines and returns a status code indicating
        whether the code forms a complete Python block or not, after processing
        all input lines for special IPython syntax.

        Any exceptions generated in compilation are swallowed, but if an
        exception was produced, the method returns True.

        Parameters
        ----------
        lines : string
          One or more lines of Python input.

        Returns
        -------
        is_complete : boolean
          True if the current input source (the result of the current input
        plus prior inputs) forms a complete Python execution block.  Note that
        this value is also stored as a private attribute (_is_complete), so it
        can be queried at any time.
        """
        if not lines:
            return super(IPythonInputSplitter, self).push(lines)

        # We must ensure all input is pure unicode
        lines = cast_unicode(lines, self.encoding)

        # If the entire input block is a cell magic, return after handling it
        # as the rest of the transformation logic should be skipped.
        if lines.startswith('%%') and not \
          (len(lines.splitlines()) == 1 and lines.strip().endswith('?')):
            return self._handle_cell_magic(lines)

        # In line mode, a cell magic can arrive in separate pieces
        if self.input_mode == 'line' and self.processing_cell_magic:
            return self._line_mode_cell_append(lines)

        # The rest of the processing is for 'normal' content, i.e. IPython
        # source that we process through our transformations pipeline.
        lines_list = lines.splitlines()

        transforms = [transform_ipy_prompt, transform_classic_prompt,
                      transform_help_end, transform_escaped,
                      transform_assign_system, transform_assign_magic]

        # Transform logic
        #
        # We only apply the line transformers to the input if we have either no
        # input yet, or complete input, or if the last line of the buffer ends
        # with ':' (opening an indented block).  This prevents the accidental
        # transformation of escapes inside multiline expressions like
        # triple-quoted strings or parenthesized expressions.
        #
        # The last heuristic, while ugly, ensures that the first line of an
        # indented block is correctly transformed.
        #
        # FIXME: try to find a cleaner approach for this last bit.

        # If we were in 'block' mode, since we're going to pump the parent
        # class by hand line by line, we need to temporarily switch out to
        # 'line' mode, do a single manual reset and then feed the lines one
        # by one.  Note that this only matters if the input has more than one
        # line.
        changed_input_mode = False

        if self.input_mode == 'cell':
            self.reset()
            changed_input_mode = True
            saved_input_mode = 'cell'
            self.input_mode = 'line'

        # Store raw source before applying any transformations to it.  Note
        # that this must be done *after* the reset() call that would otherwise
        # flush the buffer.
        self._store(lines, self._buffer_raw, 'source_raw')

        try:
            push = super(IPythonInputSplitter, self).push
            buf = self._buffer
            for line in lines_list:
                if self._is_complete or not buf or \
                       (buf and buf[-1].rstrip().endswith((':', ','))):
                    for f in transforms:
                        line = f(line)

                out = push(line)
        finally:
            if changed_input_mode:
                self.input_mode = saved_input_mode
        return out


Instruction:
How would you explain what the following Python 3 function does
def post_notification(self, ntype, sender, *args, **kwargs):

        if(ntype==None or sender==None):
            raise NotificationError(
                "Notification type and sender are required.")

        # If there are no registered observers for the type/sender pair
        if((ntype not in self.registered_types and
                None not in self.registered_types) or
            (sender not in self.registered_senders and
                None not in self.registered_senders)):
            return

        for o in self._observers_for_notification(ntype, sender):
            o(ntype, sender, *args, **kwargs)

Response: Post a notification to all registered observers.


Instruction:
Make a summary of the following Python 3 code
def _observers_for_notification(self, ntype, sender):

        keys = (
                   (ntype,sender),
                   (ntype, None),
                   (None, sender),
                   (None,None)
               )

        obs = set()
        for k in keys:
            obs.update(self.observers.get(k, set()))

        return obs

Response: Find all registered observers that should recieve a notification


Instruction:
How would you explain what the following Python 3 function does
def add_observer(self, callback, ntype, sender):
        assert(callback != None)
        self.registered_types.add(ntype)
        self.registered_senders.add(sender)
        self.observers.setdefault((ntype,sender), set()).add(callback)

Response: Add an observer callback to this notification center.


Instruction:
Given the following Python 3 function, write the documentation
def new(self, func_or_exp, *args, **kwargs):

        if callable(func_or_exp):
            kw  = kwargs.get('kw',{})
            job = BackgroundJobFunc(func_or_exp,*args,**kw)
        elif isinstance(func_or_exp, basestring):
            if not args:
                frame = sys._getframe(1)
                glob, loc = frame.f_globals, frame.f_locals
            elif len(args)==1:
                glob = loc = args[0]
            elif len(args)==2:
                glob,loc = args
            else:
                raise ValueError(
                      'Expression jobs take at most 2 args (globals,locals)')
            job = BackgroundJobExpr(func_or_exp, glob, loc)
        else:
            raise TypeError('invalid args for new job')

        if kwargs.get('daemon', False):
            job.daemon = True
        job.num = len(self.all)+1 if self.all else 0
        self.running.append(job)
        self.all[job.num] = job
        print 'Starting job # %s in a separate thread.' % job.num
        job.start()
        return job

Response: Add a new background job and start it in a separate thread.


Instruction:
Write a Python 3 script for
updating the status of the jobs in the list that have been completed and dead.

Response:
def _update_status(self):
        """Update the status of the job lists.

        This method moves finished jobs to one of two lists:
          - self.completed: jobs which completed successfully
          - self.dead: jobs which finished but died.

        It also copies those jobs to corresponding _report lists.  These lists
        are used to report jobs completed/dead since the last update, and are
        then cleared by the reporting function after each call."""

        # Status codes
        srun, scomp, sdead = self._s_running, self._s_completed, self._s_dead
        # State lists, use the actual lists b/c the public names are properties
        # that call this very function on access
        running, completed, dead = self._running, self._completed, self._dead

        # Now, update all state lists
        for num, job in enumerate(running):
            stat = job.stat_code
            if stat == srun:
                continue
            elif stat == scomp:
                completed.append(job)
                self._comp_report.append(job)
                running[num] = False
            elif stat == sdead:
                dead.append(job)
                self._dead_report.append(job)
                running[num] = False
        # Remove dead/completed jobs from running list
        running[:] = filter(None, running)


Instruction:
Can you tell what is the following Python 3 function doing
def _group_report(self,group,name):

        if group:
            print '%s jobs:' % name
            for job in group:
                print '%s : %s' % (job.num,job)
            print
            return True

Response: Report summary for a given job group. Return True if the group had any elements.


Instruction:
Create a Python 3 function to
flush a given job group Return True if the group had any elements.

Response:
def _group_flush(self,group,name):
        """Flush a given job group

        Return True if the group had any elements."""

        njobs = len(group)
        if njobs:
            plural = {1:''}.setdefault(njobs,'s')
            print 'Flushing %s %s job%s.' % (njobs,name,plural)
            group[:] = []
            return True


Instruction:
Implement a Python 3 function for
printing the status of newly finished jobs. Return True if any new jobs are reported.

Response:
def _status_new(self):
        """Print the status of newly finished jobs.

        Return True if any new jobs are reported.

        This call resets its own state every time, so it only reports jobs
        which have finished since the last time it was called."""

        self._update_status()
        new_comp = self._group_report(self._comp_report, 'Completed')
        new_dead = self._group_report(self._dead_report,
                                      'Dead, call jobs.traceback() for details')
        self._comp_report[:] = []
        self._dead_report[:] = []
        return new_comp or new_dead


Instruction:
Given the following Python 3 function, write the documentation
def status(self,verbose=0):

        self._update_status()
        self._group_report(self.running,'Running')
        self._group_report(self.completed,'Completed')
        self._group_report(self.dead,'Dead')
        # Also flush the report queues
        self._comp_report[:] = []
        self._dead_report[:] = []

Response: Print a status of all currently being managed jobs.


Instruction:
Can you implement a function in Python 3 that
removes a finished or dead job.

Response:
def remove(self,num):
        """Remove a finished (completed or dead) job."""

        try:
            job = self.all[num]
        except KeyError:
            error('Job #%s not found' % num)
        else:
            stat_code = job.stat_code
            if stat_code == self._s_running:
                error('Job #%s is still running, it can not be removed.' % num)
                return
            elif stat_code == self._s_completed:
                self.completed.remove(job)
            elif stat_code == self._s_dead:
                self.dead.remove(job)


Instruction:
How would you code a function in Python 3 to
flush all finished and dead jobs from lists.

Response:
def flush(self):
        """Flush all finished jobs (completed and dead) from lists.

        Running jobs are never flushed.

        It first calls _status_new(), to update info. If any jobs have
        completed since the last _status_new() call, the flush operation
        aborts."""

        # Remove the finished jobs from the master dict
        alljobs = self.all
        for job in self.completed+self.dead:
            del(alljobs[job.num])

        # Now flush these lists completely
        fl_comp = self._group_flush(self.completed, 'Completed')
        fl_dead = self._group_flush(self.dead, 'Dead')
        if not (fl_comp or fl_dead):
            print 'No jobs to flush.'


Instruction:
Here you have a function in Python 3, explain what it does
def result(self,num):
        try:
            return self.all[num].result
        except KeyError:
            error('Job #%s not found' % num)

Response: result ( N ) - > return the result of job N


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

        for attr in ['call','strform']:
            assert hasattr(self,attr), "Missing attribute <%s>" % attr

        # The num tag can be set by an external job manager
        self.num = None

        self.status    = BackgroundJobBase.stat_created
        self.stat_code = BackgroundJobBase.stat_created_c
        self.finished  = False
        self.result    = '<BackgroundJob has not completed>'

        # reuse the ipython traceback handler if we can get to it, otherwise
        # make a new one
        try:
            make_tb = get_ipython().InteractiveTB.text
        except:
            make_tb = AutoFormattedTB(mode = 'Context',
                                      color_scheme='NoColor',
                                      tb_offset = 1).text
        # Note that the actual API for text() requires the three args to be
        # passed in, so we wrap it in a simple lambda.
        self._make_tb = lambda : make_tb(None, None, None)

        # Hold a formatted traceback if one is generated.
        self._tb = None

        threading.Thread.__init__(self)

Response: Common initialization for all BackgroundJob objects


Instruction:
Create a Python 3 function for
inserting a value into the list variable at an appropriate index.

Response:
def insert(self, idx, value):
        """
        Inserts a value in the ``ListVariable`` at an appropriate index.

        :param idx: The index before which to insert the new value.
        :param value: The value to insert.
        """

        self._value.insert(idx, value)
        self._rebuild()


Instruction:
How would you implement a function in Python 3 that
returns a shallow copy of the Environment.

Response:
def copy(self):
        """
        Retrieve a copy of the Environment.  Note that this is a shallow
        copy.
        """

        return self.__class__(self._data.copy(), self._sensitive.copy(),
                              self._cwd)


Instruction:
Create a Python 3 function to
declare an environment variable as a special variable.

Response:
def _declare_special(self, name, sep, klass):
        """
        Declare an environment variable as a special variable.  This can
        be used even if the environment variable is not present.

        :param name: The name of the environment variable that should
                     be considered special.
        :param sep: The separator to be used.
        :param klass: The subclass of ``SpecialVariable`` used to
                      represent the variable.
        """

        # First, has it already been declared?
        if name in self._special:
            special = self._special[name]
            if not isinstance(special, klass) or sep != special._sep:
                raise ValueError('variable %s already declared as %s '
                                 'with separator "%s"' %
                                 (name, special.__class__.__name__,
                                  special._sep))

        # OK, it's new; declare it
        else:
            self._special[name] = klass(self, name, sep)


Instruction:
How would you explain what the following Python 3 function does
def declare_list(self, name, sep=os.pathsep):

        self._declare_special(name, sep, ListVariable)

Response: Declare an environment variable as a list - like special variable. This is a no - op if the environment variable is not present.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def declare_set(self, name, sep=os.pathsep):

        self._declare_special(name, sep, SetVariable)

Response: Declare an environment variable as a set - like special variable. This is a no - op if the environment variable is not set - like.


Instruction:
Given the following Python 3 function, write the documentation
def cwd(self, value):

        self._cwd = utils.canonicalize_path(self._cwd, value)

Response: Change the working directory that processes should be executed in.


Instruction:
Explain what the following Python 3 code does
def move(self, state=None):
        state = self.state if state is None else state
        route = state
        a = random.randint(self.locked_range, len(route) - 1)
        b = random.randint(self.locked_range, len(route) - 1)
        route[a], route[b] = route[b], route[a]

Response: Swaps two cities in the route.


Instruction:
Can you implement a function in Python 3 that
calculates the length of the route.

Response:
def energy(self, state=None):
        """Calculates the length of the route."""
        state = self.state if state is None else state
        route = state
        e = 0
        if self.distance_matrix:
            for i in range(len(route)):
                e += self.distance_matrix["{},{}".format(route[i-1], route[i])]
        else:
            for i in range(len(route)):
                e += distance(self.cities[route[i-1]], self.cities[route[i]])
        return e


Instruction:
Implement a function in Python 3 to
divide the cities of a TSP problem into a list of routes.

Response:
def divide(cls, divisions, problem_data):
        """divide

        :type problem_data: dict
        """
        tspp = TSPProblem(**problem_data)

        def routes_for_subgroup(cs):
            for city in cs:
                if city == tspp.start_city:
                    continue
                cities = tspp.cities.keys()
                cities.remove(tspp.start_city)
                cities.remove(city)
                random.shuffle(cities)
                route = [tspp.start_city, city] + cities
                assert len(set(route)) == len(route)
                assert len(route) == len(tspp.cities)
                yield json.dumps(route)

        if divisions:
            chunk_size = int(math.ceil(len(tspp.cities) / divisions))
        else:
            chunk_size = 1
        for subgroup in chunks(tspp.cities.keys(), chunk_size):
            routes = list(routes_for_subgroup(subgroup))
            if routes:
                yield routes


Instruction:
Write a Python 3 function for
creating an empty record

Response:
def _defaults(self, keys=None):
        """create an empty record"""
        d = {}
        keys = self._keys if keys is None else keys
        for key in keys:
            d[key] = None
        return d


Instruction:
Can you write a function in Python 3 where it
ensures that an incorrect table exists return True

Response:
def _check_table(self):
        """Ensure that an incorrect table doesn't exist

        If a bad (old) table does exist, return False
        """
        cursor = self._db.execute("PRAGMA table_info(%s)"%self.table)
        lines = cursor.fetchall()
        if not lines:
            # table does not exist
            return True
        types = {}
        keys = []
        for line in lines:
            keys.append(line[1])
            types[line[1]] = line[2]
        if self._keys != keys:
            # key mismatch
            self.log.warn('keys mismatch')
            return False
        for key in self._keys:
            if types[key] != self._types[key]:
                self.log.warn(
                    'type mismatch: %s: %s != %s'%(key,types[key],self._types[key])
                )
                return False
        return True


Instruction:
Write a Python 3 function for
connecting to the database and get new session number.

Response:
def _init_db(self):
        """Connect to the database and get new session number."""
        # register adapters
        sqlite3.register_adapter(dict, _adapt_dict)
        sqlite3.register_converter('dict', _convert_dict)
        sqlite3.register_adapter(list, _adapt_bufs)
        sqlite3.register_converter('bufs', _convert_bufs)
        # connect to the db
        dbfile = os.path.join(self.location, self.filename)
        self._db = sqlite3.connect(dbfile, detect_types=sqlite3.PARSE_DECLTYPES,
            # isolation_level = None)#,
             cached_statements=64)
        # print dir(self._db)
        first_table = previous_table = self.table
        i=0
        while not self._check_table():
            i+=1
            self.table = first_table+'_%i'%i
            self.log.warn(
                "Table %s exists and doesn't match db format, trying %s"%
                (previous_table, self.table)
            )
            previous_table = self.table

        self._db.execute("""CREATE TABLE IF NOT EXISTS %s
                (msg_id text PRIMARY KEY,
                header dict text,
                content dict text,
                buffers bufs blob,
                submitted timestamp,
                client_uuid text,
                engine_uuid text,
                started timestamp,
                completed timestamp,
                resubmitted text,
                received timestamp,
                result_header dict text,
                result_content dict text,
                result_buffers bufs blob,
                queue text,
                pyin text,
                pyout text,
                pyerr text,
                stdout text,
                stderr text)
                """%self.table)
        self._db.commit()


Instruction:
Can you tell what is the following Python 3 function doing
def _render_expression(self, check):
        expressions = []
        args = []

        skeys = set(check.keys())
        skeys.difference_update(set(self._keys))
        skeys.difference_update(set(['buffers', 'result_buffers']))
        if skeys:
            raise KeyError("Illegal testing key(s): %s"%skeys)

        for name,sub_check in check.iteritems():
            if isinstance(sub_check, dict):
                for test,value in sub_check.iteritems():
                    try:
                        op = operators[test]
                    except KeyError:
                        raise KeyError("Unsupported operator: %r"%test)
                    if isinstance(op, tuple):
                        op, join = op

                    if value is None and op in null_operators:
                        expr = "%s %s" % (name, null_operators[op])
                    else:
                        expr = "%s %s ?"%(name, op)
                        if isinstance(value, (tuple,list)):
                            if op in null_operators and any([v is None for v in value]):
                                # equality tests don't work with NULL
                                raise ValueError("Cannot use %r test with NULL values on SQLite backend"%test)
                            expr = '( %s )'%( join.join([expr]*len(value)) )
                            args.extend(value)
                        else:
                            args.append(value)
                    expressions.append(expr)
            else:
                # it's an equality check
                if sub_check is None:
                    expressions.append("%s IS NULL" % name)
                else:
                    expressions.append("%s = ?"%name)
                    args.append(sub_check)

        expr = " AND ".join(expressions)
        return expr, args

Response: Turn a mongodb - style search dict into an SQL query.


Instruction:
Given the following Python 3 function, write the documentation
def add_record(self, msg_id, rec):
        d = self._defaults()
        d.update(rec)
        d['msg_id'] = msg_id
        line = self._dict_to_list(d)
        tups = '(%s)'%(','.join(['?']*len(line)))
        self._db.execute("INSERT INTO %s VALUES %s"%(self.table, tups), line)

Response: Add a new Task Record by msg_id.


Instruction:
Can 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."""
        cursor = self._db.execute("""SELECT * FROM %s WHERE msg_id==?"""%self.table, (msg_id,))
        line = cursor.fetchone()
        if line is None:
            raise KeyError("No such msg: %r"%msg_id)
        return self._list_to_dict(line)


Instruction:
Implement a function in Python 3 to
update the data in an existing record.

Response:
def update_record(self, msg_id, rec):
        """Update the data in an existing record."""
        query = "UPDATE %s SET "%self.table
        sets = []
        keys = sorted(rec.keys())
        values = []
        for key in keys:
            sets.append('%s = ?'%key)
            values.append(rec[key])
        query += ', '.join(sets)
        query += ' WHERE msg_id == ?'
        values.append(msg_id)
        self._db.execute(query, values)


Instruction:
Write a Python 3 script to
remove a record from the DB.

Response:
def drop_matching_records(self, check):
        """Remove a record from the DB."""
        expr,args = self._render_expression(check)
        query = "DELETE FROM %s WHERE %s"%(self.table, expr)
        self._db.execute(query,args)


Instruction:
Can you implement a function in Python 3 that
finds records matching a query dict optionally extracting subset of keys.

Response:
def find_records(self, check, keys=None):
        """Find records matching a query dict, optionally extracting subset of keys.

        Returns list of matching records.

        Parameters
        ----------

        check: dict
            mongodb-style query argument
        keys: list of strs [optional]
            if specified, the subset of keys to extract.  msg_id will *always* be
            included.
        """
        if keys:
            bad_keys = [ key for key in keys if key not in self._keys ]
            if bad_keys:
                raise KeyError("Bad record key(s): %s"%bad_keys)

        if keys:
            # ensure msg_id is present and first:
            if 'msg_id' in keys:
                keys.remove('msg_id')
            keys.insert(0, 'msg_id')
            req = ', '.join(keys)
        else:
            req = '*'
        expr,args = self._render_expression(check)
        query = """SELECT %s FROM %s WHERE %s"""%(req, self.table, expr)
        cursor = self._db.execute(query, args)
        matches = cursor.fetchall()
        records = []
        for line in matches:
            rec = self._list_to_dict(line, keys)
            records.append(rec)
        return records


Instruction:
Can you implement a function in Python 3 that
gets all msg_ids ordered by time submitted.

Response:
def get_history(self):
        """get all msg_ids, ordered by time submitted."""
        query = """SELECT msg_id FROM %s ORDER by submitted ASC"""%self.table
        cursor = self._db.execute(query)
        # will be a list of length 1 tuples
        return [ tup[0] for tup in cursor.fetchall()]


Instruction:
Write a Python 3 script to
read a config file check validity with a JSON Schema as specs and get default values from default_file.

Response:
def parse(self, config_file=None, specs=None, default_file=None):
        """Read a config_file, check the validity with a JSON Schema as specs
        and get default values from default_file if asked.

        All parameters are optionnal.

        If there is no config_file defined, read the venv base
        dir and try to get config/app.yml.

        If no specs, don't validate anything.

        If no default_file, don't merge with default values."""

        self._config_exists(config_file)
        self._specs_exists(specs)

        self.loaded_config = anyconfig.load(self.config_file, ac_parser='yaml')

        if default_file is not None:
            self._merge_default(default_file)

        if self.specs is None:
            return self.loaded_config

        self._validate()

        return self.loaded_config


Instruction:
How would you explain what the following Python 3 function does
def table(rows):
    '''
    Output a simple table with several columns.
    '''

    output = '
'

    for row in rows:
        output += '
'
        for column in row:
            output += '{s}	'.format(s=column)
        output += '
'

    output += '
'

    return output

Response: Output a simple table with several columns.


Instruction:
Given the following Python 3 function, write the documentation
def link(url, text='', classes='', target='', get="", **kwargs):
    '''
    Output a link tag.
    '''

    if not (url.startswith('http') or url.startswith('/')):
        # Handle additional reverse args.
        urlargs = {}

        for arg, val in kwargs.items():
            if arg[:4] == "url_":
                urlargs[arg[4:]] = val

        url = reverse(url, kwargs=urlargs)
        if get:
            url += '?' + get

    return html.tag('a', text or url, {
        'class': classes, 'target': target, 'href': url})

Response: Output a link tag.


Instruction:
How would you explain what the following Python 3 function does
def jsfile(url):
    '''
    Output a script tag to a js file.
    '''

    if not url.startswith('http://') and not url[:1] == '/':
        #add media_url for relative paths
        url = settings.STATIC_URL + url

    return '<script type="text/javascript" src="{src}"></script>'.format(
        src=url)

Response: Output a script tag to a js file.


Instruction:
Given the following Python 3 function, write the documentation
def cssfile(url):
    '''
    Output a link tag to a css stylesheet.
    '''

    if not url.startswith('http://') and not url[:1] == '/':
        #add media_url for relative paths
        url = settings.STATIC_URL + url

    return '<link href="{src}" rel="stylesheet">'.format(src=url)

Response: Output a link tag to a css stylesheet.


Instruction:
How would you explain what the following Python 3 function does
def sub(value, arg):
    try:
        return valid_numeric(value) - valid_numeric(arg)
    except (ValueError, TypeError):
        try:
            return value - arg
        except Exception:
            return ''

Response: Subtract the arg from the value.


Instruction:
How would you explain what the following Python 3 function does
def mul(value, arg):
    try:
        return valid_numeric(value) * valid_numeric(arg)
    except (ValueError, TypeError):
        try:
            return value * arg
        except Exception:
            return ''

Response: Multiply the value with the arg.


Instruction:
How would you explain what the following Python 3 function does
def div(value, arg):
    try:
        return valid_numeric(value) / valid_numeric(arg)
    except (ValueError, TypeError):
        try:
            return value / arg
        except Exception:
            return ''

Response: Divide the value by the arg.


Instruction:
Write a Python 3 script to
return the modulo value.

Response:
def mod(value, arg):
    """Return the modulo value."""
    try:
        return valid_numeric(value) % valid_numeric(arg)
    except (ValueError, TypeError):
        try:
            return value % arg
        except Exception:
            return ''


Instruction:
How would you explain what the following Python 3 function does
def model_verbose(obj, capitalize=True):

    if isinstance(obj, ModelForm):
        name = obj._meta.model._meta.verbose_name
    elif isinstance(obj, Model):
        name = obj._meta.verbose_name
    else:
        raise Exception('Unhandled type: ' + type(obj))

    return name.capitalize() if capitalize else name

Response: Return the verbose name of a model.


Instruction:
Write a Python 3 script for
using as a class decorator to add extra methods to your model manager. Example usage: class Article(django.db.models.Model): published = models.DateTimeField() ... @extendManager class objects(object): def getPublished(self): return self.filter(published__lte = django.utils.timezone.now()).order_by('-published') ... publishedArticles = Article.objects.getPublished()

Response:
def extendManager(mixinClass):
	'''
	Use as a class decorator to add extra methods to your model manager.
	Example usage:

		class Article(django.db.models.Model):
			published = models.DateTimeField()
			...

			@extendManager
			class objects(object):
				def getPublished(self):
					return self.filter(published__lte = django.utils.timezone.now()).order_by('-published')

		...

		publishedArticles = Article.objects.getPublished()
	'''

	class MixinManager(models.Manager, mixinClass):
		class MixinQuerySet(models.query.QuerySet, mixinClass):
			pass
		def get_queryset(self):
			return self.MixinQuerySet(self.model, using = self._db)

	return MixinManager()


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

    config_option_help="'show' - displays configured options, 'set [section] [name] [value]' - sets config under a section,'set [name] [value]' - sets configuration globally"

    parser = OptionParser()

    parser.add_option("-a", "--add", action="store", type="string", dest="addfile", help="adds a notes")
    parser.add_option("-c", "--config", action="store", type="string", dest="config", help=config_option_help)
    parser.add_option("-e", "--edit", action="store", type="string", dest="editfile", help="edits a notes")
    parser.add_option("-o", "--open", action="store", type="string", dest="openfile", help="opens a notes")
    parser.add_option("-r", "--remove", action="store", type="string", dest="remove", help="removes a notes")

    options, args = parser.parse_args()

    if options.config:
        if options.config == "show":
            config_option_list=''
            config_sections = config.sections()
            for section in config_sections:
                config_option_list=config_option_list+section+"\n"
                section_items =config.items(section)
                for item in section_items:
                    config_option_list=config_option_list+"    "+item[0]+"    "+item[1]+"\n"
            print config_option_list
            quit()

    def add_notes(note_name,existing_tags):
       call([editor,environ["HOME"] + "/.mypy/myhelp/notes/"+note_name+".note"])
       definedtags = raw_input("Define Tags (separated by spaces): ").split(" ")
       definedtags.append(note_name)
       print definedtags
       print existing_tags
       definedtags=list(set(definedtags)-set(existing_tags))
       print definedtags
       if len(definedtags)>0:
           modify_tags_xml(note_name,definedtags,files,rootfiles,tags,roottags,tree,TAGS_XML_DIR)

    def get_tags_from_file(note_name):
        fil = get_file_from_files(note_name)
        filetags = fil.iter('tag')
        filetaglist=[]
        for tag in filetags:
            filetaglist.append(tag.text)
        return filetaglist

    if options.addfile:
       existing_tags=[]
       if isFile(options.addfile,files):
           existing_tags=get_tags_from_file(options.addfile)
           raw_input("Note exists with tags - "+" ".join(existing_tags)+"\nDo you want to edit the notes ? [Press enter to continue]\n")
       add_notes(options.addfile,existing_tags)
       quit()

    if options.editfile:
        if isFile(options.editfile,files):
            add_notes(note_name,[])
        else:
           raw_input("Note doesn't exist.\nDo you want add note ? [Press enter to continue]")
           add_notes(note_name)

    if options.remove:
        pass

    if len(args) != 1:
        print "Please use a search term\n example : myhelp <some tag word> "
        quit()
    _key_File = "Note"
    _key_Results = "    Results                                     "
    table={_key_Results:[]}
    for tag in tags:
        if tag.attrib["value"] == args[0]:
            fileelements = tag.iter("file")
            for fileelement in fileelements:
                f = open(
                    environ["HOME"] + "/.mypy/myhelp/notes/" + fileelement.text+".note", "r")
		table[_key_Results].append(f.read()+"\r\n\tfile: ~/.mypy/myhelp/notes/"+fileelement.text+".note")
                f.close()

    print tabulate(table,headers=[],tablefmt="rst")

Response: Main function for the main function of the main function of the main function of the main function of the main function of the main function of the main function of the main function of the main function of the main function of the main function of the main function of the main function of the main function.


Instruction:
Given the following Python 3 function, write the documentation
def split_user_input(line, pattern=None):
    # We need to ensure that the rest of this routine deals only with unicode
    encoding = get_stream_enc(sys.stdin, 'utf-8')
    line = py3compat.cast_unicode(line, encoding)

    if pattern is None:
        pattern = line_split
    match = pattern.match(line)
    if not match:
        # print "match failed for line '%s'" % line
        try:
            ifun, the_rest = line.split(None,1)
        except ValueError:
            # print "split failed for line '%s'" % line
            ifun, the_rest = line, u''
        pre = re.match('^(\s*)(.*)',line).groups()[0]
        esc = ""
    else:
        pre, esc, ifun, the_rest = match.groups()

    #print 'line:<%s>' % line # dbg
    #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun.strip(),the_rest) # dbg
    return pre, esc or '', ifun.strip(), the_rest.lstrip()

Response:
Split user input into initial whitespace escape character function part
    and the rest.


Instruction:
How would you code a function in Python 3 to
return a class name if the current URL matches the route name specified in urlName.

Response:
def current(context, urlName, className = 'active', **kwargs):
	'''
	Return a class name (string) if the current URL matches the route name specified in ``className``.
	If any URL keyword arguments are provided, they must be matched as well.

	:param urlName: The route name that the current URL should match. Example: 'accounts:login'.
	:param className: The string that is returned if the current URL matches the specified one.
	:param kwargs: Any extra URL keyword arguments that must also be present in the current URL for it to match.
	:returns: ``className`` if the current URL matches, or an empty string otherwise.
	'''
	matches = pathMatches(context['request'].path, urlName, **kwargs)
	return className if matches else ''


Instruction:
Write a Python 3 function that can
check if a path matches a URL.

Response:
def pathMatches(path, urlName, **kwargs):
	'''
	:param path: str
	:param urlName: str
	:returns: bool.
	'''
	resolved = urlresolvers.resolve(path)

	# Different URL name => the current URL cannot match.
	resolvedName = '{r.namespace}:{r.url_name}'.format(r = resolved) if resolved.namespace else resolved.url_name
	if urlName != resolvedName:
		return False

	# If any of the current keyword args is missing or different, the URL cannot match.
	for key, value in kwargs.items():
		currentArg = resolved.kwargs.get(key)
		if currentArg is None or str(value) != str(currentArg):
			return False

	return True


Instruction:
Can you create a Python 3 function that
registers command - line options.

Response:
def options(self, parser, env):
        """
        Register command-line options.
        """
        parser.add_option("--processes", action="store",
                          default=env.get('NOSE_PROCESSES', 0),
                          dest="multiprocess_workers",
                          metavar="NUM",
                          help="Spread test run among this many processes. "
                          "Set a number equal to the number of processors "
                          "or cores in your machine for best results. "
                          "[NOSE_PROCESSES]")
        parser.add_option("--process-timeout", action="store",
                          default=env.get('NOSE_PROCESS_TIMEOUT', 10),
                          dest="multiprocess_timeout",
                          metavar="SECONDS",
                          help="Set timeout for return of results from each "
                          "test runner process. [NOSE_PROCESS_TIMEOUT]")
        parser.add_option("--process-restartworker", action="store_true",
                          default=env.get('NOSE_PROCESS_RESTARTWORKER', False),
                          dest="multiprocess_restartworker",
                          help="If set, will restart each worker process once"
                          " their tests are done, this helps control memory "
                          "leaks from killing the system. "
                          "[NOSE_PROCESS_RESTARTWORKER]")


Instruction:
Write a Python 3 function that can
run the tests in the test queue inside of the suite fixtures.

Response:
def run(self, result):
        """Run tests in suite inside of suite fixtures.
        """
        # proxy the result for myself
        log.debug("suite %s (%s) run called, tests: %s",
                  id(self), self, self._tests)
        if self.resultProxy:
            result, orig = self.resultProxy(result, self), result
        else:
            result, orig = result, result
        try:
            #log.debug('setUp for %s', id(self));
            self.setUp()
        except KeyboardInterrupt:
            raise
        except:
            self.error_context = 'setup'
            result.addError(self, self._exc_info())
            return
        try:
            for test in self._tests:
                if (isinstance(test,nose.case.Test)
                    and self.arg is not None):
                    test.test.arg = self.arg
                else:
                    test.arg = self.arg
                test.testQueue = self.testQueue
                test.tasks = self.tasks
                if result.shouldStop:
                    log.debug("stopping")
                    break
                # each nose.case.Test will create its own result proxy
                # so the cases need the original result, to avoid proxy
                # chains
                #log.debug('running test %s in suite %s', test, self);
                try:
                    test(orig)
                except KeyboardInterrupt, e:
                    timeout = isinstance(e, TimedOutException)
                    if timeout:
                        msg = 'Timeout when running test %s in suite %s'
                    else:
                        msg = 'KeyboardInterrupt when running test %s in suite %s'
                    log.debug(msg, test, self)
                    err = (TimedOutException,TimedOutException(str(test)),
                           sys.exc_info()[2])
                    test.config.plugins.addError(test,err)
                    orig.addError(test,err)
                    if not timeout:
                        raise
        finally:
            self.has_run = True
            try:
                #log.debug('tearDown for %s', id(self));
                self.tearDown()
            except KeyboardInterrupt:
                raise
            except:
                self.error_context = 'teardown'
                result.addError(self, self._exc_info())


Instruction:
Write a Python 3 script for
adding a builtin and save the original.

Response:
def add_builtin(self, key, value):
        """Add a builtin and save the original."""
        bdict = __builtin__.__dict__
        orig = bdict.get(key, BuiltinUndefined)
        if value is HideBuiltin:
            if orig is not BuiltinUndefined: #same as 'key in bdict'
                self._orig_builtins[key] = orig
                del bdict[key]
        else:
            self._orig_builtins[key] = orig
            bdict[key] = value


Instruction:
Can you generate a brief explanation for the following Python 3 code
def remove_builtin(self, key, orig):
        if orig is BuiltinUndefined:
            del __builtin__.__dict__[key]
        else:
            __builtin__.__dict__[key] = orig

Response: Remove an added builtin and re - set the original.


Instruction:
Write a Python 3 script to
store ipython references in the __builtin__ namespace.

Response:
def activate(self):
        """Store ipython references in the __builtin__ namespace."""

        add_builtin = self.add_builtin
        for name, func in self.auto_builtins.iteritems():
            add_builtin(name, func)


Instruction:
Implement a function in Python 3 to
deactivate the current state of the object.

Response:
def deactivate(self):
        """Remove any builtins which might have been added by add_builtins, or
        restore overwritten ones to their previous values."""
        remove_builtin = self.remove_builtin
        for key, val in self._orig_builtins.iteritems():
            remove_builtin(key, val)
        self._orig_builtins.clear()
        self._builtins_added = False


Instruction:
Implement a function in Python 3 to
find the true URL name of a package when the given name isn t quite correct.

Response:
def _find_url_name(self, index_url, url_name, req):
        """
        Finds the true URL name of a package, when the given name isn't quite
        correct.
        This is usually used to implement case-insensitivity.
        """
        if not index_url.url.endswith('/'):
            # Vaguely part of the PyPI API... weird but true.
            # FIXME: bad to modify this?
            index_url.url += '/'
        page = self._get_page(index_url, req)
        if page is None:
            logger.critical('Cannot fetch index base URL %s', index_url)
            return
        norm_name = normalize_name(req.url_name)
        for link in page.links:
            base = posixpath.basename(link.path.rstrip('/'))
            if norm_name == normalize_name(base):
                logger.debug(
                    'Real name of requirement %s is %s', url_name, base,
                )
                return base
        return None


Instruction:
Make a summary of the following Python 3 code
def _link_package_versions(self, link, search_name):
        platform = get_platform()

        version = None
        if link.egg_fragment:
            egg_info = link.egg_fragment
        else:
            egg_info, ext = link.splitext()
            if not ext:
                if link not in self.logged_links:
                    logger.debug('Skipping link %s; not a file', link)
                    self.logged_links.add(link)
                return
            if egg_info.endswith('.tar'):
                # Special double-extension case:
                egg_info = egg_info[:-4]
                ext = '.tar' + ext
            if ext not in self._known_extensions():
                if link not in self.logged_links:
                    logger.debug(
                        'Skipping link %s; unknown archive format: %s',
                        link,
                        ext,
                    )
                    self.logged_links.add(link)
                return
            if "macosx10" in link.path and ext == '.zip':
                if link not in self.logged_links:
                    logger.debug('Skipping link %s; macosx10 one', link)
                    self.logged_links.add(link)
                return
            if ext == wheel_ext:
                try:
                    wheel = Wheel(link.filename)
                except InvalidWheelFilename:
                    logger.debug(
                        'Skipping %s because the wheel filename is invalid',
                        link
                    )
                    return
                if (pkg_resources.safe_name(wheel.name).lower()
                        != pkg_resources.safe_name(search_name).lower()):
                    logger.debug(
                        'Skipping link %s; wrong project name (not %s)',
                        link,
                        search_name,
                    )
                    return
                if not wheel.supported():
                    logger.debug(
                        'Skipping %s because it is not compatible with this '
                        'Python',
                        link,
                    )
                    return
                # This is a dirty hack to prevent installing Binary Wheels from
                # PyPI unless it is a Windows or Mac Binary Wheel. This is
                # paired with a change to PyPI disabling uploads for the
                # same. Once we have a mechanism for enabling support for
                # binary wheels on linux that deals with the inherent problems
                # of binary distribution this can be removed.
                comes_from = getattr(link, "comes_from", None)
                if (
                        (
                            not platform.startswith('win')
                            and not platform.startswith('macosx')
                            and not platform == 'cli'
                        )
                        and comes_from is not None
                        and urllib_parse.urlparse(
                            comes_from.url
                        ).netloc.endswith(PyPI.netloc)):
                    if not wheel.supported(tags=supported_tags_noarch):
                        logger.debug(
                            "Skipping %s because it is a pypi-hosted binary "
                            "Wheel on an unsupported platform",
                            link,
                        )
                        return
                version = wheel.version

        if not version:
            version = self._egg_info_matches(egg_info, search_name, link)
        if version is None:
            logger.debug(
                'Skipping link %s; wrong project name (not %s)',
                link,
                search_name,
            )
            return

        if (link.internal is not None
                and not link.internal
                and not normalize_name(search_name).lower()
                in self.allow_external
                and not self.allow_all_external):
            # We have a link that we are sure is external, so we should skip
            #   it unless we are allowing externals
            logger.debug("Skipping %s because it is externally hosted.", link)
            self.need_warn_external = True
            return

        if (link.verifiable is not None
                and not link.verifiable
                and not (normalize_name(search_name).lower()
                         in self.allow_unverified)):
            # We have a link that we are sure we cannot verify its integrity,
            #   so we should skip it unless we are allowing unsafe installs
            #   for this requirement.
            logger.debug(
                "Skipping %s because it is an insecure and unverifiable file.",
                link,
            )
            self.need_warn_unverified = True
            return

        match = self._py_version_re.search(version)
        if match:
            version = version[:match.start()]
            py_version = match.group(1)
            if py_version != sys.version[:3]:
                logger.debug(
                    'Skipping %s because Python version is incorrect', link
                )
                return
        logger.debug('Found link %s, version: %s', link, version)

        return InstallationCandidate(search_name, version, link)

Response: Return an iterable of triples that can be extracted from the given pkg_resources. Link object.


Instruction:
How would you explain what the following Python 3 function does
def explicit_rel_links(self, rels=('homepage', 'download')):
        rels = set(rels)

        for anchor in self.parsed.findall(".//a"):
            if anchor.get("rel") and anchor.get("href"):
                found_rels = set(anchor.get("rel").split())
                # Determine the intersection between what rels were found and
                #   what rels were being looked for
                if found_rels & rels:
                    href = anchor.get("href")
                    url = self.clean_link(
                        urllib_parse.urljoin(self.base_url, href)
                    )
                    yield Link(url, self, trusted=False)

Response: Yields all links with the given relations


Instruction:
Can you generate the documentation for the following Python 3 function
def send_multi_alt_email(
    subject,  # single line with no line-breaks
    text_content,
    to_emails,
    html_content=None,
    from_email=DEFAULT_FROM_EMAIL,
    fail_silently=True
    ):
    """
    Send a message to one more email address(s).
    With text content as primary and html content as alternative.
    """
    messenger = EmailMultiAlternatives(subject, text_content, from_email, to_emails)
    if html_content:
        messenger.attach_alternative(html_content, "text/html")
    try:
        messenger.send()
    except Exception as e:
        if not fail_silently:
            raise

Response: Sends a message to one more email addresses.