Instruction:
How would you code a function in Python 3 to
read and parse a RiveScript document and return a data structure that represents all of the useful contents of the RiveScript document.

Response:
def parse(self, filename, code):
        """Read and parse a RiveScript document.

        Returns a data structure that represents all of the useful contents of
        the document, in this format::

            {
                "begin": { # "begin" data
                    "global": {}, # map of !global vars
                    "var": {},    # bot !var's
                    "sub": {},    # !sub substitutions
                    "person": {}, # !person substitutions
                    "array": {},  # !array lists
                },
                "topics": { # main reply data
                    "random": { # (topic name)
                        "includes": {}, # map of included topics (values=1)
                        "inherits": {}, # map of inherited topics
                        "triggers": [   # array of triggers
                            {
                                "trigger": "hello bot",
                                "reply": [], # array of replies
                                "condition": [], # array of conditions
                                "redirect": None, # redirect command
                                "previous": None, # 'previous' reply
                            },
                            # ...
                        ]
                    }
                }
                "objects": [ # parsed object macros
                    {
                        "name": "",     # object name
                        "language": "", # programming language
                        "code": [],     # array of lines of code
                    }
                ]
            }

        Args:
            filename (str): The name of the file that the code came from, for
                syntax error reporting purposes.
            code (str[]): The source code to parse.

        Returns:
            dict: The aforementioned data structure.
        """

        # Eventual returned structure ("abstract syntax tree" but not really)
        ast = {
            "begin": {
                "global": {},
                "var": {},
                "sub": {},
                "person": {},
                "array": {},
            },
            "topics": {},
            "objects": [],
        }

        # Track temporary variables.
        topic   = 'random'  # Default topic=random
        lineno  = 0         # Line numbers for syntax tracking
        comment = False     # In a multi-line comment
        inobj   = False     # In an object
        objname = ''        # The name of the object we're in
        objlang = ''        # The programming language of the object
        objbuf  = []        # Object contents buffer
        curtrig = None      # Pointer to the current trigger in ast.topics
        isThat  = None      # Is a %Previous trigger

        # Local (file scoped) parser options.
        local_options = dict(
            concat="none",  # Concat mode for ^Continue command
        )

        # Read each line.
        for lp, line in enumerate(code):
            lineno += 1

            self.say("Line: " + line + " (topic: " + topic + ") incomment: " + str(inobj))
            if len(line.strip()) == 0:  # Skip blank lines
                continue

            # In an object?
            if inobj:
                if re.match(RE.objend, line):
                    # End the object.
                    if len(objname):
                        ast["objects"].append({
                            "name": objname,
                            "language": objlang,
                            "code": objbuf,
                        })
                    objname = ''
                    objlang = ''
                    objbuf  = []
                    inobj   = False
                else:
                    objbuf.append(line)
                continue

            line = line.strip()  # Trim excess space. We do it down here so we
                                 # don't mess up python objects!
            line = RE.ws.sub(" ", line)  # Replace the multiple whitespaces by single whitespace

            # Look for comments.
            if line[:2] == '//':  # A single-line comment.
                continue
            elif line[0] == '#':
                self.warn("Using the # symbol for comments is deprecated", filename, lineno)
            elif line[:2] == '/*':  # Start of a multi-line comment.
                if '*/' not in line:  # Cancel if the end is here too.
                    comment = True
                continue
            elif '*/' in line:
                comment = False
                continue
            if comment:
                continue

            # Separate the command from the data.
            if len(line) < 2:
                self.warn("Weird single-character line '" + line + "' found.", filename, lineno)
                continue
            cmd = line[0]
            line = line[1:].strip()

            # Ignore inline comments if there's a space before the // symbols.
            if " //" in line:
                line = line.split(" //")[0].strip()

            # Run a syntax check on this line.
            syntax_error = self.check_syntax(cmd, line)
            if syntax_error:
                # There was a syntax error! Are we enforcing strict mode?
                syntax_error = "Syntax error in " + filename + " line " + str(lineno) + ": " \
                    + syntax_error + " (near: " + cmd + " " + line + ")"
                if self.strict:
                    raise Exception(syntax_error)
                else:
                    self.warn(syntax_error)
                    return  # Don't try to continue

            # Reset the %Previous state if this is a new +Trigger.
            if cmd == '+':
                isThat = None

            # Do a lookahead for ^Continue and %Previous commands.
            for i in range(lp + 1, len(code)):
                lookahead = code[i].strip()
                if len(lookahead) < 2:
                    continue
                lookCmd = lookahead[0]
                lookahead = lookahead[1:].strip()
                lookahead = re.sub(RE.space, ' ', lookahead)  # Replace the `\s` in the message

                # Only continue if the lookahead line has any data.
                if len(lookahead) != 0:
                    # The lookahead command has to be either a % or a ^.
                    if lookCmd != '^' and lookCmd != '%':
                        break

                    # If the current command is a +, see if the following is
                    # a %.
                    if cmd == '+':
                        if lookCmd == '%':
                            isThat = lookahead
                            break
                        else:
                            isThat = None

                    # If the current command is a ! and the next command(s) are
                    # ^, we'll tack each extension on as a line break (which is
                    # useful information for arrays).
                    if cmd == '!':
                        if lookCmd == '^':
                            line += "<crlf>" + lookahead
                        continue

                    # If the current command is not a ^ and the line after is
                    # not a %, but the line after IS a ^, then tack it on to the
                    # end of the current line.
                    if cmd != '^' and lookCmd != '%':
                        if lookCmd == '^':
                            line += self.concat_modes.get(
                                local_options["concat"], ""
                            ) + lookahead
                        else:
                            break

            self.say("Command: " + cmd + "; line: " + line)

            # Handle the types of RiveScript commands.
            if cmd == '!':
                # ! DEFINE
                halves = re.split(RE.equals, line, 2)
                left = re.split(RE.ws, halves[0].strip(), 2)
                value, type, var = '', '', ''
                if len(halves) == 2:
                    value = halves[1].strip()
                if len(left) >= 1:
                    type = left[0].strip()
                    if len(left) >= 2:
                        var = ' '.join(left[1:]).strip()

                # Remove 'fake' line breaks unless this is an array.
                if type != 'array':
                    value = re.sub(RE.crlf, '', value)

                # Handle version numbers.
                if type == 'version':
                    # Verify we support it.
                    try:
                        if float(value) > rs_version:
                            self.warn("Unsupported RiveScript version. We only support " + rs_version, filename, lineno)
                            return
                    except:
                        self.warn("Error parsing RiveScript version number: not a number", filename, lineno)
                    continue

                # All other types of defines require a variable and value name.
                if len(var) == 0:
                    self.warn("Undefined variable name", filename, lineno)
                    continue
                elif len(value) == 0:
                    self.warn("Undefined variable value", filename, lineno)
                    continue

                # Handle the rest of the types.
                if type == 'local':
                    # Local file-scoped parser options.
                    self.say("\tSet parser option " + var + " = " + value)
                    local_options[var] = value
                elif type == 'global':
                    # 'Global' variables
                    self.say("\tSet global " + var + " = " + value)

                    if value == '<undef>':
                        try:
                            del(ast["begin"]["global"][var])
                        except:
                            self.warn("Failed to delete missing global variable", filename, lineno)
                    else:
                        ast["begin"]["global"][var] = value

                    # Handle flipping debug and depth vars.
                    if var == 'debug':
                        if value.lower() == 'true':
                            value = True
                        else:
                            value = False
                    elif var == 'depth':
                        try:
                            value = int(value)
                        except:
                            self.warn("Failed to set 'depth' because the value isn't a number!", filename, lineno)
                    elif var == 'strict':
                        if value.lower() == 'true':
                            value = True
                        else:
                            value = False
                elif type == 'var':
                    # Bot variables
                    self.say("\tSet bot variable " + var + " = " + value)

                    if value == '<undef>':
                        try:
                            del(ast["begin"]["var"][var])
                        except:
                            self.warn("Failed to delete missing bot variable", filename, lineno)
                    else:
                        ast["begin"]["var"][var] = value
                elif type == 'array':
                    # Arrays
                    self.say("\tArray " + var + " = " + value)

                    if value == '<undef>':
                        try:
                            del(ast["begin"]["array"][var])
                        except:
                            self.warn("Failed to delete missing array", filename, lineno)
                        continue

                    # Did this have multiple parts?
                    parts = value.split("<crlf>")

                    # Process each line of array data.
                    fields = []
                    for val in parts:
                        if '|' in val:
                            fields.extend(val.split('|'))
                        else:
                            fields.extend(re.split(RE.ws, val))

                    # Convert any remaining '\s' escape codes into spaces.
                    for f in fields:
                        f = f.replace('\s', ' ')

                    ast["begin"]["array"][var] = fields
                elif type == 'sub':
                    # Substitutions
                    self.say("\tSubstitution " + var + " => " + value)

                    if value == '<undef>':
                        try:
                            del(ast["begin"]["sub"][var])
                        except:
                            self.warn("Failed to delete missing substitution", filename, lineno)
                    else:
                        ast["begin"]["sub"][var] = value
                elif type == 'person':
                    # Person Substitutions
                    self.say("\tPerson Substitution " + var + " => " + value)

                    if value == '<undef>':
                        try:
                            del(ast["begin"]["person"][var])
                        except:
                            self.warn("Failed to delete missing person substitution", filename, lineno)
                    else:
                        ast["begin"]["person"][var] = value
                else:
                    self.warn("Unknown definition type '" + type + "'", filename, lineno)
            elif cmd == '>':
                # > LABEL
                temp = re.split(RE.ws, line)
                type   = temp[0]
                name   = ''
                fields = []
                if len(temp) >= 2:
                    name = temp[1]
                if len(temp) >= 3:
                    fields = temp[2:]

                # Handle the label types.
                if type == 'begin':
                    # The BEGIN block.
                    self.say("\tFound the BEGIN block.")
                    type = 'topic'
                    name = '__begin__'
                if type == 'topic':
                    # Starting a new topic.
                    self.say("\tSet topic to " + name)
                    curtrig = None
                    topic  = name

                    # Initialize the topic tree.
                    self._init_topic(ast["topics"], topic)

                    # Does this topic include or inherit another one?
                    mode = ''  # or 'inherits' or 'includes'
                    if len(fields) >= 2:
                        for field in fields:
                            if field == 'includes':
                                mode = 'includes'
                            elif field == 'inherits':
                                mode = 'inherits'
                            elif mode != '':
                                # This topic is either inherited or included.
                                if mode == 'includes':
                                    ast["topics"][name]["includes"][field] = 1
                                else:
                                    ast["topics"][name]["inherits"][field] = 1
                elif type == 'object':
                    # If a field was provided, it should be the programming
                    # language.
                    lang = None
                    if len(fields) > 0:
                        lang = fields[0].lower()

                    # Only try to parse a language we support.
                    curtrig = None
                    if lang is None:
                        self.warn("Trying to parse unknown programming language", filename, lineno)
                        lang = 'python'  # Assume it's Python.

                    # We have a handler, so start loading the code.
                    objname = name
                    objlang = lang
                    objbuf  = []
                    inobj   = True
                else:
                    self.warn("Unknown label type '" + type + "'", filename, lineno)
            elif cmd == '<':
                # < LABEL
                type = line

                if type == 'begin' or type == 'topic':
                    self.say("\tEnd topic label.")
                    topic = 'random'
                elif type == 'object':
                    self.say("\tEnd object label.")
                    inobj = False
            elif cmd == '+':
                # + TRIGGER
                self.say("\tTrigger pattern: " + line)

                # Initialize the topic tree.
                self._init_topic(ast["topics"], topic)
                curtrig = {
                    "trigger": line,
                    "reply": [],
                    "condition": [],
                    "redirect": None,
                    "previous": isThat,
                }
                ast["topics"][topic]["triggers"].append(curtrig)
            elif cmd == '-':
                # - REPLY
                if curtrig is None:
                    self.warn("Response found before trigger", filename, lineno)
                    continue

                self.say("\tResponse: " + line)
                curtrig["reply"].append(line.strip())
            elif cmd == '%':
                # % PREVIOUS
                pass  # This was handled above.
            elif cmd == '^':
                # ^ CONTINUE
                pass  # This was handled above.
            elif cmd == '@':
                # @ REDIRECT
                if curtrig is None:
                    self.warn("Redirect found before trigger", filename, lineno)
                    continue

                self.say("\tRedirect: " + line)
                curtrig["redirect"] = line.strip()
            elif cmd == '*':
                # * CONDITION
                if curtrig is None:
                    self.warn("Condition found before trigger", filename, lineno)
                    continue

                self.say("\tAdding condition: " + line)
                curtrig["condition"].append(line.strip())
            else:
                self.warn("Unrecognized command \"" + cmd + "\"", filename, lineno)
                continue

        return ast


