Instruction:
Can you tell what is the following Python 3 function doing
def copy(self):
        return self.__class__(self, key=self._key, load=self._load)

Response: Return a shallow copy of the sorted list.


Instruction:
Here you have a function in Python 3, explain what it does
def append(self, val):
        _maxes, _lists, _keys = self._maxes, self._lists, self._keys

        key = self._key(val)

        if not _maxes:
            _maxes.append(key)
            _keys.append([key])
            _lists.append([val])
            self._len = 1
            return

        pos = len(_keys) - 1

        if key < _keys[pos][-1]:
            msg = '{0} not in sort order at index {1}'.format(repr(val), self._len)
            raise ValueError(msg)

        _maxes[pos] = key
        _keys[pos].append(key)
        _lists[pos].append(val)
        self._len += 1
        self._expand(pos)

Response: Append the element val to the list. Raises a ValueError if the element val would violate the sort order.


Instruction:
Write a Python 3 function that can
extend the list by appending all elements from the given list. Raises a ValueError if the sort order is violated.

Response:
def extend(self, values):
        """
        Extend the list by appending all elements from the *values*. Raises a
        ValueError if the sort order would be violated.
        """
        _maxes, _keys, _lists, _load = self._maxes, self._keys, self._lists, self._load

        if not isinstance(values, list):
            values = list(values)

        keys = list(map(self._key, values))

        if any(keys[pos - 1] > keys[pos]
               for pos in range(1, len(keys))):
            raise ValueError('given sequence not in sort order')

        offset = 0

        if _maxes:
            if keys[0] < _keys[-1][-1]:
                msg = '{0} not in sort order at index {1}'.format(repr(values[0]), self._len)
                raise ValueError(msg)

            if len(_keys[-1]) < self._half:
                _lists[-1].extend(values[:_load])
                _keys[-1].extend(keys[:_load])
                _maxes[-1] = _keys[-1][-1]
                offset = _load

        len_keys = len(_keys)

        for idx in range(offset, len(keys), _load):
            _lists.append(values[idx:(idx + _load)])
            _keys.append(keys[idx:(idx + _load)])
            _maxes.append(_keys[-1][-1])

        _index = self._index

        if len_keys == len(_keys):
            len_index = len(_index)
            if len_index > 0:
                len_values = len(values)
                child = len_index - 1
                while child:
                    _index[child] += len_values
                    child = (child - 1) >> 1
                _index[0] += len_values
        else:
            del _index[:]

        self._len += len(values)


Instruction:
Write a Python 3 script to
remove and returns the item at the specified index.

Response:
def pop(self, idx=-1):
        """
        Remove and return item at *idx* (default last).  Raises IndexError if
        list is empty or index is out of range.  Negative indices are supported,
        as for slice indices.
        """
        if (idx < 0 and -idx > self._len) or (idx >= self._len):
            raise IndexError('pop index out of range')

        pos, idx = self._pos(idx)
        val = self._lists[pos][idx]
        self._delete(pos, idx)

        return val


Instruction:
How would you code a function in Python 3 to
function decorator for methods not implemented in Python 2. 6.

Response:
def not26(func):
    """Function decorator for methods not implemented in Python 2.6."""

    @wraps(func)
    def errfunc(*args, **kwargs):
        raise NotImplementedError

    if hexversion < 0x02070000:
        return errfunc
    else:
        return func


Instruction:
Given the following Python 3 function, write the documentation
def copy(self):
        return self.__class__(self._key, self._load, self._iteritems())

Response: Return a shallow copy of the sorted dictionary.


Instruction:
Can you write a function in Python 3 where it
removes the entry with the given key from the dictionary. If the key is not in the dictionary a KeyError is raised. If the default is given a KeyError is raised.

Response:
def pop(self, key, default=_NotGiven):
        """
        If *key* is in the dictionary, remove it and return its value,
        else return *default*. If *default* is not given and *key* is not in
        the dictionary, a KeyError is raised.
        """
        if key in self:
            self._list_remove(key)
            return self._pop(key)
        else:
            if default is _NotGiven:
                raise KeyError(key)
            else:
                return default


Instruction:
Write a Python 3 function that can
set the value of the key to the given default value. If the key is not in the dictionary insert it with a value of default. If the key is not in the dictionary insert it with a value of None.

Response:
def setdefault(self, key, default=None):
        """
        If *key* is in the dictionary, return its value.  If not, insert *key*
        with a value of *default* and return *default*.  *default* defaults to
        ``None``.
        """
        if key in self:
            return self[key]
        else:
            self._setitem(key, default)
            self._list_add(key)
            return default


Instruction:
How would you code a function in Python 3 to
return the index of the key value in the keyview.

Response:
def index(self, value, start=None, stop=None):
        """
        Return the smallest *k* such that `keysview[k] == value` and `start <= k
        < end`.  Raises `KeyError` if *value* is not present.  *stop* defaults
        to the end of the set.  *start* defaults to the beginning.  Negative
        indexes are supported, as for slice indices.
        """
        return self._list.index(value, start, stop)


Instruction:
Can you generate the documentation for the following Python 3 function
def create_summary(self):
        if not self.ignore_self:
            res = summary.summarize(muppy.get_objects())
        else:
            # If the user requested the data required to store summaries to be
            # ignored in the summaries, we need to identify all objects which
            # are related to each summary stored.
            # Thus we build a list of all objects used for summary storage as
            # well as a dictionary which tells us how often an object is
            # referenced by the summaries.
            # During this identification process, more objects are referenced,
            # namely int objects identifying referenced objects as well as the
            # correspondind count.
            # For all these objects it will be checked wether they are
            # referenced from outside the monitor's scope. If not, they will be
            # subtracted from the snapshot summary, otherwise they are
            # included (as this indicates that they are relevant to the
            # application).

            all_of_them = []  # every single object
            ref_counter = {}  # how often it is referenced; (id(o), o) pairs
            def store_info(o):
                all_of_them.append(o)
                if id(o) in ref_counter:
                    ref_counter[id(o)] += 1
                else:
                    ref_counter[id(o)] = 1

            # store infos on every single object related to the summaries
            store_info(self.summaries)
            for k, v in self.summaries.items():
                store_info(k)
                summary._traverse(v, store_info)

            # do the summary
            res = summary.summarize(muppy.get_objects())

            # remove ids stored in the ref_counter
            for _id in ref_counter:
                # referenced in frame, ref_counter, ref_counter.keys()
                if len(gc.get_referrers(_id)) == (3):
                    summary._subtract(res, _id)
            for o in all_of_them:
                # referenced in frame, summary, all_of_them
                if len(gc.get_referrers(o)) == (ref_counter[id(o)] + 2):
                    summary._subtract(res, o)

        return res

Response: Create a summary for the object which is referenced by the object.


Instruction:
Can you implement a function in Python 3 that
computes the diff between two summaries.

Response:
def diff(self, summary1=None, summary2=None):
        """Compute diff between to summaries.

        If no summary is provided, the diff from the last to the current
        summary is used. If summary1 is provided the diff from summary1
        to the current summary is used. If summary1 and summary2 are
        provided, the diff between these two is used.

        """
        res = None
        if summary2 is None:
            self.s1 = self.create_summary()
            if summary1 is None:
                res = summary.get_diff(self.s0, self.s1)
            else:
                res = summary.get_diff(summary1, self.s1)
            self.s0 = self.s1
        else:
            if summary1 is not None:
                res = summary.get_diff(summary1, summary2)
            else:
                raise ValueError("You cannot provide summary2 without summary1.")
        return summary._sweep(res)


Instruction:
Can you tell what is the following Python 3 function doing
def print_diff(self, summary1=None, summary2=None):
        summary.print_(self.diff(summary1=summary1, summary2=summary2))

Response: Compute diff between to summaries and print it.


Instruction:
How would you explain what the following Python 3 function does
def _get_objects(self, ignore=[]):
        def remove_ignore(objects, ignore=[]):
            # remove all objects listed in the ignore list
            res = []
            for o in objects:
                if not compat.object_in_list(o, ignore):
                    res.append(o)
            return res

        tmp = gc.get_objects()
        ignore.append(inspect.currentframe()) #PYCHOK change ignore
        ignore.append(self) #PYCHOK change ignore
        if hasattr(self, 'o0'): ignore.append(self.o0) #PYCHOK change ignore
        if hasattr(self, 'o1'): ignore.append(self.o1) #PYCHOK change ignore
        ignore.append(ignore) #PYCHOK change ignore
        ignore.append(remove_ignore) #PYCHOK change ignore
        # this implies that referenced objects are also ignored
        tmp = remove_ignore(tmp, ignore)
        res = []
        for o in tmp:
            # gc.get_objects returns only container objects, but we also want
            # the objects referenced by them
            refs = muppy.get_referents(o)
            for ref in refs:
                if not muppy._is_containerobject(ref):
                    # we already got the container objects, now we only add
                    # non-container objects
                    res.append(ref)
        res.extend(tmp)
        res = muppy._remove_duplicates(res)
        if ignore is not None:
            # repeat to filter out objects which may have been referenced
            res = remove_ignore(res, ignore)
        # manual cleanup, see comment above
        del ignore[:]
        return res

Response: Get all currently existing objects.


Instruction:
Make a summary of the following Python 3 code
def get_diff(self, ignore=[]):
        # ignore this and the caller frame
        ignore.append(inspect.currentframe()) #PYCHOK change ignore
        self.o1 = self._get_objects(ignore)
        diff = muppy.get_diff(self.o0, self.o1)
        self.o0 = self.o1
        # manual cleanup, see comment above
        del ignore[:] #PYCHOK change ignore
        return diff

Response: Get the diff to the last time the state of objects was measured.


Instruction:
How would you implement a function in Python 3 that
prints the diff to the last time the state of objects was measured.

Response:
def print_diff(self, ignore=[]):
        """Print the diff to the last time the state of objects was measured.

        keyword arguments
        ignore -- list of objects to ignore
        """
        # ignore this and the caller frame
        ignore.append(inspect.currentframe()) #PYCHOK change ignore
        diff = self.get_diff(ignore)
        print("Added objects:")
        summary.print_(summary.summarize(diff['+']))
        print("Removed objects:")
        summary.print_(summary.summarize(diff['-']))
        # manual cleanup, see comment above
        del ignore[:]


Instruction:
Make a summary of the following Python 3 code
def hamming(seq1, seq2, normalized=False):
	L = len(seq1)
	if L != len(seq2):
		raise ValueError("expected two strings of the same length")
	if L == 0:
		return 0.0 if normalized else 0  # equal
	dist = sum(c1 != c2 for c1, c2 in zip(seq1, seq2))
	if normalized:
		return dist / float(L)
	return dist

Response: Compute the Hamming distance between the two sequences seq1 and seq2.


Instruction:
Create a Python 3 function for
computing the Jaccard distance between the two sequences seq1 and seq2.

Response:
def jaccard(seq1, seq2):
	"""Compute the Jaccard distance between the two sequences `seq1` and `seq2`.
	They should contain hashable items.

	The return value is a float between 0 and 1, where 0 means equal, and 1 totally different.
	"""
	set1, set2 = set(seq1), set(seq2)
	return 1 - len(set1 & set2) / float(len(set1 | set2))


