Instruction:
Can you implement a function in Python 3 that
opens MATLAB using specified connection or DCOM + protocol on Windows where matlab_location is set.

Response:
def open():
    global _MATLAB_RELEASE
    '''Opens MATLAB using specified connection (or DCOM+ protocol on Windows)where matlab_location  '''
    if is_win:
        ret = MatlabConnection()
        ret.open()
        return ret
    else:
        if settings.MATLAB_PATH != 'guess':
            matlab_path = settings.MATLAB_PATH + '/bin/matlab'
        elif _MATLAB_RELEASE != 'latest':
            matlab_path = discover_location(_MATLAB_RELEASE)
        else:
            # Latest release is found in __init__.by, i.e. higher logical level
            raise MatlabReleaseNotFound('Please select a matlab release or set its location.')
        try:
            ret = MatlabConnection(matlab_path)
            ret.open()
        except Exception:
            #traceback.print_exc(file=sys.stderr)
            raise MatlabReleaseNotFound('Could not open matlab, is it in %s?' % matlab_path)
        return ret


Instruction:
How would you explain what the following Python 3 function does
def _list_releases():
    '''
    Tries to guess matlab process release version and location path on
    osx machines.

    The paths we will search are in the format:
    /Applications/MATLAB_R[YEAR][VERSION].app/bin/matlab
    We will try the latest version first. If no path is found, None is reutrned.
    '''
    if is_linux():
        base_path = '/usr/local/MATLAB/R%d%s/bin/matlab'
    else:
        # assume mac
        base_path = '/Applications/MATLAB_R%d%s.app/bin/matlab'
    years = range(2050,1990,-1)
    release_letters = ('h', 'g', 'f', 'e', 'd', 'c', 'b', 'a')
    for year in years:
        for letter in release_letters:
            release = 'R%d%s' % (year, letter)
            matlab_path = base_path % (year, letter)
            if os.path.exists(matlab_path):
                yield (release, matlab_path)

Response: Yields a list of all available MATLAB process releases and location paths.


Instruction:
How would you explain what the following Python 3 function does
def is_valid_release_version(version):
    '''Checks that the given version code is valid.'''
    return version is not None and len(version) == 6 and version[0] == 'R' \
            and int(version[1:5]) in range(1990, 2050) \
            and version[5] in ('h', 'g', 'f', 'e', 'd', 'c', 'b', 'a')

Response: Checks that the given version code is valid.


Instruction:
Given the following Python 3 function, write the documentation
def find_matlab_version(process_path):
    bin_path = os.path.dirname(process_path)
    matlab_path = os.path.dirname(bin_path)
    matlab_dir_name = os.path.basename(matlab_path)
    version = matlab_dir_name
    if not is_linux():
        version = matlab_dir_name.replace('MATLAB_', '').replace('.app', '')
    if not is_valid_release_version(version):
        return None
    return version

Response: Tries to guess matlab s version according to its process path.