Instruction:
Write a Python 3 script to
check the syntax of a line of RiveScript code.

Response:
def check_syntax(self, cmd, line):
        """Syntax check a line of RiveScript code.

        Args:
            str cmd: The command symbol for the line of code, such as one
                of ``+``, ``-``, ``*``, ``>``, etc.
            str line: The remainder of the line of code, such as the text of
                a trigger or reply.

        Return:
            str: A string syntax error message or ``None`` if no errors.
        """

        # Run syntax checks based on the type of command.
        if cmd == '!':
            # ! Definition
            #   - Must be formatted like this:
            #     ! type name = value
            #     OR
            #     ! type = value
            match = re.match(RE.def_syntax, line)
            if not match:
                return "Invalid format for !Definition line: must be '! type name = value' OR '! type = value'"
        elif cmd == '>':
            # > Label
            #   - The "begin" label must have only one argument ("begin")
            #   - "topic" labels must be lowercased but can inherit other topics (a-z0-9_\s)
            #   - "object" labels must follow the same rules as "topic", but don't need to be lowercase
            parts = re.split(" ", line, 2)
            if parts[0] == "begin" and len(parts) > 1:
                return "The 'begin' label takes no additional arguments, should be verbatim '> begin'"
            elif parts[0] == "topic":
                search = re.search(RE.name_syntax, line)
                if search:
                    return "Topics should be lowercased and contain only numbers and letters"
            elif parts[0] == "object":
                search = re.search(RE.obj_syntax, line) # Upper case is allowed
                if search:
                    return "Objects can only contain numbers and letters"
        elif cmd == '+' or cmd == '%' or cmd == '@':
            # + Trigger, % Previous, @ Redirect
            #   This one is strict. The triggers are to be run through the regexp engine,
            #   therefore it should be acceptable for the regexp engine.
            #   - Entirely lowercase
            #   - No symbols except: ( | ) [ ] * _ # @ { } < > =
            #   - All brackets should be matched
            #   - No empty option with pipe such as ||, [|, |], (|, |) and whitespace between
            parens = 0  # Open parenthesis
            square = 0  # Open square brackets
            curly  = 0  # Open curly brackets
            angle  = 0  # Open angled brackets

            # Count brackets.
            for char in line:
                if char == '(':
                    parens += 1
                elif char == ')':
                    parens -= 1
                elif char == '[':
                    square += 1
                elif char == ']':
                    square -= 1
                elif char == '{':
                    curly += 1
                elif char == '}':
                    curly -= 1
                elif char == '<':
                    angle += 1
                elif char == '>':
                    angle -= 1
                elif char == '|':
                    if parens == 0 and square == 0:   # Pipe outside the alternative and option
                        return "Pipe | must be within parenthesis brackets or square brackets"

                if (angle != 0) and (char in {"(", ")", "[", "]", "{", "}"}):
                    return "Angle bracket must be closed before closing or opening other type of brackets"

                total = parens + square + curly   # At each character, not more than 1 bracket opens, except <>
                for special_char_count in [parens, square, curly, angle, total]:
                    if special_char_count not in (0, 1):
                        return "Unbalanced brackets"

            # Any mismatches?
            if parens != 0:
                return "Unmatched parenthesis brackets"
            elif square != 0:
                return "Unmatched square brackets"
            elif curly != 0:
                return "Unmatched curly brackets"
            elif angle != 0:
                return "Unmatched angle brackets"

            # Check for empty pipe
            search = re.search(RE.empty_pipe, line)
            if search:
                return "Piped arrays can't include blank entries"

            # In UTF-8 mode, most symbols are allowed.
            if self.utf8:
                search = re.search(RE.utf8_trig, line)
                if search:
                    return "Triggers can't contain uppercase letters, backslashes or dots in UTF-8 mode."
            else:
                search = re.search(RE.trig_syntax, line)
                if search:
                    return "Triggers may only contain lowercase letters, numbers, and these symbols: ( | ) [ ] * _ # @ { } < > ="
        elif cmd == '-' or cmd == '^' or cmd == '/':
            # - Trigger, ^ Continue, / Comment
            # These commands take verbatim arguments, so their syntax is loose.
            pass
        elif cmd == '*':
            # * Condition
            #   Syntax for a conditional is as follows:
            #   * value symbol value => response
            match = re.match(RE.cond_syntax, line)
            if not match:
                return "Invalid format for !Condition: should be like '* value symbol value => response'"

        return None


Instruction:
How would you explain what the following Python 3 function does
def dump_to_response(request, app_label=None, exclude=None,
                     filename_prefix=None):
    """Utility function that dumps the given app/model to an HttpResponse.
    """
    app_label = app_label or []
    exclude = exclude
    try:
        filename = '%s.%s' % (datetime.now().isoformat(),
                              settings.SMUGGLER_FORMAT)
        if filename_prefix:
            filename = '%s_%s' % (filename_prefix, filename)
        if not isinstance(app_label, list):
            app_label = [app_label]
        response = serialize_to_response(app_label, exclude)
        response['Content-Disposition'] = 'attachment; filename=%s' % filename
        return response
    except CommandError as e:
        messages.error(
            request,
            _('An exception occurred while dumping data: %s') % force_text(e))
    return HttpResponseRedirect(request.build_absolute_uri().split('dump')[0])

Response: Utility function that dumps the given app and model to an HttpResponse.


Instruction:
Can you generate the documentation for the following Python 3 function
def dump_data(request):
    # Try to grab app_label data
    app_label = request.GET.get('app_label', [])
    if app_label:
        app_label = app_label.split(',')
    return dump_to_response(request, app_label=app_label,
                            exclude=settings.SMUGGLER_EXCLUDE_LIST)

Response: Exports data from whole project.


Instruction:
Create a Python 3 function to
export data from a model.

Response:
def dump_model_data(request, app_label, model_label):
    """Exports data from a model.
    """
    return dump_to_response(request, '%s.%s' % (app_label, model_label),
                            [], '-'.join((app_label, model_label)))


Instruction:
Create a Python 3 function for
yielding the items in the topological order of the items in the input dictionary.

Response:
def toposort(data):
    """Dependencies are expressed as a dictionary whose keys are items
and whose values are a set of dependent items. Output is a list of
sets in topological order. The first set consists of items with no
dependences, each subsequent set consists of items that depend upon
items in the preceeding sets.
"""

    # Special case empty input.
    if len(data) == 0:
        return

    # Copy the input so as to leave it unmodified.
    data = data.copy()

    # Ignore self dependencies.
    for k, v in data.items():
        v.discard(k)
    # Find all items that don't depend on anything.
    extra_items_in_deps = functools.reduce(
        set.union, data.values()
    ) - set(data.keys())

    # Add empty dependences where needed.
    data.update(dict((item, set()) for item in extra_items_in_deps))
    while True:
        ordered = set(item for item, dep in data.items() if len(dep) == 0)
        if not ordered:
            break
        yield ordered
        data = dict(
            (item, (dep - ordered))
            for item, dep in data.items()
            if item not in ordered
        )
    if len(data) != 0:
        raise ValueError(
            'Cyclic dependencies exist among these items: {}'
            .format(', '.join(repr(x) for x in data.items()))
        )