Instruction:
Can you tell what is the following Python 3 function doing
def sorensen(seq1, seq2):
	set1, set2 = set(seq1), set(seq2)
	return 1 - (2 * len(set1 & set2) / float(len(set1) + len(set2)))

Response: Compute the Sorensen distance between the two sequences seq1 and seq2.


Instruction:
How would you explain what the following Python 3 function does
def lcsubstrings(seq1, seq2, positions=False):
	L1, L2 = len(seq1), len(seq2)
	ms = []
	mlen = last = 0
	if L1 < L2:
		seq1, seq2 = seq2, seq1
		L1, L2 = L2, L1

	column = array('L', range(L2))

	for i in range(L1):
		for j in range(L2):
			old = column[j]
			if seq1[i] == seq2[j]:
				if i == 0 or j == 0:
					column[j] = 1
				else:
					column[j] = last + 1
				if column[j] > mlen:
					mlen = column[j]
					ms = [(i, j)]
				elif column[j] == mlen:
					ms.append((i, j))
			else:
				column[j] = 0
			last = old

	if positions:
		return (mlen, tuple((i - mlen + 1, j - mlen + 1) for i, j in ms if ms))
	return set(seq1[i - mlen + 1:i + 1] for i, _ in ms if ms)

Response: Find the longest common substring in the sequences seq1 and seq2.


Instruction:
Explain what the following Python 3 code does
def background_color(self, node, depth):
        if self.color_mapping is None:
            self.color_mapping = {}
        color = self.color_mapping.get(node.key)
        if color is None:
            depth = len(self.color_mapping)
            red = (depth * 10) % 255
            green = 200 - ((depth * 5) % 200)
            blue = (depth * 25) % 200
            self.color_mapping[node.key] = color = wx.Colour(red, green, blue)
        return color

Response: Create a color for each node


Instruction:
Given the following Python 3 function, write the documentation
def SetPercentage(self, percent, total):
        self.percentageView = percent
        self.total = total

Response: Set whether to display percentage values and total for doing so.


Instruction:
How would you explain what the following Python 3 function does
def runprofilerandshow(funcname, profilepath, argv='', *args, **kwargs):
    '''
    Run a functions profiler and show it in a GUI visualisation using RunSnakeRun
    Note: can also use calibration for more exact results
    '''
    functionprofiler.runprofile(funcname+'(\''+argv+'\')', profilepath, *args, **kwargs)
    print 'Showing profile (windows should open in the background)'; sys.stdout.flush();
    functionprofiler.browseprofilegui(profilepath)

Response: Run a functions profiler and show it in a GUI visualisation using RunSnakeRun


Instruction:
How would you code a function in Python 3 to
make a call graph Note: be sure to install GraphViz prior to printing the dot graph!

Response:
def callgraph(func):
    ''' Makes a call graph
    Note: be sure to install GraphViz prior to printing the dot graph!
    '''
    import pycallgraph
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        pycallgraph.start_trace()
        func(*args, **kwargs)
        pycallgraph.save_dot('callgraph.log')
        pycallgraph.make_dot_graph('callgraph.png')
        #pycallgraph.make_dot_graph('callgraph.jpg', format='jpg', tool='neato')
    return wrapper


Instruction:
Create a Python 3 function to
convert a long integer to a byte string.

Response:
def _long2bytes(n, blocksize=0):
    """Convert a long integer to a byte string.

    If optional blocksize is given and greater than zero, pad the front
    of the byte string with binary zeros so that the length is a multiple
    of blocksize.
    """

    # After much testing, this algorithm was deemed to be the fastest.
    s = ''
    pack = struct.pack
    while n > 0:
        ### CHANGED FROM '>I' TO '<I'. (DCG)
        s = pack('<I', n & 0xffffffffL) + s
        ### --------------------------
        n = n >> 32

    # Strip off leading zeros.
    for i in range(len(s)):
        if s[i] <> '\000':
            break
    else:
        # Only happens when n == 0.
        s = '\000'
        i = 0

    s = s[i:]

    # Add back some pad bytes. This could be done more efficiently
    # w.r.t. the de-padding being done above, but sigh...
    if blocksize > 0 and len(s) % blocksize:
        s = (blocksize - len(s) % blocksize) * '\000' + s

    return s


Instruction:
Can you create a Python 3 function that
wraps for calling functions F G H and I from Appl. Crypto.

Response:
def XX(func, a, b, c, d, x, s, ac):
    """Wrapper for call distribution to functions F, G, H and I.

    This replaces functions FF, GG, HH and II from "Appl. Crypto.
    Rotation is separate from addition to prevent recomputation
    (now summed-up in one function).
    """

    res = 0L
    res = res + a + func(b, c, d)
    res = res + x
    res = res + ac
    res = res & 0xffffffffL
    res = _rotateLeft(res, s)
    res = res & 0xffffffffL
    res = res + b

    return res & 0xffffffffL


Instruction:
How would you explain what the following Python 3 function does
def init(self):
        "Initialize the message-digest and set all fields to zero."

        self.length = 0L
        self.input = []

        # Load magic initialization constants.
        self.A = 0x67452301L
        self.B = 0xefcdab89L
        self.C = 0x98badcfeL
        self.D = 0x10325476L

Response: Initialize the message - digest and set all fields to zero.


Instruction:
Create a Python 3 function for
transforming the input array into a new array of the same size.

Response:
def _transform(self, inp):
        """Basic MD5 step transforming the digest based on the input.

        Note that if the Mysterious Constants are arranged backwards
        in little-endian order and decrypted with the DES they produce
        OCCULT MESSAGES!
        """

        a, b, c, d = A, B, C, D = self.A, self.B, self.C, self.D

        # Round 1.

        S11, S12, S13, S14 = 7, 12, 17, 22

        a = XX(F, a, b, c, d, inp[ 0], S11, 0xD76AA478L) # 1
        d = XX(F, d, a, b, c, inp[ 1], S12, 0xE8C7B756L) # 2
        c = XX(F, c, d, a, b, inp[ 2], S13, 0x242070DBL) # 3
        b = XX(F, b, c, d, a, inp[ 3], S14, 0xC1BDCEEEL) # 4
        a = XX(F, a, b, c, d, inp[ 4], S11, 0xF57C0FAFL) # 5
        d = XX(F, d, a, b, c, inp[ 5], S12, 0x4787C62AL) # 6
        c = XX(F, c, d, a, b, inp[ 6], S13, 0xA8304613L) # 7
        b = XX(F, b, c, d, a, inp[ 7], S14, 0xFD469501L) # 8
        a = XX(F, a, b, c, d, inp[ 8], S11, 0x698098D8L) # 9
        d = XX(F, d, a, b, c, inp[ 9], S12, 0x8B44F7AFL) # 10
        c = XX(F, c, d, a, b, inp[10], S13, 0xFFFF5BB1L) # 11
        b = XX(F, b, c, d, a, inp[11], S14, 0x895CD7BEL) # 12
        a = XX(F, a, b, c, d, inp[12], S11, 0x6B901122L) # 13
        d = XX(F, d, a, b, c, inp[13], S12, 0xFD987193L) # 14
        c = XX(F, c, d, a, b, inp[14], S13, 0xA679438EL) # 15
        b = XX(F, b, c, d, a, inp[15], S14, 0x49B40821L) # 16

        # Round 2.

        S21, S22, S23, S24 = 5, 9, 14, 20

        a = XX(G, a, b, c, d, inp[ 1], S21, 0xF61E2562L) # 17
        d = XX(G, d, a, b, c, inp[ 6], S22, 0xC040B340L) # 18
        c = XX(G, c, d, a, b, inp[11], S23, 0x265E5A51L) # 19
        b = XX(G, b, c, d, a, inp[ 0], S24, 0xE9B6C7AAL) # 20
        a = XX(G, a, b, c, d, inp[ 5], S21, 0xD62F105DL) # 21
        d = XX(G, d, a, b, c, inp[10], S22, 0x02441453L) # 22
        c = XX(G, c, d, a, b, inp[15], S23, 0xD8A1E681L) # 23
        b = XX(G, b, c, d, a, inp[ 4], S24, 0xE7D3FBC8L) # 24
        a = XX(G, a, b, c, d, inp[ 9], S21, 0x21E1CDE6L) # 25
        d = XX(G, d, a, b, c, inp[14], S22, 0xC33707D6L) # 26
        c = XX(G, c, d, a, b, inp[ 3], S23, 0xF4D50D87L) # 27
        b = XX(G, b, c, d, a, inp[ 8], S24, 0x455A14EDL) # 28
        a = XX(G, a, b, c, d, inp[13], S21, 0xA9E3E905L) # 29
        d = XX(G, d, a, b, c, inp[ 2], S22, 0xFCEFA3F8L) # 30
        c = XX(G, c, d, a, b, inp[ 7], S23, 0x676F02D9L) # 31
        b = XX(G, b, c, d, a, inp[12], S24, 0x8D2A4C8AL) # 32

        # Round 3.

        S31, S32, S33, S34 = 4, 11, 16, 23

        a = XX(H, a, b, c, d, inp[ 5], S31, 0xFFFA3942L) # 33
        d = XX(H, d, a, b, c, inp[ 8], S32, 0x8771F681L) # 34
        c = XX(H, c, d, a, b, inp[11], S33, 0x6D9D6122L) # 35
        b = XX(H, b, c, d, a, inp[14], S34, 0xFDE5380CL) # 36
        a = XX(H, a, b, c, d, inp[ 1], S31, 0xA4BEEA44L) # 37
        d = XX(H, d, a, b, c, inp[ 4], S32, 0x4BDECFA9L) # 38
        c = XX(H, c, d, a, b, inp[ 7], S33, 0xF6BB4B60L) # 39
        b = XX(H, b, c, d, a, inp[10], S34, 0xBEBFBC70L) # 40
        a = XX(H, a, b, c, d, inp[13], S31, 0x289B7EC6L) # 41
        d = XX(H, d, a, b, c, inp[ 0], S32, 0xEAA127FAL) # 42
        c = XX(H, c, d, a, b, inp[ 3], S33, 0xD4EF3085L) # 43
        b = XX(H, b, c, d, a, inp[ 6], S34, 0x04881D05L) # 44
        a = XX(H, a, b, c, d, inp[ 9], S31, 0xD9D4D039L) # 45
        d = XX(H, d, a, b, c, inp[12], S32, 0xE6DB99E5L) # 46
        c = XX(H, c, d, a, b, inp[15], S33, 0x1FA27CF8L) # 47
        b = XX(H, b, c, d, a, inp[ 2], S34, 0xC4AC5665L) # 48

        # Round 4.

        S41, S42, S43, S44 = 6, 10, 15, 21

        a = XX(I, a, b, c, d, inp[ 0], S41, 0xF4292244L) # 49
        d = XX(I, d, a, b, c, inp[ 7], S42, 0x432AFF97L) # 50
        c = XX(I, c, d, a, b, inp[14], S43, 0xAB9423A7L) # 51
        b = XX(I, b, c, d, a, inp[ 5], S44, 0xFC93A039L) # 52
        a = XX(I, a, b, c, d, inp[12], S41, 0x655B59C3L) # 53
        d = XX(I, d, a, b, c, inp[ 3], S42, 0x8F0CCC92L) # 54
        c = XX(I, c, d, a, b, inp[10], S43, 0xFFEFF47DL) # 55
        b = XX(I, b, c, d, a, inp[ 1], S44, 0x85845DD1L) # 56
        a = XX(I, a, b, c, d, inp[ 8], S41, 0x6FA87E4FL) # 57
        d = XX(I, d, a, b, c, inp[15], S42, 0xFE2CE6E0L) # 58
        c = XX(I, c, d, a, b, inp[ 6], S43, 0xA3014314L) # 59
        b = XX(I, b, c, d, a, inp[13], S44, 0x4E0811A1L) # 60
        a = XX(I, a, b, c, d, inp[ 4], S41, 0xF7537E82L) # 61
        d = XX(I, d, a, b, c, inp[11], S42, 0xBD3AF235L) # 62
        c = XX(I, c, d, a, b, inp[ 2], S43, 0x2AD7D2BBL) # 63
        b = XX(I, b, c, d, a, inp[ 9], S44, 0xEB86D391L) # 64

        A = (A + a) & 0xffffffffL
        B = (B + b) & 0xffffffffL
        C = (C + c) & 0xffffffffL
        D = (D + d) & 0xffffffffL

        self.A, self.B, self.C, self.D = A, B, C, D