Instruction:
How would you explain what the following Python 3 function does
def open(self, print_matlab_welcome=False):
        '''Opens the matlab process.'''
        if self.process and not self.process.returncode:
            raise MatlabConnectionError('Matlab(TM) process is still active. Use close to '
                                            'close it')
        self.process = subprocess.Popen(
                [self.matlab_process_path, '-nojvm', '-nodesktop'],
                stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        flags = fcntl.fcntl(self.process.stdout, fcntl.F_GETFL)
        fcntl.fcntl(self.process.stdout, fcntl.F_SETFL, flags| os.O_NONBLOCK)

        if print_matlab_welcome:
            self._sync_output()
        else:
            self._sync_output(None)

Response: Opens the matlab process.


Instruction:
Can you write a function in Python 3 where it
evaluates a matlab expression synchronously.

Response:
def eval(self,
                     expression,
                     identify_errors=True,
                     print_expression=True,
                     on_new_output=sys.stdout.write):
        """ Evaluates a matlab expression synchronously.

        If identify_erros is true, and the last output line after evaluating the
        expressions begins with '???' and excpetion is thrown with the matlab error
        following the '???'.
        If on_new_output is not None, it will be called whenever a new output is
        encountered. The default value prints the new output to the screen.
        The return value of the function is the matlab output following the call.
        """
        self._check_open()
        if print_expression:
            print expression
        self.process.stdin.write(expression)
        self.process.stdin.write('\n')
        ret = self._sync_output(on_new_output)
        # TODO(dani): Use stderr to identify errors.
        if identify_errors and ret.rfind('???') != -1:
            begin = ret.rfind('???') + 4
            end = ret.find('\n', begin)
            raise MatlabError(ret[begin:end])
        return ret


Instruction:
Explain what the following Python 3 code does
def put(self, name_to_val, oned_as='row', on_new_output=None):
        self._check_open()
        # We can't give stdin to mlabio.savemat because it needs random access :(
        temp = StringIO()
        mlabio.savemat(temp, name_to_val, oned_as=oned_as)
        temp.seek(0)
        temp_str = temp.read()
        temp.close()
        self.process.stdin.write('load stdio;\n')
        self._read_until('ack load stdio\n', on_new_output=on_new_output)
        self.process.stdin.write(temp_str)
        #print 'sent %d kb' % (len(temp_str) / 1024)
        self._read_until('ack load finished\n', on_new_output=on_new_output)
        self._sync_output(on_new_output=on_new_output)

Response: Loads a dictionary of variable names into the matlab shell.


Instruction:
Here you have a function in Python 3, explain what it does
def get(self,
                    names_to_get,
                    extract_numpy_scalars=True,
                    on_new_output=None):
        """ Loads the requested variables from the matlab shell.

        names_to_get can be either a variable name, a list of variable names, or
        None.
        If it is a variable name, the values is returned.
        If it is a list, a dictionary of variable_name -> value is returned.
        If it is None, a dictionary with all variables is returned.

        If extract_numpy_scalars is true, the method will convert numpy scalars
        (0-dimension arrays) to a regular python variable.
        """
        self._check_open()
        single_item = isinstance(names_to_get, (unicode, str))
        if single_item:
            names_to_get = [names_to_get]
        if names_to_get == None:
            self.process.stdin.write('save stdio;\n')
        else:
            # Make sure that we throw an excpetion if the names are not defined.
            for name in names_to_get:
                self.eval('%s;' % name, print_expression=False, on_new_output=on_new_output)
            #print 'save(\'stdio\', \'%s\');\n' % '\', \''.join(names_to_get)
            self.process.stdin.write(
                "save('stdio', '%s', '-v7');\n" % '\', \''.join(names_to_get))
        # We have to read to a temp buffer because mlabio.loadmat needs
        # random access :(
        self._read_until('start_binary\n', on_new_output=on_new_output)
        #print 'got start_binary'
        temp_str = self._sync_output(on_new_output=on_new_output)
        #print 'got all outout'
        # Remove expected output and "\n>>"
        # TODO(dani): Get rid of the unecessary copy.
        # MATLAB 2010a adds an extra >> so we need to remove more spaces.
        if self.matlab_version == (2010, 'a'):
            temp_str = temp_str[:-len(self.expected_output_end)-6]
        else:
            temp_str = temp_str[:-len(self.expected_output_end)-3]
        temp = StringIO(temp_str)
        #print ('____')
        #print len(temp_str)
        #print ('____')
        ret = mlabio.loadmat(temp, chars_as_strings=True, squeeze_me=True)
        #print '******'
        #print ret
        #print '******'
        temp.close()
        if single_item:
            return ret.values()[0]
        for key in ret.iterkeys():
            while ret[key].shape and ret[key].shape[-1] == 1:
                ret[key] = ret[key][0]
            if extract_numpy_scalars:
                if isinstance(ret[key], np.ndarray) and not ret[key].shape:
                    ret[key] = ret[key].tolist()
        #print 'done'
        return ret

Response: Loads the requested variables from the matlab shell.


Instruction:
Write a Python 3 script to
return the named groups in a regular expression compiled or as string

Response:
def rexGroups(rex):
    """Return the named groups in a regular expression (compiled or as string)
    in occuring order.

    >>> rexGroups(r'(?P<name>\w+) +(?P<surname>\w+)')
    ('name', 'surname')

    """
    if isinstance(rex,basestring): rex = re.compile(rex)
    return zip(*sorted([(n,g) for (g,n) in rex.groupindex.items()]))[1]


Instruction:
Can you generate the documentation for the following Python 3 function
def div(a,b):
    res, fail = divmod(a,b)
    if fail:
        raise ValueError("%r does not divide %r" % (b,a))
    else:
        return res

Response: divide two lists of items


Instruction:
Given the following Python 3 function, write the documentation
def ipshuffle(l, random=None):
    import random as _random
    _random.shuffle(l, random)
    return l

Response: r Shuffle list l inplace and return it.


Instruction:
Write a Python 3 function that can
pass file to func and ensure the file is closed afterwards.

Response:
def withFile(file, func, mode='r', expand=False):
    """Pass `file` to `func` and ensure the file is closed afterwards. If
       `file` is a string, open according to `mode`; if `expand` is true also
       expand user and vars.
    """
    file = _normalizeToFile(file, mode=mode, expand=expand)
    try:      return func(file)
    finally:  file.close()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def slurpLines(file, expand=False):
    file = _normalizeToFile(file, "r", expand)
    try:     return file.readlines()
    finally: file.close()

Response: r Read in a complete file and return a list of lines.


Instruction:
Can you tell what is the following Python 3 function doing
def slurpChompedLines(file, expand=False):
    f=_normalizeToFile(file, "r", expand)
    try: return list(chompLines(f))
    finally: f.close()

Response: r Return file a list of chomped lines. See slurpLines.


Instruction:
Create a Python 3 function to
create a new tempfile write s to it and return the filename.

Response:
def strToTempfile(s, suffix=None, prefix=None, dir=None, binary=False):
    """Create a new tempfile, write ``s`` to it and return the filename.
    `suffix`, `prefix` and `dir` are like in `tempfile.mkstemp`.
    """
    fd, filename = tempfile.mkstemp(**dict((k,v) for (k,v) in
                                           [('suffix',suffix),('prefix',prefix),('dir', dir)]
                                           if v is not None))
    spitOut(s, fd, binary)
    return filename


Instruction:
Given the following Python 3 function, write the documentation
def spitOutLines(lines, file, expand=False):
    file = _normalizeToFile(file, mode="w", expand=expand)
    try:     file.writelines(lines)
    finally: file.close()

Response: r Write all the lines to file.


Instruction:
Can you generate the documentation for the following Python 3 function
def readProcess(cmd, *args):
    import popen2
    BUFSIZE=1024
    import select
    popen = popen2.Popen3((cmd,) + args, capturestderr=True)
    which = {id(popen.fromchild): [],
             id(popen.childerr):  []}
    select = Result(select.select)
    read   = Result(os.read)
    try:
        popen.tochild.close() # XXX make sure we're not waiting forever
        while select([popen.fromchild, popen.childerr], [], []):
            readSomething = False
            for f in select.result[0]:
                while read(f.fileno(), BUFSIZE):
                    which[id(f)].append(read.result)
                    readSomething = True
            if not readSomething:
                break
        out, err = ["".join(which[id(f)])
                    for f in [popen.fromchild, popen.childerr]]
        returncode = popen.wait()

        if os.WIFEXITED(returncode):
            exit = os.WEXITSTATUS(returncode)
        else:
            exit = returncode or 1 # HACK: ensure non-zero
    finally:
        try:
            popen.fromchild.close()
        finally:
            popen.childerr.close()
    return out or "", err or "", exit

Response: r Similar to os. popen2. popen3 but returns 2 strings stdout and exit code.


Instruction:
Can you generate the documentation for the following Python 3 function
def runProcess(cmd, *args):
    from os import spawnvp, P_WAIT
    return spawnvp(P_WAIT, cmd, (cmd,) + args)

Response: Run a process in the executable path with the given args and return the exit status.


Instruction:
How would you code a function in Python 3 to
like os. path. splitext but does not treat dotfiles as extensions.

Response:
def splitext(p):
    r"""Like the normal splitext (in posixpath), but doesn't treat dotfiles
    (e.g. .emacs) as extensions. Also uses os.sep instead of '/'."""

    root, ext = os.path.splitext(p)
    # check for dotfiles
    if (not root or root[-1] == os.sep): # XXX: use '/' or os.sep here???
        return (root + ext, "")
    else:
        return root, ext


Instruction:
Can you generate a brief explanation for the following Python 3 code
def bipart(func, seq):

    if func is None: func = bool
    res = [[],[]]
    for i in seq: res[not not func(i)].append(i)
    return res

Response: r Like a partitioning version of filter. Returns


Instruction:
Can you generate the documentation for the following Python 3 function
def notUnique(iterable, reportMax=INF):
    hash = {}
    n=0
    if reportMax < 1:
        raise ValueError("`reportMax` must be >= 1 and is %r" % reportMax)
    for item in iterable:
        count = hash[item] = hash.get(item, 0) + 1
        if count > 1:
            yield item
            n += 1
            if n >= reportMax:
                return

Response:
Returns the elements in iterable that aren t unique ; stops after it found
    reportMax non - unique elements.


Instruction:
Can you tell what is the following Python 3 function doing
def unweave(iterable, n=2):
    res = [[] for i in range(n)]
    i = 0
    for x in iterable:
        res[i % n].append(x)
        i += 1
    return res

Response:
r Divide iterable in n lists so that every nth element belongs to
    list n.


Instruction:
Can you tell what is the following Python 3 function doing
def weave(*iterables):
    iterables = map(iter, iterables)
    while True:
        for it in iterables: yield it.next()

Response: r Returns a sequence that can be used to generate a sequence from the given iterable.


Instruction:
Make a summary of the following Python 3 code
def atIndices(indexable, indices, default=__unique):
    if default is __unique:
        return [indexable[i] for i in indices]
    else:
        res = []
        for i in indices:
            try:
                res.append(indexable[i])
            except (IndexError, KeyError):
                res.append(default)
        return res

Response: r Return a list of items in indexable at positions indices.


Instruction:
Explain what the following Python 3 code does
def iterate(f, n=None, last=__unique):
    if n is not None:
        def funciter(start):
            for i in xrange(n): yield start; start = f(start)
    else:
        def funciter(start):
            while True:
                yield start
                last = f(start)
                if last == start: return
                last, start = start, last
    return funciter

Response: Iterate over the elements of a sequence of elements.


Instruction:
How would you explain what the following Python 3 function does
def stretch(iterable, n=2):
    times = range(n)
    for item in iterable:
        for i in times: yield item

Response: r Repeat each item in iterable n times.


Instruction:
Can you tell what is the following Python 3 function doing
def splitAt(iterable, indices):
    iterable = iter(iterable)
    now = 0
    for to in indices:
        try:
            res = []
            for i in range(now, to): res.append(iterable.next())
        except StopIteration: yield res; return
        yield res
        now = to
    res = list(iterable)
    if res: yield res

Response: r Yields chunks of iterable split at the points in indices.


Instruction:
Implement a Python 3 function for
returning a copy of dict d updated with dict e.

Response:
def update(d, e):
    """Return a copy of dict `d` updated with dict `e`."""
    res = copy.copy(d)
    res.update(e)
    return res


Instruction:
How would you explain what the following Python 3 function does
def invertDict(d, allowManyToOne=False):
    res = dict(izip(d.itervalues(), d.iterkeys()))
    if not allowManyToOne and len(res) != len(d):
        raise ValueError("d can't be inverted!")
    return res

Response:
r Return an inverted version of dict d so that values become keys and
    vice versa.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def flatten(seq, isSeq=isSeq):
    return [a for elt in seq
            for a in (isSeq(elt) and flatten(elt, isSeq) or
                      [elt])]

Response: r Returns a flattened version of a sequence seq as a list.


Instruction:
Can you tell what is the following Python 3 function doing
def without(seq1, seq2):
    if isSet(seq2): d2 = seq2
    else: d2 = set(seq2)
    return [elt for elt in seq1 if elt not in d2]

Response: r Returns a list with all elements in seq2 removed from seq1.


Instruction:
Given the following Python 3 function, write the documentation
def some(predicate, *seqs):
    try:
        if len(seqs) == 1: return ifilter(bool,imap(predicate, seqs[0])).next()
        else:             return ifilter(bool,starmap(predicate, izip(*seqs))).next()
    except StopIteration: return False

Response: True if all elements in the sequence pass the predicate.


Instruction:
Given the following Python 3 function, write the documentation
def every(predicate, *iterables):
    try:
        if len(iterables) == 1: ifilterfalse(predicate, iterables[0]).next()
        else:                  ifilterfalse(bool, starmap(predicate, izip(*iterables))).next()
    except StopIteration: return True
    else: return False

Response: r Like some but only returns True if all the elements of iterables satisfy predicate.


Instruction:
Make a summary of the following Python 3 code
def timeCall(*funcAndArgs, **kwargs):

    func, args = funcAndArgs[0], funcAndArgs[1:]
    start = time.time()
    func(*args, **kwargs)
    return time.time() - start

Response: r Returns the time it takes to call a function with the remaining arguments and kwargs.


Instruction:
Can you generate the documentation for the following Python 3 function
def unescape(s):
    if re.search(r'(?<!\\)\\(\\\\)*[uU]', s) or isinstance(s, unicode):
        return unescapeUnicode(s)
    else:
        return unescapeAscii(s)

Response:
r"""Inverse of `escape`.
    >>> unescape(r'\x41\n\x42\n\x43')
    'A\nB\nC'
    >>> unescape(r'\u86c7')
    u'\u86c7'
    >>> unescape(u'ah')
    u'ah'


Instruction:
Can you generate the documentation for the following Python 3 function
def lineAndColumnAt(s, pos):
    if pos >= len(s):
        raise IndexError("`pos` %d not in string" % pos)
    # *don't* count last '\n', if it is at pos!
    line = s.count('\n',0,pos)
    if line:
        return line + 1, pos - s.rfind('\n',0,pos) - 1
    else:
        return 1, pos

Response: r Return line and column of pos in s. Lines start with 1 columns with 0.


Instruction:
Explain what the following Python 3 code does
def prin(*args, **kwargs):
    print >> kwargs.get('out',None), " ".join([str(arg) for arg in args])

Response:
r Prints out all arguments as
   .


Instruction:
Can you generate a brief explanation for the following Python 3 code
def addVars(filename, varNamesStr, outOf=None):
    filename, varnames, outOf = __saveVarsHelper(filename, varNamesStr, outOf)
    f = None
    try:
        f = open(filename, "rb")
        h = cPickle.load(f)
        f.close()

        h.update(dict(zip(varnames, atIndices(outOf, varnames))))
        f = open(filename, "wb")
        cPickle.dump( h, f , 1 )
    finally:
        if f: f.close()

Response: r Like saveVars but appends additional variables to file.


Instruction:
Can you generate the documentation for the following Python 3 function
def loadDict(filename):
    filename = os.path.expanduser(filename)
    if not splitext(filename)[1]: filename += ".bpickle"
    f = None
    try:
        f = open(filename, "rb")
        varH = cPickle.load(f)
    finally:
        if f: f.close()
    return varH

Response:
Return the variables pickled into filename with saveVars
    as a dict.


Instruction:
Can you tell what is the following Python 3 function doing
def loadVars(filename, ask=True, into=None, only=None):

    filename = os.path.expanduser(filename)
    if into is None: into = magicGlobals()
    varH = loadDict(filename)
    toUnpickle = only or varH.keys()
    alreadyDefined = filter(into.has_key, toUnpickle)
    if alreadyDefined and ask:
	print "The following vars already exist; overwrite (yes/NO)?\n",\
	      "\n".join(alreadyDefined)
	if raw_input() != "yes":
	    toUnpickle = without(toUnpickle, alreadyDefined)
    if not toUnpickle:
	print "nothing to unpickle"
	return None
    print "unpickling:\n",\
	  "\n".join(sorted(toUnpickle))
    for k in varH.keys():
        if k not in toUnpickle:
            del varH[k]
    into.update(varH)

Response: r Loads the variables from a file into a dictionary.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def makePrintReturner(pre="", post="" ,out=None):
    def printReturner(arg):
        myArgs = [pre, arg, post]
        prin(*myArgs, **{'out':out})
        return arg
    return printReturner

Response: r Returns a function that prints out their argument and returns it unmodified.


Instruction:
Implement a Python 3 function for
returning a verbose version of the given container instance.

Response:
def asVerboseContainer(cont, onGet=None, onSet=None, onDel=None):
    """Returns a 'verbose' version of container instance `cont`, that will
       execute `onGet`, `onSet` and `onDel` (if not `None`) every time
       __getitem__, __setitem__ and __delitem__ are called, passing `self`, `key`
       (and `value` in the case of set). E.g:

       >>> l = [1,2,3]
       >>> l = asVerboseContainer(l,
       ...                onGet=lambda s,k:k==2 and prin('Got two:', k),
       ...                onSet=lambda s,k,v:k == v and prin('k == v:', k, v),
       ...                onDel=lambda s,k:k == 1 and prin('Deleting one:', k))
       >>> l
       [1, 2, 3]
       >>> l[1]
       2
       >>> l[2]
       Got two: 2
       3
       >>> l[2] = 22
       >>> l[2] = 2
       k == v: 2 2
       >>> del l[2]
       >>> del l[1]
       Deleting one: 1

    """
    class VerboseContainer(type(cont)):
        if onGet:
            def __getitem__(self, key):
                onGet(self, key)
                return super(VerboseContainer, self).__getitem__(key)
        if onSet:
            def __setitem__(self, key, value):
                onSet(self, key, value)
                return super(VerboseContainer, self).__setitem__(key, value)
        if onDel:
            def __delitem__(self, key):
                onDel(self, key)
                return super(VerboseContainer, self).__delitem__(key)
    return VerboseContainer(cont)


Instruction:
Here you have a function in Python 3, explain what it does
def d2attrs(*args, **kwargs):
    (d, self), args = args[:2], args[2:]
    if args[0] == 'all!':
        assert len(args) == 1
        for k in d: setattr(self, k, d[k])
    else:
        if len(args) != len(set(args)) or set(kwargs) & set(args):
            raise ValueError('Duplicate keys: %s' %
                             list(notUnique(args)) + list(set(kwargs) & set(args)))
        for k in args:
            if k in kwargs: raise ValueError('%s specified twice' % k)
            setattr(self, k, d[k])
        for dk in kwargs:
            setattr(self, dk, d.get(dk, kwargs[dk]))

Response: Utility function to remove attributes from a dictionary.


Instruction:
Write a Python 3 function that can
return the index of the first element in an iterable.

Response:
def argmax(iterable, key=None, both=False):
    """
    >>> argmax([4,2,-5])
    0
    >>> argmax([4,2,-5], key=abs)
    2
    >>> argmax([4,2,-5], key=abs, both=True)
    (2, 5)
    """
    if key is not None:
        it = imap(key, iterable)
    else:
        it = iter(iterable)
    score, argmax = reduce(max, izip(it, count()))
    if both:
        return argmax, score
    return argmax


Instruction:
Can you create a Python 3 function that
returns true if num is an integer.

Response:
def isInt(num):
    """Returns true if `num` is (sort of) an integer.
    >>> isInt(3) == isInt(3.0) == 1
    True
    >>> isInt(3.2)
    False
    >>> import numpy
    >>> isInt(numpy.array(1))
    True
    >>> isInt(numpy.array([1]))
    False
    """
    try:
        len(num) # FIXME fails for Numeric but Numeric is obsolete
    except:
        try:
            return (num - math.floor(num) == 0) == True
        except: return False
    else: return False


Instruction:
Can you generate the documentation for the following Python 3 function
def mapConcat(func, *iterables):
    return [e for l in imap(func, *iterables) for e in l]

Response: Similar to map but uses a list of lists to produce a new list of items.


Instruction:
Write a Python 3 function that can
unfold a set of items into a list of items.

Response:
def unfold(seed, by, last = __unique):
    """
    >>> list(unfold(1234, lambda x: divmod(x,10)))[::-1]
    [1, 2, 3, 4]
    >>> sum(imap(operator.mul,unfold(1234, lambda x:divmod(x,10)), iterate(lambda x:x*10)(1)))
    1234
    >>> g = unfold(1234, lambda x:divmod(x,10))
    >>> reduce((lambda (total,pow),digit:(total+pow*digit, 10*pow)), g, (0,1))
    (1234, 10000)
    """

    while True:
        seed, val = by(seed);
        if last == seed: return
        last = seed; yield val


Instruction:
Implement a Python 3 function for
reducing a sequence of numbers.

Response:
def reduceR(f, sequence, initial=__unique):
    """*R*ight reduce.
    >>> reduceR(lambda x,y:x/y, [1.,2.,3.,4]) == 1./(2./(3./4.)) == (1./2.)*(3./4.)
    True
    >>> reduceR(lambda x,y:x-y, iter([1,2,3]),4) == 1-(2-(3-4)) == (1-2)+(3-4)
    True
    """
    try: rev = reversed(sequence)
    except TypeError: rev = reversed(list(sequence))
    if initial is __unique: return reduce(lambda x,y:f(y,x), rev)
    else:                   return reduce(lambda x,y:f(y,x), rev, initial)


Instruction:
Can you create a Python 3 function that
composes a list of functions to a single function.

Response:
def compose(*funcs):
    """Compose `funcs` to a single function.

    >>> compose(operator.abs, operator.add)(-2,-3)
    5
    >>> compose()('nada')
    'nada'
    >>> compose(sorted, set, partial(filter, None))(range(3)[::-1]*2)
    [1, 2]
    """
    # slightly optimized for most common cases and hence verbose
    if len(funcs) == 2: f0,f1=funcs; return lambda *a,**kw: f0(f1(*a,**kw))
    elif len(funcs) == 3: f0,f1,f2=funcs; return lambda *a,**kw: f0(f1(f2(*a,**kw)))
    elif len(funcs) == 0: return lambda x:x     # XXX single kwarg
    elif len(funcs) == 1: return funcs[0]
    else:
        def composed(*args,**kwargs):
            y = funcs[-1](*args,**kwargs)
            for f in funcs[:0:-1]: y = f(y)
            return y
        return composed


Instruction:
Given the following Python 3 function, write the documentation
def romanNumeral(n):
    if 0 > n > 4000: raise ValueError('``n`` must lie between 1 and 3999: %d' % n)
    roman   = 'I IV  V  IX  X  XL   L  XC    C   CD    D   CM     M'.split()
    arabic  = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000]
    res = []
    while n>0:
        pos = bisect.bisect_right(arabic, n)-1
        fit = n//arabic[pos]
        res.append(roman[pos]*fit); n -= fit * arabic[pos]
    return "".join(res)

Response: Returns a roman numeral string for the given integer n.


Instruction:
How would you code a function in Python 3 to
return the first n elements of an iterable.

Response:
def first(n, it, constructor=list):
    """
    >>> first(3,iter([1,2,3,4]))
    [1, 2, 3]
    >>> first(3,iter([1,2,3,4]), iter) #doctest: +ELLIPSIS
    <itertools.islice object at ...>
    >>> first(3,iter([1,2,3,4]), tuple)
    (1, 2, 3)
    """
    return constructor(itertools.islice(it,n))


Instruction:
Make a summary of the following Python 3 code
def drop(n, it, constructor=list):
    return constructor(itertools.islice(it,n,None))

Response: Drop n elements from an iterable.


Instruction:
Here you have a function in Python 3, explain what it does
def run(self, func, *args, **kwargs):
        if self.dry:
            return self.dryRun(func, *args, **kwargs)
        else:
            return self.wetRun(func, *args, **kwargs)

Response: Same as self. run except that it will return the result of the function.


Instruction:
Here you have a function in Python 3, explain what it does
def dryRun(self, func, *args, **kwargs):

        print >> self.out, \
              self.formatterDict.get(func, self.defaultFormatter)(func, *args, **kwargs)

Response:
Instead of running function with * args and ** kwargs just print
           out the function call.


Instruction:
Create a Python 3 function for
iterating over all the bridges in the system.

Response:
def iterbridges():
    ''' Iterate over all the bridges in the system. '''
    net_files = os.listdir(SYSFS_NET_PATH)
    for d in net_files:
        path = os.path.join(SYSFS_NET_PATH, d)
        if not os.path.isdir(path):
            continue
        if os.path.exists(os.path.join(path, b"bridge")):
            yield Bridge(d)


Instruction:
How would you explain what the following Python 3 function does
def addbr(name):
    ''' Create new bridge with the given name '''
    fcntl.ioctl(ifconfig.sockfd, SIOCBRADDBR, name)
    return Bridge(name)

Response: Create a new bridge with the given name.


Instruction:
Implement a Python 3 function for
iterating over all the interfaces in this bridge.

Response:
def iterifs(self):
        ''' Iterate over all the interfaces in this bridge. '''
        if_path = os.path.join(SYSFS_NET_PATH, self.name, b"brif")
        net_files = os.listdir(if_path)
        for iface in net_files:
            yield iface


Instruction:
Given the following Python 3 function, write the documentation
def addif(self, iface):
        ''' Add the interface with the given name to this bridge. Equivalent to
            brctl addif [bridge] [interface]. '''
        if type(iface) == ifconfig.Interface:
            devindex = iface.index
        else:
            devindex = ifconfig.Interface(iface).index
        ifreq = struct.pack('16si', self.name, devindex)
        fcntl.ioctl(ifconfig.sockfd, SIOCBRADDIF, ifreq)
        return self

Response: Add the given interface to this bridge. Equivalent to calling brctl addif.


Instruction:
Write a Python 3 function that can
remove the interface with the given name from this bridge.

Response:
def delif(self, iface):
        ''' Remove the interface with the given name from this bridge.
            Equivalent to brctl delif [bridge] [interface]'''
        if type(iface) == ifconfig.Interface:
            devindex = iface.index
        else:
            devindex = ifconfig.Interface(iface).index
        ifreq = struct.pack('16si', self.name, devindex)
        fcntl.ioctl(ifconfig.sockfd, SIOCBRDELIF, ifreq)
        return self


Instruction:
Can you generate a brief explanation for the following Python 3 code
def delete(self):
        ''' Brings down the bridge interface, and removes it. Equivalent to
        ifconfig [bridge] down && brctl delbr [bridge]. '''
        self.down()
        fcntl.ioctl(ifconfig.sockfd, SIOCBRDELBR, self.name)
        return self

Response: Delete the current instance of the class.


Instruction:
Write a Python 3 function for
getting a random string identifier

Response:
def _get_random_id():
    """ Get a random (i.e., unique) string identifier"""
    symbols = string.ascii_uppercase + string.ascii_lowercase + string.digits
    return ''.join(random.choice(symbols) for _ in range(15))


Instruction:
Can you write a function in Python 3 where it
gets a filename of a built - in library file.

Response:
def get_lib_filename(category, name):
    """ Get a filename of a built-in library file. """
    base_dir = os.path.dirname(os.path.abspath(__file__))
    if category == 'js':
        filename = os.path.join('js', '{0}.js'.format(name))
    elif category == 'css':
        filename = os.path.join('css', '{0}.css'.format(name))
    elif category == 'html':
        filename = os.path.join('html', '{0}.html'.format(name))
    else:
        raise ValueError("Unknown category")
    return os.path.join(base_dir, 'lib', filename)


Instruction:
How would you implement a function in Python 3 that
outputs the Jupyter Notebook.

Response:
def output_notebook(
        d3js_url="//d3js.org/d3.v3.min",
        requirejs_url="//cdnjs.cloudflare.com/ajax/libs/require.js/2.1.10/require.min.js",
        html_template=None
):
    """ Import required Javascript libraries to Jupyter Notebook. """

    if html_template is None:
        html_template = read_lib('html', 'setup')

    setup_html = populate_template(
        html_template,
        d3js=d3js_url,
        requirejs=requirejs_url
    )

    display_html(setup_html)
    return


Instruction:
Create a Python 3 function to
create HTML code block given the graph Javascript and CSS.

Response:
def create_graph_html(js_template, css_template, html_template=None):
    """ Create HTML code block given the graph Javascript and CSS. """
    if html_template is None:
        html_template = read_lib('html', 'graph')

    # Create div ID for the graph and give it to the JS and CSS templates so
    # they can reference the graph.
    graph_id = 'graph-{0}'.format(_get_random_id())
    js = populate_template(js_template, graph_id=graph_id)
    css = populate_template(css_template, graph_id=graph_id)

    return populate_template(
        html_template,
        graph_id=graph_id,
        css=css,
        js=js
    )


Instruction:
Implement a Python 3 function for
returning newest compatible version.

Response:
def get_newest_possible_languagetool_version():
    """Return newest compatible version.

    >>> version = get_newest_possible_languagetool_version()
    >>> version in [JAVA_6_COMPATIBLE_VERSION, LATEST_VERSION]
    True

    """
    java_path = find_executable('java')
    if not java_path:
        # Just ignore this and assume an old version of Java. It might not be
        # found because of a PATHEXT-related issue
        # (https://bugs.python.org/issue2200).
        return JAVA_6_COMPATIBLE_VERSION

    output = subprocess.check_output([java_path, '-version'],
                                     stderr=subprocess.STDOUT,
                                     universal_newlines=True)
    # https://www.oracle.com/technetwork/java/javase/versioning-naming-139433.html
    match = re.search(
        r'^java version "(?P<major1>\d+)\.(?P<major2>\d+)\.[^"]+"$',
        output,
        re.MULTILINE)
    if not match:
        raise SystemExit(
            'Could not parse Java version from """{}""".'.format(output))

    java_version = (int(match.group('major1')), int(match.group('major2')))
    if java_version >= (1, 7):
        return LATEST_VERSION
    elif java_version >= (1, 6):
        warn('grammar-check would be able to use a newer version of '
             'LanguageTool if you had Java 7 or newer installed')
        return JAVA_6_COMPATIBLE_VERSION
    else:
        raise SystemExit(
            'You need at least Java 6 to use grammar-check')


Instruction:
Can you create a Python 3 function that
iterates over all the interfaces in the system.

Response:
def iterifs(physical=True):
    ''' Iterate over all the interfaces in the system. If physical is
        true, then return only real physical interfaces (not 'lo', etc).'''
    net_files = os.listdir(SYSFS_NET_PATH)
    interfaces = set()
    virtual = set()
    for d in net_files:
        path = os.path.join(SYSFS_NET_PATH, d)
        if not os.path.isdir(path):
            continue
        if not os.path.exists(os.path.join(path, b"device")):
            virtual.add(d)
        interfaces.add(d)

    # Some virtual interfaces don't show up in the above search, for example,
    # subinterfaces (e.g. eth0:1). To find those, we have to do an ioctl
    if not physical:
        # ifconfig gets a max of 30 interfaces. Good enough for us too.
        ifreqs = array.array("B", b"\x00" * SIZE_OF_IFREQ * 30)
        buf_addr, _buf_len = ifreqs.buffer_info()
        ifconf = struct.pack("iP", SIZE_OF_IFREQ * 30, buf_addr)
        ifconf_res = fcntl.ioctl(sockfd, SIOCGIFCONF, ifconf)
        ifreqs_len, _ = struct.unpack("iP", ifconf_res)

        assert ifreqs_len % SIZE_OF_IFREQ == 0, (
            "Unexpected amount of data returned from ioctl. "
            "You're probably running on an unexpected architecture")

        res = ifreqs.tostring()
        for i in range(0, ifreqs_len, SIZE_OF_IFREQ):
            d = res[i:i+16].strip(b'\0')
            interfaces.add(d)

    results = interfaces - virtual if physical else interfaces
    for d in results:
        yield Interface(d)


Instruction:
Can you generate the documentation for the following Python 3 function
def up(self):
        ''' Bring up the bridge interface. Equivalent to ifconfig [iface] up. '''

        # Get existing device flags
        ifreq = struct.pack('16sh', self.name, 0)
        flags = struct.unpack('16sh', fcntl.ioctl(sockfd, SIOCGIFFLAGS, ifreq))[1]

        # Set new flags
        flags = flags | IFF_UP
        ifreq = struct.pack('16sh', self.name, flags)
        fcntl.ioctl(sockfd, SIOCSIFFLAGS, ifreq)

Response: Bring up the bridge interface. Equivalent to ifconfig [ iface up.


Instruction:
Given the following Python 3 function, write the documentation
def is_up(self):
        ''' Return True if the interface is up, False otherwise. '''

        # Get existing device flags
        ifreq = struct.pack('16sh', self.name, 0)
        flags = struct.unpack('16sh', fcntl.ioctl(sockfd, SIOCGIFFLAGS, ifreq))[1]

        # Set new flags
        if flags & IFF_UP:
            return True
        else:
            return False

Response: Return True if the interface is up False otherwise.


Instruction:
Can you tell what is the following Python 3 function doing
def get_mac(self):
        ''' Obtain the device's mac address. '''
        ifreq = struct.pack('16sH14s', self.name, AF_UNIX, b'\x00'*14)
        res = fcntl.ioctl(sockfd, SIOCGIFHWADDR, ifreq)
        address = struct.unpack('16sH14s', res)[2]
        mac = struct.unpack('6B8x', address)

        return ":".join(['%02X' % i for i in mac])

Response: Obtain the device s mac address.


Instruction:
Make a summary of the following Python 3 code
def set_mac(self, newmac):
        ''' Set the device's mac address. Device must be down for this to
            succeed. '''
        macbytes = [int(i, 16) for i in newmac.split(':')]
        ifreq = struct.pack('16sH6B8x', self.name, AF_UNIX, *macbytes)
        fcntl.ioctl(sockfd, SIOCSIFHWADDR, ifreq)

Response: Set the device s mac address.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_index(self):
        ''' Convert an interface name to an index value. '''
        ifreq = struct.pack('16si', self.name, 0)
        res = fcntl.ioctl(sockfd, SIOCGIFINDEX, ifreq)
        return struct.unpack("16si", res)[1]

Response: Convert an interface name to an index value.


Instruction:
Can you tell what is the following Python 3 function doing
def set_pause_param(self, autoneg, rx_pause, tx_pause):
        # create a struct ethtool_pauseparm
        # create a struct ifreq with its .ifr_data pointing at the above
        ecmd = array.array('B', struct.pack('IIII',
            ETHTOOL_SPAUSEPARAM, bool(autoneg), bool(rx_pause), bool(tx_pause)))
        buf_addr, _buf_len = ecmd.buffer_info()
        ifreq = struct.pack('16sP', self.name, buf_addr)
        fcntl.ioctl(sockfd, SIOCETHTOOL, ifreq)

Response: Set the pause parameter for the current set of inter - frame types.


Instruction:
Implement a function in Python 3 to
split a string with comma or space - separated elements into a list.

Response:
def split_elements(value):
    """Split a string with comma or space-separated elements into a list."""
    l = [v.strip() for v in value.split(',')]
    if len(l) == 1:
        l = value.split()
    return l


Instruction:
Can you implement a function in Python 3 that
generates Python 2 code from Python 3 code.

Response:
def generate_py2k(config, py2k_dir=PY2K_DIR, run_tests=False):
    """Generate Python 2 code from Python 3 code."""
    def copy(src, dst):
        if (not os.path.isfile(dst) or
                os.path.getmtime(src) > os.path.getmtime(dst)):
            shutil.copy(src, dst)
            return dst
        return None

    def copy_data(src, dst):
        if (not os.path.isfile(dst) or
                os.path.getmtime(src) > os.path.getmtime(dst) or
                os.path.getsize(src) != os.path.getsize(dst)):
            shutil.copy(src, dst)
            return dst
        return None

    copied_py_files = []
    test_scripts = []

    if not os.path.isdir(py2k_dir):
        os.makedirs(py2k_dir)

    packages_root = get_cfg_value(config, 'files', 'packages_root')

    for name in get_cfg_value(config, 'files', 'packages'):
        name = name.replace('.', os.path.sep)
        py3k_path = os.path.join(packages_root, name)
        py2k_path = os.path.join(py2k_dir, py3k_path)
        if not os.path.isdir(py2k_path):
            os.makedirs(py2k_path)
        for fn in os.listdir(py3k_path):
            path = os.path.join(py3k_path, fn)
            if not os.path.isfile(path):
                continue
            if not os.path.splitext(path)[1].lower() == '.py':
                continue
            new_path = os.path.join(py2k_path, fn)
            if copy(path, new_path):
                copied_py_files.append(new_path)

    for name in get_cfg_value(config, 'files', 'modules'):
        name = name.replace('.', os.path.sep) + '.py'
        py3k_path = os.path.join(packages_root, name)
        py2k_path = os.path.join(py2k_dir, py3k_path)
        dirname = os.path.dirname(py2k_path)
        if not os.path.isdir(dirname):
            os.makedirs(dirname)
        if copy(py3k_path, py2k_path):
            copied_py_files.append(py2k_path)

    for name in get_cfg_value(config, 'files', 'scripts'):
        py3k_path = os.path.join(packages_root, name)
        py2k_path = os.path.join(py2k_dir, py3k_path)
        dirname = os.path.dirname(py2k_path)
        if not os.path.isdir(dirname):
            os.makedirs(dirname)
        if copy(py3k_path, py2k_path):
            copied_py_files.append(py2k_path)

    setup_py_path = os.path.abspath(__file__)

    for pattern in get_cfg_value(config, 'files', 'extra_files'):
        for path in glob.glob(pattern):
            if os.path.abspath(path) == setup_py_path:
                continue
            py2k_path = os.path.join(py2k_dir, path)
            py2k_dirname = os.path.dirname(py2k_path)
            if not os.path.isdir(py2k_dirname):
                os.makedirs(py2k_dirname)
            filename = os.path.split(path)[1]
            ext = os.path.splitext(filename)[1].lower()
            if ext == '.py':
                if copy(path, py2k_path):
                    copied_py_files.append(py2k_path)
            else:
                copy_data(path, py2k_path)
            if (os.access(py2k_path, os.X_OK) and
                    re.search(r"\btest\b|_test\b|\btest_", filename)):
                test_scripts.append(py2k_path)

    for package, patterns in get_package_data(
            get_cfg_value(config, 'files', 'package_data')).items():
        for pattern in patterns:
            py3k_pattern = os.path.join(packages_root, package, pattern)
            for py3k_path in glob.glob(py3k_pattern):
                py2k_path = os.path.join(py2k_dir, py3k_path)
                py2k_dirname = os.path.dirname(py2k_path)
                if not os.path.isdir(py2k_dirname):
                    os.makedirs(py2k_dirname)
                copy_data(py3k_path, py2k_path)

    if copied_py_files:
        copied_py_files.sort()
        try:
            run_3to2(copied_py_files)
            write_py2k_header(copied_py_files)
        except:
            shutil.rmtree(py2k_dir)
            raise

    if run_tests:
        for script in test_scripts:
            subprocess.check_call([script])


Instruction:
How would you explain what the following Python 3 function does
def get_default_if():
    f = open ('/proc/net/route', 'r')
    for line in f:
        words = line.split()
        dest = words[1]
        try:
            if (int (dest) == 0):
                interf = words[0]
                break
        except ValueError:
            pass
    return interf

Response: Returns the default interface if the route is not set


Instruction:
How would you explain what the following Python 3 function does
def get_default_gw():
    octet_list = []
    gw_from_route = None
    f = open ('/proc/net/route', 'r')
    for line in f:
        words = line.split()
        dest = words[1]
        try:
            if (int (dest) == 0):
                gw_from_route = words[2]
                break
        except ValueError:
            pass

    if not gw_from_route:
        return None

    for i in range(8, 1, -2):
        octet = gw_from_route[i-2:i]
        octet = int(octet, 16)
        octet_list.append(str(octet))

    gw_ip = ".".join(octet_list)

    return gw_ip

Response: Returns the default gateway


Instruction:
Can you create a Python 3 function that
creates the module instance of the GraphiteClient.

Response:
def init(init_type='plaintext_tcp', *args, **kwargs):
    """
    Create the module instance of the GraphiteClient.
    """
    global _module_instance
    reset()

    validate_init_types = ['plaintext_tcp', 'plaintext', 'pickle_tcp',
                           'pickle', 'plain']

    if init_type not in validate_init_types:
        raise GraphiteSendException(
            "Invalid init_type '%s', must be one of: %s" %
            (init_type, ", ".join(validate_init_types)))

    # Use TCP to send data to the plain text receiver on the graphite server.
    if init_type in ['plaintext_tcp', 'plaintext', 'plain']:
        _module_instance = GraphiteClient(*args, **kwargs)

    # Use TCP to send pickled data to the pickle receiver on the graphite
    # server.
    if init_type in ['pickle_tcp', 'pickle']:
        _module_instance = GraphitePickleClient(*args, **kwargs)

    return _module_instance


Instruction:
Write a Python 3 script to
send the metrics to the graphite server.

Response:
def send(*args, **kwargs):
    """ Make sure that we have an instance of the GraphiteClient.
    Then send the metrics to the graphite server.
    User consumable method.
    """
    if not _module_instance:
        raise GraphiteSendException(
            "Must call graphitesend.init() before sending")

    _module_instance.send(*args, **kwargs)
    return _module_instance


Instruction:
Can you tell what is the following Python 3 function doing
def send_dict(*args, **kwargs):
    if not _module_instance:
        raise GraphiteSendException(
            "Must call graphitesend.init() before sending")
    _module_instance.send_dict(*args, **kwargs)
    return _module_instance

Response: Send the metrics to the graphite server.


Instruction:
Make a summary of the following Python 3 code
def send_list(*args, **kwargs):
    if not _module_instance:
        raise GraphiteSendException(
            "Must call graphitesend.init() before sending")
    _module_instance.send_list(*args, **kwargs)
    return _module_instance

Response: Send a list of metrics to the graphite server.


Instruction:
Implement a function in Python 3 to
allow the module to be called from the cli.

Response:
def cli():
    """ Allow the module to be called from the cli. """
    import argparse

    parser = argparse.ArgumentParser(description='Send data to graphite')

    # Core of the application is to accept a metric and a value.
    parser.add_argument('metric', metavar='metric', type=str,
                        help='name.of.metric')
    parser.add_argument('value', metavar='value', type=int,
                        help='value of metric as int')

    args = parser.parse_args()
    metric = args.metric
    value = args.value

    graphitesend_instance = init()
    graphitesend_instance.send(metric, value)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def connect(self):
        self.socket = socket.socket()
        self.socket.settimeout(self.timeout_in_seconds)
        try:
            self.socket.connect(self.addr)
        except socket.timeout:
            raise GraphiteSendException(
                "Took over %d second(s) to connect to %s" %
                (self.timeout_in_seconds, self.addr))
        except socket.gaierror:
            raise GraphiteSendException(
                "No address associated with hostname %s:%s" % self.addr)
        except Exception as error:
            raise GraphiteSendException(
                "unknown exception while connecting to %s - %s" %
                (self.addr, error)
            )

        return self.socket

Response: Connect to the graphite server and return a socket object.


Instruction:
Can you tell what is the following Python 3 function doing
def autoreconnect(self, sleep=1, attempt=3, exponential=True, jitter=5):

        p = 0

        while attempt is None or attempt > 0:
            try:
                self.reconnect()
                return True
            except GraphiteSendException:

                if exponential:
                    p += 1
                    time.sleep(pow(sleep, p) + random.randint(1, jitter))
                else:
                    time.sleep(sleep)

                attempt -= 1

        return False

Response:
Attempts to reconnect with some delay between two attempts and exponential growing sleep seconds between two attempts each try
        exponential = False will attempt to reconnect with some delay in range 1.. jitter 5 will attempt to reconnect with some delay


Instruction:
Can you generate a brief explanation for the following Python 3 code
def disconnect(self):
        try:
            self.socket.shutdown(1)

        # If its currently a socket, set it to None
        except AttributeError:
            self.socket = None
        except Exception:
            self.socket = None

        # Set the self.socket to None, no matter what.
        finally:
            self.socket = None

Response: Disconnects from the graphite server.


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

        if self.dryrun:
            return message

        if not self.socket:
            raise GraphiteSendException(
                "Socket was not created before send"
            )

        sending_function = self._send
        if self._autoreconnect:
            sending_function = self._send_and_reconnect

        try:
            if self.asynchronous and gevent:
                gevent.spawn(sending_function, message)
            else:
                sending_function(message)
        except Exception as e:
            self._handle_send_error(e)

        return "sent {0} long message: {1}".format(len(message), message[:75])

Response: Dispatch the different steps of sending to the specified message


Instruction:
Can you write a function in Python 3 where it
sends _message_ to Graphite Server and attempt to reconnect on failure.

Response:
def _send_and_reconnect(self, message):
        """Send _message_ to Graphite Server and attempt reconnect on failure.

        If _autoreconnect_ was specified, attempt to reconnect if first send
        fails.

        :raises AttributeError: When the socket has not been set.
        :raises socket.error: When the socket connection is no longer valid.
        """
        try:
            self.socket.sendall(message.encode("ascii"))
        except (AttributeError, socket.error):
            if not self.autoreconnect():
                raise
            else:
                self.socket.sendall(message.encode("ascii"))


Instruction:
Can you generate the documentation for the following Python 3 function
def send(self, metric, value, timestamp=None, formatter=None):
        if formatter is None:
            formatter = self.formatter
        message = formatter(metric, value, timestamp)
        message = self. _presend(message)
        return self._dispatch_send(message)

Response: Format a single metric value pair and send it to the graphite server.


Instruction:
Given the following Python 3 function, write the documentation
def send_dict(self, data, timestamp=None, formatter=None):
        if formatter is None:
            formatter = self.formatter

        metric_list = []

        for metric, value in data.items():
            tmp_message = formatter(metric, value, timestamp)
            metric_list.append(tmp_message)

        message = "".join(metric_list)
        return self._dispatch_send(message)

Response: Format a dict of metric name and value pairs and send them all to the graphite server.


Instruction:
How would you implement a function in Python 3 that
checks if socket has been monkey patched by gevent and if so enable asynchronous mode.

Response:
def enable_asynchronous(self):
        """Check if socket have been monkey patched by gevent"""

        def is_monkey_patched():
            try:
                from gevent import monkey, socket
            except ImportError:
                return False
            if hasattr(monkey, "saved"):
                return "socket" in monkey.saved
            return gevent.socket.socket == socket.socket

        if not is_monkey_patched():
            raise Exception("To activate asynchonoucity, please monkey patch"
                            " the socket module with gevent")
        return True


Instruction:
How would you explain what the following Python 3 function does
def str2listtuple(self, string_message):
        "Covert a string that is ready to be sent to graphite into a tuple"

        if type(string_message).__name__ not in ('str', 'unicode'):
            raise TypeError("Must provide a string or unicode")

        if not string_message.endswith('\n'):
            string_message += "\n"

        tpl_list = []
        for line in string_message.split('\n'):
            line = line.strip()
            if not line:
                continue
            path, metric, timestamp = (None, None, None)
            try:
                (path, metric, timestamp) = line.split()
            except ValueError:
                raise ValueError(
                    "message must contain - metric_name, value and timestamp '%s'"
                    % line)
            try:
                timestamp = float(timestamp)
            except ValueError:
                raise ValueError("Timestamp must be float or int")

            tpl_list.append((path, (timestamp, metric)))

        if len(tpl_list) == 0:
            raise GraphiteSendException("No messages to send")

        payload = pickle.dumps(tpl_list)
        header = struct.pack("!L", len(payload))
        message = header + payload

        return message

Response: Covert a string that is ready to be sent to graphite into a tuple


Instruction:
Explain what the following Python 3 code does
def _send(self, message):

        # An option to lowercase the entire message
        if self.lowercase_metric_names:
            message = message.lower()

        # convert the message into a pickled payload.
        message = self.str2listtuple(message)

        try:
            self.socket.sendall(message)
        # Capture missing socket.
        except socket.gaierror as error:
            raise GraphiteSendException(
                "Failed to send data to %s, with error: %s" %
                (self.addr, error))  # noqa

        # Capture socket closure before send.
        except socket.error as error:
            raise GraphiteSendException(
                "Socket closed before able to send data to %s, "
                "with error: %s" %
                (self.addr, error))  # noqa

        except Exception as error:
            raise GraphiteSendException(
                "Unknown error while trying to send data down socket to %s, "
                "error: %s" %
                (self.addr, error))  # noqa

        return "sent %d long pickled message" % len(message)

Response: Given a message send it to the graphite server.


Instruction:
How would you explain what the following Python 3 function does
def clean_metric_name(self, metric_name):
        if not self._clean_metric_name:
            return metric_name
        metric_name = str(metric_name)
        for _from, _to in self.cleaning_replacement_list:
            metric_name = metric_name.replace(_from, _to)
        return metric_name

Response: Clean the metric name to remove any characters that are not free of control chars spaces tabs etc.


Instruction:
Make a summary of the following Python 3 code
def _get_languages(cls) -> set:
        if not cls._server_is_alive():
            cls._start_server_on_free_port()
        url = urllib.parse.urljoin(cls._url, 'Languages')
        languages = set()
        for e in cls._get_root(url, num_tries=1):
            languages.add(e.get('abbr'))
            languages.add(e.get('abbrWithVariant'))
        return languages

Response: Get supported languages by querying the server.


Instruction:
Create a Python 3 function to
get matches element attributes.

Response:
def _get_attrib(cls):
        """Get matches element attributes."""
        if not cls._server_is_alive():
            cls._start_server_on_free_port()
        params = {'language': FAILSAFE_LANGUAGE, 'text': ''}
        data = urllib.parse.urlencode(params).encode()
        root = cls._get_root(cls._url, data, num_tries=1)
        return root.attrib


Instruction:
Can you create a Python 3 function that
patches method for PageAdmin. get_form.

Response:
def get_form(self, request, obj=None, **kwargs):
    """
    Patched method for PageAdmin.get_form.

    Returns a page form without the base field 'meta_description' which is
    overridden in djangocms-page-meta.

    This is triggered in the page add view and in the change view if
    the meta description of the page is empty.
    """
    language = get_language_from_request(request, obj)
    form = _BASE_PAGEADMIN__GET_FORM(self, request, obj, **kwargs)
    if not obj or not obj.get_meta_description(language=language):
        form.base_fields.pop('meta_description', None)

    return form


Instruction:
Can you write a function in Python 3 where it
creates the cache key for the current page and language

Response:
def get_cache_key(page, language):
    """
    Create the cache key for the current page and language
    """
    from cms.cache import _get_cache_key
    try:
        site_id = page.node.site_id
    except AttributeError:  # CMS_3_4
        site_id = page.site_id
    return _get_cache_key('page_meta', page, language, site_id)


Instruction:
Can you generate the documentation for the following Python 3 function
def get_page_meta(page, language):
    from django.core.cache import cache
    from meta.views import Meta
    from .models import PageMeta, TitleMeta

    try:
        meta_key = get_cache_key(page, language)
    except AttributeError:
        return None
    gplus_server = 'https://plus.google.com'
    meta = cache.get(meta_key)
    if not meta:
        meta = Meta()
        title = page.get_title_obj(language)
        meta.extra_custom_props = []

        meta.title = page.get_page_title(language)
        if not meta.title:
            meta.title = page.get_title(language)

        if title.meta_description:
            meta.description = title.meta_description.strip()
        try:
            titlemeta = title.titlemeta
            if titlemeta.description:
                meta.description = titlemeta.description.strip()
            if titlemeta.keywords:
                meta.keywords = titlemeta.keywords.strip().split(',')
            meta.locale = titlemeta.locale
            meta.og_description = titlemeta.og_description.strip()
            if not meta.og_description:
                meta.og_description = meta.description
            meta.twitter_description = titlemeta.twitter_description.strip()
            if not meta.twitter_description:
                meta.twitter_description = meta.description
            meta.gplus_description = titlemeta.gplus_description.strip()
            if not meta.gplus_description:
                meta.gplus_description = meta.description
            if titlemeta.image:
                meta.image = title.titlemeta.image.canonical_url or title.titlemeta.image.url
            for item in titlemeta.extra.all():
                attribute = item.attribute
                if not attribute:
                    attribute = item.DEFAULT_ATTRIBUTE
                meta.extra_custom_props.append((attribute, item.name, item.value))
        except (TitleMeta.DoesNotExist, AttributeError):
            # Skipping title-level metas
            if meta.description:
                meta.og_description = meta.description
                meta.twitter_description = meta.description
                meta.gplus_description = meta.description
        defaults = {
            'object_type': meta_settings.FB_TYPE,
            'og_type': meta_settings.FB_TYPE,
            'og_app_id': meta_settings.FB_APPID,
            'fb_pages': meta_settings.FB_PAGES,
            'og_profile_id': meta_settings.FB_PROFILE_ID,
            'og_publisher': meta_settings.FB_PUBLISHER,
            'og_author_url': meta_settings.FB_AUTHOR_URL,
            'twitter_type': meta_settings.TWITTER_TYPE,
            'twitter_site': meta_settings.TWITTER_SITE,
            'twitter_author': meta_settings.TWITTER_AUTHOR,
            'gplus_type': meta_settings.GPLUS_TYPE,
            'gplus_author': meta_settings.GPLUS_AUTHOR,
        }
        try:
            pagemeta = page.pagemeta
            meta.object_type = pagemeta.og_type
            meta.og_type = pagemeta.og_type
            meta.og_app_id = pagemeta.og_app_id
            meta.fb_pages = pagemeta.fb_pages
            meta.og_profile_id = pagemeta.og_author_fbid
            meta.twitter_type = pagemeta.twitter_type
            meta.twitter_site = pagemeta.twitter_site
            meta.twitter_author = pagemeta.twitter_author
            meta.gplus_type = pagemeta.gplus_type
            meta.gplus_author = pagemeta.gplus_author
            if meta.og_type == 'article':
                meta.og_publisher = pagemeta.og_publisher
                meta.og_author_url = pagemeta.og_author_url
                try:
                    from djangocms_page_tags.utils import get_title_tags, get_page_tags
                    tags = list(get_title_tags(page, language))
                    tags += list(get_page_tags(page))
                    meta.tag = ','.join([tag.name for tag in tags])
                except ImportError:
                    # djangocms-page-tags not available
                    pass
            if not meta.image and pagemeta.image:
                meta.image = pagemeta.image.canonical_url or pagemeta.image.url
            for item in pagemeta.extra.all():
                attribute = item.attribute
                if not attribute:
                    attribute = item.DEFAULT_ATTRIBUTE
                meta.extra_custom_props.append((attribute, item.name, item.value))
        except PageMeta.DoesNotExist:
            pass
        if meta.gplus_author and not meta.gplus_author.startswith('http'):
            if not meta.gplus_author.startswith('/'):
                meta.gplus_author = '{0}/{1}'.format(gplus_server, meta.gplus_author)
            else:
                meta.gplus_author = '{0}{1}'.format(gplus_server, meta.gplus_author)
        if page.publication_date:
            meta.published_time = page.publication_date.isoformat()
        if page.changed_date:
            meta.modified_time = page.changed_date.isoformat()
        if page.publication_end_date:
            meta.expiration_time = page.publication_end_date.isoformat()
        for attr, val in defaults.items():
            if not getattr(meta, attr, '') and val:
                setattr(meta, attr, val)
        meta.url = page.get_absolute_url(language)
    return meta

Response: Returns all the meta information for the given page in the given language.


Instruction:
How would you explain what the following Python 3 function does
def begin(self, address=MPR121_I2CADDR_DEFAULT, i2c=None, **kwargs):
        # Assume we're using platform's default I2C bus if none is specified.
        if i2c is None:
            import Adafruit_GPIO.I2C as I2C
            i2c = I2C
            # Require repeated start conditions for I2C register reads.  Unfortunately
            # the MPR121 is very sensitive and requires repeated starts to read all
            # the registers.
            I2C.require_repeated_start()
        # Save a reference to the I2C device instance for later communication.
        self._device = i2c.get_i2c_device(address, **kwargs)
        return self._reset()

Response:
Initialize communication with the MPR121.

        Can specify a custom I2C address for the device using the address
        parameter (defaults to 0x5A). Optional i2c parameter allows specifying a
        custom I2C bus source (defaults to platform's I2C bus).

        Returns True if communication with the MPR121 was established, otherwise
        returns False.


Instruction:
Can you tell what is the following Python 3 function doing
def set_thresholds(self, touch, release):
        assert touch >= 0 and touch <= 255, 'touch must be between 0-255 (inclusive)'
        assert release >= 0 and release <= 255, 'release must be between 0-255 (inclusive)'
        # Set the touch and release register value for all the inputs.
        for i in range(12):
            self._i2c_retry(self._device.write8, MPR121_TOUCHTH_0 + 2*i, touch)
            self._i2c_retry(self._device.write8, MPR121_RELEASETH_0 + 2*i, release)

Response:
Set the touch and release threshold for all inputs to the provided
        values.


Instruction:
Explain what the following Python 3 code does
def filtered_data(self, pin):
        assert pin >= 0 and pin < 12, 'pin must be between 0-11 (inclusive)'
        return self._i2c_retry(self._device.readU16LE, MPR121_FILTDATA_0L + pin*2)

Response: Return the value of the MPR121_FILTDATA register for the provided pin. Useful for debugging.


Instruction:
Can you create a Python 3 function that
returns the baseline data register value for the provided pin. Useful for debugging.

Response:
def baseline_data(self, pin):
        """Return baseline data register value for the provided pin (0-11).
        Useful for debugging.
        """
        assert pin >= 0 and pin < 12, 'pin must be between 0-11 (inclusive)'
        bl = self._i2c_retry(self._device.readU8, MPR121_BASELINE_0 + pin)
        return bl << 2


Instruction:
Explain what the following Python 3 code does
def touched(self):
        t = self._i2c_retry(self._device.readU16LE, MPR121_TOUCHSTATUS_L)
        return t & 0x0FFF

Response:
Return touch state of all pins as a 12 - bit value where each bit
        represents a pin and 0 not being touched.


Instruction:
Given the following Python 3 function, write the documentation
def is_touched(self, pin):
        assert pin >= 0 and pin < 12, 'pin must be between 0-11 (inclusive)'
        t = self.touched()
        return (t & (1 << pin)) > 0

Response:
Returns True if the specified pin is being touched otherwise returns
        False.


Instruction:
Here you have a function in Python 3, explain what it does
def profileit(func):
    def wrapper(*args, **kwargs):
        datafn = func.__name__ + ".profile" # Name the data file sensibly
        prof = cProfile.Profile()
        prof.enable()
        retval = prof.runcall(func, *args, **kwargs)
        prof.disable()
        stats = pstats.Stats(prof)
        try:
            stats.sort_stats('cumtime').print_stats()
        except KeyError:
            pass  # breaks in python 2.6
        return retval

    return wrapper

Response: Decorator straight up stolen from stackoverflow


Instruction:
How would you code a function in Python 3 to
return a function that filters log lines by the provided IP range.

Response:
def filter_ip_range(ip_range):
    """Filter :class:`.Line` objects by IP range.

    Both *192.168.1.203* and *192.168.1.10* are valid if the provided ip
    range is ``192.168.1`` whereas *192.168.2.103* is not valid (note the
    *.2.*).

    :param ip_range: IP range that you want to filter to.
    :type ip_range: string
    :returns: a function that filters by the provided IP range.
    :rtype: function
    """
    def filter_func(log_line):
        ip = log_line.get_ip()
        if ip:
            return ip.startswith(ip_range)

    return filter_func


Instruction:
Can you create a Python 3 function that
filters log lines by their response time.

Response:
def filter_slow_requests(slowness):
    """Filter :class:`.Line` objects by their response time.

    :param slowness: minimum time, in milliseconds, a server needs to answer
      a request. If the server takes more time than that the log line is
      accepted.
    :type slowness: string
    :returns: a function that filters by the server response time.
    :rtype: function
    """
    def filter_func(log_line):
        slowness_int = int(slowness)
        return slowness_int <= log_line.time_wait_response

    return filter_func


Instruction:
Write a Python 3 script for
filtering out log lines that have a waiting time less than max_waiting time.

Response:
def filter_wait_on_queues(max_waiting):
    """Filter :class:`.Line` objects by their queueing time in
    HAProxy.

    :param max_waiting: maximum time, in milliseconds, a request is waiting on
      HAProxy prior to be delivered to a backend server. If HAProxy takes less
      than that time the log line is counted.
    :type max_waiting: string
    :returns: a function that filters by HAProxy queueing time.
    :rtype: function
    """
    def filter_func(log_line):
        waiting = int(max_waiting)
        return waiting >= log_line.time_wait_queues

    return filter_func


Instruction:
Given the following Python 3 function, write the documentation
def filter_time_frame(start, delta):
    start_value = start
    delta_value = delta
    end_value = None

    if start_value is not '':
        start_value = _date_str_to_datetime(start_value)

    if delta_value is not '':
        delta_value = _delta_str_to_timedelta(delta_value)

    if start_value is not '' and delta_value is not '':
        end_value = start_value + delta_value

    def filter_func(log_line):
        if start_value is '':
            return True
        elif start_value > log_line.accept_date:
            return False

        if end_value is None:
            return True
        elif end_value < log_line.accept_date:
            return False

        return True

    return filter_func

Response: Filter log lines by their connection time.


Instruction:
How would you code a function in Python 3 to
filter the log lines by the response size.

Response:
def filter_response_size(size):
    """Filter :class:`.Line` objects by the response size (in bytes).

    Specially useful when looking for big file downloads.

    :param size: Minimum amount of bytes a response body weighted.
    :type size: string
    :returns: a function that filters by the response size.
    :rtype: function
    """
    if size.startswith('+'):
        size_value = int(size[1:])
    else:
        size_value = int(size)

    def filter_func(log_line):
        bytes_read = log_line.bytes_read
        if bytes_read.startswith('+'):
            bytes_read = int(bytes_read[1:])
        else:
            bytes_read = int(bytes_read)

        return bytes_read >= size_value

    return filter_func


Instruction:
Make a summary of the following Python 3 code
def _get_fields_for_model(model):
    fields = []
    for name in model._sa_class_manager:
        prop = getattr(model, name)
        if isinstance(prop.property, RelationshipProperty):
            for pk in prop.property.mapper.primary_key:
                fields.append('{0}.{1}'.format(name, pk.name))
        else:
            fields.append(name)
    return tuple(fields)

Response: Gets all of the fields on the model.


Instruction:
Write a Python 3 function that can
get the necessary relationships for the resource by inspecting the SQLAlchemy ORM model for relationships.

Response:
def _get_relationships(model):
    """
    Gets the necessary relationships for the resource
    by inspecting the sqlalchemy model for relationships.

    :param DeclarativeMeta model: The SQLAlchemy ORM model.
    :return: A tuple of Relationship/ListRelationship instances
        corresponding to the relationships on the Model.
    :rtype: tuple
    """
    relationships = []
    for name, relationship in inspect(model).relationships.items():
        class_ = relationship.mapper.class_
        if relationship.uselist:
            rel = ListRelationship(name, relation=class_.__name__)
        else:
            rel = Relationship(name, relation=class_.__name__)
        relationships.append(rel)
    return tuple(relationships)


Instruction:
Make a summary of the following Python 3 code
def create_resource(model, session_handler, resource_bases=(CRUDL,),
                    relationships=None, links=None, preprocessors=None,
                    postprocessors=None, fields=None, paginate_by=100,
                    auto_relationships=True, pks=None, create_fields=None,
                    update_fields=None, list_fields=None, append_slash=False):
        """
        Creates a ResourceBase subclass by inspecting a SQLAlchemy
        Model. This is somewhat more restrictive than explicitly
        creating managers and resources.  However, if you only need
        any of the basic CRUD+L operations,

        :param sqlalchemy.Model model:  This is the model that
            will be inspected to create a Resource and Manager from.
            By default, all of it's fields will be exposed, although
            this can be overridden using the fields attribute.
        :param tuple resource_bases: A tuple of ResourceBase subclasses.
            Defaults to the restmixins.CRUDL class only.  However if you only
            wanted Update and Delete you could pass in
            ```(restmixins.Update,  restmixins.Delete)``` which
            would cause the resource to inherit from those two.
            Additionally, you could create your own mixins and pass them in
            as the resource_bases
        :param tuple relationships: extra relationships to pass
            into the ResourceBase constructor.  If auto_relationships
            is set to True, then they will be appended to these relationships.
        :param tuple links: Extra links to pass into the ResourceBase as
            the class _links attribute.  Defaults to an empty tuple.
        :param tuple preprocessors: Preprocessors for the resource class attribute.
        :param tuple postprocessors: Postprocessors for the resource class attribute.
        :param ripozo_sqlalchemy.SessionHandler|ripozo_sqlalchemy.ScopedSessionHandler session_handler: A session handler
            to use when instantiating an instance of the Manager class created
            from the model.  This is responsible for getting and handling
            sessions in both normal cases and exceptions.
        :param tuple fields: The fields to expose on the api.  Defaults to
            all of the fields on the model.
        :param bool auto_relationships: If True, then the SQLAlchemy Model
            will be inspected for relationships and they will be automatically
            appended to the relationships on the resource class attribute.
        :param list create_fields: A list of the fields that are valid when
            creating a resource.  By default this will be the fields without
            any primary keys included
        :param list update_fields: A list of the fields that are valid when
            updating a resource.  By default this will be the fields without
            any primary keys included
        :param list list_fields: A list of the fields that will be returned
            when the list endpoint is requested.  Defaults to the fields
            attribute.
        :param bool append_slash: A flag to forcibly append slashes to
            the end of urls.
        :return: A ResourceBase subclass and AlchemyManager subclass
        :rtype: ResourceMetaClass
        """
        relationships = relationships or tuple()
        if auto_relationships:
            relationships += _get_relationships(model)
        links = links or tuple()
        preprocessors = preprocessors or tuple()
        postprocessors = postprocessors or tuple()
        pks = pks or _get_pks(model)
        fields = fields or _get_fields_for_model(model)
        list_fields = list_fields or fields

        create_fields = create_fields or [x for x in fields if x not in set(pks)]
        update_fields = update_fields or [x for x in fields if x not in set(pks)]

        manager_cls_attrs = dict(paginate_by=paginate_by, fields=fields, model=model,
                                 list_fields=list_fields, create_fields=create_fields,
                                 update_fields=update_fields)
        manager_class = type(str(model.__name__), (AlchemyManager,), manager_cls_attrs)
        manager = manager_class(session_handler)

        resource_cls_attrs = dict(preprocessors=preprocessors,
                                  postprocessors=postprocessors,
                                  _relationships=relationships, _links=links,
                                  pks=pks, manager=manager, append_slash=append_slash)
        res_class = ResourceMetaClass(str(model.__name__), resource_bases, resource_cls_attrs)
        return res_class

Response: Creates a new ResourceBase subclass from a SQLAlchemy model.


Instruction:
How would you explain what the following Python 3 function does
def _is_pickle_valid(self):
        if not os.path.exists(self._pickle_file):
            return False
        else:
            file_mtime = os.path.getmtime(self.logfile)
            pickle_mtime = os.path.getmtime(self._pickle_file)
            if file_mtime > pickle_mtime:
                return False
        return True

Response:
Logic to decide if the file should be processed or just needs to
            be loaded from its pickle data.


Instruction:
Create a Python 3 function to
load data from a pickle file.

Response:
def _load(self):
        """Load data from a pickle file. """
        with open(self._pickle_file, 'rb') as source:
            pickler = pickle.Unpickler(source)

            for attribute in self._pickle_attributes:
                pickle_data = pickler.load()
                setattr(self, attribute, pickle_data)


Instruction:
Explain what the following Python 3 code does
def _save(self):
        with open(self._pickle_file, 'wb') as source:
            pickler = pickle.Pickler(source, pickle.HIGHEST_PROTOCOL)

            for attribute in self._pickle_attributes:
                attr = getattr(self, attribute, None)
                pickler.dump(attr)

Response: Save the attributes defined on _pickle_attributes in a pickle file.


Instruction:
How would you code a function in Python 3 to
parse data from data stream and replace object lines.

Response:
def parse_data(self, logfile):
        """Parse data from data stream and replace object lines.

        :param logfile: [required] Log file data stream.
        :type logfile: str
        """

        for line in logfile:
            stripped_line = line.strip()
            parsed_line = Line(stripped_line)

            if parsed_line.valid:
                self._valid_lines.append(parsed_line)
            else:
                self._invalid_lines.append(stripped_line)
        self.total_lines = len(self._valid_lines) + len(self._invalid_lines)


Instruction:
Write a Python 3 function for
filtering the log lines by a given function.

Response:
def filter(self, filter_func, reverse=False):
        """Filter current log lines by a given filter function.

        This allows to drill down data out of the log file by filtering the
        relevant log lines to analyze.

        For example, filter by a given IP so only log lines for that IP are
        further processed with commands (top paths, http status counter...).

        :param filter_func: [required] Filter method, see filters.py for all
          available filters.
        :type filter_func: function
        :param reverse: negate the filter (so accept all log lines that return
          ``False``).
        :type reverse: boolean
        :returns: a new instance of Log containing only log lines
          that passed the filter function.
        :rtype: :class:`Log`
        """
        new_log_file = Log()
        new_log_file.logfile = self.logfile

        new_log_file.total_lines = 0

        new_log_file._valid_lines = []
        new_log_file._invalid_lines = self._invalid_lines[:]

        # add the reverse conditional outside the loop to keep the loop as
        # straightforward as possible
        if not reverse:
            for i in self._valid_lines:
                if filter_func(i):
                    new_log_file.total_lines += 1
                    new_log_file._valid_lines.append(i)
        else:
            for i in self._valid_lines:
                if not filter_func(i):
                    new_log_file.total_lines += 1
                    new_log_file._valid_lines.append(i)

        return new_log_file


Instruction:
Here you have a function in Python 3, explain what it does
def commands(cls):
        cmds = [cmd[4:] for cmd in dir(cls) if cmd.startswith('cmd_')]
        return cmds

Response: Returns a list of all methods that start with cmd_.


Instruction:
Can you tell what is the following Python 3 function doing
def cmd_http_methods(self):
        methods = defaultdict(int)
        for line in self._valid_lines:
            methods[line.http_request_method] += 1
        return methods

Response: Reports a breakdown of how many HTTP methods have been made per HTTPHTTPMethod.


Instruction:
Implement a function in Python 3 to
report a breakdown of how many requests have been made per IP.

Response:
def cmd_ip_counter(self):
        """Reports a breakdown of how many requests have been made per IP.

        .. note::
          To enable this command requests need to provide a header with the
          forwarded IP (usually X-Forwarded-For) and be it the only header
          being captured.
        """
        ip_counter = defaultdict(int)
        for line in self._valid_lines:
            ip = line.get_ip()
            if ip is not None:
                ip_counter[ip] += 1
        return ip_counter


Instruction:
Can you tell what is the following Python 3 function doing
def cmd_status_codes_counter(self):
        status_codes = defaultdict(int)
        for line in self._valid_lines:
            status_codes[line.status_code] += 1
        return status_codes

Response: Generate statistics about HTTP status codes. 404 500 and so on.


Instruction:
How would you code a function in Python 3 to
generate statistics about HTTP requests path.

Response:
def cmd_request_path_counter(self):
        """Generate statistics about HTTP requests' path."""
        paths = defaultdict(int)
        for line in self._valid_lines:
            paths[line.http_request_path] += 1
        return paths


Instruction:
Can you generate the documentation for the following Python 3 function
def cmd_slow_requests(self):
        slow_requests = [
            line.time_wait_response
            for line in self._valid_lines
            if line.time_wait_response > 1000
        ]
        return slow_requests

Response:
List all requests that took a certain amount of time to be
        processed.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def cmd_average_response_time(self):
        average = [
            line.time_wait_response
            for line in self._valid_lines
            if line.time_wait_response >= 0
        ]

        divisor = float(len(average))
        if divisor > 0:
            return sum(average) / float(len(average))
        return 0

Response: Returns the average response time of all non aborted requests.


Instruction:
How would you code a function in Python 3 to
return the average queue time of all non aborted requests.

Response:
def cmd_average_waiting_time(self):
        """Returns the average queue time of all, non aborted, requests."""
        average = [
            line.time_wait_queues
            for line in self._valid_lines
            if line.time_wait_queues >= 0
        ]

        divisor = float(len(average))
        if divisor > 0:
            return sum(average) / float(len(average))
        return 0


Instruction:
Implement a Python 3 function for
generating statistics regarding how many requests were processed by each downstream server.

Response:
def cmd_server_load(self):
        """Generate statistics regarding how many requests were processed by
        each downstream server.
        """
        servers = defaultdict(int)
        for line in self._valid_lines:
            servers[line.server_name] += 1
        return servers


Instruction:
Explain what the following Python 3 code does
def cmd_queue_peaks(self):
        threshold = 1
        peaks = []
        current_peak = 0
        current_queue = 0

        current_span = 0
        first_on_queue = None

        for line in self._valid_lines:
            current_queue = line.queue_backend

            if current_queue > 0:
                current_span += 1

                if first_on_queue is None:
                    first_on_queue = line.accept_date

            if current_queue == 0 and current_peak > threshold:
                data = {
                    'peak': current_peak,
                    'span': current_span,
                    'first': first_on_queue,
                    'last': line.accept_date,
                }
                peaks.append(data)
                current_peak = 0
                current_span = 0
                first_on_queue = None

            if current_queue > current_peak:
                current_peak = current_queue

        # case of a series that does not end
        if current_queue > 0 and current_peak > threshold:
            data = {
                'peak': current_peak,
                'span': current_span,
                'first': first_on_queue,
                'last': line.accept_date,
            }
            peaks.append(data)

        return peaks

Response: Generate a list of the requests peaks on the queue.


Instruction:
Write a Python 3 script for
generating statistics on how many requests are made via HTTP and how many requests are made via SSL.

Response:
def cmd_connection_type(self):
        """Generates statistics on how many requests are made via HTTP and how
        many are made via SSL.

        .. note::
          This only works if the request path contains the default port for
          SSL (443).

        .. warning::
          The ports are hardcoded, they should be configurable.
        """
        https = 0
        non_https = 0
        for line in self._valid_lines:
            if line.is_https():
                https += 1
            else:
                non_https += 1
        return https, non_https


Instruction:
Can you tell what is the following Python 3 function doing
def cmd_requests_per_minute(self):
        if len(self._valid_lines) == 0:
            return

        current_minute = self._valid_lines[0].accept_date
        current_minute_counter = 0
        requests = []
        one_minute = timedelta(minutes=1)

        def format_and_append(append_to, date, counter):
            seconds_and_micro = timedelta(
                seconds=date.second,
                microseconds=date.microsecond,
            )
            minute_formatted = date - seconds_and_micro
            append_to.append((minute_formatted, counter))

        # note that _valid_lines is kept sorted by date
        for line in self._valid_lines:
            line_date = line.accept_date
            if line_date - current_minute < one_minute and \
                    line_date.minute == current_minute.minute:
                current_minute_counter += 1

            else:
                format_and_append(
                    requests,
                    current_minute,
                    current_minute_counter,
                )
                current_minute_counter = 1
                current_minute = line_date

        if current_minute_counter > 0:
            format_and_append(
                requests,
                current_minute,
                current_minute_counter,
            )

        return requests

Response: Generates statistics on how many requests were made per minute.


Instruction:
Make a summary of the following Python 3 code
def cmd_print(self):
        if not self._valid_lines:
            return ''
        return '\n'.join([line.raw_line for line in self._valid_lines]) + '\n'

Response: Returns the raw lines to be printed.


Instruction:
Write a Python 3 function for
sorting the log lines by the accept date.

Response:
def _sort_lines(self):
        """Haproxy writes its logs after having gathered all information
        related to each specific connection. A simple request can be
        really quick but others can be really slow, thus even if one connection
        is logged later, it could have been accepted before others that are
        already processed and logged.

        This method sorts all valid log lines by their acceptance date,
        providing the real order in which connections where made to the server.
        """
        self._valid_lines = sorted(
            self._valid_lines,
            key=lambda line: line.accept_date,
        )


Instruction:
Write a Python 3 script for
sorting a dictionary with at least two fields on each of them sorting by the second element.

Response:
def _sort_and_trim(data, reverse=False):
        """Sorts a dictionary with at least two fields on each of them sorting
        by the second element.

        .. warning::
          Right now is hardcoded to 10 elements, improve the command line
          interface to allow to send parameters to each command or globally.
        """
        threshold = 10
        data_list = data.items()
        data_list = sorted(
            data_list,
            key=lambda data_info: data_info[1],
            reverse=reverse,
        )
        return data_list[:threshold]


Instruction:
Write a Python 3 function that can
wrap a function that actually accesses the database. It injects a session into the method and attempts to handle it after the function has run. :param method func: The method that is interacting with the database.

Response:
def db_access_point(func):
    """
    Wraps a function that actually accesses the database.
    It injects a session into the method and attempts to handle
    it after the function has run.

    :param method func: The method that is interacting with the database.
    """
    @wraps(func)
    def wrapper(self, *args, **kwargs):
        """
        Wrapper responsible for handling
        sessions
        """
        session = self.session_handler.get_session()
        try:
            resp = func(self, session, *args, **kwargs)
        except Exception as exc:
            self.session_handler.handle_session(session, exc=exc)
            raise exc
        else:
            self.session_handler.handle_session(session)
            return resp
    return wrapper


Instruction:
Can you create a Python 3 function that
returns the python type of the column with the given name.

Response:
def _get_field_python_type(model, name):
        """
        Gets the python type for the attribute on the model
        with the name provided.

        :param Model model: The SqlAlchemy model class.
        :param unicode name: The column name on the model
            that you are attempting to get the python type.
        :return: The python type of the column
        :rtype: type
        """
        try:
            return getattr(model, name).property.columns[0].type.python_type
        except AttributeError:  # It's a relationship
            parts = name.split('.')
            model = getattr(model, parts.pop(0)).comparator.mapper.class_
            return AlchemyManager._get_field_python_type(model, '.'.join(parts))
        except NotImplementedError:
            # This is for pickle type columns.
            return object


Instruction:
Can you write a function in Python 3 where it
takes a field name and returns an appropriate BaseField instance for that column.

Response:
def get_field_type(cls, name):
        """
        Takes a field name and gets an appropriate BaseField instance
        for that column.  It inspects the Model that is set on the manager
        to determine what the BaseField subclass should be.

        :param unicode name:
        :return: A BaseField subclass that is appropriate for
            translating a string input into the appropriate format.
        :rtype: ripozo.viewsets.fields.base.BaseField
        """
        python_type = cls._get_field_python_type(cls.model, name)
        if python_type in _COLUMN_FIELD_MAP:
            field_class = _COLUMN_FIELD_MAP[python_type]
            return field_class(name)
        return BaseField(name)


Instruction:
Create a Python 3 function to
create a new instance of the self. model and persists it to the database.

Response:
def create(self, session, values, *args, **kwargs):
        """
        Creates a new instance of the self.model
        and persists it to the database.

        :param dict values: The dictionary of values to
            set on the model.  The key is the column name
            and the value is what it will be set to.  If
            the cls._create_fields is defined then it will
            use those fields.  Otherwise, it will use the
            fields defined in cls.fields
        :param Session session: The sqlalchemy session
        :return: The serialized model.  It will use the self.fields
            attribute for this.
        :rtype: dict
        """
        model = self.model()
        model = self._set_values_on_model(model, values, fields=self.create_fields)
        session.add(model)
        session.commit()
        return self.serialize_model(model)


Instruction:
How would you explain what the following Python 3 function does
def retrieve(self, session, lookup_keys, *args, **kwargs):
        model = self._get_model(lookup_keys, session)
        return self.serialize_model(model)

Response: Retrieves a model using the lookup keys provided.


Instruction:
Write a Python 3 script to
retrieve a list of the models for this manager.

Response:
def retrieve_list(self, session, filters, *args, **kwargs):
        """
        Retrieves a list of the model for this manager.
        It is restricted by the filters provided.

        :param Session session: The SQLAlchemy session to use
        :param dict filters: The filters to restrict the returned
            models on
        :return: A tuple of the list of dictionary representation
            of the models and the dictionary of meta data
        :rtype: list, dict
        """
        query = self.queryset(session)
        translator = IntegerField('tmp')
        pagination_count = translator.translate(
            filters.pop(self.pagination_count_query_arg, self.paginate_by)
        )
        pagination_pk = translator.translate(
            filters.pop(self.pagination_pk_query_arg, 1)
        )
        pagination_pk -= 1  # logic works zero based. Pagination shouldn't be though

        query = query.filter_by(**filters)

        if pagination_pk:
            query = query.offset(pagination_pk * pagination_count)
        if pagination_count:
            query = query.limit(pagination_count + 1)

        count = query.count()
        next_link = None
        previous_link = None
        if count > pagination_count:
            next_link = {self.pagination_pk_query_arg: pagination_pk + 2,
                         self.pagination_count_query_arg: pagination_count}
        if pagination_pk > 0:
            previous_link = {self.pagination_pk_query_arg: pagination_pk,
                             self.pagination_count_query_arg: pagination_count}

        field_dict = self.dot_field_list_to_dict(self.list_fields)
        props = self.serialize_model(query[:pagination_count], field_dict=field_dict)
        meta = dict(links=dict(next=next_link, previous=previous_link))
        return props, meta


Instruction:
Can you create a Python 3 function that
updates the model with the specified lookup_keys and returns the dictified object.

Response:
def update(self, session, lookup_keys, updates, *args, **kwargs):
        """
        Updates the model with the specified lookup_keys and returns
        the dictified object.

        :param Session session: The SQLAlchemy session to use
        :param dict lookup_keys: A dictionary mapping the fields
            and their expected values
        :param dict updates: The columns and the values to update
            them to.
        :return: The dictionary of keys and values for the retrieved
            model.  The only values returned will be those specified by
            fields attrbute on the class
        :rtype: dict
        :raises: NotFoundException
        """
        model = self._get_model(lookup_keys, session)
        model = self._set_values_on_model(model, updates, fields=self.update_fields)
        session.commit()
        return self.serialize_model(model)