Instruction:
Can you generate a brief explanation for the following Python 3 code
def reorder_dag(sequence,
                depends_getter=lambda x: x.depends_on,
                name_getter=lambda x: x.app_name,
                impatience_max=100):
    """
    DAG = Directed Acyclic Graph
    If we have something like:
        C depends on B
        B depends on A
        A doesn't depend on any

    Given the order of [C, B, A] expect it to return [A, B, C]

    parameters:

        :sequence: some sort of iterable list

        :depends_getter: a callable that extracts the depends on sub-list

        :name_getter: a callable that extracts the name

        :impatience_max: a max count that is reached before we end up in
                         an infinite loop.
    """

    jobs = collections.defaultdict(list)
    map_ = {}
    _count_roots = 0
    for each in sequence:
        name = name_getter(each)
        depends_on = depends_getter(each)
        if depends_on is None:
            depends_on = []
        elif isinstance(depends_on, tuple):
            depends_on = list(depends_on)
        elif not isinstance(depends_on, list):
            depends_on = [depends_on]
        if not depends_on:
            _count_roots += 1
        jobs[name] += depends_on
        map_[name] = each

    if not _count_roots:
        raise CircularDAGError("No job is at the root")

    try:
        jobs = dict(zip(jobs.keys(), map(set, jobs.values())))
        ordered_jobs = list(toposort_flatten(jobs))
    except ValueError, e:
        raise CircularDAGError(e)

    return [map_[x] for x in ordered_jobs if x in map_]

Response: This function will reorder the list of items in the DAG.


Instruction:
Can you write a function in Python 3 where it
converts a frequency string into a number of seconds that a certain frequency string represents.

Response:
def convert_frequency(frequency):
    """return the number of seconds that a certain frequency string represents.
    For example: `1d` means 1 day which means 60 * 60 * 24 seconds.
    The recognized formats are:
        10d  : 10 days
        3m   : 3 minutes
        12h  : 12 hours
    """
    number = int(re.findall('\d+', frequency)[0])
    unit = re.findall('[^\d]+', frequency)[0]
    if unit == 'h':
        number *= 60 * 60
    elif unit == 'm':
        number *= 60
    elif unit == 'd':
        number *= 60 * 60 * 24
    elif unit:
        raise FrequencyDefinitionError(unit)
    return number


Instruction:
Write a Python 3 function that can
return a nicely formatted string of the time between d and now.

Response:
def timesince(d, now):
    """
    Taken from django.utils.timesince and modified to simpler requirements.

    Takes two datetime objects and returns the time between d and now
    as a nicely formatted string, e.g. "10 minutes". If d occurs after now,
    then "0 minutes" is returned.

    Units used are years, months, weeks, days, hours, and minutes.
    Seconds and microseconds are ignored. Up to two adjacent units will be
    displayed. For example, "2 weeks, 3 days" and "1 year, 3 months" are
    possible outputs, but "2 weeks, 3 hours" and "1 year, 5 days" are not.

    Adapted from
    http://web.archive.org/web/20060617175230/\
    http://blog.natbat.co.uk/archive/2003/Jun/14/time_since
    """
    def pluralize(a, b):
        def inner(n):
            if n == 1:
                return a % n
            return b % n
        return inner

    def ugettext(s):
        return s

    chunks = (
        (60 * 60 * 24 * 365, pluralize('%d year', '%d years')),
        (60 * 60 * 24 * 30, pluralize('%d month', '%d months')),
        (60 * 60 * 24 * 7, pluralize('%d week', '%d weeks')),
        (60 * 60 * 24, pluralize('%d day', '%d days')),
        (60 * 60, pluralize('%d hour', '%d hours')),
        (60, pluralize('%d minute', '%d minutes')),
        (0, pluralize('%d second', '%d seconds'))
    )
    # Convert datetime.date to datetime.datetime for comparison.
    if not isinstance(d, datetime.datetime):
        d = datetime.datetime(d.year, d.month, d.day)
    if now and not isinstance(now, datetime.datetime):
        now = datetime.datetime(now.year, now.month, now.day)

    delta = now - d
    # ignore microseconds
    since = delta.days * 24 * 60 * 60 + delta.seconds
    if since <= 0:
        # d is in the future compared to now, stop processing.
        # We'll use the last chunk (highest granularity)
        _, name = chunks[-1]
        return name(0)
    for i, (seconds, name) in enumerate(chunks):
        if seconds > 0:
            count = since // seconds
            if count != 0:
                break
        else:
            count = since

    result = name(count)
    if i + 1 < len(chunks):
        # Now get the second item
        seconds2, name2 = chunks[i + 1]
        if seconds2 > 0:
            count2 = (since - (seconds * count)) // seconds2
        else:
            count2 = since - (seconds * count)
        if count2 != 0:
            result += ugettext(', ') + name2(count2)

    return result


Instruction:
Implement a function in Python 3 to
return a named connection.

Response:
def connection(self, name=None):
        """return a named connection.

        This function will return a named connection by either finding one
        in its pool by the name or creating a new one.  If no name is given,
        it will use the name of the current executing thread as the name of
        the connection.

        parameters:
            name - a name as a string
        """
        if not name:
            name = self._get_default_connection_name()
        if name in self.pool:
            return self.pool[name]
        self.pool[name] = psycopg2.connect(self.dsn)
        return self.pool[name]


Instruction:
Can you tell what is the following Python 3 function doing
def close_connection(self, connection, force=False):
        if force:
            try:
                connection.close()
            except self.operational_exceptions:
                self.config.logger.error('ConnectionFactory - failed closing')
            for name, conn in self.pool.iteritems():
                if conn is connection:
                    break
            del self.pool[name]
        else:
            pass

Response:
overriding the baseclass function this routine will decline to
        close a connection at the end of a transaction context. This routine will close all open connections.


Instruction:
Implement a function in Python 3 to
raise the KeyboardInterrupt which will cause the app to effectively shutdown closing all the structures and resources and restart the app if restart is set to True

Response:
def respond_to_SIGHUP(signal_number, frame, logger=None):
    """raise the KeyboardInterrupt which will cause the app to effectively
    shutdown, closing all it resources.  Then, because it sets 'restart' to
    True, the app will reread all the configuration information, rebuild all
    of its structures and resources and start running again"""
    global restart
    restart = True
    if logger:
        logger.info('detected SIGHUP')
    raise KeyboardInterrupt


Instruction:
Write a Python 3 function that can
generate a series of integers used for the length of the sleep between retries. It produces after exhausting the list, it repeats the last value from the list forever. This generator will never raise the StopIteration exception.

Response:
def backoff_generator(self):
        """Generate a series of integers used for the length of the sleep
        between retries.  It produces after exhausting the list, it repeats
        the last value from the list forever.  This generator will never raise
        the StopIteration exception."""
        for x in self.config.backoff_delays:
            yield x
        while True:
            yield self.config.backoff_delays[-1]


Instruction:
Can you tell what is the following Python 3 function doing
def responsive_sleep(self, seconds, wait_reason=''):
        for x in xrange(int(seconds)):
            if (self.config.wait_log_interval and
                    not x % self.config.wait_log_interval):
                self.config.logger.debug(
                    '%s: %dsec of %dsec' % (wait_reason, x, seconds)
                )
            self.quit_check()
            time.sleep(1.0)

Response:
Sleep for the specified number of seconds logging every
        wait_log_interval seconds with progress info.


Instruction:
Here you have a function in Python 3, explain what it does
def as_backfill_cron_app(cls):
    #----------------------------------------------------------------------
    def main(self, function=None):
        return super(cls, self).main(
            function=function,
            once=False,
        )
    cls.main = main
    cls._is_backfill_app = True
    return cls

Response: a class decorator for Crontabber Apps that allows the base class to be a backfill CronApp.


Instruction:
Can you tell what is the following Python 3 function doing
def with_single_transaction(resource_name):
    transaction_executor_attr_name = "%s_transaction_executor" % resource_name

    def class_decorator(cls):
        def _run_proxy(self, *args, **kwargs):
            getattr(self, transaction_executor_attr_name)(
                self.run,
                *args,
                **kwargs
            )
        cls._run_proxy = _run_proxy
        return cls
    return class_decorator

Response: a class decorator for Crontabber Apps that returns a class that will be used in a single transaction.


Instruction:
Can you generate the documentation for the following Python 3 function
def check_time(value):
    try:
        h, m = value.split(':')
        h = int(h)
        m = int(m)
        if h >= 24 or h < 0:
            raise ValueError
        if m >= 60 or m < 0:
            raise ValueError
    except ValueError:
        raise TimeDefinitionError("Invalid definition of time %r" % value)

Response: check that it s a value like 03 : 45 or 1 : 1


Instruction:
Can you generate a brief explanation for the following Python 3 code
def keys(self):
        keys = []
        for app_name, __ in self.items():
            keys.append(app_name)
        return keys

Response: return a list of all app_names


Instruction:
Write a Python 3 script for
returning all the app_names and their values as tuples

Response:
def items(self):
        """return all the app_names and their values as tuples"""
        sql = """
            SELECT
                app_name,
                next_run,
                first_run,
                last_run,
                last_success,
                depends_on,
                error_count,
                last_error
            FROM crontabber"""
        columns = (
            'app_name',
            'next_run', 'first_run', 'last_run', 'last_success',
            'depends_on', 'error_count', 'last_error'
        )
        items = []
        for record in self.transaction_executor(execute_query_fetchall, sql):
            row = dict(zip(columns, record))
            items.append((row.pop('app_name'), row))
        return items


Instruction:
How would you explain what the following Python 3 function does
def values(self):
        values = []
        for __, data in self.items():
            values.append(data)
        return values

Response: return a list of all state values