Instruction:
Can you generate the documentation for the following Python 3 function
def update(self, inBuf):
        leninBuf = long(len(inBuf))

        # Compute number of bytes mod 64.
        index = (self.count[0] >> 3) & 0x3FL

        # Update number of bits.
        self.count[0] = self.count[0] + (leninBuf << 3)
        if self.count[0] < (leninBuf << 3):
            self.count[1] = self.count[1] + 1
        self.count[1] = self.count[1] + (leninBuf >> 29)

        partLen = 64 - index

        if leninBuf >= partLen:
            self.input[index:] = map(None, inBuf[:partLen])
            self._transform(_bytelist2long(self.input))
            i = partLen
            while i + 63 < leninBuf:
                self._transform(_bytelist2long(map(None, inBuf[i:i+64])))
                i = i + 64
            else:
                self.input = map(None, inBuf[i:leninBuf])
        else:
            i = 0
            self.input = self.input + map(None, inBuf)

Response: Add to the current message and update the md5 object with the string arg.


Instruction:
Can you create a Python 3 function that
terminates the message - digest computation and return digest.

Response:
def digest(self):
        """Terminate the message-digest computation and return digest.

        Return the digest of the strings passed to the update()
        method so far. This is a 16-byte string which may contain
        non-ASCII characters, including null bytes.
        """

        A = self.A
        B = self.B
        C = self.C
        D = self.D
        input = [] + self.input
        count = [] + self.count

        index = (self.count[0] >> 3) & 0x3fL

        if index < 56:
            padLen = 56 - index
        else:
            padLen = 120 - index

        padding = ['\200'] + ['\000'] * 63
        self.update(padding[:padLen])

        # Append length (before padding).
        bits = _bytelist2long(self.input[:56]) + count

        self._transform(bits)

        # Store state in digest.
        digest = _long2bytes(self.A << 96, 16)[:4] + \
                 _long2bytes(self.B << 64, 16)[4:8] + \
                 _long2bytes(self.C << 32, 16)[8:12] + \
                 _long2bytes(self.D, 16)[12:]

        self.A = A
        self.B = B
        self.C = C
        self.D = D
        self.input = input
        self.count = count

        return digest


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

        d = map(None, self.digest())
        d = map(ord, d)
        d = map(lambda x:"%02x" % x, d)
        d = string.join(d, '')

        return d

Response: Terminate and return digest in HEX form.


Instruction:
Write a Python 3 script for
returning value used to compare size of this node

Response:
def value( self, node, parent=None ):
        """Return value used to compare size of this node"""
        # this is the *weighted* size/contribution of the node
        try:
            return node['contribution']
        except KeyError, err:
            contribution = int(node.get('totsize',0)/float( len(node.get('parents',())) or 1))
            node['contribution'] = contribution
            return contribution


Instruction:
Can you tell what is the following Python 3 function doing
def label( self, node ):
        result = []
        if node.get('type'):
            result.append( node['type'] )
        if node.get('name' ):
            result.append( node['name'] )
        elif node.get('value') is not None:
            result.append( unicode(node['value'])[:32])
        if 'module' in node and not node['module'] in result:
            result.append( ' in %s'%( node['module'] ))
        if node.get( 'size' ):
            result.append( '%s'%( mb( node['size'] )))
        if node.get( 'totsize' ):
            result.append( '(%s)'%( mb( node['totsize'] )))
        parent_count = len( node.get('parents',()))
        if parent_count > 1:
            result.append( '/%s refs'%( parent_count ))
        return " ".join(result)

Response: Return textual description of this node


Instruction:
Write a Python 3 function for
retrieving the set of parents for the given node

Response:
def parents( self, node ):
        """Retrieve/calculate the set of parents for the given node"""
        if 'index' in node:
            index = node['index']()
            parents = list(meliaeloader.children( node, index, 'parents' ))
            return parents
        return []


Instruction:
Can you tell what is the following Python 3 function doing
def best_parent( self, node, tree_type=None ):
        parents = self.parents(node)
        selected_parent = None
        if node['type'] == 'type':
            module = ".".join( node['name'].split( '.' )[:-1] )
            if module:
                for mod in parents:
                    if mod['type'] == 'module' and mod['name'] == module:
                        selected_parent = mod
        if parents and selected_parent is None:
            parents.sort( key = lambda x: self.value(node, x) )
            return parents[-1]
        return selected_parent

Response: Choose the best parent for a given node


Instruction:
Can you generate the documentation for the following Python 3 function
def Gooey(f=None,
          advanced=True,
          language='english',
          show_config=True,
          program_name=None,
          program_description=None,
          default_size=(610, 530),
          required_cols=2,
          optional_cols=2,
          dump_build_config=False,
          monospace_display=False):
  '''
  Decorator for client code's main function.
  Serializes argparse data to JSON for use with the Gooey front end
  '''

  params = locals()

  def build(payload):
    def run_gooey(self, args=None, namespace=None):
      source_path = sys.argv[0]
      build_spec = config_generator.create_from_parser(self, source_path, payload_name=payload.__name__, **params)

      if dump_build_config:
        config_path = os.path.join(os.getcwd(), 'gooey_config.json')
        print( 'Writing Build Config to: {}'.format(config_path))
        with open(config_path, 'w') as f:
          f.write(json.dumps(build_spec, indent=2))
      application.run(build_spec)

    def inner2(*args, **kwargs):
      ArgumentParser.original_parse_args = ArgumentParser.parse_args
      ArgumentParser.parse_args = run_gooey
      return payload(*args, **kwargs)

    inner2.__name__ = payload.__name__
    return inner2

  def run_without_gooey(func):
    return lambda: func()

  if IGNORE_COMMAND in sys.argv:
    sys.argv.remove(IGNORE_COMMAND)
    if callable(f):
      return run_without_gooey(f)
    return run_without_gooey

  if callable(f):
    return build(f)
  return build

Response: A function that returns a Gooey client code s main function.


Instruction:
How would you code a function in Python 3 to
build a list of wx Component objects from a Json widget list

Response:
def build_components(widget_list):
  '''
  :param widget_list: list of dicts containing widget info (name, type, etc..)
  :return: ComponentList

  Converts the Json widget information into concrete wx Widget types
  '''
  required_args, optional_args  = partition(widget_list, is_required)
  checkbox_args, general_args = partition(map(build_widget, optional_args), is_checkbox)

  required_args = map(build_widget, required_args)
  optional_args = general_args + checkbox_args

  return ComponentList(required_args, optional_args)


Instruction:
Make a summary of the following Python 3 code
def _merge_asized(base, other, level=0):
    ref2key = lambda ref: ref.name.split(':')[0]
    base.size += other.size
    base.flat += other.flat
    if level > 0:
        base.name = ref2key(base)
    # Add refs from other to base. Any new refs are appended.
    base.refs = list(base.refs) # we may need to append items
    refs = {}
    for ref in base.refs:
        refs[ref2key(ref)] = ref
    for ref in other.refs:
        key = ref2key(ref)
        if key in refs:
            _merge_asized(refs[key], ref, level=level+1)
        else:
            # Don't modify existing Asized instances => deepcopy
            base.refs.append(deepcopy(ref))
            base.refs[-1].name = key

Response: Merge two Asized instances into base.


Instruction:
Write a Python 3 script to
merge the snapshot size information of multiple tracked objects at time tref into merged.

Response:
def _merge_objects(tref, merged, obj):
    """
    Merge the snapshot size information of multiple tracked objects.  The
    tracked object `obj` is scanned for size information at time `tref`.
    The sizes are merged into **Asized** instance `merged`.
    """
    size = None
    for (timestamp, tsize) in obj.snapshots:
        if timestamp == tref:
            size = tsize
    if size:
        _merge_asized(merged, size)


Instruction:
Implement a Python 3 function for
converting the stack - trace to a nice readable format.

Response:
def _format_trace(trace):
    """
    Convert the (stripped) stack-trace to a nice readable format. The stack
    trace `trace` is a list of frame records as returned by
    **inspect.stack** but without the frame objects.
    Returns a string.
    """
    lines = []
    for fname, lineno, func, src, _ in trace:
        if src:
            for line in src:
                lines.append('    '+line.strip()+'\n')
        lines.append('  %s:%4d in %s\n' % (fname, lineno, func))
    return ''.join(lines)


Instruction:
Can you generate the documentation for the following Python 3 function
def load_stats(self, fdump):
        if isinstance(fdump, type('')):
            fdump = open(fdump, 'rb')
        self.index = pickle.load(fdump)
        self.snapshots = pickle.load(fdump)
        self.sorted = []

Response: Load the data from a dump file.


Instruction:
Can you tell what is the following Python 3 function doing
def dump_stats(self, fdump, close=True):
        if self.tracker:
            self.tracker.stop_periodic_snapshots()

        if isinstance(fdump, type('')):
            fdump = open(fdump, 'wb')
        pickle.dump(self.index, fdump, protocol=pickle.HIGHEST_PROTOCOL)
        pickle.dump(self.snapshots, fdump, protocol=pickle.HIGHEST_PROTOCOL)
        if close:
            fdump.close()

Response: Dump the logged data to a file.


Instruction:
Can you generate the documentation for the following Python 3 function
def _init_sort(self):
        if not self.sorted:
            # Identify the snapshot that tracked the largest amount of memory.
            tmax = None
            maxsize = 0
            for snapshot in self.snapshots:
                if snapshot.tracked_total > maxsize:
                    tmax = snapshot.timestamp
            for key in list(self.index.keys()):
                for tobj in self.index[key]:
                    tobj.classname = key
                    tobj.size = tobj.get_max_size()
                    tobj.tsize = tobj.get_size_at_time(tmax)
                self.sorted.extend(self.index[key])

Response: Initialize the data to be sorted.


Instruction:
Implement a Python 3 function for
sorting the tracked objects according to the supplied criteria.