Instruction:
How would you explain what the following Python 3 function does
def pop(self, key, default=_marker):
        try:
            popped = self[key]
            del self[key]
            return popped
        except KeyError:
            if default == _marker:
                raise
            return default

Response: remove the item by key returning the item with the specified key or default if no item is specified.


Instruction:
Can you generate the documentation for the following Python 3 function
def nagios(self, stream=sys.stdout):
        warnings = []
        criticals = []
        for class_name, job_class in self.config.crontabber.jobs.class_list:
            if job_class.app_name in self.job_state_database:
                info = self.job_state_database.get(job_class.app_name)
                if not info.get('error_count', 0):
                    continue
                error_count = info['error_count']
                # trouble!
                serialized = (
                    '%s (%s) | %s | %s' %
                    (job_class.app_name,
                     class_name,
                     info['last_error']['type'],
                     info['last_error']['value'])
                )
                if (
                    error_count == 1 and
                    hasattr(job_class, "_is_backfill_app")
                ):
                    # just a warning for now
                    warnings.append(serialized)
                else:
                    # anything worse than that is critical
                    criticals.append(serialized)

        if criticals:
            stream.write('CRITICAL - ')
            stream.write('; '.join(criticals))
            stream.write('\n')
            return 2
        elif warnings:
            stream.write('WARNING - ')
            stream.write('; '.join(warnings))
            stream.write('\n')
            return 1
        stream.write('OK - All systems nominal')
        stream.write('\n')
        return 0

Response: Print out the nagios of the current state.


Instruction:
How would you code a function in Python 3 to
remove the job from the state.

Response:
def reset_job(self, description):
        """remove the job from the state.
        if means that next time we run, this job will start over from scratch.
        """
        class_list = self.config.crontabber.jobs.class_list
        class_list = self._reorder_class_list(class_list)
        for class_name, job_class in class_list:
            if (
                job_class.app_name == description or
                description == job_class.__module__ + '.' + job_class.__name__
            ):
                if job_class.app_name in self.job_state_database:
                    self.config.logger.info('App reset')
                    self.job_state_database.pop(job_class.app_name)
                else:
                    self.config.logger.warning('App already reset')
                return
        raise JobNotFoundError(description)


Instruction:
Can you tell what is the following Python 3 function doing
def time_to_run(self, class_, time_):
        app_name = class_.app_name
        try:
            info = self.job_state_database[app_name]
        except KeyError:
            if time_:
                h, m = [int(x) for x in time_.split(':')]
                # only run if this hour and minute is < now
                now = utc_now()
                if now.hour > h:
                    return True
                elif now.hour == h and now.minute >= m:
                    return True
                return False
            else:
                # no past information, run now
                return True
        next_run = info['next_run']

        if not next_run:
            # It has never run before.
            # If it has an active ongoing status it means two
            # independent threads tried to start it. The second one
            # (by a tiny time margin) will have a job_class whose
            # `ongoing` value has already been set.
            # If that's the case, let it through because it will
            # commence and break due to RowLevelLockError in the
            # state's __setitem__ method.
            return bool(info['ongoing'])

        if next_run < utc_now():
            return True
        return False

Response: return true if the job is time to run the job.


Instruction:
Implement a function in Python 3 to
compare the list of configured jobs with the jobs in the state database

Response:
def audit_ghosts(self):
        """compare the list of configured jobs with the jobs in the state"""
        print_header = True
        for app_name in self._get_ghosts():
            if print_header:
                print_header = False
                print (
                    "Found the following in the state database but not "
                    "available as a configured job:"
                )
            print "\t%s" % (app_name,)


Instruction:
Here you have a function in Python 3, explain what it does
def export_js(
            self,
            js_module_name=JS_MODULE_NAME,
            js_template=JS_ES6_IMPORT_EXPORT_TEMPLATE,
            js_indent=JS_INDENTATION):
        '''Export the grammar to a JavaScript file which can be
        used with the js-lrparsing module.

        Two templates are available:
            Grammar.JS_WINDOW_TEMPLATE
            Grammar.JS_ES6_IMPORT_EXPORT_TEMPLATE (default)
        '''

        language = []
        refs = []
        classes = {'Grammar'}
        indent = 0
        cname = self.__class__.__name__ if 'import ' in js_template else None

        for name in self._order:
            elem = getattr(self, name, None)
            if not isinstance(elem, Element):
                continue
            if not hasattr(elem, '_export_js'):
                continue
            language.append('{indent}{var} {name} = {value};'.format(
                indent=js_indent,
                name=name,
                var='static' if cname else 'var',
                value=elem._export_js(js_indent, indent, classes, cname)))

        for name, ref in self._refs.items():
            refs.append(
                    '{pre}{name}.set({value});'
                    .format(
                        pre='{}.'.format(cname) if cname else js_indent,
                        name=name,
                        value=ref._element._export_js(
                            js_indent,
                            -1 if cname else indent,
                            classes,
                            cname)))

        if 'Rule' in classes:
            classes.remove('Rule')

        return js_template.format(
            name=self.__class__.__name__,
            indent=js_indent,
            js_module=js_module_name,
            datetime=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),
            language='\n'.join(language),
            refs='\n{}'.format('\n'.join(refs)),
            arguments=',\n'.join(map(lambda s:
                                     js_indent * 3 + s, classes)),
            re_keywords=self.RE_KEYWORDS.pattern.replace('\\', '\\\\'),
            classes=', '.join(classes),
            constructors=',\n'.join(
                map(lambda s: js_indent + s,
                    ['.'.join([
                        'window',
                        js_module_name, n]) for n in classes])))

Response: Exports the grammar to a JavaScript file which can be used with the js - lrparsing module.


Instruction:
Can you implement a function in Python 3 that
exports the grammar to a python file which can be used with the pyleri module. This can be used to auto - create a grammar and an export of the final result is required.

Response:
def export_py(
            self,
            py_module_name=PY_MODULE_NAME,
            py_template=PY_TEMPLATE,
            py_indent=PY_INDENTATION):
        '''Export the grammar to a python file which can be
        used with the pyleri module. This can be useful when python code
        if used to auto-create a grammar and an export of the final result is
        required.'''

        language = []
        classes = {'Grammar'}
        indent = 0

        for name in self._order:
            elem = getattr(self, name, None)
            if not isinstance(elem, Element):
                continue
            if not hasattr(elem, '_export_py'):
                continue
            language.append('{indent}{name} = {value}'.format(
                indent=py_indent,
                name=name,
                value=elem._export_py(py_indent, indent, classes)))

        for name, ref in self._refs.items():
            language.append(
                '{indent}{name} = {value}'
                .format(
                    indent=py_indent,
                    name=name,
                    value=ref._element._export_py(
                        py_indent,
                        indent,
                        classes)))

        return py_template.format(
            name=self.__class__.__name__,
            indent=py_indent,
            py_module=py_module_name,
            datetime=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),
            language='\n'.join(language),
            re_keywords=self.RE_KEYWORDS.pattern.replace('\\', '\\\\'),
            imports='\n'.join(
                map(lambda s: s, [
                    ' '.join(['from', py_module_name, 'import', n])
                    for n in classes if n != 'Rule'])))


Instruction:
Write a Python 3 function that can
export the grammar to a c file which can be used with the libcleri module.

Response:
def export_c(self, target=C_TARGET, c_indent=C_INDENTATION, headerf=None):
        '''Export the grammar to a c (source and header) file which can be
        used with the libcleri module.'''
        language = []
        indent = 0
        enums = set()
        for name in self._order:
            elem = getattr(self, name, None)
            if not isinstance(elem, Element):
                continue
            if not hasattr(elem, '_export_c'):
                continue
            language.append(
                '{indent}cleri_t * {name} = {value};'.format(
                    indent=c_indent,
                    name=name,
                    value=elem._export_c(c_indent, indent, enums)))

        for name, ref in self._refs.items():
            language.append(
                '{indent}cleri_ref_set({name}, {value});'
                .format(
                    indent=c_indent,
                    name=name,
                    value=ref._element._export_c(
                        c_indent,
                        indent,
                        enums,
                        ref)))

        pattern = self.RE_KEYWORDS.pattern.replace('\\', '\\\\')
        if not pattern.startswith('^'):
            pattern = '^' + pattern

        enums = ',\n'.join([
            '{}{}'.format(c_indent, gid)
            for gid in sorted(enums)]) + ','

        header_file = '"{}.h"'.format(target) if headerf is None else headerf

        fun = target.strip('/').replace('/', '_')

        return (self.__class__.C_TEMPLATE_C.format(
                    name=self.__class__.__name__,
                    target=target,
                    header_file=header_file,
                    fun=fun,
                    indent=c_indent,
                    datetime=time.strftime(
                        '%Y-%m-%d %H:%M:%S',
                        time.localtime()),
                    language='\n'.join(language),
                    re_keywords=pattern),
                self.__class__.C_TEMPLATE_H.format(
                    name=self.__class__.__name__,
                    target=target,
                    fun=fun,
                    guard=target.upper().replace('/', '_').replace('\\', '_'),
                    datetime=time.strftime(
                        '%Y-%m-%d %H:%M:%S',
                        time.localtime()),
                    language='\n'.join(language),
                    enums=enums))


Instruction:
Can you tell what is the following Python 3 function doing
def export_go(
            self,
            go_template=GO_TEMPLATE,
            go_indent=GO_INDENTATION,
            go_package=GO_PACKAGE):
        '''Export the grammar to a Go file which can be
        used with the goleri module.'''

        language = []
        enums = set()
        indent = 0
        pattern = self.RE_KEYWORDS.pattern.replace('`', '` + "`" + `')
        if not pattern.startswith('^'):
            pattern = '^' + pattern

        for name in self._order:
            elem = getattr(self, name, None)
            if not isinstance(elem, Element):
                continue
            if not hasattr(elem, '_export_go'):
                continue
            language.append('{indent}{name} := {value}'.format(
                indent=go_indent,
                name=camel_case(name),
                value=elem._export_go(go_indent, indent, enums)))

        for name, ref in self._refs.items():
            language.append(
                    '{indent}{name}.Set({value})'
                    .format(
                        indent=go_indent,
                        name=camel_case(name),
                        value=ref._element._export_go(
                            go_indent,
                            indent,
                            enums)))

        enums = ' = iota\n'.join([
            '{}{}'.format(go_indent, gid)
            for gid in sorted(enums)]) + ' = iota'

        return go_template.format(
            name=self.__class__.__name__,
            indent=go_indent,
            package=go_package,
            datetime=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),
            language='\n'.join(language),
            re_keywords=pattern,
            enums=enums)

Response:
Export the grammar to a Go file which can be
            used with the goleri module.


Instruction:
Can you create a Python 3 function that
exports the grammar to a Java file which can be used with the jleri module.

Response:
def export_java(
            self,
            java_template=JAVA_TEMPLATE,
            java_indent=JAVA_INDENTATION,
            java_package=JAVA_PACKAGE,
            is_public=True):
        '''Export the grammar to a Java file which can be
        used with the jleri module.'''

        language = []
        enums = set()
        classes = {'jleri.Grammar', 'jleri.Element'}

        refs = []
        indent = 0
        pattern = self.RE_KEYWORDS.pattern.replace('\\', '\\\\')
        if not pattern.startswith('^'):
            pattern = '^' + pattern

        for name in self._order:
            elem = getattr(self, name, None)
            if not isinstance(elem, Element):
                continue
            if not hasattr(elem, '_export_java'):
                continue
            language.append(
                '{indent}private static final Element {name} = {value};'
                .format(
                    indent=java_indent,
                    name=name.upper(),
                    value=elem._export_java(
                        java_indent, indent, enums, classes)))

        enum_str = ',\n'.join([
            '{indent}{indent}{gid}'.format(
                indent=java_indent,
                gid=gid)
            for gid in sorted(enums)])

        for name, ref in self._refs.items():
            refs.append(
                    '{indent}{indent}((Ref) {name}).set({value});'
                    .format(
                        indent=java_indent,
                        name=name.upper(),
                        value=ref._element._export_java(
                            java_indent,
                            -2,
                            enums,
                            classes)))
        return java_template.format(
            name=self.__class__.__name__,
            imports='\n'.join(
                map(lambda s: s, [
                    'import {};'.format(c)
                    for c in sorted(classes) if c != 'Rule'])),
            indent=java_indent,
            package='' if java_package is None
                    else 'package {};\n'.format(java_package),
            datetime=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),
            language='\n'.join(language),
            re_keywords=pattern,
            refs='' if not refs else '{}\n'.format('\n'.join(refs)),
            enums=enum_str,
            public='public ' if is_public else '')


Instruction:
Here you have a function in Python 3, explain what it does
def parse(self, string):
        '''Parse some string to the Grammar.

        Returns a nodeResult with the following attributes:
         - is_valid: True when the string is successfully parsed
                     by the Grammar.
         - pos: position in the string where parsing ended.
                (this is the end of the string when is_valid is True)
         - expecting: a list containing possible elements at position
                      'pos' in the string.
         - tree: the parse_tree containing a structured
                 result for the given string.
        '''
        self._string = string
        self._expecting = Expecting()
        self._cached_kw_match.clear()
        self._len_string = len(string)
        self._pos = None
        tree = Node(self._element, string, 0, self._len_string)
        node_res = Result(*self._walk(
            self._element,
            0,
            tree.children,
            self._element,
            True))

        # get rest if anything
        rest = self._string[node_res.pos:].lstrip()

        # set is_valid to False if we have 'rest' left.
        if node_res.is_valid and rest:
            node_res.is_valid = False

        # add end_of_statement to expecting if this is possible
        if not self._expecting.required and rest:
            self._expecting.set_mode_required(node_res.pos, True)
            self._expecting.update(end_of_statement, node_res.pos)

        node_res.expecting = self._expecting.get_expecting()

        # add expecting and correct pos to node_res if node_res is not valid
        if not node_res.is_valid:
            node_res.pos = self._expecting.pos

        node_res.tree = tree

        return node_res

Response: Parse some string to the Grammar.


Instruction:
Can you generate the documentation for the following Python 3 function
def figure_buffer(figs):
    '''Extract raw image buffer from matplotlib figure shaped as 1xHxWx3.'''
    assert len(figs) > 0, 'No figure buffers given. Forgot to return from draw call?'
    buffers = []
    w, h = figs[0].canvas.get_width_height()
    for f in figs:
        wf, hf = f.canvas.get_width_height()
        assert wf == w and hf == h, 'All canvas objects need to have same size'
        buffers.append(np.fromstring(f.canvas.tostring_rgb(), dtype=np.uint8).reshape(h, w, 3))

    return np.stack(buffers)

Response: Extract raw image buffer from matplotlib figure shaped as 1xHxWx3.


Instruction:
How would you explain what the following Python 3 function does
def figure_tensor(func, **tf_pyfunc_kwargs):
    '''Decorate matplotlib drawing routines.

    This dectorator is meant to decorate functions that return matplotlib
    figures. The decorated function has to have the following signature

        def decorated(*args, **kwargs) -> figure or iterable of figures

    where `*args` can be any positional argument and `**kwargs` are any
    keyword arguments. The decorated function returns a tensor of shape
    `[NumFigures, Height, Width, 3]` of type `tf.uint8`.

    The drawing code is invoked during running of TensorFlow sessions,
    at a time when all positional tensor arguments have been evaluated
    by the session. The decorated function is then passed the tensor values.
    All non tensor arguments remain unchanged.
    '''

    name = tf_pyfunc_kwargs.pop('name', func.__name__)

    @wraps(func)
    def wrapper(*func_args, **func_kwargs):
        tf_args = PositionalTensorArgs(func_args)

        def pyfnc_callee(*tensor_values, **unused):
            try:
                figs = as_list(func(*tf_args.mix_args(tensor_values), **func_kwargs))
                for f in figs:
                    f.canvas.draw()
                return figure_buffer(figs)
            except Exception:
                print('-'*5 + 'tfmpl catched exception' + '-'*5)
                print(traceback.format_exc())
                print('-'*20)
                raise

        return tf.py_func(pyfnc_callee, tf_args.tensor_args, tf.uint8, name=name, **tf_pyfunc_kwargs)
    return wrapper

Response: Decorate drawing routines. This dectorator is meant to decorate functions that return matplotlib figure objects.


Instruction:
Can you write a function in Python 3 where it
decorates matplotlib drawing routines that return a tensor of shape 2x2 array of size 3.

Response:
def blittable_figure_tensor(func, init_func, **tf_pyfunc_kwargs):
    '''Decorate matplotlib drawing routines with blitting support.

    This dectorator is meant to decorate functions that return matplotlib
    figures. The decorated function has to have the following signature

        def decorated(*args, **kwargs) -> iterable of artists

    where `*args` can be any positional argument and `**kwargs` are any
    keyword arguments. The decorated function returns a tensor of shape
    `[NumFigures, Height, Width, 3]` of type `tf.uint8`.

    Besides the actual drawing function, `blittable_figure_tensor` requires
    a `init_func` argument with the following signature

        def init(*args, **kwargs) -> iterable of figures, iterable of artists

    The init function is meant to create and initialize figures, as well as to
    perform drawing that is meant to be done only once. Any set of artits to be
    updated in later drawing calls should also be allocated in init. The
    initialize function must have the same positional and keyword arguments
    as the decorated function. It is called once before the decorated function
    is called.

    The drawing code / init function is invoked during running of TensorFlow
    sessions, at a time when all positional tensor arguments have been
    evaluated by the session. The decorated / init function is then passed the
    tensor values. All non tensor arguments remain unchanged.
    '''
    name = tf_pyfunc_kwargs.pop('name', func.__name__)
    assert callable(init_func), 'Init function not callable'

    @wraps(func)
    def wrapper(*func_args, **func_kwargs):
        figs = None
        bgs = None

        tf_args = PositionalTensorArgs(func_args)

        def pyfnc_callee(*tensor_values, **unused):

            try:
                nonlocal figs, bgs
                pos_args = tf_args.mix_args(tensor_values)

                if figs is None:
                    figs, artists = init_func(*pos_args, **func_kwargs)
                    figs = as_list(figs)
                    artists = as_list(artists)
                    for f in figs:
                        f.canvas.draw()
                    for a in artists:
                        a.set_animated(True)
                    bgs = [f.canvas.copy_from_bbox(f.bbox) for f in figs]

                artists = as_list(func(*pos_args, **func_kwargs))

                for f, bg in zip(figs, bgs):
                    f.canvas.restore_region(bg)
                for a in artists:
                    a.axes.draw_artist(a)
                for f in figs:
                    f.canvas.blit(f.bbox)

                return figure_buffer(figs)
            except Exception:
                print('-'*5 + 'tfmpl catched exception' + '-'*5)
                print(traceback.format_exc())
                print('-'*20)
                raise

        return tf.py_func(pyfnc_callee, tf_args.tensor_args, tf.uint8, name=name, **tf_pyfunc_kwargs)
    return wrapper


Instruction:
Here you have a function in Python 3, explain what it does
def draw_confusion_matrix(matrix):
    '''Draw confusion matrix for MNIST.'''
    fig = tfmpl.create_figure(figsize=(7,7))
    ax = fig.add_subplot(111)
    ax.set_title('Confusion matrix for MNIST classification')

    tfmpl.plots.confusion_matrix.draw(
        ax, matrix,
        axis_labels=['Digit ' + str(x) for x in range(10)],
        normalize=True
    )

    return fig

Response: Draw confusion matrix for MNIST classification.