Response:
def sort_stats(self, *args):
        """
        Sort the tracked objects according to the supplied criteria. The
        argument is a string identifying the basis of a sort (example: 'size'
        or 'classname'). When more than one key is provided, then additional
        keys are used as secondary criteria when there is equality in all keys
        selected before them. For example, ``sort_stats('name', 'size')`` will
        sort all the entries according to their class name, and resolve all
        ties (identical class names) by sorting by size.  The criteria are
        fields in the tracked object instances. Results are stored in the
        ``self.sorted`` list which is used by ``Stats.print_stats()`` and other
        methods. The fields available for sorting are:

            'classname'
                the name with which the class was registered
            'name'
                the classname
            'birth'
                creation timestamp
            'death'
                destruction timestamp
            'size'
                the maximum measured size of the object
            'tsize'
                the measured size during the largest snapshot
            'repr'
                string representation of the object

        Note that sorts on size are in descending order (placing most memory
        consuming items first), whereas name, repr, and creation time searches
        are in ascending order (alphabetical).

        The function returns self to allow calling functions on the result::

            stats.sort_stats('size').reverse_order().print_stats()
        """

        criteria = ('classname', 'tsize', 'birth', 'death',
                    'name', 'repr', 'size')

        if not set(criteria).issuperset(set(args)):
            raise ValueError("Invalid sort criteria")

        if not args:
            args = criteria

        def args_to_tuple(obj):
            keys = []
            for attr in args:
                attribute = getattr(obj, attr)
                if attr in ('tsize', 'size'):
                    attribute = -attribute
                keys.append(attribute)
            return tuple(keys)

        self._init_sort()
        self.sorted.sort(key=args_to_tuple)

        return self


Instruction:
Given the following Python 3 function, write the documentation
def annotate_snapshot(self, snapshot):
        if hasattr(snapshot, 'classes'):
            return

        snapshot.classes = {}

        for classname in list(self.index.keys()):
            total = 0
            active = 0
            merged = Asized(0, 0)
            for tobj in self.index[classname]:
                _merge_objects(snapshot.timestamp, merged, tobj)
                total += tobj.get_size_at_time(snapshot.timestamp)
                if tobj.birth < snapshot.timestamp and \
                    (tobj.death is None or tobj.death > snapshot.timestamp):
                    active += 1

            try:
                pct = total * 100.0 / snapshot.total
            except ZeroDivisionError: # pragma: no cover
                pct = 0
            try:
                avg = total / active
            except ZeroDivisionError:
                avg = 0

            snapshot.classes[classname] = dict(sum=total,
                                               avg=avg,
                                               pct=pct,
                                               active=active)
            snapshot.classes[classname]['merged'] = merged

Response: Annotate a snapshot with the statistical data.


Instruction:
Make a summary of the following Python 3 code
def _print_refs(self, refs, total, prefix='    ',
                    level=1, minsize=0, minpct=0.1):
        """
        Print individual referents recursively.
        """
        lrefs = list(refs)
        lrefs.sort(key=lambda x: x.size)
        lrefs.reverse()
        for ref in lrefs:
            if ref.size > minsize and (ref.size*100.0/total) > minpct:
                self.stream.write('%-50s %-14s %3d%% [%d]\n' % (
                    trunc(prefix+str(ref.name), 50),
                    pp(ref.size),
                    int(ref.size*100.0/total),
                    level
                ))
                self._print_refs(ref.refs, total, prefix=prefix+'  ',
                                 level=level+1)

Response: Print all the referents in the tree.


Instruction:
Can you tell what is the following Python 3 function doing
def print_object(self, tobj):
        if tobj.death:
            self.stream.write('%-32s ( free )   %-35s\n' % (
                trunc(tobj.name, 32, left=1), trunc(tobj.repr, 35)))
        else:
            self.stream.write('%-32s 0x%08x %-35s\n' % (
                trunc(tobj.name, 32, left=1),
                tobj.id,
                trunc(tobj.repr, 35)
            ))
        if tobj.trace:
            self.stream.write(_format_trace(tobj.trace))
        for (timestamp, size) in tobj.snapshots:
            self.stream.write('  %-30s %s\n' % (
                pp_timestamp(timestamp), pp(size.size)
            ))
            self._print_refs(size.refs, size.size)
        if tobj.death is not None:
            self.stream.write('  %-30s finalize\n' % (
                pp_timestamp(tobj.death),
            ))

Response: Print the gathered information of the object tobj in human - readable format.


Instruction:
Write a Python 3 function that can
write tracked objects to stdout.

Response:
def print_stats(self, clsname=None, limit=1.0):
        """
        Write tracked objects to stdout.  The output can be filtered and
        pruned.  Only objects are printed whose classname contain the substring
        supplied by the `clsname` argument.  The output can be pruned by
        passing a `limit` value.

        :param clsname: Only print objects whose classname contain the given
            substring.
        :param limit: If `limit` is a float smaller than one, only the supplied
            percentage of the total tracked data is printed. If `limit` is
            bigger than one, this number of tracked objects are printed.
            Tracked objects are first filtered, and then pruned (if specified).
        """
        if self.tracker:
            self.tracker.stop_periodic_snapshots()

        if not self.sorted:
            self.sort_stats()

        _sorted = self.sorted

        if clsname:
            _sorted = [to for to in _sorted if clsname in to.classname]

        if limit < 1.0:
            limit = max(1, int(len(self.sorted) * limit))
        _sorted = _sorted[:int(limit)]

        # Emit per-instance data
        for tobj in _sorted:
            self.print_object(tobj)


Instruction:
Can you create a Python 3 function that
prints summary for each class in the current instance.

Response:
def print_summary(self):
        """
        Print per-class summary for each snapshot.
        """
        # Emit class summaries for each snapshot
        classlist = self.tracked_classes

        fobj = self.stream

        fobj.write('---- SUMMARY '+'-'*66+'\n')
        for snapshot in self.snapshots:
            self.annotate_snapshot(snapshot)
            fobj.write('%-35s %11s %12s %12s %5s\n' % (
                trunc(snapshot.desc, 35),
                'active',
                pp(snapshot.asizeof_total),
                'average',
                'pct'
            ))
            for classname in classlist:
                info = snapshot.classes.get(classname)
                fobj.write('  %-33s %11d %12s %12s %4d%%\n' % (
                    trunc(classname, 33),
                    info['active'],
                    pp(info['sum']),
                    pp(info['avg']),
                    info['pct']
                ))
        fobj.write('-'*79+'\n')


Instruction:
Create a Python 3 function to
print all the referents in the tree.

Response:
def _print_refs(self, fobj, refs, total, level=1, minsize=0, minpct=0.1):
        """
        Print individual referents recursively.
        """
        lrefs = list(refs)
        lrefs.sort(key=lambda x: x.size)
        lrefs.reverse()
        if level == 1:
            fobj.write('<table>\n')
        for ref in lrefs:
            if ref.size > minsize and (ref.size*100.0/total) > minpct:
                data = dict(level=level,
                            name=trunc(str(ref.name), 128),
                            size=pp(ref.size),
                            pct=ref.size*100.0/total)
                fobj.write(self.refrow % data)
                self._print_refs(fobj, ref.refs, total, level=level+1)
        if level == 1:
            fobj.write("</table>\n")