Instruction:
Explain what the following Python 3 code does
def from_labels_and_predictions(labels, predictions, num_classes):
    '''Compute a confusion matrix from labels and predictions.

    A drop-in replacement for tf.confusion_matrix that works on CPU data
    and not tensors.

    Params
    ------
    labels : array-like
        1-D array of real labels for classification
    predicitions: array-like
        1-D array of predicted label classes
    num_classes: scalar
        Total number of classes

    Returns
    -------
    matrix : NxN array
        Array of shape [num_classes, num_classes] containing the confusion values.
    '''
    assert len(labels) == len(predictions)
    cm = np.zeros((num_classes, num_classes), dtype=np.int32)
    for i in range(len(labels)):
        cm[labels[i], predictions[i]] += 1
    return cm

Response: Compute a confusion matrix from labels and predictions.


Instruction:
Can you write a function in Python 3 where it
plots a confusion matrix.

Response:
def draw(ax, cm, axis_labels=None, normalize=False):
    '''Plot a confusion matrix.

    Inspired by
    https://stackoverflow.com/questions/41617463/tensorflow-confusion-matrix-in-tensorboard

    Params
    ------
    ax : axis
        Axis to plot on
    cm : NxN array
        Confusion matrix

    Kwargs
    ------
    axis_labels : array-like
        Array of size N containing axis labels
    normalize : bool
        Whether to plot counts or ratios.
    '''

    cm = np.asarray(cm)
    num_classes = cm.shape[0]

    if normalize:
        with np.errstate(invalid='ignore', divide='ignore'):
            cm = cm / cm.sum(1, keepdims=True)
        cm = np.nan_to_num(cm, copy=True)

    po = np.get_printoptions()
    np.set_printoptions(precision=2)

    ax.imshow(cm, cmap='Oranges')

    ticks = np.arange(num_classes)

    ax.set_xlabel('Predicted')
    ax.set_xticks(ticks)
    ax.xaxis.set_label_position('bottom')
    ax.xaxis.tick_bottom()

    ax.set_ylabel('Actual')
    ax.set_yticks(ticks)
    ax.yaxis.set_label_position('left')
    ax.yaxis.tick_left()

    if axis_labels is not None:
        ticklabels = [re.sub(r'([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))', r'\1 ', x) for x in axis_labels]
        ticklabels = ['\n'.join(wrap(l, 20)) for l in ticklabels]
        ax.set_xticklabels(ticklabels, rotation=-90,  ha='center')
        ax.set_yticklabels(ticklabels, va ='center')

    for i, j in product(range(num_classes), range(num_classes)):
        if cm[i,j] == 0:
            txt = '.'
        elif normalize:
            txt = '{:.2f}'.format(cm[i,j])
        else:
            txt = '{}'.format(cm[i,j])
        ax.text(j, i, txt, horizontalalignment="center", verticalalignment='center', color= "black", fontsize=7)

    np.set_printoptions(**po)


Instruction:
Can you generate the documentation for the following Python 3 function
def create_figure(*fig_args, **fig_kwargs):
    '''Create a single figure.

    Args and Kwargs are passed to `matplotlib.figure.Figure`.

    This routine is provided in order to avoid usage of pyplot which
    is stateful and not thread safe. As drawing routines in tf-matplotlib
    are called from py-funcs in their respective thread, avoid usage
    of pyplot where possible.
    '''

    fig = Figure(*fig_args, **fig_kwargs)
    # Attach canvas
    FigureCanvas(fig)
    return fig

Response: Create a single figure.


Instruction:
Given the following Python 3 function, write the documentation
def create_figures(n, *fig_args, **fig_kwargs):
    '''Create multiple figures.

    Args and Kwargs are passed to `matplotlib.figure.Figure`.

    This routine is provided in order to avoid usage of pyplot which
    is stateful and not thread safe. As drawing routines in tf-matplotlib
    are called from py-funcs in their respective thread, avoid usage
    of pyplot where possible.
    '''
    return [create_figure(*fig_args, **fig_kwargs) for _ in range(n)]

Response:
Create multiple figures.

    Args and Kwargs are passed to `matplotlib.figure.Figure`.

    This routine is provided in order to avoid usage of pyplot which
    is stateful and not thread safe. As drawing routines in tf-matplotlib
    are called from py-funcs in their respective thread, avoid usage
    of pyplot where possible.


Instruction:
Given the following Python 3 function, write the documentation
def vararg_decorator(f):
    '''Decorator to handle variable argument decorators.'''

    @wraps(f)
    def decorator(*args, **kwargs):
        if len(args) == 1 and len(kwargs) == 0 and callable(args[0]):
            return f(args[0])
        else:
            return lambda realf: f(realf, *args, **kwargs)

    return decorator

Response: Decorator to handle variable argument decorators.


Instruction:
Create a Python 3 function to
ensure x is of list type.

Response:
def as_list(x):
    '''Ensure `x` is of list type.'''

    if x is None:
        x = []
    elif not isinstance(x, Sequence):
        x = [x]
    return list(x)


Instruction:
Make a summary of the following Python 3 code
def all(self, page=1, per_page=10, order_by="latest"):
        return self._all("/photos", page=page, per_page=per_page, order_by=order_by)

Response: Get a single page from the list of all photos.


Instruction:
Can you create a Python 3 function that
gets a single page of the curated photos.

Response:
def curated(self, page=1, per_page=10, order_by="latest"):
        """
        Get a single page from the list of the curated photos (front-page’s photos).

        :param page [integer]: Page number to retrieve. (Optional; default: 1)
        :param per_page [integer]: Number of items per page. (Optional; default: 10)
        :param order_by [string]: How to sort the photos. Optional.
        (Valid values: latest, oldest, popular; default: latest)
        :return: [Array]: A single page of the curated Photo list.
        """
        return self._all("/photos/curated", page=page, per_page=per_page, order_by=order_by)


Instruction:
Implement a Python 3 function for
retrieving a single photo from the Unsplash API.

Response:
def get(self, photo_id, width=None, height=None, rect=None):
        """
        Retrieve a single photo.

        Note: Supplying the optional w or h parameters will result
        in the custom photo URL being added to the 'urls' object:

        :param photo_id [string]: The photo’s ID. Required.
        :param width [integer]: Image width in pixels.
        :param height [integer]: Image height in pixels.
        :param rect [string]: 4 comma-separated integers representing x, y, width, height of the cropped rectangle.
        :return: [Photo]: The Unsplash Photo.
        """
        url = "/photos/%s" % photo_id
        params = {
            "w": width,
            "h": height,
            "rect": rect
        }
        result = self._get(url, params=params)
        return PhotoModel.parse(result)


Instruction:
Explain what the following Python 3 code does
def search(self, query, category=None, orientation=None, page=1, per_page=10):
        if orientation and orientation not in self.orientation_values:
            raise Exception()
        params = {
            "query": query,
            "category": category,
            "orientation": orientation,
            "page": page,
            "per_page": per_page
        }
        url = "/photos/search"
        result = self._get(url, params=params)
        return PhotoModel.parse_list(result)

Response: Search for photos from a photo search.


Instruction:
Can you implement a function in Python 3 that
retrieves a random photo from the Unsplash API.

Response:
def random(self, count=1, **kwargs):
        """
        Retrieve a single random photo, given optional filters.

        Note: If supplying multiple category ID’s,
        the resulting photos will be those that
        match all of the given categories, not ones that match any category.

        Note: You can’t use the collections and query parameters in the same request

        Note: When supplying a count parameter
        - and only then - the response will be an array of photos,
        even if the value of count is 1.

        All parameters are optional, and can be combined to narrow
        the pool of photos from which a random one will be chosen.

        :param count [integer]: The number of photos to return. (Default: 1; max: 30)
        :param category: Category ID(‘s) to filter selection. If multiple, comma-separated. (deprecated)
        :param collections: Public collection ID(‘s) to filter selection. If multiple, comma-separated
        :param featured: Limit selection to featured photos.
        :param username: 	Limit selection to a single user.
        :param query: Limit selection to photos matching a search term.
        :param w: Image width in pixels.
        :param h: Image height in pixels.
        :param orientation: Filter search results by photo orientation.
        Valid values are landscape, portrait, and squarish.
        :return: [Array] or [Photo]: A single page of the curated Photo list or The Unsplash Photo. .
        :raise UnsplashError: If the given orientation is not in the default orientation values.
        """
        kwargs.update({"count": count})
        orientation = kwargs.get("orientation", None)
        if orientation and orientation not in self.orientation_values:
            raise Exception()
        url = "/photos/random"
        result = self._get(url, params=kwargs)
        return PhotoModel.parse_list(result)


Instruction:
Implement a Python 3 function for
retrieving a single photo s stats.

Response:
def stats(self, photo_id):
        """
        Retrieve a single photo’s stats.

        :param photo_id [string]: The photo’s ID. Required.
        :return: [Stat]: The Unsplash Stat.
        """
        url = "/photos/%s/stats" % photo_id
        result = self._get(url)
        return StatModel.parse(result)


Instruction:
Explain what the following Python 3 code does
def like(self, photo_id):
        url = "/photos/%s/like" % photo_id
        result = self._post(url)
        return PhotoModel.parse(result)

Response: Like a photo on behalf of the logged - in user.


Instruction:
Create a Python 3 function for
removing a user’s like of a photo. Note: This action is idempotent; sending the DELETE request to a single photo multiple times has no additional effect. :param photo_id [string]: The photo’s ID. Required. :return: [Photo]: The Unsplash Photo.

Response:
def unlike(self, photo_id):
        """
        Remove a user’s like of a photo.

        Note: This action is idempotent; sending the DELETE request
        to a single photo multiple times has no additional effect.

        :param photo_id [string]: The photo’s ID. Required.
        :return: [Photo]: The Unsplash Photo.
        """
        url = "/photos/%s/like" % photo_id
        result = self._delete(url)
        return PhotoModel.parse(result)


Instruction:
How would you explain what the following Python 3 function does
def photos(self, query, page=1, per_page=10):
        url = "/search/photos"
        data = self._search(url, query, page=page, per_page=per_page)
        data["results"] = PhotoModel.parse_list(data.get("results"))
        return data

Response: Get a single page of photo results for a query.


Instruction:
Implement a Python 3 function for
getting a single page of collection results for a query.

Response:
def collections(self, query, page=1, per_page=10):
        """
        Get a single page of collection results for a query.

        :param query [string]: Search terms.
        :param page [integer]: Page number to retrieve. (Optional; default: 1)
        :param per_page [integer]: Number of items per page. (Optional; default: 10)
        :return: [dict]: {u'total': 0, u'total_pages': 0, u'results': [Collection]}
        """
        url = "/search/collections"
        data = self._search(url, query, page=page, per_page=per_page)
        data["results"] = CollectionModel.parse_list(data.get("results"))
        return data


Instruction:
Explain what the following Python 3 code does
def users(self, query, page=1, per_page=10):
        url = "/search/users"
        data = self._search(url, query, page=page, per_page=per_page)
        data["results"] = UserModel.parse_list(data.get("results"))
        return data

Response: Get a single page of user results for a query.


Instruction:
Create a Python 3 function for
getting a single page from the list of all collections.

Response:
def all(self, page=1, per_page=10):
        """
        Get a single page from the list of all collections.

        :param page [integer]: Page number to retrieve. (Optional; default: 1)
        :param per_page [integer]: Number of items per page. (Optional; default: 10)
        :return: [Array]: A single page of the Collection list.
        """
        url = "/collections"
        result = self._all(url, page=page, per_page=per_page)
        return CollectionModel.parse_list(result)


Instruction:
Explain what the following Python 3 code does
def get(self, collection_id):
        url = "/collections/%s" % collection_id
        result = self._get(url)
        return CollectionModel.parse(result)

Response: Retrieve a single collection.


Instruction:
Can you tell what is the following Python 3 function doing
def get_curated(self, collection_id):
        url = "/collections/curated/%s" % collection_id
        result = self._get(url)
        return CollectionModel.parse(result)

Response: Retrieve a single curated collection.


Instruction:
Write a Python 3 function for
retrieving a collection s photos.

Response:
def photos(self, collection_id, page=1, per_page=10):
        """
        Retrieve a collection’s photos.

        :param collection_id [string]: The collection’s ID. Required.
        :param page [integer]: Page number to retrieve. (Optional; default: 1)
        :param per_page [integer]: Number of items per page. (Optional; default: 10)
        :return: [Array]: A single page of the Photo list.
        """
        url = "/collections/%s/photos" % collection_id
        result = self._all(url, page=page, per_page=per_page)
        return PhotoModel.parse_list(result)


Instruction:
Write a Python 3 script to
retrieve a list of collections related to this one.

Response:
def related(self, collection_id):
        """
        Retrieve a list of collections related to this one.

        :param collection_id [string]: The collection’s ID. Required.
        :return: [Array]: A single page of the Collection list.
        """
        url = "/collections/%s/related" % collection_id
        result = self._get(url)
        return CollectionModel.parse_list(result)


Instruction:
Implement a function in Python 3 to
create a new Unsplash Collection.

Response:
def create(self, title, description=None, private=False):
        """
        Create a new collection.
        This requires the 'write_collections' scope.

        :param title [string]: The title of the collection. (Required.)
        :param description [string]: The collection’s description. (Optional.)
        :param private [boolean]: Whether to make this collection private. (Optional; default false).
        :return: [Collection]: The Unsplash Collection.
        """
        url = "/collections"
        data = {
            "title": title,
            "description": description,
            "private": private
        }
        result = self._post(url, data=data)
        return CollectionModel.parse(result)


Instruction:
How would you explain what the following Python 3 function does
def update(self, collection_id, title=None, description=None, private=False):
        url = "/collections/%s" % collection_id
        data = {
            "title": title,
            "description": description,
            "private": private
        }
        result = self._put(url, data=data)
        return CollectionModel.parse(result)

Response: Update an existing Unsplash Collection.


Instruction:
Can you generate the documentation for the following Python 3 function
def add_photo(self, collection_id, photo_id):
        url = "/collections/%s/add" % collection_id
        data = {
            "collection_id": collection_id,
            "photo_id": photo_id
        }
        result = self._post(url, data=data) or {}
        return CollectionModel.parse(result.get("collection")), PhotoModel.parse(result.get("photo"))

Response: Adds a photo to one of the logged - in user s unsplash collections.


Instruction:
Given the following Python 3 function, write the documentation
def remove_photo(self, collection_id, photo_id):
        url = "/collections/%s/remove" % collection_id
        data = {
            "collection_id": collection_id,
            "photo_id": photo_id
        }
        result = self._delete(url, data=data) or {}
        return CollectionModel.parse(result.get("collection")), PhotoModel.parse(result.get("photo"))

Response: Removes a photo from one of the logged - in users s unsplash collections.


Instruction:
Given the following Python 3 function, write the documentation
def total(self):
        url = "/stats/total"
        result = self._get(url)
        return StatModel.parse(result)

Response: Get a list of counts for all of the Unsplash s stats


Instruction:
Write a Python 3 function for
getting the overall Unsplash stats for the past 30 days.

Response:
def month(self):
        """
        Get the overall Unsplash stats for the past 30 days.

        :return [Stat]: The Unsplash Stat.
        """
        url = "/stats/month"
        result = self._get(url)
        return StatModel.parse(result)


Instruction:
Can you implement a function in Python 3 that
gets the access token for the user.

Response:
def get_access_token(self, code):
        """
        Getting access token
        :param code [string]: The authorization code supplied to the callback by Unsplash.
        :return [string]: access token
        """
        self.token = self.oauth.fetch_token(
            token_url=self.access_token_url,
            client_id=self.client_id,
            client_secret=self.client_secret,
            scope=self.scope,
            code=code
        )
        return self.token.get("access_token")


Instruction:
Here you have a function in Python 3, explain what it does
def refresh_token(self):
        self.token = self.oauth.refresh_token(self.access_token_url, refresh_token=self.get_refresh_token())
        self.access_token = self.token.get("access_token")

Response: Refresh the current expired access token


Instruction:
Implement a Python 3 function for
parsing a list of JSON objects into a result set of model instances.

Response:
def parse_list(cls, data):
        """Parse a list of JSON objects into a result set of model instances."""
        results = ResultSet()
        data = data or []
        for obj in data:
            if obj:
                results.append(cls.parse(obj))
        return results


Instruction:
Can you generate the documentation for the following Python 3 function
def get_auth_header(self):
        if self.api.is_authenticated:
            return {"Authorization": "Bearer %s" % self.api.access_token}
        return {"Authorization": "Client-ID %s" % self.api.client_id}

Response: Returns the authorization header according to the authentication procedure


Instruction:
Can you write a function in Python 3 where it
gets the currently - logged in user.

Response:
def me(self):
        """
        Get the currently-logged in user.

        Note: To access a user’s private data,
        the user is required to authorize the 'read_user' scope.
        Without it, this request will return a '403 Forbidden' response.

        Note: Without a Bearer token (i.e. using a Client-ID token)
        this request will return a '401 Unauthorized' response.

        :return: [User]: The Unsplash User.
        """
        url = "/me"
        result = self._get(url)
        return UserModel.parse(result)


Instruction:
Given the following Python 3 function, write the documentation
def update(self, **kwargs):
        url = "/me"
        result = self._put(url, data=kwargs)
        return UserModel.parse(result)

Response: Update the currently - logged in user s local cache.


Instruction:
Explain what the following Python 3 code does
def get(self, username, width=None, height=None):
        url = "/users/{username}".format(username=username)
        params = {
            "w": width,
            "h": height
        }
        result = self._get(url, params=params)
        return UserModel.parse(result)

Response: Retrieve public details on a given user.


Instruction:
Here you have a function in Python 3, explain what it does
def portfolio(self, username):
        url = "/users/{username}/portfolio".format(username=username)
        result = self._get(url)
        return LinkModel.parse(result)

Response: Retrieve a single user s portfolio link.


Instruction:
Create a Python 3 function for
getting a list of photos uploaded by a user.

Response:
def photos(self, username, page=1, per_page=10, order_by="latest"):
        """
        Get a list of photos uploaded by a user.

        :param username [string]: The user’s username. Required.
        :param page [integer]: Page number to retrieve. (Optional; default: 1)
        :param per_page [integer]: Number of items per page. (Optional; default: 10)
        :param order_by [string]: How to sort the photos. Optional.
        (Valid values: latest, oldest, popular; default: latest)
        :return: [Array]: A single page of the Photo list.
        """
        url = "/users/{username}/photos".format(username=username)
        result = self._photos(url, username, page=page, per_page=per_page, order_by=order_by)
        return PhotoModel.parse_list(result)


Instruction:
Can you tell what is the following Python 3 function doing
def collections(self, username, page=1, per_page=10):
        url = "/users/{username}/collections".format(username=username)
        params = {
            "page": page,
            "per_page": per_page
        }
        result = self._get(url, params=params)
        return CollectionModel.parse_list(result)

Response: Get a list of collections created by the user.


Instruction:
Write a Python 3 function for
returning a partition of the string based on width

Response:
def wrap(self, width):
        """Returns a partition of the string based on `width`"""
        res = []
        prev_state = set()
        part = []
        cwidth = 0
        for char, _width, state in zip(self._string, self._width, self._state):
            if cwidth + _width > width:
                if prev_state:
                    part.append(self.ANSI_RESET)
                res.append("".join(part))
                prev_state = set()
                part = []
                cwidth = 0
            cwidth += _width
            if prev_state == state:
                pass
            elif prev_state <= state:
                part.extend(state - prev_state)
            else:
                part.append(self.ANSI_RESET)
                part.extend(state)
            prev_state = state
            part.append(char)
        if prev_state:
            part.append(self.ANSI_RESET)
        if part:
            res.append("".join(part))
        return res