Instruction:
How would you explain what the following Python 3 function does
def print_class_details(self, fname, classname):
        fobj = open(fname, "w")
        fobj.write(self.header % (classname, self.style))

        fobj.write("
%s
\n" % (classname))

        sizes = [tobj.get_max_size() for tobj in self.index[classname]]
        total = 0
        for s in sizes:
            total += s
        data = {'cnt': len(self.index[classname]), 'cls': classname}
        data['avg'] = pp(total / len(sizes))
        data['max'] = pp(max(sizes))
        data['min'] = pp(min(sizes))
        fobj.write(self.class_summary % data)

        fobj.write(self.charts[classname])

        fobj.write("
Coalesced Referents per Snapshot
\n")
        for snapshot in self.snapshots:
            if classname in snapshot.classes:
                merged = snapshot.classes[classname]['merged']
                fobj.write(self.class_snapshot % {
                    'name': snapshot.desc, 'cls':classname, 'total': pp(merged.size)
                })
                if merged.refs:
                    self._print_refs(fobj, merged.refs, merged.size)
                else:
                    fobj.write('
No per-referent sizes recorded.
\n')

        fobj.write("
Instances
\n")
        for tobj in self.index[classname]:
            fobj.write('
\n')
            fobj.write('
Instance	%s at 0x%08x
\n' % (tobj.name, tobj.id))
            if tobj.repr:
                fobj.write("
Representation	%s
\n" % tobj.repr)
            fobj.write("
Lifetime	%s - %s
\n" % (pp_timestamp(tobj.birth), pp_timestamp(tobj.death)))
            if tobj.trace:
                trace = "
%s
" % (_format_trace(tobj.trace))
                fobj.write("
Instantiation	%s
\n" % trace)
            for (timestamp, size) in tobj.snapshots:
                fobj.write("
%s	" % pp_timestamp(timestamp))
                if not size.refs:
                    fobj.write("%s
\n" % pp(size.size))
                else:
                    fobj.write("%s" % pp(size.size))
                    self._print_refs(fobj, size.refs, size.size)
                    fobj.write("
\n")
            fobj.write("
\n")

        fobj.write(self.footer)
        fobj.close()

Response: Print detailed statistics and instances for the class classname.


Instruction:
Implement a function in Python 3 to
create the title page.

Response:
def create_title_page(self, filename, title=''):
        """
        Output the title page.
        """
        fobj = open(filename, "w")
        fobj.write(self.header % (title, self.style))

        fobj.write("<h1>%s</h1>\n" % title)
        fobj.write("<h2>Memory distribution over time</h2>\n")
        fobj.write(self.charts['snapshots'])

        fobj.write("<h2>Snapshots statistics</h2>\n")
        fobj.write('<table id="nb">\n')

        classlist = list(self.index.keys())
        classlist.sort()

        for snapshot in self.snapshots:
            fobj.write('<tr><td>\n')
            fobj.write('<table id="tl" rules="rows">\n')
            fobj.write("<h3>%s snapshot at %s</h3>\n" % (
                snapshot.desc or 'Untitled',
                pp_timestamp(snapshot.timestamp)
            ))

            data = {}
            data['sys']      = pp(snapshot.system_total.vsz)
            data['tracked']  = pp(snapshot.tracked_total)
            data['asizeof']  = pp(snapshot.asizeof_total)
            data['overhead'] = pp(getattr(snapshot, 'overhead', 0))

            fobj.write(self.snapshot_summary % data)

            if snapshot.tracked_total:
                fobj.write(self.snapshot_cls_header)
                for classname in classlist:
                    data = snapshot.classes[classname].copy()
                    data['cls'] = '<a href="%s">%s</a>' % (self.relative_path(self.links[classname]), classname)
                    data['sum'] = pp(data['sum'])
                    data['avg'] = pp(data['avg'])
                    fobj.write(self.snapshot_cls % data)
            fobj.write('</table>')
            fobj.write('</td><td>\n')
            if snapshot.tracked_total:
                fobj.write(self.charts[snapshot])
            fobj.write('</td></tr>\n')

        fobj.write("</table>\n")
        fobj.write(self.footer)
        fobj.close()


Instruction:
Implement a Python 3 function for
creating a chart that depicts the lifetime of the instance registered with the given classname. The output is written to filename.

Response:
def create_lifetime_chart(self, classname, filename=''):
        """
        Create chart that depicts the lifetime of the instance registered with
        `classname`. The output is written to `filename`.
        """
        try:
            from pylab import figure, title, xlabel, ylabel, plot, savefig
        except ImportError:
            return HtmlStats.nopylab_msg % (classname+" lifetime")

        cnt = []
        for tobj in self.index[classname]:
            cnt.append([tobj.birth, 1])
            if tobj.death:
                cnt.append([tobj.death, -1])
        cnt.sort()
        for i in range(1, len(cnt)):
            cnt[i][1] += cnt[i-1][1]
            #if cnt[i][0] == cnt[i-1][0]:
            #    del cnt[i-1]

        x = [t for [t,c] in cnt]
        y = [c for [t,c] in cnt]

        figure()
        xlabel("Execution time [s]")
        ylabel("Instance #")
        title("%s instances" % classname)
        plot(x, y, 'o')
        savefig(filename)

        return self.chart_tag % (os.path.basename(filename))


Instruction:
Here you have a function in Python 3, explain what it does
def create_snapshot_chart(self, filename=''):
        try:
            from pylab import figure, title, xlabel, ylabel, plot, fill, legend, savefig
            import matplotlib.mlab as mlab
        except ImportError:
            return self.nopylab_msg % ("memory allocation")

        classlist = self.tracked_classes

        times = [snapshot.timestamp for snapshot in self.snapshots]
        base = [0] * len(self.snapshots)
        poly_labels = []
        polys = []
        for cn in classlist:
            pct = [snapshot.classes[cn]['pct'] for snapshot in self.snapshots]
            if max(pct) > 3.0:
                sz = [float(fp.classes[cn]['sum'])/(1024*1024) for fp in self.snapshots]
                sz = [sx+sy for sx, sy in zip(base, sz)]
                xp, yp = mlab.poly_between(times, base, sz)
                polys.append( ((xp, yp), {'label': cn}) )
                poly_labels.append(cn)
                base = sz

        figure()
        title("Snapshot Memory")
        xlabel("Execution Time [s]")
        ylabel("Virtual Memory [MiB]")

        sizes = [float(fp.asizeof_total)/(1024*1024) for fp in self.snapshots]
        plot(times, sizes, 'r--', label='Total')
        sizes = [float(fp.tracked_total)/(1024*1024) for fp in self.snapshots]
        plot(times, sizes, 'b--', label='Tracked total')

        for (args, kwds) in polys:
            fill(*args, **kwds)
        legend(loc=2)
        savefig(filename)

        return self.chart_tag % (self.relative_path(filename))

Response:
Create a chart that depicts the memory allocation over time apportioned to
            the tracked classes.


Instruction:
Can you implement a function in Python 3 that
creates a pie chart that depicts the distribution of the allocated memory for a given snapshot.

Response:
def create_pie_chart(self, snapshot, filename=''):
        """
        Create a pie chart that depicts the distribution of the allocated memory
        for a given `snapshot`. The chart is saved to `filename`.
        """
        try:
            from pylab import figure, title, pie, axes, savefig
            from pylab import sum as pylab_sum
        except ImportError:
            return self.nopylab_msg % ("pie_chart")

        # Don't bother illustrating a pie without pieces.
        if not snapshot.tracked_total:
            return ''

        classlist = []
        sizelist = []
        for k, v in list(snapshot.classes.items()):
            if v['pct'] > 3.0:
                classlist.append(k)
                sizelist.append(v['sum'])
        sizelist.insert(0, snapshot.asizeof_total - pylab_sum(sizelist))
        classlist.insert(0, 'Other')
        #sizelist = [x*0.01 for x in sizelist]

        title("Snapshot (%s) Memory Distribution" % (snapshot.desc))
        figure(figsize=(8,8))
        axes([0.1, 0.1, 0.8, 0.8])
        pie(sizelist, labels=classlist)
        savefig(filename, dpi=50)

        return self.chart_tag % (self.relative_path(filename))


Instruction:
How would you code a function in Python 3 to
create HTML page and additional files in a directory derived from fname.

Response:
def create_html(self, fname, title="ClassTracker Statistics"):
        """
        Create HTML page `fname` and additional files in a directory derived
        from `fname`.
        """
        # Create a folder to store the charts and additional HTML files.
        self.basedir = os.path.dirname(os.path.abspath(fname))
        self.filesdir = os.path.splitext(fname)[0] + '_files'
        if not os.path.isdir(self.filesdir):
            os.mkdir(self.filesdir)
        self.filesdir = os.path.abspath(self.filesdir)
        self.links = {}

        # Annotate all snapshots in advance
        self.annotate()

        # Create charts. The tags to show the images are returned and stored in
        # the self.charts dictionary. This allows to return alternative text if
        # the chart creation framework is not available.
        self.charts = {}
        fn = os.path.join(self.filesdir, 'timespace.png')
        self.charts['snapshots'] = self.create_snapshot_chart(fn)

        for fp, idx in zip(self.snapshots, list(range(len(self.snapshots)))):
            fn = os.path.join(self.filesdir, 'fp%d.png' % (idx))
            self.charts[fp] = self.create_pie_chart(fp, fn)

        for cn in list(self.index.keys()):
            fn = os.path.join(self.filesdir, cn.replace('.', '_')+'-lt.png')
            self.charts[cn] = self.create_lifetime_chart(cn, fn)

        # Create HTML pages first for each class and then the index page.
        for cn in list(self.index.keys()):
            fn = os.path.join(self.filesdir, cn.replace('.', '_')+'.html')
            self.links[cn]  = fn
            self.print_class_details(fn, cn)

        self.create_title_page(fname, title=title)


Instruction:
How would you explain what the following Python 3 function does
def select_from(self, parent_path):
        path_cls = type(parent_path)
        is_dir = path_cls.is_dir
        exists = path_cls.exists
        listdir = parent_path._accessor.listdir
        return self._select_from(parent_path, is_dir, exists, listdir)

Response:
Iterate over all child paths of parent_path matched by this
        selector. This can contain parent_path itself.


Instruction:
How would you explain what the following Python 3 function does
def relative_to(self, *other):
        # For the purpose of this method, drive and root are considered
        # separate parts, i.e.:
        #   Path('c:/').relative_to('c:')  gives Path('/')
        #   Path('c:/').relative_to('/')   raise ValueError
        if not other:
            raise TypeError("need at least one argument")
        parts = self._parts
        drv = self._drv
        root = self._root
        if root:
            abs_parts = [drv, root] + parts[1:]
        else:
            abs_parts = parts
        to_drv, to_root, to_parts = self._parse_args(other)
        if to_root:
            to_abs_parts = [to_drv, to_root] + to_parts[1:]
        else:
            to_abs_parts = to_parts
        n = len(to_abs_parts)
        cf = self._flavour.casefold_parts
        if (root or drv) if n == 0 else cf(abs_parts[:n]) != cf(to_abs_parts):
            formatted = self._format_parsed_parts(to_drv, to_root, to_parts)
            raise ValueError("{!r} does not start with {!r}"
                             .format(str(self), str(formatted)))
        return self._from_parsed_parts('', root if n == 1 else '',
                                       abs_parts[n:])

Response:
Return the relative path to another path identified by the passed
        arguments.


Instruction:
Implement a function in Python 3 to
iterate over this subtree and yield all existing files of any kind matching the given pattern.

Response:
def glob(self, pattern):
        """Iterate over this subtree and yield all existing files (of any
        kind, including directories) matching the given pattern.
        """
        pattern = self._flavour.casefold(pattern)
        drv, root, pattern_parts = self._flavour.parse_parts((pattern,))
        if drv or root:
            raise NotImplementedError("Non-relative patterns are unsupported")
        selector = _make_selector(tuple(pattern_parts))
        for p in selector.select_from(self):
            yield p


Instruction:
Write a Python 3 script for
writing the data to the file in bytes mode.

Response:
def write_bytes(self, data):
        """
        Open the file in bytes mode, write to it, and close the file.
        """
        if not isinstance(data, six.binary_type):
            raise TypeError(
                'data must be %s, not %s' %
                (six.binary_type.__class__.__name__, data.__class__.__name__))
        with self.open(mode='wb') as f:
            return f.write(data)


Instruction:
How would you code a function in Python 3 to
write the data to the log file in text mode.

Response:
def write_text(self, data, encoding=None, errors=None):
        """
        Open the file in text mode, write to it, and close the file.
        """
        if not isinstance(data, six.text_type):
            raise TypeError(
                'data must be %s, not %s' %
                (six.text_type.__class__.__name__, data.__class__.__name__))
        with self.open(mode='w', encoding=encoding, errors=errors) as f:
            return f.write(data)


Instruction:
Given the following Python 3 function, write the documentation
def relpath_posix(recwalk_result, pardir, fromwinpath=False):
    ''' Helper function to convert all paths to relative posix like paths (to ease comparison) '''
    return recwalk_result[0], path2unix(os.path.join(os.path.relpath(recwalk_result[0], pardir),recwalk_result[1]), nojoin=True, fromwinpath=fromwinpath)

Response: Helper function to convert all paths to relative posix like paths


Instruction:
Can you create a Python 3 function that
sorts a dict of paths parts and organically to the paths alphabetical order

Response:
def sort_dict_of_paths(d):
    """ Sort a dict containing paths parts (ie, paths divided in parts and stored as a list). Top paths will be given precedence over deeper paths. """
    # Find the path that is the deepest, and count the number of parts
    max_rec = max(len(x) if x else 0 for x in d.values())
    # Pad other paths with empty parts to fill in, so that all paths will have the same number of parts (necessary to compare correctly, else deeper paths may get precedence over top ones, since the folder name will be compared to filenames!)
    for key in d.keys():
        if d[key]:
            d[key] = ['']*(max_rec-len(d[key])) + d[key]
    # Sort the dict relatively to the paths alphabetical order
    d_sort = sorted(d.items(), key=lambda x: x[1])
    return d_sort


Instruction:
Given the following Python 3 function, write the documentation
def sort_group(d, return_only_first=False):
    ''' Sort a dictionary of relative paths and cluster equal paths together at the same time '''
    # First, sort the paths in order (this must be a couple: (parent_dir, filename), so that there's no ambiguity because else a file at root will be considered as being after a folder/file since the ordering is done alphabetically without any notion of tree structure).
    d_sort = sort_dict_of_paths(d)
    # Pop the first item in the ordered list
    base_elt = (-1, None)
    while (base_elt[1] is None and d_sort):
        base_elt = d_sort.pop(0)
    # No element, then we just return
    if base_elt[1] is None:
        return None
    # Else, we will now group equivalent files together (remember we are working on multiple directories, so we can have multiple equivalent relative filepaths, but of course the absolute filepaths are different).
    else:
        # Init by creating the first group and pushing the first ordered filepath into the first group
        lst = []
        lst.append([base_elt])
        if d_sort:
            # For each subsequent filepath
            for elt in d_sort:
                # If the filepath is not empty (generator died)
                if elt[1] is not None:
                    # If the filepath is the same to the latest grouped filepath, we add it to the same group
                    if elt[1] == base_elt[1]:
                        lst[-1].append(elt)
                    # Else the filepath is different: we create a new group, add the filepath to this group, and replace the latest grouped filepath
                    else:
                        if return_only_first: break  # break here if we only need the first group
                        lst.append([elt])
                        base_elt = elt # replace the latest grouped filepath
        return lst

Response: Sort a dictionary of relative paths and cluster equal paths together at the same time.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def majority_vote_byte_scan(relfilepath, fileslist, outpath, blocksize=65535, default_char_null=False):
    '''Takes a list of files in string format representing the same data, and disambiguate by majority vote: for position in string, if the character is not the same accross all entries, we keep the major one. If none, it will be replaced by a null byte (because we can't know if any of the entries are correct about this character).
    relfilepath is the filename or the relative file path relative to the parent directory (ie, this is the relative path so that we can compare the files from several directories).'''
    # The idea of replication combined with ECC was a bit inspired by this paper: Friedman, Roy, Yoav Kantor, and Amir Kantor. "Combining Erasure-Code and Replication Redundancy Schemes for Increased Storage and Repair Efficiency in P2P Storage Systems.", 2013, Technion, Computer Science Department, Technical Report CS-2013-03
    # But it is a very well known concept in redundancy engineering, usually called triple-modular redundancy (which is here extended to n-modular since we can supply any number of files we want, not just three).
    # Preference in case of ambiguity is always given to the file of the first folder.

    fileshandles = []
    for filepath in fileslist:
        if filepath:
            # Already a file handle? Just store it in the fileshandles list
            if hasattr(filepath, 'read'):
                fileshandles.append(filepath)
            # Else it's a string filepath, open the file
            else:
                fileshandles.append(open(filepath, 'rb'))

    # Create and open output (merged) file, except if we were already given a file handle
    if hasattr(outpath, 'write'):
        outfile = outpath
    else:
        outpathfull = os.path.join(outpath, relfilepath)
        pardir = os.path.dirname(outpathfull)
        if not os.path.exists(pardir):
            os.makedirs(pardir)
        outfile = open(outpathfull, 'wb')

    # Cannot vote if there's not at least 3 files!
    # In this case, just copy the file from the first folder, verbatim
    if len(fileshandles) < 3:
        # If there's at least one input file, then copy it verbatim to the output folder
        if fileshandles:
            create_dir_if_not_exist(os.path.dirname(outpathfull))
            buf = 1
            while (buf):
                buf = fileshandles[0].read()
                outfile.write(buf)
                outfile.flush()
        return (1, "Error with file %s: only %i copies available, cannot vote (need at least 3)! Copied the first file from the first folder, verbatim." % (relfilepath, len(fileshandles)))

    errors = []
    entries = [1]*len(fileshandles)  # init with 0 to start the while loop
    while (entries.count('') < len(fileshandles)):
        final_entry = []
        # Read a block from all input files into memory
        for i in xrange(len(fileshandles)):
            entries[i] = fileshandles[i].read(blocksize)

        # End of file for all files, we exit
        if entries.count('') == len(fileshandles):
            break
        # Else if there's only one file, just copy the file's content over
        elif len(entries) == 1:
            final_entry = entries[0]

        # Else, do the majority vote
        else:
            # Walk along each column (imagine the strings being rows in a matrix, then we pick one column at each iteration = all characters at position i of each string), so that we can compare these characters easily
            for i in xrange(max(len(entry) for entry in entries)):
                hist = {} # kind of histogram, we just memorize how many times a character is presented at the position i in each string TODO: use collections.Counter instead of dict()?
                # Extract the character at position i of each string and compute the histogram at the same time (number of time this character appear among all strings at this position i)
                for entry in entries:
                    # Check if we are not beyond the current entry's length
                    if i < len(entry): # TODO: check this line, this should allow the vote to continue even if some files are shorter than others
                        # Extract the character and use it to contribute to the histogram
                        # TODO: add warning message when one file is not of the same size as the others
                        key = str(ord(entry[i])) # convert to the ascii value to avoid any funky problem with encoding in dict keys
                        hist[key] = hist.get(key, 0) + 1 # increment histogram for this value. If it does not exists, use 0. (essentially equivalent to hist[key] += 1 but with exception management if key did not already exists)
                # If there's only one character (it's the same accross all strings at position i), then it's an exact match, we just save the character and we can skip to the next iteration
                if len(hist) == 1:
                    final_entry.append(chr(int(hist.iterkeys().next())))
                    continue
                # Else, the character is different among different entries, we will pick the major one (mode)
                elif len(hist) > 1:
                    # Sort the dict by value (and reverse because we want the most frequent first)
                    skeys = sorted(hist, key=hist.get, reverse=True)
                    # Ambiguity! If each entries present a different character (thus the major has only an occurrence of 1), then it's too ambiguous and we just set a null byte to signal that
                    if hist[skeys[0]] == 1:
                        if default_char_null:
                            if default_char_null is True:
                                final_entry.append("\x00")
                            else:
                                final_entry.append(default_char_null)
                        else:
                            # Use the entry of the first file that is still open
                            first_char = ''
                            for entry in entries:
                                # Found the first file that has a character at this position: store it and break loop
                                if i < len(entry):
                                    first_char = entry[i]
                                    break
                            # Use this character in spite of ambiguity
                            final_entry.append(first_char)
                        errors.append(outfile.tell() + i) # Print an error indicating the characters that failed
                    # Else if there is a tie (at least two characters appear with the same frequency), then we just pick one of them
                    elif hist[skeys[0]] == hist[skeys[1]]:
                        final_entry.append(chr(int(skeys[0]))) # TODO: find a way to account for both characters. Maybe return two different strings that will both have to be tested? (eg: maybe one has a tampered hash, both will be tested and if one correction pass the hash then it's ok we found the correct one)
                    # Else we have a clear major character that appear in more entries than any other character, then we keep this one
                    else:
                        final_entry.append(chr(int(skeys[0]))) # alternative one-liner: max(hist.iteritems(), key=operator.itemgetter(1))[0]
                    continue
            # Concatenate to a string (this is faster than using a string from the start and concatenating at each iteration because Python strings are immutable so Python has to copy over the whole string, it's in O(n^2)
            final_entry = ''.join(final_entry)
            # Commit to output file
            outfile.write(final_entry)
            outfile.flush()

    # Errors signaling
    if errors:
        error_msg = "Unrecoverable corruptions (because of ambiguity) in file %s on characters: %s." % (relfilepath, [hex(int(x)) for x in errors]) # Signal to user that this file has unrecoverable corruptions (he may try to fix the bits manually or with his own script)
        return (1, error_msg) # return an error
    # Close all input files
    for fh in fileshandles:
        fh.close()
    # Close output file
    if outfile != outpath:  # close only if we were not given a file handle in the first place
        outfile.flush()
        outfile.close()
    return (0, None)

Response: Takes a list of files in string format representing the same data and disambiguates by majority vote.


Instruction:
Here you have a function in Python 3, explain what it does
def synchronize_files(inputpaths, outpath, database=None, tqdm_bar=None, report_file=None, ptee=None, verbose=False):
    ''' Main function to synchronize files contents by majority vote
    The main job of this function is to walk through the input folders and align the files, so that we can compare every files across every folders, one by one.
    The whole trick here is to align files, so that we don't need to memorize all the files in memory and we compare all equivalent files together: to do that, we ensure that we walk through the input directories in alphabetical order, and we pick the relative filepath at the top of the alphabetical order, this ensures the alignment of files between different folders, without memorizing  the whole trees structures.
    '''
    # (Generator) Files Synchronization Algorithm:
    # Needs a function stable_dir_walking, which will walk through directories recursively but in always the same order on all platforms (same order for files but also for folders), whatever order it is, as long as it is stable.
    # Until there's no file in any of the input folders to be processed:
    # - curfiles <- load first file for each folder by using stable_dir_walking on each input folder.
    # - curfiles_grouped <- group curfiles_ordered:
    #    * curfiles_ordered <- order curfiles alphabetically (need to separate the relative parent directory and the filename, to account for both without ambiguity)
    #    * curfiles_grouped <- empty list
    #    * curfiles_grouped[0] = add first element in curfiles_ordered
    #    * last_group = 0
    #    * for every subsequent element nextelt in curfiles_ordered:
    #        . if nextelt == curfiles_grouped[last_group][0]: add nextelt into curfiles_grouped[last_group] (the latest group in curfiles_grouped)
    #        . else: create a new group in curfiles_grouped (last_group += 1) and add nextelt into curfiles_grouped[last_group]
    # At this stage, curfiles_grouped[0] should contain a group of files with the same relative filepath from different input folders, and since we used stable_dir_walking, we are guaranteed that this file is the next to be processed in alphabetical order.
    # - Majority vote byte-by-byte for each of curfiles_grouped[0], and output winning byte to the output file.
    # - Update files list alignment: we will now ditch files in curfiles_grouped[0] from curfiles, and replace by the next files respectively from each respective folder. Since we processed in alphabetical (or whatever) order, the next loaded files will match the files in other curfiles_grouped groups that we could not process before.
    # At this point (after the loop), all input files have been processed in order, without maintaining the whole files list in memory, just one file per input folder.

    # Init files walking generator for each inputpaths
    recgen = [recwalk(path, sorting=True) for path in inputpaths]
    curfiles = {}
    recgen_exhausted = {}
    recgen_exhausted_count = 0
    nbpaths = len(inputpaths)
    retcode = 0

    if not ptee: ptee = sys.stdout

    # Open report file and write header
    if report_file is not None:
        rfile = open(report_file, 'wb')
        r_writer = csv.writer(rfile, delimiter='|', lineterminator='\n', quotechar='"')
        r_header = ["filepath"] + ["dir%i" % (i+1) for i in xrange(nbpaths)] + ["hash-correct", "error_code", "errors"]
        r_length = len(r_header)
        r_writer.writerow(r_header)

    # Initialization: load the first batch of files, one for each folder
    for i in xrange(len(recgen)):
        recgen_exhausted[i] = False
        try:
            if curfiles.get(i, None) is None:
                curfiles[i] = relpath_posix(recgen[i].next(), inputpaths[i])[1]
        except StopIteration:
            recgen_exhausted[i] = True
            recgen_exhausted_count += 1

    # Files lists alignment loop
    while recgen_exhausted_count < nbpaths:
        errcode = 0
        errmsg = None

        # Init a new report's row
        if report_file: r_row = ["-"] * r_length

        # -- Group equivalent relative filepaths together
        #print curfiles # debug
        curfiles_grouped = sort_group(curfiles, True)

        # -- Extract first group of equivalent filepaths (this allows us to process with the same alphabetical order on all platforms)
        # Note that the remaining files in other groups will be processed later, because their alphabetical order is higher to the first group, this means that the first group is to be processed now
        to_process = curfiles_grouped[0]
        #print to_process # debug

        # -- Byte-by-byte majority vote on the first group of files
        # Need the relative filepath also (note that there's only one since it's a group of equivalent relative filepaths, only the absolute path is different between files of a same group)
        relfilepath = path2unix(os.path.join(*to_process[0][1]))
        if report_file: r_row[0] = relfilepath
        if verbose: ptee.write("- Processing file %s." % relfilepath)
        # Generate output path
        outpathfull = os.path.join(outpath, relfilepath)
        create_dir_if_not_exist(os.path.dirname(outpathfull))
        # Initialize the list of absolute filepaths
        fileslist = []
        for elt in to_process:
            i = elt[0]
            fileslist.append(os.path.join(inputpaths[i], os.path.join(*elt[1])))
            if report_file: r_row[i+1] = 'X' # put an X in the report file below each folder that contains this file
        # If there's only one file, just copy it over
        if len(to_process) == 1:
            shutil.copyfile(fileslist[0], outpathfull)
            id = to_process[0][0]
            if report_file: r_row[id+1] = 'O'
        # Else, merge by majority vote
        else:
            # Before-merge check using rfigc database, if provided
            # If one of the files in the input folders is already correct, just copy it over
            correct_file = None
            if database:
                for id, filepath in enumerate(fileslist):
                    if rfigc.main("-i \"%s\" -d \"%s\" -m --silent" % (filepath, database)) == 0:
                        correct_file = filepath
                        correct_id = to_process[id][0]
                        break

            # If one correct file was found, copy it over
            if correct_file:
                create_dir_if_not_exist(os.path.dirname(outpathfull))
                shutil.copyfile(correct_file, outpathfull)
                if report_file:
                    r_row[correct_id+1] = "O"
                    r_row[-3] = "OK"
            # Else, we need to do the majority vote merge
            else:
                # Do the majority vote merge
                errcode, errmsg = majority_vote_byte_scan(relfilepath, fileslist, outpath)

        # After-merge/move check using rfigc database, if provided
        if database:
            if rfigc.main("-i \"%s\" -d \"%s\" -m --silent" % (outpathfull, database)) == 1:
                errcode = 1
                r_row[-3] = "KO"
                if not errmsg: errmsg = ''
                errmsg += " File could not be totally repaired according to rfigc database."
            else:
                if report_file:
                    r_row[-3] = "OK"
                    if errmsg: errmsg += " But merged file is correct according to rfigc database."

        # Display errors if any
        if errcode:
            if report_file:
                r_row[-2] = "KO"
                r_row[-1] = errmsg
            ptee.write(errmsg)
            retcode = 1
        else:
            if report_file: r_row[-2] = "OK"

        # Save current report's row
        if report_file:
            r_writer.writerow(r_row)

        # -- Update files lists alignment (ie, retrieve new files but while trying to keep the alignment)
        for elt in to_process:  # for files of the first group (the ones we processed)
            i = elt[0]
            # Walk their respective folders and load up the next file
            try:
                if not recgen_exhausted.get(i, False):
                    curfiles[i] = relpath_posix(recgen[i].next(), inputpaths[i])[1]
            # If there's no file left in this folder, mark this input folder as exhausted and continue with the others
            except StopIteration:
                curfiles[i] = None
                recgen_exhausted[i] = True
                recgen_exhausted_count += 1
        if tqdm_bar: tqdm_bar.update()
    if tqdm_bar: tqdm_bar.close()

    # Closing report file
    if report_file:
        # Write list of directories and legend
        rfile.write("\n=> Input directories:")
        for id, ipath in enumerate(inputpaths):
            rfile.write("\n\t- dir%i = %s" % ((id+1), ipath))
        rfile.write("\n=> Output directory: %s" % outpath)
        rfile.write("\n=> Legend: X=existing/selected for majority vote, O=only used this file, - = not existing, OK = check correct, KO = check incorrect (file was not recovered)\n")
        # Close the report file handle
        rfile.close()

    return retcode

Response: This function is used to synchronize files contents by majority. It will recursively walk through the input folders and align the files with the same filepath.


Instruction:
Write a Python 3 script for
getting a tree of referrers of the root object.

Response:
def get_tree(self):
        """Get a tree of referrers of the root object."""
        self.ignore.append(inspect.currentframe())
        return self._get_tree(self.root, self.maxdepth)


Instruction:
Explain what the following Python 3 code does
def print_tree(self, tree=None):
        if tree is None:
            self._print(self.root, '', '')
        else:
            self._print(tree, '', '')

Response: Print referrers tree to console.


Instruction:
Can you create a Python 3 function that
computes and print a new line of the tree.

Response:
def _print(self, tree, prefix, carryon):
        """Compute and print a new line of the tree.

        This is a recursive function.

        arguments
        tree -- tree to print
        prefix -- prefix to the current line to print
        carryon -- prefix which is used to carry on the vertical lines

        """
        level = prefix.count(self.cross) + prefix.count(self.vline)
        len_children = 0
        if isinstance(tree , _Node):
            len_children = len(tree.children)

        # add vertex
        prefix += str(tree)
        # and as many spaces as the vertex is long
        carryon += self.space * len(str(tree))
        if (level == self.maxdepth) or (not isinstance(tree, _Node)) or\
           (len_children == 0):
            self.stream.write(prefix+'\n')
            return
        else:
            # add in between connections
            prefix += self.hline
            carryon += self.space
            # if there is more than one branch, add a cross
            if len(tree.children) > 1:
                prefix += self.cross
                carryon += self.vline
            prefix += self.hline
            carryon += self.space

            if len_children > 0:
                # print the first branch (on the same line)
                self._print(tree.children[0], prefix, carryon)
                for b in range(1, len_children):
                    # the caryon becomes the prefix for all following children
                    prefix = carryon[:-2] + self.cross + self.hline
                    # remove the vlines for any children of last branch
                    if b == (len_children-1):
                        carryon = carryon[:-2] + 2*self.space
                    self._print(tree.children[b], prefix, carryon)
                    # leave a free line before the next branch
                    if b == (len_children-1):
                        if len(carryon.strip(' ')) == 0:
                            return
                        self.stream.write(carryon[:-2].rstrip()+'\n')


Instruction:
Here you have a function in Python 3, explain what it does
def print_tree(self, filename, tree=None):
        old_stream = self.stream
        self.stream = open(filename, 'w')
        try:
            super(FileBrowser, self).print_tree(tree=tree)
        finally:
            self.stream.close()
            self.stream = old_stream

Response: Print referrers tree to file.


Instruction:
Create a Python 3 function to
create interactive browser window. keyword arguments standalone -- Set to true

Response:
def main(self, standalone=False):
        """Create interactive browser window.

        keyword arguments
        standalone -- Set to true, if the browser is not attached to other
        windows

        """
        window = _Tkinter.Tk()
        sc = _TreeWidget.ScrolledCanvas(window, bg="white",\
                                       highlightthickness=0, takefocus=1)
        sc.frame.pack(expand=1, fill="both")
        item = _ReferrerTreeItem(window, self.get_tree(), self)
        node = _TreeNode(sc.canvas, None, item)
        node.expand()
        if standalone:
            window.mainloop()


Instruction:
Can you write a function in Python 3 where it
returns the value of the DropDown option in the order of the argument.

Response:
def getValue(self):
    '''
    Returns
      str(option_string * DropDown Value)
      e.g.
      -vvvvv
    '''
    dropdown_value = self.widget.GetValue()
    if not str(dropdown_value).isdigit():
      return ''
    arg = str(self.option_string).replace('-', '')
    repeated_args = arg * int(dropdown_value)
    return '-' + repeated_args


Instruction:
Can you generate a brief explanation for the following Python 3 code
def format_meter(n, total, elapsed, ncols=None, prefix='',
         unit=None, unit_scale=False, ascii=False):
    """
    Return a string-based progress bar given some parameters

    Parameters
    ----------
    n  : int
        Number of finished iterations.
    total  : int
        The expected total number of iterations. If None, only basic progress
        statistics are displayed (no ETA).
    elapsed  : float
        Number of seconds passed since start.
    ncols  : int, optional
        The width of the entire output message. If sepcified, dynamically
        resizes the progress meter [default: None]. The fallback meter
        width is 10.
    prefix  : str, optional
        Prefix message (included in total width).
    unit  : str, optional
        String that will be used to define the unit of each iteration.
        [default: "it"]
    unit_scale  : bool, optional
        If set, the number of iterations will be reduced/scaled
        automatically and a metric prefix following the
        International System of Units standard will be added
        (kilo, mega, etc.). [default: False]
    ascii  : bool, optional
        If not set, use unicode (smooth blocks) to fill the meter
        [default: False]. The fallback is to use ASCII characters (1-9 #).

    Returns
    -------
    out  : Formatted meter and stats, ready to display.
    """

    # in case the total is wrong (n is above the total), then
    # we switch to the mode without showing the total prediction
    # (since ETA would be wrong anyway)
    if total and n > total:
        total = None

    elapsed_str = format_interval(elapsed)
    if elapsed:
        if unit_scale:
            rate = format_sizeof(n / elapsed, suffix='')
        else:
            rate = '{0:5.2f}'.format(n / elapsed)
    else:
        rate = '?'

    rate_unit = unit if unit else 'it'
    if not unit:
        unit = ''

    n_fmt = str(n)
    total_fmt = str(total)
    if unit_scale:
        n_fmt = format_sizeof(n, suffix='')
        if total:
            total_fmt = format_sizeof(total, suffix='')

    if total:
        frac = n / total
        percentage = frac * 100

        remaining_str = format_interval(elapsed * (total-n) / n) if n else '?'

        l_bar = '{1}{0:.0f}%|'.format(percentage, prefix) if prefix else \
                '{0:3.0f}%|'.format(percentage)
        r_bar = '| {0}/{1}{2} [{3}<{4}, {5} {6}/s]'.format(
                n_fmt, total_fmt, unit, elapsed_str, remaining_str,
                rate, rate_unit)

        if ncols == 0:
            bar = ''
        else:
            N_BARS = max(1, ncols - len(l_bar) - len(r_bar)) if ncols \
                             else 10

            if ascii:
                bar_length, frac_bar_length = divmod(
                    int(frac * N_BARS * 10), 10)

                bar = '#'*bar_length
                frac_bar = chr(48 + frac_bar_length) if frac_bar_length \
                                else ' '

            else:
                bar_length, frac_bar_length = divmod(int(frac * N_BARS * 8), 8)

                bar = _unich(0x2588)*bar_length
                frac_bar = _unich(0x2590 - frac_bar_length) \
                    if frac_bar_length else ' '

        if bar_length < N_BARS:
            full_bar = bar + frac_bar + \
                ' ' * max(N_BARS - bar_length - 1, 0)  # spacing
        else:
            full_bar = bar + \
                ' ' * max(N_BARS - bar_length, 0)  # spacing

        return l_bar + full_bar + r_bar

    else:  # no progressbar nor ETA, just progress statistics
        return '{0}{1} [{2}, {3} {4}/s]'.format(
            n_fmt, unit, elapsed_str, rate, rate_unit)

Response: Returns a string - based progress bar given some parameters.


Instruction:
Here you have a function in Python 3, explain what it does
def getIcon( data ):
    import cStringIO
    stream = cStringIO.StringIO(data)
    image = wx.ImageFromStream(stream)
    icon = wx.EmptyIcon()
    icon.CopyFromBitmap(wx.BitmapFromImage(image))
    return icon

Response: Return the data from the resource as a wxIcon


Instruction:
Given the following Python 3 function, write the documentation
def main():
    logging.basicConfig(level=logging.INFO)
    app = RunSnakeRunApp(0)
    app.MainLoop()

Response: Main loop for the application


Instruction:
Explain what the following Python 3 code does
def CreateControls(self, config_parser):
        self.CreateMenuBar()
        self.SetupToolBar()
        self.CreateStatusBar()
        self.leftSplitter = wx.SplitterWindow(
            self
        )
        self.rightSplitter = wx.SplitterWindow(
            self.leftSplitter
        )
        self.listControl = listviews.DataView(
            self.leftSplitter,
            columns = PROFILE_VIEW_COLUMNS,
            name='mainlist',
        )
        self.squareMap = squaremap.SquareMap(
            self.rightSplitter,
            padding = 6,
            labels = True,
            adapter = self.adapter,
            square_style = True,
        )
        self.tabs = wx.Notebook(
            self.rightSplitter,
        )

        self.CreateSourceWindow(self.tabs)

        self.calleeListControl = listviews.DataView(
            self.tabs,
            columns = PROFILE_VIEW_COLUMNS,
            name='callee',
        )
        self.allCalleeListControl = listviews.DataView(
            self.tabs,
            columns = PROFILE_VIEW_COLUMNS,
            name='allcallee',
        )
        self.allCallerListControl = listviews.DataView(
            self.tabs,
            columns = PROFILE_VIEW_COLUMNS,
            name='allcaller',
        )
        self.callerListControl = listviews.DataView(
            self.tabs,
            columns = PROFILE_VIEW_COLUMNS,
            name='caller',
        )
        self.ProfileListControls = [
            self.listControl,
            self.calleeListControl,
            self.allCalleeListControl,
            self.callerListControl,
            self.allCallerListControl,
        ]
        self.tabs.AddPage(self.calleeListControl, _('Callees'), True)
        self.tabs.AddPage(self.allCalleeListControl, _('All Callees'), False)
        self.tabs.AddPage(self.callerListControl, _('Callers'), False)
        self.tabs.AddPage(self.allCallerListControl, _('All Callers'), False)
        if editor:
            self.tabs.AddPage(self.sourceCodeControl, _('Source Code'), False)
        self.rightSplitter.SetSashSize(10)
        # calculate size as proportional value for initial display...
        self.LoadState( config_parser )
        width, height = self.GetSizeTuple()
        rightsplit = 2 * (height // 3)
        leftsplit = width // 3
        self.rightSplitter.SplitHorizontally(self.squareMap, self.tabs,
                                             rightsplit)
        self.leftSplitter.SplitVertically(self.listControl, self.rightSplitter,
                                          leftsplit)
        squaremap.EVT_SQUARE_HIGHLIGHTED(self.squareMap,
                                         self.OnSquareHighlightedMap)
        squaremap.EVT_SQUARE_SELECTED(self.listControl,
                                      self.OnSquareSelectedList)
        squaremap.EVT_SQUARE_SELECTED(self.squareMap, self.OnSquareSelectedMap)
        squaremap.EVT_SQUARE_ACTIVATED(self.squareMap, self.OnNodeActivated)
        for control in self.ProfileListControls:
            squaremap.EVT_SQUARE_ACTIVATED(control, self.OnNodeActivated)
            squaremap.EVT_SQUARE_HIGHLIGHTED(control,
                                             self.OnSquareHighlightedList)
        self.moreSquareViewItem.Check(self.squareMap.square_style)

Response: Create our control classes


Instruction:
Write a Python 3 script to
create our menu - bar for triggering operations

Response:
def CreateMenuBar(self):
        """Create our menu-bar for triggering operations"""
        menubar = wx.MenuBar()
        menu = wx.Menu()
        menu.Append(ID_OPEN, _('&Open Profile'), _('Open a cProfile file'))
        menu.Append(ID_OPEN_MEMORY, _('Open &Memory'), _('Open a Meliae memory-dump file'))
        menu.AppendSeparator()
        menu.Append(ID_EXIT, _('&Close'), _('Close this RunSnakeRun window'))
        menubar.Append(menu, _('&File'))
        menu = wx.Menu()
#        self.packageMenuItem = menu.AppendCheckItem(
#            ID_PACKAGE_VIEW, _('&File View'),
#            _('View time spent by package/module')
#        )
        self.percentageMenuItem = menu.AppendCheckItem(
            ID_PERCENTAGE_VIEW, _('&Percentage View'),
            _('View time spent as percent of overall time')
        )
        self.rootViewItem = menu.Append(
            ID_ROOT_VIEW, _('&Root View (Home)'),
            _('View the root of the tree')
        )
        self.backViewItem = menu.Append(
            ID_BACK_VIEW, _('&Back'), _('Go back in your viewing history')
        )
        self.upViewItem = menu.Append(
            ID_UP_VIEW, _('&Up'),
            _('Go "up" to the parent of this node with the largest cumulative total')
        )
        self.moreSquareViewItem = menu.AppendCheckItem(
            ID_MORE_SQUARE, _('&Hierarchic Squares'),
            _('Toggle hierarchic squares in the square-map view')
        )

        # This stuff isn't really all that useful for profiling,
        # it's more about how to generate graphics to describe profiling...
        self.deeperViewItem = menu.Append(
            ID_DEEPER_VIEW, _('&Deeper'), _('View deeper squaremap views')
        )
        self.shallowerViewItem = menu.Append(
            ID_SHALLOWER_VIEW, _('&Shallower'), _('View shallower squaremap views')
        )
#        wx.ToolTip.Enable(True)
        menubar.Append(menu, _('&View'))

        self.viewTypeMenu =wx.Menu( )
        menubar.Append(self.viewTypeMenu, _('View &Type'))

        self.SetMenuBar(menubar)

        wx.EVT_MENU(self, ID_EXIT, lambda evt: self.Close(True))
        wx.EVT_MENU(self, ID_OPEN, self.OnOpenFile)
        wx.EVT_MENU(self, ID_OPEN_MEMORY, self.OnOpenMemory)

        wx.EVT_MENU(self, ID_PERCENTAGE_VIEW, self.OnPercentageView)
        wx.EVT_MENU(self, ID_UP_VIEW, self.OnUpView)
        wx.EVT_MENU(self, ID_DEEPER_VIEW, self.OnDeeperView)
        wx.EVT_MENU(self, ID_SHALLOWER_VIEW, self.OnShallowerView)
        wx.EVT_MENU(self, ID_ROOT_VIEW, self.OnRootView)
        wx.EVT_MENU(self, ID_BACK_VIEW, self.OnBackView)
        wx.EVT_MENU(self, ID_MORE_SQUARE, self.OnMoreSquareToggle)


Instruction:
Make a summary of the following Python 3 code
def CreateSourceWindow(self, tabs):
        if editor and self.sourceCodeControl is None:
            self.sourceCodeControl = wx.py.editwindow.EditWindow(
                self.tabs, -1
            )
            self.sourceCodeControl.SetText(u"")
            self.sourceFileShown = None
            self.sourceCodeControl.setDisplayLineNumbers(True)

Response: Create our source - view window for tabs


Instruction:
Write a Python 3 function for
creating the toolbar for common actions

Response:
def SetupToolBar(self):
        """Create the toolbar for common actions"""
        tb = self.CreateToolBar(self.TBFLAGS)
        tsize = (24, 24)
        tb.ToolBitmapSize = tsize
        open_bmp = wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR,
                                            tsize)
        tb.AddLabelTool(ID_OPEN, "Open", open_bmp, shortHelp="Open",
                        longHelp="Open a (c)Profile trace file")
        if not osx:
            tb.AddSeparator()
#        self.Bind(wx.EVT_TOOL, self.OnOpenFile, id=ID_OPEN)
        self.rootViewTool = tb.AddLabelTool(
            ID_ROOT_VIEW, _("Root View"),
            wx.ArtProvider.GetBitmap(wx.ART_GO_HOME, wx.ART_TOOLBAR, tsize),
            shortHelp=_("Display the root of the current view tree (home view)")
        )
        self.rootViewTool = tb.AddLabelTool(
            ID_BACK_VIEW, _("Back"),
            wx.ArtProvider.GetBitmap(wx.ART_GO_BACK, wx.ART_TOOLBAR, tsize),
            shortHelp=_("Back to the previously activated node in the call tree")
        )
        self.upViewTool = tb.AddLabelTool(
            ID_UP_VIEW, _("Up"),
            wx.ArtProvider.GetBitmap(wx.ART_GO_UP, wx.ART_TOOLBAR, tsize),
            shortHelp=_("Go one level up the call tree (highest-percentage parent)")
        )
        if not osx:
            tb.AddSeparator()
        # TODO: figure out why the control is sizing the label incorrectly on Linux
        self.percentageViewTool = wx.CheckBox(tb, -1, _("Percent    "))
        self.percentageViewTool.SetToolTip(wx.ToolTip(
            _("Toggle display of percentages in list views")))
        tb.AddControl(self.percentageViewTool)
        wx.EVT_CHECKBOX(self.percentageViewTool,
                        self.percentageViewTool.GetId(), self.OnPercentageView)

        self.viewTypeTool= wx.Choice( tb, -1, choices= getattr(self.loader,'ROOTS',[]) )
        self.viewTypeTool.SetToolTip(wx.ToolTip(
            _("Switch between different hierarchic views of the data")))
        wx.EVT_CHOICE( self.viewTypeTool, self.viewTypeTool.GetId(), self.OnViewTypeTool )
        tb.AddControl( self.viewTypeTool )
        tb.Realize()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def ConfigureViewTypeChoices( self, event=None ):
        self.viewTypeTool.SetItems( getattr( self.loader, 'ROOTS', [] ))
        if self.loader and self.viewType in self.loader.ROOTS:
            self.viewTypeTool.SetSelection( self.loader.ROOTS.index( self.viewType ))

        # configure the menu with the available choices...
        def chooser( typ ):
            def Callback( event ):
                if typ != self.viewType:
                    self.viewType = typ
                    self.OnRootView( event )
            return Callback
        # Clear all previous items
        for item in self.viewTypeMenu.GetMenuItems():
            self.viewTypeMenu.DeleteItem( item )
        if self.loader and self.loader.ROOTS:
            for root in self.loader.ROOTS:
                item = wx.MenuItem(
                    self.viewTypeMenu, -1, root.title(),
                    _("View hierarchy by %(name)s")%{
                        'name': root.title(),
                    },
                    kind=wx.ITEM_RADIO,
                )
                item.SetCheckable( True )
                self.viewTypeMenu.AppendItem( item )
                item.Check( root == self.viewType )
                wx.EVT_MENU( self, item.GetId(), chooser( root ))

Response: Configure the set of view types in the toolbar and menus


Instruction:
Write a Python 3 function for
requesting to open a new profile file

Response:
def OnOpenFile(self, event):
        """Request to open a new profile file"""
        dialog = wx.FileDialog(self, style=wx.OPEN|wx.FD_MULTIPLE)
        if dialog.ShowModal() == wx.ID_OK:
            paths = dialog.GetPaths()
            if self.loader:
                # we've already got a displayed data-set, open new window...
                frame = MainFrame()
                frame.Show(True)
                frame.load(*paths)
            else:
                self.load(*paths)


Instruction:
Given the following Python 3 function, write the documentation
def OnOpenMemory(self, event):
        dialog = wx.FileDialog(self, style=wx.OPEN)
        if dialog.ShowModal() == wx.ID_OK:
            path = dialog.GetPath()
            if self.loader:
                # we've already got a displayed data-set, open new window...
                frame = MainFrame()
                frame.Show(True)
                frame.load_memory(path)
            else:
                self.load_memory(path)

Response: Request to open a new profile file


Instruction:
Can you generate a brief explanation for the following Python 3 code
def SetPackageView(self, directoryView):
        self.directoryView = not self.directoryView
        self.packageMenuItem.Check(self.directoryView)
        self.packageViewTool.SetValue(self.directoryView)
        if self.loader:
            self.SetModel(self.loader)
        self.RecordHistory()

Response: Sets whether to use directory based view