Instruction:
Explain what the following Python 3 code does
def max_table_width(self):
        offset = ((self._column_count - 1)
                  * termwidth(self.column_separator_char))
        offset += termwidth(self.left_border_char)
        offset += termwidth(self.right_border_char)
        self._max_table_width = max(self._max_table_width,
                                    offset + self._column_count)
        return self._max_table_width

Response: get or set the maximum width of the table.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _initialize_table(self, column_count):
        header = [''] * column_count
        alignment = [self.default_alignment] * column_count
        width = [0] * column_count
        padding = [self.default_padding] * column_count

        self._column_count = column_count
        self._column_headers = HeaderData(self, header)
        self._column_alignments = AlignmentMetaData(self, alignment)
        self._column_widths = PositiveIntegerMetaData(self, width)
        self._left_padding_widths = PositiveIntegerMetaData(self, padding)
        self._right_padding_widths = PositiveIntegerMetaData(self, padding)

Response: Initializes the internal table data structures.


Instruction:
How would you code a function in Python 3 to
set the style of the table from a predefined set of styles.

Response:
def set_style(self, style):
        """Set the style of the table from a predefined set of styles.

        Parameters
        ----------
        style: Style

            It can be one of the following:

            * beautifulTable.STYLE_DEFAULT
            * beautifultable.STYLE_NONE
            * beautifulTable.STYLE_DOTTED
            * beautifulTable.STYLE_MYSQL
            * beautifulTable.STYLE_SEPARATED
            * beautifulTable.STYLE_COMPACT
            * beautifulTable.STYLE_MARKDOWN
            * beautifulTable.STYLE_RESTRUCTURED_TEXT
            * beautifultable.STYLE_BOX
            * beautifultable.STYLE_BOX_DOUBLED
            * beautifultable.STYLE_BOX_ROUNDED
            * beautifultable.STYLE_GRID
        """
        if not isinstance(style, enums.Style):
            allowed = ("{}.{}".format(type(self).__name__, i.name)
                       for i in enums.Style)
            error_msg = ("allowed values for style are: "
                         + ', '.join(allowed))
            raise ValueError(error_msg)
        style_template = style.value
        self.left_border_char = style_template.left_border_char
        self.right_border_char = style_template.right_border_char
        self.top_border_char = style_template.top_border_char
        self.bottom_border_char = style_template.bottom_border_char
        self.header_separator_char = style_template.header_separator_char
        self.column_separator_char = style_template.column_separator_char
        self.row_separator_char = style_template.row_separator_char
        self.intersect_top_left = style_template.intersect_top_left
        self.intersect_top_mid = style_template.intersect_top_mid
        self.intersect_top_right = style_template.intersect_top_right
        self.intersect_header_left = style_template.intersect_header_left
        self.intersect_header_mid = style_template.intersect_header_mid
        self.intersect_header_right = style_template.intersect_header_right
        self.intersect_row_left = style_template.intersect_row_left
        self.intersect_row_mid = style_template.intersect_row_mid
        self.intersect_row_right = style_template.intersect_row_right
        self.intersect_bottom_left = style_template.intersect_bottom_left
        self.intersect_bottom_mid = style_template.intersect_bottom_mid
        self.intersect_bottom_right = style_template.intersect_bottom_right


Instruction:
Write a Python 3 script for
calculating width of column automatically based on data.

Response:
def _calculate_column_widths(self):
        """Calculate width of column automatically based on data."""
        table_width = self.get_table_width()
        lpw, rpw = self._left_padding_widths, self._right_padding_widths
        pad_widths = [(lpw[i] + rpw[i]) for i in range(self._column_count)]
        max_widths = [0 for index in range(self._column_count)]
        offset = table_width - sum(self._column_widths) + sum(pad_widths)
        self._max_table_width = max(self._max_table_width,
                                    offset + self._column_count)

        for index, column in enumerate(zip(*self._table)):
            max_length = 0
            for i in column:
                for j in to_unicode(i).split('\n'):
                    output_str = get_output_str(j, self.detect_numerics,
                                                self.numeric_precision,
                                                self.sign_mode.value)
                    max_length = max(max_length, termwidth(output_str))
            for i in to_unicode(self._column_headers[index]).split('\n'):
                output_str = get_output_str(i, self.detect_numerics,
                                            self.numeric_precision,
                                            self.sign_mode.value)
                max_length = max(max_length, termwidth(output_str))
            max_widths[index] += max_length

        sum_ = sum(max_widths)
        desired_sum = self._max_table_width - offset

        # Set flag for columns who are within their fair share
        temp_sum = 0
        flag = [0] * len(max_widths)
        for i, width in enumerate(max_widths):
            if width <= int(desired_sum / self._column_count):
                temp_sum += width
                flag[i] = 1
            else:
                # Allocate atleast 1 character width to the column
                temp_sum += 1

        avail_space = desired_sum - temp_sum
        actual_space = sum_ - temp_sum
        shrinked_columns = {}

        # Columns which exceed their fair share should be shrinked based on
        # how much space is left for the table
        for i, width in enumerate(max_widths):
            self.column_widths[i] = width
            if not flag[i]:
                new_width = 1 + int((width-1) * avail_space / actual_space)
                if new_width < width:
                    self.column_widths[i] = new_width
                    shrinked_columns[new_width] = i

        # Divide any remaining space among shrinked columns
        if shrinked_columns:
            extra = (self._max_table_width
                     - offset
                     - sum(self.column_widths))
            actual_space = sum(shrinked_columns)

            if extra > 0:
                for i, width in enumerate(sorted(shrinked_columns)):
                    index = shrinked_columns[width]
                    extra_width = int(width * extra / actual_space)
                    self.column_widths[i] += extra_width
                    if i == (len(shrinked_columns) - 1):
                        extra = (self._max_table_width
                                 - offset
                                 - sum(self.column_widths))
                        self.column_widths[index] += extra

        for i in range(self.column_count):
            self.column_widths[i] += pad_widths[i]


Instruction:
Can you generate the documentation for the following Python 3 function
def sort(self, key, reverse=False):
        if isinstance(key, int):
            index = key
        elif isinstance(key, basestring):
            index = self.get_column_index(key)
        else:
            raise TypeError("'key' must either be 'int' or 'str'")
        self._table.sort(key=operator.itemgetter(index), reverse=reverse)

Response: Stable sort of the table with respect to a column.


Instruction:
Create a Python 3 function for
getting the index of a column from it s header.

Response:
def get_column_index(self, header):
        """Get index of a column from it's header.

        Parameters
        ----------
        header: str
            header of the column.

        Raises
        ------
        ValueError:
            If no column could be found corresponding to `header`.
        """
        try:
            index = self._column_headers.index(header)
            return index
        except ValueError:
            raise_suppressed(KeyError(("'{}' is not a header for any "
                                       "column").format(header)))


Instruction:
How would you explain what the following Python 3 function does
def get_column(self, key):
        if isinstance(key, int):
            index = key
        elif isinstance(key, basestring):
            index = self.get_column_index(key)
        else:
            raise TypeError(("key must be an int or str, "
                             "not {}").format(type(key).__name__))
        return iter(map(operator.itemgetter(index), self._table))

Response: Return an iterator to a column.


Instruction:
Write a Python 3 function for
removing and return a column from the table.

Response:
def pop_column(self, index=-1):
        """Remove and return row at index (default last).

        Parameters
        ----------
        index : int, str
            index of the column, or the header of the column.
            If index is specified, then normal list rules apply.

        Raises
        ------
        TypeError:
            If index is not an instance of `int`, or `str`.

        IndexError:
            If Table is empty.
        """
        if isinstance(index, int):
            pass
        elif isinstance(index, basestring):
            index = self.get_column_index(index)
        else:
            raise TypeError(("column index must be an integer or a string, "
                             "not {}").format(type(index).__name__))
        if self._column_count == 0:
            raise IndexError("pop from empty table")
        if self._column_count == 1:
            # This is the last column. So we should clear the table to avoid
            # empty rows
            self.clear(clear_metadata=True)
        else:
            # Not the last column. safe to pop from row
            self._column_count -= 1
            self._column_alignments._pop(index)
            self._column_widths._pop(index)
            self._left_padding_widths._pop(index)
            self._right_padding_widths._pop(index)
            self._column_headers._pop(index)
            for row in self._table:
                row._pop(index)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def insert_row(self, index, row):
        row = self._validate_row(row)
        row_obj = RowData(self, row)
        self._table.insert(index, row_obj)

Response: Insert a row before index in the table.


Instruction:
How would you implement a function in Python 3 that
updates a column named header in the table.

Response:
def update_row(self, key, value):
        """Update a column named `header` in the table.

        If length of column is smaller than number of rows, lets say
        `k`, only the first `k` values in the column is updated.

        Parameters
        ----------
        key : int or slice
            index of the row, or a slice object.

        value : iterable
            If an index is specified, `value` should be an iterable
            of appropriate length. Instead if a slice object is
            passed as key, value should be an iterable of rows.

        Raises
        ------
        IndexError:
            If index specified is out of range.

        TypeError:
            If `value` is of incorrect type.

        ValueError:
            If length of row does not matches number of columns.
        """
        if isinstance(key, int):
            row = self._validate_row(value, init_table_if_required=False)
            row_obj = RowData(self, row)
            self._table[key] = row_obj
        elif isinstance(key, slice):
            row_obj_list = []
            for row in value:
                row_ = self._validate_row(row, init_table_if_required=True)
                row_obj_list.append(RowData(self, row_))
            self._table[key] = row_obj_list
        else:
            raise TypeError("key must be an integer or a slice object")


Instruction:
Can you tell what is the following Python 3 function doing
def update_column(self, header, column):
        index = self.get_column_index(header)
        if not isinstance(header, basestring):
            raise TypeError("header must be of type str")
        for row, new_item in zip(self._table, column):
            row[index] = new_item

Response: Update a column in the table with the given header.