Instruction:
Create a Python 3 function for
setting whether to display percentage or absolute values

Response:
def SetPercentageView(self, percentageView):
        """Set whether to display percentage or absolute values"""
        self.percentageView = percentageView
        self.percentageMenuItem.Check(self.percentageView)
        self.percentageViewTool.SetValue(self.percentageView)
        total = self.adapter.value( self.loader.get_root( self.viewType ) )
        for control in self.ProfileListControls:
            control.SetPercentage(self.percentageView, total)
        self.adapter.SetPercentage(self.percentageView, total)


Instruction:
Write a Python 3 script for
requesting to move up the hierarchy to highest - weight parent

Response:
def OnUpView(self, event):
        """Request to move up the hierarchy to highest-weight parent"""
        node = self.activated_node
        parents = []
        selected_parent = None

        if node:
            if hasattr( self.adapter, 'best_parent' ):
                selected_parent = self.adapter.best_parent( node )
            else:
                parents = self.adapter.parents( node )
            if parents:
                if not selected_parent:
                    parents.sort(key = lambda a: self.adapter.value(node, a))
                    selected_parent = parents[-1]
                class event:
                    node = selected_parent
                self.OnNodeActivated(event)
            else:
                self.SetStatusText(_('No parents for the currently selected node: %(node_name)s')
                                   % dict(node_name=self.adapter.label(node)))
        else:
            self.SetStatusText(_('No currently selected node'))


Instruction:
How would you implement a function in Python 3 that
backs view event handler

Response:
def OnBackView(self, event):
        """Request to move backward in the history"""
        self.historyIndex -= 1
        try:
            self.RestoreHistory(self.history[self.historyIndex])
        except IndexError, err:
            self.SetStatusText(_('No further history available'))


Instruction:
Explain what the following Python 3 code does
def OnRootView(self, event):
        self.adapter, tree, rows = self.RootNode()
        self.squareMap.SetModel(tree, self.adapter)
        self.RecordHistory()
        self.ConfigureViewTypeChoices()

Response: Reset view to the root of the tree


Instruction:
Can you generate the documentation for the following Python 3 function
def OnNodeActivated(self, event):
        self.activated_node = self.selected_node = event.node
        self.squareMap.SetModel(event.node, self.adapter)
        self.squareMap.SetSelected( event.node )
        if editor:
            if self.SourceShowFile(event.node):
                if hasattr(event.node,'lineno'):
                    self.sourceCodeControl.GotoLine(event.node.lineno)
        self.RecordHistory()

Response: Double - click or enter on a node in some control...


Instruction:
Create a Python 3 function to
show the given file in the source - code view ( attempt it anyway

Response:
def SourceShowFile(self, node):
        """Show the given file in the source-code view (attempt it anyway)"""
        filename = self.adapter.filename( node )
        if filename and self.sourceFileShown != filename:
            try:
                data = open(filename).read()
            except Exception, err:
                # TODO: load from zips/eggs? What about .pyc issues?
                return None
            else:
                #self.sourceCodeControl.setText(data)
                self.sourceCodeControl.ClearAll()
                self.sourceCodeControl.AppendText( data )
        return filename


Instruction:
Can you generate a brief explanation for the following Python 3 code
def OnSquareSelected(self, event):
        self.selected_node = event.node
        self.calleeListControl.integrateRecords(self.adapter.children( event.node) )
        self.callerListControl.integrateRecords(self.adapter.parents( event.node) )

Response: Square selected event handler


Instruction:
Here you have a function in Python 3, explain what it does
def OnMoreSquareToggle( self, event ):
        self.squareMap.square_style = not self.squareMap.square_style
        self.squareMap.Refresh()
        self.moreSquareViewItem.Check(self.squareMap.square_style)

Response: More Square toggle event handler


Instruction:
Explain what the following Python 3 code does
def RecordHistory(self):
        if not self.restoringHistory:
            record = self.activated_node
            if self.historyIndex < -1:
                try:
                    del self.history[self.historyIndex+1:]
                except AttributeError, err:
                    pass
            if (not self.history) or record != self.history[-1]:
                self.history.append(record)
            del self.history[:-200]
            self.historyIndex = -1

Response: Record the given node in the history - set.


Instruction:
Can you tell what is the following Python 3 function doing
def load(self, *filenames):
        if len(filenames) == 1:
            if os.path.basename( filenames[0] ) == 'index.coldshot':
                return self.load_coldshot( os.path.dirname( filenames[0]) )
            elif os.path.isdir( filenames[0] ):
                return self.load_coldshot( filenames[0] )
        try:
            self.loader = pstatsloader.PStatsLoader(*filenames)
            self.ConfigureViewTypeChoices()
            self.SetModel( self.loader )
            self.viewType = self.loader.ROOTS[0]
            self.SetTitle(_("Run Snake Run: %(filenames)s")
                          % {'filenames': ', '.join(filenames)[:120]})
        except (IOError, OSError, ValueError,MemoryError), err:
            self.SetStatusText(
                _('Failure during load of %(filenames)s: %(err)s'
            ) % dict(
                filenames=" ".join([repr(x) for x in filenames]),
                err=err
            ))

Response: Load our dataset (iteratively)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def SetModel(self, loader):
        self.loader = loader
        self.adapter, tree, rows = self.RootNode()
        self.listControl.integrateRecords(rows.values())
        self.activated_node = tree
        self.squareMap.SetModel(tree, self.adapter)
        self.RecordHistory()

Response: Set our overall model and populate sub - controls


Instruction:
Write a Python 3 function that can
return our current root node and appropriate adapter for it

Response:
def RootNode(self):
        """Return our current root node and appropriate adapter for it"""
        tree = self.loader.get_root( self.viewType )
        adapter = self.loader.get_adapter( self.viewType )
        rows = self.loader.get_rows( self.viewType )

        adapter.SetPercentage(self.percentageView, adapter.value( tree ))

        return adapter, tree, rows


Instruction:
Write a Python 3 script for
retrieving window state to be restored on the next run...

Response:
def SaveState( self, config_parser ):
        """Retrieve window state to be restored on the next run..."""
        if not config_parser.has_section( 'window' ):
            config_parser.add_section( 'window' )
        if self.IsMaximized():
            config_parser.set( 'window', 'maximized', str(True))
        else:
            config_parser.set( 'window', 'maximized', str(False))
        size = self.GetSizeTuple()
        position = self.GetPositionTuple()
        config_parser.set( 'window', 'width', str(size[0]) )
        config_parser.set( 'window', 'height', str(size[1]) )
        config_parser.set( 'window', 'x', str(position[0]) )
        config_parser.set( 'window', 'y', str(position[1]) )

        for control in self.ProfileListControls:
            control.SaveState( config_parser )

        return config_parser


Instruction:
Make a summary of the following Python 3 code
def LoadState( self, config_parser ):
        if not config_parser:
            return
        if (
            not config_parser.has_section( 'window' ) or (
                config_parser.has_option( 'window','maximized' ) and
                config_parser.getboolean( 'window', 'maximized' )
            )
        ):
            self.Maximize(True)
        try:
            width,height,x,y = [
                config_parser.getint( 'window',key )
                for key in ['width','height','x','y']
            ]
            self.SetPosition( (x,y))
            self.SetSize( (width,height))
        except ConfigParser.NoSectionError, err:
            # the file isn't written yet, so don't even warn...
            pass
        except Exception, err:
            # this is just convenience, if it breaks in *any* way, ignore it...
            log.error(
                "Unable to load window preferences, ignoring: %s", traceback.format_exc()
            )

        try:
            font_size = config_parser.getint('window', 'font_size')
        except Exception:
            pass # use the default, by default
        else:
            font = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
            font.SetPointSize(font_size)
            for ctrl in self.ProfileListControls:
                ctrl.SetFont(font)

        for control in self.ProfileListControls:
            control.LoadState( config_parser )

        self.config = config_parser
        wx.EVT_CLOSE( self, self.OnCloseWindow )

Response: Load our window state from the given config_parser instance


Instruction:
Create a Python 3 function to
compute the edit distance between two sequences and return it.

Response:
def fast_comp(seq1, seq2, transpositions=False):
	"""Compute the distance between the two sequences `seq1` and `seq2` up to a
	maximum of 2 included, and return it. If the edit distance between the two
	sequences is higher than that, -1 is returned.

	If `transpositions` is `True`, transpositions will be taken into account for
	the computation of the distance. This can make a difference, e.g.:

		>>> fast_comp("abc", "bac", transpositions=False)
		2
		>>> fast_comp("abc", "bac", transpositions=True)
		1

	This is faster than `levenshtein` by an order of magnitude, but on the
	other hand is of limited use.

	The algorithm comes from `http://writingarchives.sakura.ne.jp/fastcomp`.
	I've added transpositions support to the original code.
	"""
	replace, insert, delete = "r", "i", "d"

	L1, L2  = len(seq1), len(seq2)
	if L1 < L2:
		L1, L2 = L2, L1
		seq1, seq2 = seq2, seq1

	ldiff = L1 - L2
	if ldiff == 0:
		models = (insert+delete, delete+insert, replace+replace)
	elif ldiff == 1:
		models = (delete+replace, replace+delete)
	elif ldiff == 2:
		models = (delete+delete,)
	else:
		return -1

	res = 3
	for model in models:
		i = j = c = 0
		while (i < L1) and (j < L2):
			if seq1[i] != seq2[j]:
				c = c+1
				if 2 < c:
					break

				if transpositions and ldiff != 2 \
            	and i < L1 - 1 and j < L2 - 1 \
            	and seq1[i+1] == seq2[j] and seq1[i] == seq2[j+1]:
					i, j = i+2, j+2
				else:
					cmd = model[c-1]
					if cmd == delete:
						i = i+1
					elif cmd == insert:
						j = j+1
					else:
						assert cmd == replace
						i,j = i+1, j+1
			else:
				i,j = i+1, j+1

		if 2 < c:
			continue
		elif i < L1:
			if L1-i <= model[c:].count(delete):
				c = c + (L1-i)
			else:
				continue
		elif j < L2:
			if L2-j <= model[c:].count(insert):
				c = c + (L2-j)
			else:
				continue

		if c < res:
			res = c

	if res == 3:
		res = -1
	return res


Instruction:
Write a Python 3 function that can
check if a path is an actual file that exists'''

Response:
def is_file(dirname):
    '''Checks if a path is an actual file that exists'''
    if not os.path.isfile(dirname):
        msg = "{0} is not an existing file".format(dirname)
        raise argparse.ArgumentTypeError(msg)
    else:
        return dirname


Instruction:
Make a summary of the following Python 3 code
def is_dir(dirname):
    '''Checks if a path is an actual directory that exists'''
    if not os.path.isdir(dirname):
        msg = "{0} is not a directory".format(dirname)
        raise argparse.ArgumentTypeError(msg)
    else:
        return dirname

Response: Checks if a path is an actual directory that exists'''


Instruction:
How would you code a function in Python 3 to
check if a path is an actual directory that exists or a file

Response:
def is_dir_or_file(dirname):
    '''Checks if a path is an actual directory that exists or a file'''
    if not os.path.isdir(dirname) and not os.path.isfile(dirname):
        msg = "{0} is not a directory nor a file".format(dirname)
        raise argparse.ArgumentTypeError(msg)
    else:
        return dirname


Instruction:
Can you generate the documentation for the following Python 3 function
def fullpath(relpath):
    '''Relative path to absolute'''
    if (type(relpath) is object or type(relpath) is file):
        relpath = relpath.name
    return os.path.abspath(os.path.expanduser(relpath))

Response: Relative path to absolute


Instruction:
Can you generate the documentation for the following Python 3 function
def recwalk(inputpath, sorting=True):
    '''Recursively walk through a folder. This provides a mean to flatten out the files restitution (necessary to show a progress bar). This is a generator.'''
    # If it's only a single file, return this single file
    if os.path.isfile(inputpath):
        abs_path = fullpath(inputpath)
        yield os.path.dirname(abs_path), os.path.basename(abs_path)
    # Else if it's a folder, walk recursively and return every files
    else:
        for dirpath, dirs, files in walk(inputpath):
            if sorting:
                files.sort()
                dirs.sort() # sort directories in-place for ordered recursive walking
            for filename in files:
                yield (dirpath, filename)

Response: Recursively walk through a folder and return all files and directories in the folder.


Instruction:
Write a Python 3 function for
converting a path given in any format converts to posix path format fromwinpath forces the input path to be recognized as a Windows path

Response:
def path2unix(path, nojoin=False, fromwinpath=False):
    '''From a path given in any format, converts to posix path format
    fromwinpath=True forces the input path to be recognized as a Windows path (useful on Unix machines to unit test Windows paths)'''
    if fromwinpath:
        pathparts = list(PureWindowsPath(path).parts)
    else:
        pathparts = list(PurePath(path).parts)
    if nojoin:
        return pathparts
    else:
        return posixpath.join(*pathparts)


Instruction:
Can you tell what is the following Python 3 function doing
def get_next_entry(file, entrymarker="\xFE\xFF\xFE\xFF\xFE\xFF\xFE\xFF\xFE\xFF", only_coord=True, blocksize=65535):
    '''Find or read the next ecc entry in a given ecc file.
    Call this function multiple times with the same file handle to get subsequent markers positions (this is not a generator but it works very similarly, because it will continue reading from the file's current cursor position -- this can be used advantageously if you want to read only a specific entry by seeking before supplying the file handle).
    This will read any string length between two entrymarkers.
    The reading is very tolerant, so it will always return any valid entry (but also scrambled entries if any, but the decoding will ensure everything's ok).
    `file` is a file handle, not the path to the file.'''
    found = False
    start = None # start and end vars are the relative position of the starting/ending entrymarkers in the current buffer
    end = None
    startcursor = None # startcursor and endcursor are the absolute position of the starting/ending entrymarkers inside the database file
    endcursor = None
    buf = 1
    # Sanity check: cannot screen the file's content if the window is of the same size as the pattern to match (the marker)
    if blocksize <= len(entrymarker): blocksize = len(entrymarker) + 1
    # Continue the search as long as we did not find at least one starting marker and one ending marker (or end of file)
    while (not found and buf):
        # Read a long block at once, we will readjust the file cursor after
        buf = file.read(blocksize)
        # Find the start marker (if not found already)
        if start is None or start == -1:
            start = buf.find(entrymarker); # relative position of the starting marker in the currently read string
            if start >= 0 and not startcursor: # assign startcursor only if it's empty (meaning that we did not find the starting entrymarker, else if found we are only looking for
                startcursor = file.tell() - len(buf) + start # absolute position of the starting marker in the file
            if start >= 0: start = start + len(entrymarker)
        # If we have a starting marker, we try to find a subsequent marker which will be the ending of our entry (if the entry is corrupted we don't care: it won't pass the entry_to_dict() decoding or subsequent steps of decoding and we will just pass to the next ecc entry). This allows to process any valid entry, no matter if previous ones were scrambled.
        if startcursor is not None and startcursor >= 0:
            end = buf.find(entrymarker, start)
            if end < 0 and len(buf) < blocksize: # Special case: we didn't find any ending marker but we reached the end of file, then we are probably in fact just reading the last entry (thus there's no ending marker for this entry)
                end = len(buf) # It's ok, we have our entry, the ending marker is just the end of file
            # If we found an ending marker (or if end of file is reached), then we compute the absolute cursor value and put the file reading cursor back in position, just before the next entry (where the ending marker is if any)
            if end >= 0:
                endcursor = file.tell() - len(buf) + end
                # Make sure we are not redetecting the same marker as the start marker
                if endcursor > startcursor:
                    file.seek(endcursor)
                    found = True
                else:
                    end = -1
                    encursor = None
        #print("Start:", start, startcursor)
        #print("End: ", end, endcursor)
        # Stop criterion to avoid infinite loop: in the case we could not find any entry in the rest of the file and we reached the EOF, we just quit now
        if len(buf) < blocksize: break
        # Did not find the full entry in one buffer? Reinit variables for next iteration, but keep in memory startcursor.
        if start > 0: start = 0 # reset the start position for the end buf find at next iteration (ie: in the arithmetic operations to compute the absolute endcursor position, the start entrymarker won't be accounted because it was discovered in a previous buffer).
        if not endcursor: file.seek(file.tell()-len(entrymarker)) # Try to fix edge case where blocksize stops the buffer exactly in the middle of the ending entrymarker. The starting marker should always be ok because it should be quite close (or generally immediately after) the previous entry, but the end depends on the end of the current entry (size of the original file), thus the buffer may miss the ending entrymarker. should offset file.seek(-len(entrymarker)) before searching for ending.

    if found: # if an entry was found, we seek to the beginning of the entry and then either read the entry from file or just return the markers positions (aka the entry bounds)
        file.seek(startcursor + len(entrymarker))
        if only_coord:
            # Return only coordinates of the start and end markers
            # Note: it is useful to just return the reading positions and not the entry itself because it can get quite huge and may overflow memory, thus we will read each ecc blocks on request using a generator.
            return [startcursor + len(entrymarker), endcursor]
        else:
            # Return the full entry's content
            return file.read(endcursor - startcursor - len(entrymarker))
    else:
        # Nothing found (or no new entry to find, we've already found them all), so we return None
        return None

Response: Find or read the next entry in a given ecc file.


Instruction:
Make a summary of the following Python 3 code
def remove_if_exist(path):  # pragma: no cover
    if os.path.exists(path):
        if os.path.isdir(path):
            shutil.rmtree(path)
            return True
        elif os.path.isfile(path):
            os.remove(path)
            return True
    return False

Response: Delete a file or a directory recursively if it exists else no exception is raised


Instruction:
Create a Python 3 function to
copy a file or a directory tree deleting the destination before processing

Response:
def copy_any(src, dst, only_missing=False):  # pragma: no cover
    """Copy a file or a directory tree, deleting the destination before processing"""
    if not only_missing:
        remove_if_exist(dst)
    if os.path.exists(src):
        if os.path.isdir(src):
            if not only_missing:
                shutil.copytree(src, dst, symlinks=False, ignore=None)
            else:
                for dirpath, filepath in recwalk(src):
                    srcfile = os.path.join(dirpath, filepath)
                    relpath = os.path.relpath(srcfile, src)
                    dstfile = os.path.join(dst, relpath)
                    if not os.path.exists(dstfile):
                        create_dir_if_not_exist(os.path.dirname(dstfile))
                        shutil.copyfile(srcfile, dstfile)
                        shutil.copystat(srcfile, dstfile)
            return True
        elif os.path.isfile(src) and (not only_missing or not os.path.exists(dst)):
            shutil.copyfile(src, dst)
            shutil.copystat(src, dst)
            return True
    return False


Instruction:
Can you generate the documentation for the following Python 3 function
def group_files_by_size(fileslist, multi):  # pragma: no cover
    ''' Cluster files into the specified number of groups, where each groups total size is as close as possible to each other.

    Pseudo-code (O(n^g) time complexity):
    Input: number of groups G per cluster, list of files F with respective sizes
    - Order F by descending size
    - Until F is empty:
        - Create a cluster X
        - A = Pop first item in F
        - Put A in X[0] (X[0] is thus the first group in cluster X)
        For g in 1..len(G)-1 :
            - B = Pop first item in F
            - Put B in X[g]
            - group_size := size(B)
            If group_size != size(A):
                While group_size < size(A):
                    - Find next item C in F which size(C) <= size(A) - group_size
                    - Put C in X[g]
                    - group_size := group_size + size(C)
    '''
    flord = OrderedDict(sorted(fileslist.items(), key=lambda x: x[1], reverse=True))
    if multi <= 1:
        fgrouped = {}
        i = 0
        for x in flord.keys():
            i += 1
            fgrouped[i] = [[x]]
        return fgrouped

    fgrouped = {}
    i = 0
    while flord:
        i += 1
        fgrouped[i] = []
        big_key, big_value = flord.popitem(0)
        fgrouped[i].append([big_key])
        for j in xrange(multi-1):
            cluster = []
            if not flord: break
            child_key, child_value = flord.popitem(0)
            cluster.append(child_key)
            if child_value == big_value:
                fgrouped[i].append(cluster)
                continue
            else:
                diff = big_value - child_value
                for key, value in flord.iteritems():
                    if value <= diff:
                        cluster.append(key)
                        del flord[key]
                        if value == diff:
                            break
                        else:
                            child_value += value
                            diff = big_value - child_value
                fgrouped[i].append(cluster)
    return fgrouped

Response: Group files into the specified number of groups where each group has the same size.


Instruction:
How would you implement a function in Python 3 that
gives a files list with sizes output a list where the files are grouped in nbgroups per cluster.

Response:
def group_files_by_size_fast(fileslist, nbgroups, mode=1):  # pragma: no cover
    '''Given a files list with sizes, output a list where the files are grouped in nbgroups per cluster.

    Pseudo-code for algorithm in O(n log(g)) (thank's to insertion sort or binary search trees)
    See for more infos: http://cs.stackexchange.com/questions/44406/fast-algorithm-for-clustering-groups-of-elements-given-their-size-time/44614#44614
    For each file:
        - If to-fill list is empty or file.size > first-key(to-fill):
          * Create cluster c with file in first group g1
          * Add to-fill[file.size].append([c, g2], [c, g3], ..., [c, gn])
        - Else:
          * ksize = first-key(to-fill)
          * c, g = to-fill[ksize].popitem(0)
          * Add file to cluster c in group g
          * nsize = ksize - file.size
          * if nsize > 0:
            . to-fill[nsize].append([c, g])
            . sort to-fill if not an automatic ordering structure
        '''
    ftofill = SortedList()
    ftofill_pointer = {}
    fgrouped = [] # [] or {}
    ford = sorted(fileslist.iteritems(), key=lambda x: x[1])
    last_cid = -1
    while ford:
        fname, fsize = ford.pop()
        #print "----\n"+fname, fsize
        #if ftofill: print "beforebranch", fsize, ftofill[-1]
        #print ftofill
        if not ftofill or fsize > ftofill[-1]:
            last_cid += 1
            #print "Branch A: create cluster %i" % last_cid
            fgrouped.append([])
            #fgrouped[last_cid] = []
            fgrouped[last_cid].append([fname])
            if mode==0:
                for g in xrange(nbgroups-1, 0, -1):
                    fgrouped[last_cid].append([])
                    if not fsize in ftofill_pointer:
                        ftofill_pointer[fsize] = []
                    ftofill_pointer[fsize].append((last_cid, g))
                    ftofill.add(fsize)
            else:
                for g in xrange(1, nbgroups):
                    try:
                        fgname, fgsize = ford.pop()
                        #print "Added to group %i: %s %i" % (g, fgname, fgsize)
                    except IndexError:
                        break
                    fgrouped[last_cid].append([fgname])
                    diff_size = fsize - fgsize
                    if diff_size > 0:
                        if not diff_size in ftofill_pointer:
                            ftofill_pointer[diff_size] = []
                        ftofill_pointer[diff_size].append((last_cid, g))
                        ftofill.add(diff_size)
        else:
            #print "Branch B"
            ksize = ftofill.pop()
            c, g = ftofill_pointer[ksize].pop()
            #print "Assign to cluster %i group %i" % (c, g)
            fgrouped[c][g].append(fname)
            nsize = ksize - fsize
            if nsize > 0:
                if not nsize in ftofill_pointer:
                    ftofill_pointer[nsize] = []
                ftofill_pointer[nsize].append((c, g))
                ftofill.add(nsize)
    return fgrouped


Instruction:
Given the following Python 3 function, write the documentation
def group_files_by_size_simple(fileslist, nbgroups):  # pragma: no cover
    ford = sorted(fileslist.iteritems(), key=lambda x: x[1], reverse=True)
    ford = [[x[0]] for x in ford]
    return [group for group in grouper(nbgroups, ford)]

Response: Simple and fast files grouping strategy


Instruction:
Here you have a function in Python 3, explain what it does
def grouped_count_sizes(fileslist, fgrouped):  # pragma: no cover
    '''Compute the total size per group and total number of files. Useful to check that everything is OK.'''
    fsizes = {}
    total_files = 0
    allitems = None
    if isinstance(fgrouped, dict):
        allitems = fgrouped.iteritems()
    elif isinstance(fgrouped, list):
        allitems = enumerate(fgrouped)
    for fkey, cluster in allitems:
        fsizes[fkey] = []
        for subcluster in cluster:
            tot = 0
            if subcluster is not None:
                for fname in subcluster:
                    tot += fileslist[fname]
                    total_files += 1
            fsizes[fkey].append(tot)
    return fsizes, total_files

Response: Compute the total size per group and total number of files. Useful to check that everything is OK.


Instruction:
Here you have a function in Python 3, explain what it does
def GetOptions(self):
    values = [c.GetValue()
              for c in chain(*self.widgets)
              if c.GetValue() is not None]
    return ' '.join(values)

Response: Returns the collective values from all of the available widgets in the current panel


Instruction:
Given the following Python 3 function, write the documentation
def GetValue(self):
    '''
    Positionals have no associated options_string,
    so only the supplied arguments are returned.
    The order is assumed to be the same as the order
    of declaration in the client code

    Returns
      "argument_value"
    '''
    self.AssertInitialization('Positional')
    if str(self._widget.GetValue()) == EMPTY:
      return None
    return self._widget.GetValue()

Response: Returns the value of the argument.


Instruction:
How would you code a function in Python 3 to
return the value of the choice argument

Response:
def GetValue(self):
    '''
    Returns
      "--option_name argument"
    '''
    self.AssertInitialization('Choice')
    if self._widget.GetValue() == self._DEFAULT_VALUE:
      return None
    return ' '.join(
      [self._action.option_strings[0] if self._action.option_strings else '',  # get the verbose copy if available
       self._widget.GetValue()])


Instruction:
Can you create a Python 3 function that
returns the value of the option.

Response:
def GetValue(self):
    '''
    General options are key/value style pairs (conceptually).
    Thus the name of the option, as well as the argument to it
    are returned
    e.g.
      >>> myscript --outfile myfile.txt
    returns
      "--Option Value"
    '''
    self.AssertInitialization('Optional')
    value = self._widget.GetValue()
    if not value or len(value) <= 0:
      return None
    return ' '.join(
      [self._action.option_strings[0],  # get the verbose copy if available
       value])


Instruction:
How would you implement a function in Python 3 that
returns the value of the option.

Response:
def GetValue(self):
    '''
    Flag options have no param associated with them.
    Thus we only need the name of the option.
    e.g
      >>> Python -v myscript
    returns
      Options name for argument (-v)
    '''
    if not self._widget.GetValue() or len(self._widget.GetValue()) <= 0:
        return None
    else:
        return self._action.option_strings[0]


Instruction:
How would you explain what the following Python 3 function does
def Update(self, size):
    '''
    Custom wrapper calculator to account for the
    increased size of the _msg widget after being
    inlined with the wx.CheckBox
    '''
    if self._msg is None:
      return
    help_msg = self._msg
    width, height = size
    content_area = int((width / 3) * .70)

    wiggle_room = range(int(content_area - content_area * .05), int(content_area + content_area * .05))
    if help_msg.Size[0] not in wiggle_room:
      self._msg.SetLabel(self._msg.GetLabelText().replace('\n', ' '))
      self._msg.Wrap(content_area)

Response: Update the _msg widget with the given size


Instruction:
Write a Python 3 script for
returning the value of the DropDown option.

Response:
def GetValue(self):
    '''
    NOTE: Added on plane. Cannot remember exact implementation
    of counter objects. I believe that they count sequentail
    pairings of options
    e.g.
      -vvvvv
    But I'm not sure. That's what I'm going with for now.

    Returns
      str(action.options_string[0]) * DropDown Value
    '''
    dropdown_value = self._widget.GetValue()
    if not str(dropdown_value).isdigit():
      return None
    arg = str(self._action.option_strings[0]).replace('-', '')
    repeated_args = arg * int(dropdown_value)
    return '-' + repeated_args


Instruction:
Create a Python 3 function for
returning the full path to the language file

Response:
def get_path(language):
  ''' Returns the full path to the language file '''
  filename = language.lower() + '.json'
  lang_file_path = os.path.join(_DEFAULT_DIR, filename)
  if not os.path.exists(lang_file_path):
    raise IOError('Could not find {} language file'.format(language))
  return lang_file_path


Instruction:
Implement a function in Python 3 to
open and return the supplied json file

Response:
def load(filename):
  ''' Open and return the supplied json file '''
  global _DICTIONARY
  try:
    json_file = filename + '.json'
    with open(os.path.join(_DEFAULT_DIR, json_file), 'rb') as f:
      _DICTIONARY = json.load(f)
  except IOError:
    raise IOError('Language file not found. Make sure that your ',
                  'translation file is in the languages directory, ')


Instruction:
Can you tell what is the following Python 3 function doing
def safe_repr(obj, clip=None):
    try:
        s = repr(obj)
        if not clip or len(s) <= clip:
            return s
        else:
            return s[:clip-4]+'..'+s[-2:]
    except:
        return 'N/A'

Response: Convert object to string representation yielding the same result a repr and returns N / A instead of raising the exception.


Instruction:
How would you explain what the following Python 3 function does
def trunc(obj, max, left=0):
    s = str(obj)
    s = s.replace('\n', '|')
    if len(s) > max:
        if left:
            return '...'+s[len(s)-max+3:]
        else:
            return s[:(max-3)]+'...'
    else:
        return s

Response: Convert obj to string eliminate newlines and truncate the string to max characters.


Instruction:
Implement a Python 3 function for
returning a friendly timestamp represented as a string.

Response:
def pp_timestamp(t):
    """
    Get a friendly timestamp represented as a string.
    """
    if t is None:
        return ''
    h, m, s = int(t / 3600), int(t / 60 % 60), t % 60
    return "%02d:%02d:%05.2f" % (h, m, s)


Instruction:
How would you explain what the following Python 3 function does
def print_stats(self, stream=None):
        if not stream: # pragma: no cover
            stream = sys.stdout
        self.metadata.sort(key=lambda x: -x.size)
        stream.write('%-10s %8s %-12s %-46s\n' % ('id', 'size', 'type', 'representation'))
        for g in self.metadata:
            stream.write('0x%08x %8d %-12s %-46s\n' % (g.id, g.size, trunc(g.type, 12),
                trunc(g.str, 46)))
        stream.write('Garbage: %8d collected objects (%s in cycles): %12s\n' % \
            (self.count, self.num_in_cycles, pp(self.total_size)))

Response: Log annotated garbage objects to console or file.


Instruction:
Can you implement a function in Python 3 that
returns a set of profiled file names.

Response:
def getFilenameSet(self):
        """
        Returns a set of profiled file names.

        Note: "file name" is used loosely here. See python documentation for
        co_filename, linecache module and PEP302. It may not be a valid
        filesystem path.
        """
        result = set(self.file_dict)
        # Ignore profiling code. __file__ does not always provide consistent
        # results with f_code.co_filename (ex: easy_install with zipped egg),
        # so inspect current frame instead.
        # XXX: assumes all of pprofile code resides in a single file.
        result.discard(inspect.currentframe().f_code.co_filename)
        return result


Instruction:
Here you have a function in Python 3, explain what it does
def annotate(self, out, filename=None, commandline=None, relative_path=False):
        file_dict = self.file_dict
        total_time = self.total_time
        if commandline is not None:
            print >> out, 'Command line:', commandline
        print >> out, 'Total duration: %gs' % total_time
        if not total_time:
            return
        def percent(value, scale):
            if scale == 0:
                return 0
            return value * 100 / float(scale)
        for name in self._getFileNameList(filename):
            file_timing = file_dict[name]
            file_total_time = file_timing.getTotalTime()
            call_list_by_line = file_timing.getCallListByLine()
            print >> out, 'File:', name
            print >> out, 'File duration: %gs (%.2f%%)' % (file_total_time,
                percent(file_total_time, total_time))
            print >> out, _ANNOTATE_HEADER
            print >> out, _ANNOTATE_HORIZONTAL_LINE
            for lineno, _, _, hits, duration, line in self._iterFile(name,
                    call_list_by_line):
                if hits:
                    time_per_hit = duration / hits
                else:
                    time_per_hit = 0
                print >> out, _ANNOTATE_FORMAT % {
                    'lineno': lineno,
                    'hits': hits,
                    'time': duration,
                    'time_per_hit': time_per_hit,
                    'percent': percent(duration, total_time),
                    'line': line,
                },
                for _, _, hits, duration, callee_file, callee_line, \
                        callee_name in call_list_by_line.get(lineno, ()):
                    print >> out, _ANNOTATE_CALL_FORMAT % {
                        'hits': hits,
                        'time': duration,
                        'time_per_hit': duration / hits,
                        'percent': percent(duration, total_time),
                        'callee_file': callee_file,
                        'callee_line': callee_line,
                        'callee_name': callee_name,
                    }

Response: Dump annotated source code with current profiling statistics to out.


Instruction:
Write a Python 3 function for
writing data to stdout and file.

Response:
def write(self, data, end="\n", flush=True):
        """ Output data to stdout and/or file """
        if not self.nostdout:
            self.stdout.write(data+end)
        if self.file is not None:
            self.file.write(data+end)
        if flush:
            self.flush()


Instruction:
Can you implement a function in Python 3 that
forces commit changes to the file and stdout

Response:
def flush(self):
        """ Force commit changes to the file and stdout """
        if not self.nostdout:
            self.stdout.flush()
        if self.file is not None:
            self.file.flush()


Instruction:
Can you create a Python 3 function that
computes the absolute Levenshtein distance between two sequences seq1 and seq2.

Response:
def levenshtein(seq1, seq2, normalized=False, max_dist=-1):
	"""Compute the absolute Levenshtein distance between the two sequences
	`seq1` and `seq2`.

	The Levenshtein distance is the minimum number of edit operations necessary
	for transforming one sequence into the other. The edit operations allowed are:

		* deletion:     ABC -> BC, AC, AB
		* insertion:    ABC -> ABCD, EABC, AEBC..
		* substitution: ABC -> ABE, ADC, FBC..

	The `max_dist` parameter controls at which moment we should stop computing the
	distance between the provided sequences. If it is a negative integer, the
	distance will be computed until the sequences are exhausted; otherwise, the
	computation will stop at the moment the calculated distance is higher than
	`max_dist`, and then return -1. For example:

		>>> levenshtein("abc", "abcd", max_dist=1)  # dist = 1
		1
		>>> levenshtein("abc", "abcde", max_dist=1) # dist = 2
		-1

	This can be a time saver if you're not interested in the exact distance, but
	only need to check if the distance between the given sequences is below a
	given threshold.

	The `normalized` parameter is here for backward compatibility; providing
	it will result in a call to `nlevenshtein`, which should be used directly
	instead.
	"""
	if normalized:
		return nlevenshtein(seq1, seq2, method=1)

	if seq1 == seq2:
		return 0

	len1, len2 = len(seq1), len(seq2)
	if max_dist >= 0 and abs(len1 - len2) > max_dist:
		return -1
	if len1 == 0:
		return len2
	if len2 == 0:
		return len1
	if len1 < len2:
		len1, len2 = len2, len1
		seq1, seq2 = seq2, seq1

	column = array('L', range(len2 + 1))

	for x in range(1, len1 + 1):
		column[0] = x
		last = x - 1
		for y in range(1, len2 + 1):
			old = column[y]
			cost = int(seq1[x - 1] != seq2[y - 1])
			column[y] = min(column[y] + 1, column[y - 1] + 1, last + cost)
			last = old
		if max_dist >= 0 and min(column) > max_dist:
			return -1

	if max_dist >= 0 and column[len2] > max_dist:
		# stay consistent, even if we have the exact distance
		return -1
	return column[len2]


Instruction:
Write a Python 3 function for
computing the normalized Levenshtein distance between seq1 and seq2.

Response:
def nlevenshtein(seq1, seq2, method=1):
	"""Compute the normalized Levenshtein distance between `seq1` and `seq2`.

	Two normalization methods are provided. For both of them, the normalized
	distance will be a float between 0 and 1, where 0 means equal and 1
	completely different. The computation obeys the following patterns:

		0.0                       if seq1 == seq2
		1.0                       if len(seq1) == 0 or len(seq2) == 0
		edit distance / factor    otherwise

	The `method` parameter specifies which normalization factor should be used.
	It can have the value 1 or 2, which correspond to the following:

		1: the length of the shortest alignment between the sequences
		   (that is, the length of the longest sequence)
		2: the length of the longest alignment between the sequences

	Which normalization factor should be chosen is a matter of taste. The first
	one is cheap to compute. The second one is more costly, but it accounts
	better than the first one for parallelisms of symbols between the sequences.

	For the rationale behind the use of the second method, see:
	Heeringa, "Measuring Dialect Pronunciation Differences using Levenshtein
	Distance", 2004, p. 130 sq, which is available online at:
	http://www.let.rug.nl/~heeringa/dialectology/thesis/thesis.pdf
	"""

	if seq1 == seq2:
		return 0.0
	len1, len2 = len(seq1), len(seq2)
	if len1 == 0 or len2 == 0:
		return 1.0
	if len1 < len2: # minimize the arrays size
		len1, len2 = len2, len1
		seq1, seq2 = seq2, seq1

	if method == 1:
		return levenshtein(seq1, seq2) / float(len1)
	if method != 2:
		raise ValueError("expected either 1 or 2 for `method` parameter")

	column = array('L', range(len2 + 1))
	length = array('L', range(len2 + 1))

	for x in range(1, len1 + 1):

		column[0] = length[0] = x
		last = llast = x - 1

		for y in range(1, len2 + 1):

			# dist
			old = column[y]
			ic = column[y - 1] + 1
			dc = column[y] + 1
			rc = last + (seq1[x - 1] != seq2[y - 1])
			column[y] = min(ic, dc, rc)
			last = old

			# length
			lold = length[y]
			lic = length[y - 1] + 1 if ic == column[y] else 0
			ldc = length[y] + 1 if dc == column[y] else 0
			lrc = llast + 1 if rc == column[y] else 0
			length[y] = max(ldc, lic, lrc)
			llast = lold

	return column[y] / float(length[y])


Instruction:
Implement a Python 3 function for
determining all parents of a node in our tree

Response:
def parents(self, node):
        """Determine all parents of node in our tree"""
        return [
            parent for parent in
            getattr( node, 'parents', [] )
            if getattr(parent, 'tree', self.TREE) == self.TREE
        ]


Instruction:
Create a Python 3 function for
getting string reference to object. Stores a weak reference in a dictionary using the object s id as the key.

Response:
def get_ref(obj):
    """
    Get string reference to object. Stores a weak reference in a dictionary
    using the object's id as the key. If the object cannot be weakly
    referenced (e.g. dictionaries, frame objects), store a strong references
    in a classic dictionary.

    Returns the object's id as a string.
    """
    oid = id(obj)
    try:
        server.id2ref[oid] = obj
    except TypeError:
        server.id2obj[oid] = obj
    return str(oid)


Instruction:
Make a summary of the following Python 3 code
def get_obj(ref):
    oid = int(ref)
    return server.id2ref.get(oid) or server.id2obj[oid]

Response: Get object from string reference.


Instruction:
Given the following Python 3 function, write the documentation
def tracker_class(clsname):
    stats = server.stats
    if not stats:
        bottle.redirect('/tracker')
    stats.annotate()
    return dict(stats=stats, clsname=clsname)

Response: Get class instance details.


Instruction:
Write a Python 3 function for
getting reference cycle details.

Response:
def garbage_cycle(index):
    """Get reference cycle details."""
    graph = _compute_garbage_graphs()[int(index)]
    graph.reduce_to_cycles()
    objects = graph.metadata
    objects.sort(key=lambda x: -x.size)
    return dict(objects=objects, index=index)


Instruction:
Can you generate the documentation for the following Python 3 function
def _get_graph(graph, filename):
    try:
        rendered = graph.rendered_file
    except AttributeError:
        try:
            graph.render(os.path.join(server.tmpdir, filename), format='png')
            rendered = filename
        except OSError:
            rendered = None
    graph.rendered_file = rendered
    return rendered

Response: Retrieve or render a graph.


Instruction:
Can you tell what is the following Python 3 function doing
def garbage_graph(index):
    graph = _compute_garbage_graphs()[int(index)]
    reduce_graph = bottle.request.GET.get('reduce', '')
    if reduce_graph:
        graph = graph.reduce_to_cycles()
    if not graph:
        return None
    filename = 'garbage%so%s.png' % (index, reduce_graph)
    rendered_file = _get_graph(graph, filename)
    if rendered_file:
        bottle.send_file(rendered_file, root=server.tmpdir)
    else:
        return None

Response: Get graph representation of reference cycle.


Instruction:
Make a summary of the following Python 3 code
def start_profiler(host='localhost', port=8090, tracker=None, stats=None,
                   debug=False, **kwargs):
    """
    Start the web server to show profiling data. The function suspends the
    Python application (the current thread) until the web server is stopped.

    The only way to stop the server is to signal the running thread, e.g. press
    Ctrl+C in the console. If this isn't feasible for your application use
    `start_in_background` instead.

    During the execution of the web server, profiling data is (lazily) cached
    to improve performance. For example, garbage graphs are rendered when the
    garbage profiling data is requested and are simply retransmitted upon later
    requests.

    :param host: the host where the server shall run, default is localhost
    :param port: server listens on the specified port, default is 8090 to allow
        coexistance with common web applications
    :param tracker: `ClassTracker` instance, browse profiling data (on-line
        analysis)
    :param stats: `Stats` instance, analyze `ClassTracker` profiling dumps
        (useful for off-line analysis)
    """
    if tracker and not stats:
        server.stats = tracker.stats
    else:
        server.stats = stats
    try:
        server.tmpdir = mkdtemp(prefix='pympler')
        server.server = PymplerServer(host=host, port=port, **kwargs)
        bottle.debug(debug)
        bottle.run(server=server.server)
    finally:
        rmtree(server.tmpdir)

Response: Start the profiling server.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _winreg_getShellFolder( name ):
    k = _winreg.OpenKey(
        _winreg.HKEY_CURRENT_USER,
        r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
    )
    try:
        # should check that it's valid? How?
        return _winreg.QueryValueEx( k, name )[0]
    finally:
        _winreg.CloseKey( k )

Response: Get a shell folder by string name from the registry


Instruction:
How would you explain what the following Python 3 function does
def appdatadirectory(  ):
    if shell:
        # on Win32 and have Win32all extensions, best-case
        return shell_getShellFolder(shellcon.CSIDL_APPDATA)
    if _winreg:
        # on Win32, but no Win32 shell com available, this uses
        # a direct registry access, likely to fail on Win98/Me
        return _winreg_getShellFolder( 'AppData' )
    # okay, what if for some reason _winreg is missing? would we want to allow ctypes?
    ## default case, look for name in environ...
    for name in ['APPDATA', 'HOME']:
        if name in os.environ:
            return os.path.join( os.environ[name], '.config' )
    # well, someone's being naughty, see if we can get ~ to expand to a directory...
    possible = os.path.abspath(os.path.expanduser( '~/.config' ))
    if os.path.exists( possible ):
        return possible
    raise OSError( """Unable to determine user's application-data directory, no ${HOME} or ${APPDATA} in environment""" )

Response: Attempt to retrieve the current user s application - data directory


Instruction:
Write a Python 3 function that can
return a list of all known objects excluding frame objects.

Response:
def get_objects(remove_dups=True, include_frames=False):
    """Return a list of all known objects excluding frame objects.

    If (outer) frame objects shall be included, pass `include_frames=True`.  In
    order to prevent building reference cycles, the current frame object (of
    the caller of get_objects) is ignored. This will not prevent creating
    reference cycles if the object list is passed up the call-stack. Therefore,
    frame objects are not included by default.

    Keyword arguments:
    remove_dups -- if True, all duplicate objects will be removed.
    include_frames -- if True, includes frame objects.
    """
    gc.collect()

    # Do not initialize local variables before calling gc.get_objects or those
    # will be included in the list. Furthermore, ignore frame objects to
    # prevent reference cycles.
    tmp = gc.get_objects()
    tmp = [o for o in tmp if not isframe(o)]

    res = []
    for o in tmp:
        # gc.get_objects returns only container objects, but we also want
        # the objects referenced by them
        refs = get_referents(o)
        for ref in refs:
            if not _is_containerobject(ref):
                # we already got the container objects, now we only add
                # non-container objects
                res.append(ref)
    res.extend(tmp)
    if remove_dups:
        res = _remove_duplicates(res)

    if include_frames:
        for sf in stack()[2:]:
            res.append(sf[0])
    return res


Instruction:
Can you implement a function in Python 3 that
computes the total size of all elements in objects.

Response:
def get_size(objects):
    """Compute the total size of all elements in objects."""
    res = 0
    for o in objects:
        try:
            res += _getsizeof(o)
        except AttributeError:
            print("IGNORING: type=%s; o=%s" % (str(type(o)), str(o)))
    return res


Instruction:
Here you have a function in Python 3, explain what it does
def get_diff(left, right):
    res = {'+': [], '-': []}

    def partition(objects):
        """Partition the passed object list."""
        res = {}
        for o in objects:
            t = type(o)
            if type(o) not in res:
                res[t] = []
            res[t].append(o)
        return res

    def get_not_included(foo, bar):
        """Compare objects from foo with objects defined in the values of
        bar (set of partitions).
        Returns a list of all objects included in list, but not dict values.
        """
        res = []
        for o in foo:
            if not compat.object_in_list(type(o), bar):
                res.append(o)
            elif not compat.object_in_list(o, bar[type(o)]):
                res.append(o)
        return res

    # Create partitions of both lists. This will reduce the time required for
    # the comparison
    left_objects = partition(left)
    right_objects = partition(right)
    # and then do the diff
    res['+'] = get_not_included(right, left_objects)
    res['-'] = get_not_included(left, right_objects)
    return res

Response: Get the difference of two lists.


Instruction:
Write a Python 3 function for
filtering objects by type minimum size and maximum size.

Response:
def filter(objects, Type=None, min=-1, max=-1): #PYCHOK muppy filter
    """Filter objects.

    The filter can be by type, minimum size, and/or maximum size.

    Keyword arguments:
    Type -- object type to filter by
    min -- minimum object size
    max -- maximum object size

    """
    res = []
    if min > max:
        raise ValueError("minimum must be smaller than maximum")
    if Type is not None:
        res = [o for o in objects if isinstance(o, Type)]
    if min > -1:
        res = [o for o in res if _getsizeof(o) < min]
    if max > -1:
        res = [o for o in res if _getsizeof(o) > max]
    return res


Instruction:
Create a Python 3 function for
getting all referents of an object up to a certain level.

Response:
def get_referents(object, level=1):
    """Get all referents of an object up to a certain level.

    The referents will not be returned in a specific order and
    will not contain duplicate objects. Duplicate objects will be removed.

    Keyword arguments:
    level -- level of indirection to which referents considered.

    This function is recursive.

    """
    res = gc.get_referents(object)
    level -= 1
    if level > 0:
        for o in res:
            res.extend(get_referents(o, level))
    res = _remove_duplicates(res)
    return res


Instruction:
Explain what the following Python 3 code does
def _get_usage(function, *args):
    # The usage of a function is calculated by creating one summary of all
    # objects before the function is invoked and afterwards. These summaries
    # are compared and the diff is returned.
    # This function works in a 2-steps process. Before the actual function is
    # invoked an empty dummy function is measurement to identify the overhead
    # involved in the measuring process. This overhead then is subtracted from
    # the measurement performed on the passed function. The result reflects the
    # actual usage of a function call.
    # Also, a measurement is performed twice, allowing the adjustment to
    # initializing things, e.g. modules

    res = None

    def _get_summaries(function, *args):
        """Get a 2-tuple containing one summary from before, and one summary
        from after the function has been invoked.

        """
        s_before = summary.summarize(get_objects())
        function(*args)
        s_after = summary.summarize(get_objects())
        return (s_before, s_after)

    def _get_usage(function, *args):
        """Get the usage of a function call.
        This function is to be used only internally. The 'real' get_usage
        function is a wrapper around _get_usage, but the workload is done
        here.

        """
        res = []
        # init before calling
        (s_before, s_after) = _get_summaries(function, *args)
        # ignore all objects used for the measurement
        ignore = []
        if s_before != s_after:
            ignore.append(s_before)
        for row in s_before:
            # ignore refs from summary and frame (loop)
            if len(gc.get_referrers(row)) == 2:
                ignore.append(row)
            for item in row:
                # ignore refs from summary and frame (loop)
                if len(gc.get_referrers(item)) == 2:
                    ignore.append(item)
        for o in ignore:
            s_after = summary._subtract(s_after, o)
        res = summary.get_diff(s_before, s_after)
        return summary._sweep(res)

    # calibrate; twice for initialization
    def noop(): pass
    offset = _get_usage(noop)
    offset = _get_usage(noop)
    # perform operation twice to handle objects possibly used in
    # initialisation
    tmp = _get_usage(function, *args)
    tmp = _get_usage(function, *args)
    tmp = summary.get_diff(offset, tmp)
    tmp = summary._sweep(tmp)
    if len(tmp) != 0:
        res = tmp
    return res

Response: This function is used only internally. It is used by the real get_usage function.


Instruction:
How would you explain what the following Python 3 function does
def _remove_duplicates(objects):
    seen = {}
    result = []
    for item in objects:
        marker = id(item)
        if marker in seen:
            continue
        seen[marker] = 1
        result.append(item)
    return result

Response:
Remove duplicate objects.

    Inspired by http://www.peterbe.com/plog/uniqifiers-benchmark


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_optionals_without_choices(self, actions):
    boolean_actions = (
      _StoreConstAction, _StoreFalseAction,
      _StoreTrueAction
    )
    return [action
            for action in actions
            if action.option_strings
            and not action.choices
            and not isinstance(action, _CountAction)
            and not isinstance(action, _HelpAction)
            and type(action) not in boolean_actions]

Response: Returns a list of all actions which are not optional but without required choices.


Instruction:
Can you write a function in Python 3 where it
returns all instances of flag type options.

Response:
def get_flag_style_optionals(self, actions):
    """
    Gets all instances of "flag" type options.
    i.e. options which either store a const, or
    store boolean style options (e.g. StoreTrue).
    Types:
      _StoreTrueAction
      _StoreFalseAction
      _StoreConst
    """
    return [action
            for action in actions
            if isinstance(action, _StoreTrueAction)
            or isinstance(action, _StoreFalseAction)
            or isinstance(action, _StoreConstAction)]


Instruction:
Can you generate the documentation for the following Python 3 function
def resize_bitmap(parent, _bitmap, target_height):
  '''
  Resizes a bitmap to a height of 89 pixels (the
  size of the top panel), while keeping aspect ratio
  in tact
  '''
  image = wx.ImageFromBitmap(_bitmap)
  _width, _height = image.GetSize()
  if _height < target_height:
    return wx.StaticBitmap(parent, -1, wx.BitmapFromImage(image))
  ratio = float(_width) / _height
  image = image.Scale(target_height * ratio, target_height, wx.IMAGE_QUALITY_HIGH)
  return wx.StaticBitmap(parent, -1, wx.BitmapFromImage(image))

Response:
Resizes a bitmap to a height of 89 pixels while keeping aspect ratio
 in tact


Instruction:
How would you explain what the following Python 3 function does
def ilevenshtein(seq1, seqs, max_dist=-1):
	for seq2 in seqs:
		dist = levenshtein(seq1, seq2, max_dist=max_dist)
		if dist != -1:
			yield dist, seq2

Response:
Compute the Levenshtein distance between the sequence seq1 and the series
	sequences seqs.


Instruction:
Implement a function in Python 3 to
return an iterator over all the sequences in seqs which distance from the seq1 is lower or equal to 2.

Response:
def ifast_comp(seq1, seqs, transpositions=False):
	"""Return an iterator over all the sequences in `seqs` which distance from
	`seq1` is lower or equal to 2. The sequences which distance from the
	reference sequence is higher than that are dropped.

		`seq1`: the reference sequence.
		`seqs`: a series of sequences (can be a generator)
		`transpositions` has the same sense than in `fast_comp`.

	The return value is a series of pairs (distance, sequence).

	You might want to call `sorted()` on the iterator to get the results in a
	significant order:

		>>> g = ifast_comp("foo", ["fo", "bar", "foob", "foo", "foobaz"])
		>>> sorted(g)
		[(0, 'foo'), (1, 'fo'), (1, 'foob')]
	"""
	for seq2 in seqs:
		dist = fast_comp(seq1, seq2, transpositions)
		if dist != -1:
			yield dist, seq2


Instruction:
Can you generate the documentation for the following Python 3 function
def summarize(objects):
    count = {}
    total_size = {}
    for o in objects:
        otype = _repr(o)
        if otype in count:
            count[otype] += 1
            total_size[otype] += _getsizeof(o)
        else:
            count[otype] = 1
            total_size[otype] = _getsizeof(o)
    rows = []
    for otype in count:
        rows.append([otype, count[otype], total_size[otype]])
    return rows

Response: Summarize an objects list.


Instruction:
Write a Python 3 script for
getting the difference of two summaries.

Response:
def get_diff(left, right):
    """Get the difference of two summaries.

    Subtracts the values of the right summary from the values of the left
    summary.
    If similar rows appear on both sides, the are included in the summary with
    0 for number of elements and total size.
    If the number of elements of a row of the diff is 0, but the total size is
    not, it means that objects likely have changed, but not there number, thus
    resulting in a changed size.

    """
    res = []
    for row_r in right:
        found = False
        for row_l in left:
            if row_r[0] == row_l[0]:
                res.append([row_r[0], row_r[1] - row_l[1], row_r[2] - row_l[2]])
                found = True
        if not found:
            res.append(row_r)

    for row_l in left:
        found = False
        for row_r in right:
            if row_l[0] == row_r[0]:
                found = True
        if not found:
            res.append([row_l[0], -row_l[1], -row_l[2]])
    return res


Instruction:
Here you have a function in Python 3, explain what it does
def print_(rows, limit=15, sort='size', order='descending'):
    localrows = []
    for row in rows:
        localrows.append(list(row))
    # input validation
    sortby = ['type', '#', 'size']
    if sort not in sortby:
        raise ValueError("invalid sort, should be one of" + str(sortby))
    orders = ['ascending', 'descending']
    if order not in orders:
        raise ValueError("invalid order, should be one of" + str(orders))
    # sort rows
    if sortby.index(sort) == 0:
        if order == "ascending":
            localrows.sort(key=lambda x: _repr(x[0]))
        elif order == "descending":
            localrows.sort(key=lambda x: _repr(x[0]), reverse=True)
    else:
        if order == "ascending":
            localrows.sort(key=lambda x: x[sortby.index(sort)])
        elif order == "descending":
            localrows.sort(key=lambda x: x[sortby.index(sort)], reverse=True)
    # limit rows
    localrows = localrows[0:limit]
    for row in localrows:
        row[2] = stringutils.pp(row[2])
    # print rows
    localrows.insert(0,["types", "# objects", "total size"])
    _print_table(localrows)

Response: Print the rows as a summary.


Instruction:
Create a Python 3 function to
print a list of lists as a pretty table.

Response:
def _print_table(rows, header=True):
    """Print a list of lists as a pretty table.

    Keyword arguments:
    header -- if True the first row is treated as a table header

    inspired by http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/267662
    """
    border = "="
    # vertical delimiter
    vdelim = " | "
    # padding nr. of spaces are left around the longest element in the
    # column
    padding = 1
    # may be left,center,right
    justify = 'right'
    justify = {'left'   : str.ljust,
               'center' : str.center,
               'right'  : str.rjust}[justify.lower()]
    # calculate column widths (longest item in each col
    # plus "padding" nr of spaces on both sides)
    cols = zip(*rows)
    colWidths = [max([len(str(item))+2*padding for item in col]) for col in cols]
    borderline = vdelim.join([w*border for w in colWidths])
    for row in rows:
        print(vdelim.join([justify(str(item),width) for (item,width) in zip(row,colWidths)]))
        if header:
            print(borderline)
            header=False


Instruction:
Write a Python 3 function that can
return the string representation of the object o.

Response:
def _repr(o, verbosity=1):
    """Get meaning object representation.

    This function should be used when the simple str(o) output would result in
    too general data. E.g. "<type 'instance'" is less meaningful than
    "instance: Foo".

    Keyword arguments:
    verbosity -- if True the first row is treated as a table header

    """
    res = ""

    t = type(o)
    if (verbosity == 0) or (t not in representations):
        res = str(t)
    else:
        verbosity -= 1
        if len(representations[t]) < verbosity:
            verbosity = len(representations[t]) - 1
        res = representations[t][verbosity](o)

    res = address.sub('', res)
    res = type_prefix.sub('', res)
    res = type_suffix.sub('', res)

    return res


Instruction:
How would you code a function in Python 3 to
traverse all objects of a summary and call function with each as a parameter.

Response:
def _traverse(summary, function, *args):
    """Traverse all objects of a summary and call function with each as a
    parameter.

    Using this function, the following objects will be traversed:
    - the summary
    - each row
    - each item of a row
    """
    function(summary, *args)
    for row in summary:
        function(row, *args)
        for item in row:
            function(item, *args)


Instruction:
Write a Python 3 script to
remove object o from the summary by subtracting it s size.

Response:
def _subtract(summary, o):
    """Remove object o from the summary by subtracting it's size."""
    found = False
    row = [_repr(o), 1, _getsizeof(o)]
    for r in summary:
        if r[0] == row[0]:
            (r[1], r[2]) = (r[1] - row[1], r[2] - row[2])
            found = True
    if not found:
        summary.append([row[0], -row[1], -row[2]])
    return summary


Instruction:
Implement a Python 3 function for
checking if the file is okay returns False if the file is not okay None if the file is corrupt

Response:
def check_structure(filepath):
    """Returns False if the file is okay, None if file format is unsupported by PIL/PILLOW, or returns an error string if the file is corrupt."""
    #http://stackoverflow.com/questions/1401527/how-do-i-programmatically-check-whether-an-image-png-jpeg-or-gif-is-corrupted/1401565#1401565

    # Check structure only for images (not supported for other types currently)
    if filepath.lower().endswith(tuple(img_filter)):
        try:
            #try:
            im = PIL.Image.open(filepath)
            #except IOError: # File format not supported by PIL, we skip the check_structure - ARG this is also raised if a supported image file is corrupted...
                #print("File: %s: DETECTNOPE" % filepath)
                #return None
            im.verify()
        # If an error occurred, the structure is corrupted
        except Exception as e:
            return str(e)
        # Else no exception, there's no corruption
        return False
    # Else the format does not currently support structure checking, we just return None to signal we didin't check
    else:
        return None


Instruction:
How would you implement a function in Python 3 that
generates several hashes at a single sweep of the file.

Response:
def generate_hashes(filepath, blocksize=65536):
    '''Generate several hashes (md5 and sha1) in a single sweep of the file. Using two hashes lowers the probability of collision and false negative (file modified but the hash is the same). Supports big files by streaming blocks by blocks to the hasher automatically. Blocksize can be any multiple of 128.'''
    # Init hashers
    hasher_md5 = hashlib.md5()
    hasher_sha1 = hashlib.sha1()
    # Read the file blocks by blocks
    with open(filepath, 'rb') as afile:
        buf = afile.read(blocksize)
        while len(buf) > 0:
            # Compute both hashes at the same time
            hasher_md5.update(buf)
            hasher_sha1.update(buf)
            # Load the next data block from file
            buf = afile.read(blocksize)
    return (hasher_md5.hexdigest(), hasher_sha1.hexdigest())


Instruction:
Write a Python 3 function that can
return the degree of the polynomial

Response:
def get_degree(self, poly=None):
        '''Returns the degree of the polynomial'''
        if not poly:
            return self.degree
            #return len(self.coefficients) - 1
        elif poly and hasattr("coefficients", poly):
            return len(poly.coefficients) - 1
        else:
            while poly and poly[-1] == 0:
                poly.pop()   # normalize
            return len(poly)-1


Instruction:
How would you code a function in Python 3 to
compute the multiplication between two polynomials only at the specified coefficient

Response:
def mul_at(self, other, k):
        '''Compute the multiplication between two polynomials only at the specified coefficient (this is a lot cheaper than doing the full polynomial multiplication and then extract only the required coefficient)'''
        if k > (self.degree + other.degree) or k > self.degree: return 0 # optimization: if the required coefficient is above the maximum coefficient of the resulting polynomial, we can already predict that and just return 0

        term = 0

        for i in _range(min(len(self), len(other))):
            coef1 = self.coefficients[-(k-i+1)]
            coef2 = other.coefficients[-(i+1)]
            if coef1 == 0 or coef2 == 0: continue # log(0) is undefined, skip (and in addition it's a nice optimization)
            term += coef1 * coef2
        return term


Instruction:
Can you implement a function in Python 3 that
multiplies a polynomial with a scalar

Response:
def scale(self, scalar):
        '''Multiply a polynomial with a scalar'''
        return self.__class__([self.coefficients[i] * scalar for i in _range(len(self))])


Instruction:
Create a Python 3 function for
fasting division by using Extended Synthetic Division

Response:
def _fastdivmod(dividend, divisor):
        '''Fast polynomial division by using Extended Synthetic Division (aka Horner's method). Also works with non-monic polynomials.
        A nearly exact same code is explained greatly here: http://research.swtch.com/field and you can also check the Wikipedia article and the Khan Academy video.'''
        # Note: for RS encoding, you should supply divisor = mprime (not m, you need the padded message)
        msg_out = list(dividend) # Copy the dividend
        normalizer = divisor[0] # precomputing for performance
        for i in _range(len(dividend)-(len(divisor)-1)):
            msg_out[i] /= normalizer # for general polynomial division (when polynomials are non-monic), the usual way of using synthetic division is to divide the divisor g(x) with its leading coefficient (call it a). In this implementation, this means:we need to compute: coef = msg_out[i] / gen[0]. For more infos, see http://en.wikipedia.org/wiki/Synthetic_division
            coef = msg_out[i] # precaching
            if coef != 0: # log(0) is undefined, so we need to avoid that case explicitly (and it's also a good optimization)
                for j in _range(1, len(divisor)): # in synthetic division, we always skip the first coefficient of the divisior, because it's only used to normalize the dividend coefficient
                    if divisor[j] != 0: # log(0) is undefined so we need to avoid that case
                        msg_out[i + j] += -divisor[j] * coef

        # The resulting msg_out contains both the quotient and the remainder, the remainder being the size of the divisor (the remainder has necessarily the same degree as the divisor -- not length but degree == length-1 -- since it's what we couldn't divide from the dividend), so we compute the index where this separation is, and return the quotient and remainder.
        separator = -(len(divisor)-1)
        return Polynomial(msg_out[:separator]), Polynomial(msg_out[separator:])


Instruction:
Write a Python 3 function that can
fast polynomial division by using Extended Synthetic Division and optimized for GF2int computations.

Response:
def _gffastdivmod(dividend, divisor):
        '''Fast polynomial division by using Extended Synthetic Division and optimized for GF(2^p) computations (so it is not generic, must be used with GF2int).
        Transposed from the reedsolomon library: https://github.com/tomerfiliba/reedsolomon
        BEWARE: it works only for monic divisor polynomial! (which is always the case with Reed-Solomon's generator polynomials)'''

        msg_out = list(dividend) # Copy the dividend list and pad with 0 where the ecc bytes will be computed
        for i in _range(len(dividend)-(len(divisor)-1)):
            coef = msg_out[i] # precaching
            if coef != 0: # log(0) is undefined, so we need to avoid that case explicitly (and it's also a good optimization)
                for j in _range(1, len(divisor)): # in synthetic division, we always skip the first coefficient of the divisior, because it's only used to normalize the dividend coefficient (which is here useless since the divisor, the generator polynomial, is always monic)
                    #if divisor[j] != 0: # log(0) is undefined so we need to check that, but it slow things down in fact and it's useless in our case (reed-solomon encoding) since we know that all coefficients in the generator are not 0
                    msg_out[i + j] ^= divisor[j] * coef # equivalent to the more mathematically correct (but xoring directly is faster): msg_out[i + j] += -divisor[j] * coef
                    # Note: we could speed things up a bit if we could inline the table lookups, but the Polynomial class is generic, it doesn't know anything about the underlying fields and their operators. Good OOP design, bad for performances in Python because of function calls and the optimizations we can't do (such as precomputing gf_exp[divisor]). That's what is done in reedsolo lib, this is one of the reasons it is faster.

        # The resulting msg_out contains both the quotient and the remainder, the remainder being the size of the divisor (the remainder has necessarily the same degree as the divisor -- not length but degree == length-1 -- since it's what we couldn't divide from the dividend), so we compute the index where this separation is, and return the quotient and remainder.
        separator = -(len(divisor)-1)
        return Polynomial(msg_out[:separator]), Polynomial(msg_out[separator:])


Instruction:
Can you generate a brief explanation for the following Python 3 code
def evaluate(self, x):
        '''Evaluate this polynomial at value x, returning the result (which is the sum of all evaluations at each term).'''
        # Holds the sum over each term in the polynomial
        #c = 0

        # Holds the current power of x. This is multiplied by x after each term
        # in the polynomial is added up. Initialized to x^0 = 1
        #p = 1

        #for term in self.coefficients[::-1]:
        #    c = c + term * p
        #    p = p * x
        #return c

        # Faster alternative using Horner's Scheme
        y = self[0]
        for i in _range(1, len(self)):
            y = y * x + self.coefficients[i]
        return y

Response: Evaluate this polynomial at value x returning the result.


Instruction:
Write a Python 3 script to
compute the formal derivative of the polynomial

Response:
def derive(self):
        '''Compute the formal derivative of the polynomial: sum(i*coeff[i] x^(i-1))'''
        #res = [0] * (len(self)-1) # pre-allocate the list, it will be one item shorter because the constant coefficient (x^0) will be removed
        #for i in _range(2, len(self)+1): # start at 2 to skip the first coeff which is useless since it's a constant (x^0) so we +1, and because we work in reverse (lower coefficients are on the right) so +1 again
            #res[-(i-1)] = (i-1) * self[-i] # self[-i] == coeff[i] and i-1 is the x exponent (eg: x^1, x^2, x^3, etc.)
        #return Polynomial(res)

        # One liner way to do it (also a bit faster too)
        #return Polynomial( [(i-1) * self[-i] for i in _range(2, len(self)+1)][::-1] )
        # Another faster version
        L = len(self)-1
        return Polynomial( [(L-i) * self[i] for i in _range(0, len(self)-1)] )


Instruction:
Write a Python 3 function for
generalizing feature scaling (useful for variable error correction rate calculation)

Response:
def feature_scaling(x, xmin, xmax, a=0, b=1):
    '''Generalized feature scaling (useful for variable error correction rate calculation)'''
    return a + float(x - xmin) * (b - a) / (xmax - xmin)


Instruction:
Can you generate the documentation for the following Python 3 function
def entry_fields(file, entry_pos, field_delim="\xFF"):
    '''From an ecc entry position (a list with starting and ending positions), extract the metadata fields (filename, filesize, ecc for both), and the starting/ending positions of the ecc stream (containing variably encoded blocks of hash and ecc per blocks of the original file's header)'''
    # Read the the beginning of the ecc entry
    blocksize = 65535
    file.seek(entry_pos[0])
    entry = file.read(blocksize)
    entry = entry.lstrip(field_delim) # if there was some slight adjustment error (example: the last ecc block of the last file was the field_delim, then we will start with a field_delim, and thus we need to remove the trailing field_delim which is useless and will make the field detection buggy). This is not really a big problem for the previous file's ecc block: the missing ecc characters (which were mistaken for a field_delim), will just be missing (so we will lose a bit of resiliency for the last block of the previous file, but that's not a huge issue, the correction can still rely on the other characters).
    # TODO: do in a while loop in case the filename is really big (bigger than blocksize) - or in case we add intra-ecc for filename

    # Find metadata fields delimiters positions
    # TODO: automate this part, just give in argument the number of field_delim to find, and the func will find the x field_delims (the number needs to be specified in argument because the field_delim can maybe be found wrongly inside the ecc stream, which we don't want)
    first = entry.find(field_delim)
    second = entry.find(field_delim, first+len(field_delim))
    third = entry.find(field_delim, second+len(field_delim))
    fourth = entry.find(field_delim, third+len(field_delim))
    # Note: we do not try to find all the field delimiters because we optimize here: we just walk the string to find the exact number of field_delim we are looking for, and after we stop, no need to walk through the whole string.

    # Extract the content of the fields
    # Metadata fields
    relfilepath = entry[:first]
    filesize = entry[first+len(field_delim):second]
    relfilepath_ecc = entry[second+len(field_delim):third]
    filesize_ecc = entry[third+len(field_delim):fourth]
    # Ecc stream field (aka ecc blocks)
    ecc_field_pos = [entry_pos[0]+fourth+len(field_delim), entry_pos[1]] # return the starting and ending position of the rest of the ecc track, which contains blocks of hash/ecc of the original file's content.

    # Place the cursor at the beginning of the ecc_field
    file.seek(ecc_field_pos[0])

    # Try to convert to an int, an error may happen
    try:
        filesize = int(filesize)
    except Exception, e:
        print("Exception when trying to detect the filesize in ecc field (it may be corrupted), skipping: ")
        print(e)
        #filesize = 0 # avoid setting to 0, we keep as an int so that we can try to fix using intra-ecc

    # entries = [ {"message":, "ecc":, "hash":}, etc.]
    return {"relfilepath": relfilepath, "relfilepath_ecc": relfilepath_ecc, "filesize": filesize, "filesize_ecc": filesize_ecc, "ecc_field_pos": ecc_field_pos}

Response: Extract the metadata fields from an ecc entry position.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def stream_entry_assemble(hasher, file, eccfile, entry_fields, max_block_size, header_size, resilience_rates, constantmode=False):
    '''From an entry with its parameters (filename, filesize), assemble a list of each block from the original file along with the relative hash and ecc for easy processing later.'''
    # Cut the header and the ecc entry into blocks, and then assemble them so that we can easily process block by block
    eccfile.seek(entry_fields["ecc_field_pos"][0])
    curpos = file.tell()
    ecc_curpos = eccfile.tell()
    while (ecc_curpos < entry_fields["ecc_field_pos"][1]): # continue reading the input file until we reach the position of the previously detected ending marker
        # Compute the current rate, depending on where we are inside the input file (headers? later stage?)
        if curpos < header_size or constantmode: # header stage: constant rate
            rate = resilience_rates[0]
        else: # later stage 2 or 3: progressive rate
            rate = feature_scaling(curpos, header_size, entry_fields["filesize"], resilience_rates[1], resilience_rates[2]) # find the rate for the current stream of data (interpolate between stage 2 and stage 3 rates depending on the cursor position in the file)
        # From the rate, compute the ecc parameters
        ecc_params = compute_ecc_params(max_block_size, rate, hasher)
        # Extract the message block from input file, given the computed ecc parameters
        mes = file.read(ecc_params["message_size"])
        if len(mes) == 0: return # quit if message is empty (reached end-of-file), this is a safeguard if ecc pos ending was miscalculated (we thus only need the starting position to be correct)
        buf = eccfile.read(ecc_params["hash_size"]+ecc_params["ecc_size"])
        hash = buf[:ecc_params["hash_size"]]
        ecc = buf[ecc_params["hash_size"]:]

        yield {"message": mes, "hash": hash, "ecc": ecc, "rate": rate, "ecc_params": ecc_params, "curpos": curpos, "ecc_curpos": ecc_curpos}
        # Prepare for the next iteration of the loop
        curpos = file.tell()
        ecc_curpos = eccfile.tell()
    # Just a quick safe guard against ecc ending marker misdetection
    file.seek(0, os.SEEK_END) # alternative way of finding the total size: go to the end of the file
    size = file.tell()
    if curpos < size: print("WARNING: end of ecc track reached but not the end of file! Either the ecc ending marker was misdetected, or either the file hash changed! Some blocks maybe may not have been properly checked!")

Response: Assemble a list of each entry from the original file along with the relative hash and ecc for easy processing later.


Instruction:
Can you write a function in Python 3 where it
generates a stream of hash and ecc blocks of variable encoding rate and size given a file.

Response:
def stream_compute_ecc_hash(ecc_manager, hasher, file, max_block_size, header_size, resilience_rates):
    '''Generate a stream of hash/ecc blocks, of variable encoding rate and size, given a file.'''
    curpos = file.tell() # init the reading cursor at the beginning of the file
    # Find the total size to know when to stop
    #size = os.fstat(file.fileno()).st_size # old way of doing it, doesn't work with StringIO objects
    file.seek(0, os.SEEK_END) # alternative way of finding the total size: go to the end of the file
    size = file.tell()
    file.seek(0, curpos) # place the reading cursor back at the beginning of the file
    # Main encoding loop
    while curpos < size: # Continue encoding while we do not reach the end of the file
        # Calculating the encoding rate
        if curpos < header_size: # if we are still reading the file's header, we use a constant rate
            rate = resilience_rates[0]
        else: # else we use a progressive rate for the rest of the file the we calculate on-the-fly depending on our current reading cursor position in the file
            rate = feature_scaling(curpos, header_size, size, resilience_rates[1], resilience_rates[2]) # find the rate for the current stream of data (interpolate between stage 2 and stage 3 rates depending on the cursor position in the file)

        # Compute the ecc parameters given the calculated rate
        ecc_params = compute_ecc_params(max_block_size, rate, hasher)
        #ecc_manager = ECCMan(max_block_size, ecc_params["message_size"]) # not necessary to create an ecc manager anymore, as it is very costly. Now we can specify a value for k on the fly (tables for all possible values of k are pre-generated in the reed-solomon libraries)

        # Compute the ecc and hash for the current message block
        mes = file.read(ecc_params["message_size"])
        hash = hasher.hash(mes)
        ecc = ecc_manager.encode(mes, k=ecc_params["message_size"])
        #print("mes %i (%i) - ecc %i (%i) - hash %i (%i)" % (len(mes), message_size, len(ecc), ecc_params["ecc_size"], len(hash), ecc_params["hash_size"])) # DEBUGLINE

        # Return the result
        yield [hash, ecc, ecc_params]
        # Prepare for next iteration
        curpos = file.tell()


Instruction:
How would you explain what the following Python 3 function does
def compute_ecc_hash_from_string(string, ecc_manager, hasher, max_block_size, resilience_rate):
    '''Generate a concatenated string of ecc stream of hash/ecc blocks, of constant encoding rate, given a string.
    NOTE: resilience_rate here is constant, you need to supply only one rate, between 0.0 and 1.0. The encoding rate will then be constant, like in header_ecc.py.'''
    fpfile = StringIO(string)
    ecc_stream = ''.join( [str(x[1]) for x in stream_compute_ecc_hash(ecc_manager, hasher, fpfile, max_block_size, len(string), [resilience_rate])] ) # "hack" the function by tricking it to always use a constant rate, by setting the header_size=len(relfilepath), and supplying the resilience_rate_intra instead of resilience_rate_s1 (the one for header)
    return ecc_stream

Response: Generate a concatenated string of ecc stream of hash blocks of constant encoding rate given a string.


Instruction:
How would you explain what the following Python 3 function does
def ecc_correct_intra_stream(ecc_manager_intra, ecc_params_intra, hasher_intra, resilience_rate_intra, field, ecc, enable_erasures=False, erasures_char="\x00", only_erasures=False, max_block_size=65535):
    # convert strings to StringIO object so that we can trick our ecc reading functions that normally works only on files
    fpfile = StringIO(field)
    fpfile_ecc = StringIO(ecc)
    fpentry_p = {"ecc_field_pos": [0, len(field)]} # create a fake entry_pos so that the ecc reading function works correctly
    # Prepare variables
    field_correct = [] # will store each block of the corrected (or already correct) filepath
    fcorrupted = False # check if field was corrupted
    fcorrected = True # check if field was corrected (if it was corrupted)
    errmsg = ''
    # Decode each block of the filepath
    for e in stream_entry_assemble(hasher_intra, fpfile, fpfile_ecc, fpentry_p, max_block_size, len(field), [resilience_rate_intra], constantmode=True):
        # Check if this block of the filepath is OK, if yes then we just copy it over
        if ecc_manager_intra.check(e["message"], e["ecc"]):
            field_correct.append(e["message"])
        else: # Else this block is corrupted, we will try to fix it using the ecc
            fcorrupted = True
            # Repair the message block and the ecc
            try:
                repaired_block, repaired_ecc = ecc_manager_intra.decode(e["message"], e["ecc"], enable_erasures=enable_erasures, erasures_char=erasures_char, only_erasures=only_erasures)
            except (ReedSolomonError, RSCodecError), exc: # the reedsolo lib may raise an exception when it can't decode. We ensure that we can still continue to decode the rest of the file, and the other files.
                repaired_block = None
                repaired_ecc = None
                errmsg += "- Error: metadata field at offset %i: %s\n" % (entry_pos[0], exc)
            # Check if the block was successfully repaired: if yes then we copy the repaired block...
            if repaired_block is not None and ecc_manager_intra.check(repaired_block, repaired_ecc):
                field_correct.append(repaired_block)
            else: # ... else it failed, then we copy the original corrupted block and report an error later
                field_correct.append(e["message"])
                fcorrected = False
    # Join all the blocks into one string to build the final filepath
    if isinstance(field_correct[0], bytearray): field_correct = [str(x) for x in field_correct] # workaround when using --ecc_algo 3 or 4, because we get a list of bytearrays instead of str
    field = ''.join(field_correct)
    # Report errors
    return (field, fcorrupted, fcorrected, errmsg)

Response: Correct an intra - field with its corresponding intra - ecc if necessary


Instruction:
Can you create a Python 3 function that
returns the memory usage of a process or piece of code.

Response:
def memory_usage(proc=-1, interval=.1, timeout=None, run_in_place=False):
    """
    Return the memory usage of a process or piece of code

    Parameters
    ----------
    proc : {int, string, tuple}, optional
        The process to monitor. Can be given by a PID, by a string
        containing a filename or by a tuple. The tuple should contain
        three values (f, args, kw) specifies to run the function
        f(*args, **kw).  Set to -1 (default) for current process.

    interval : float, optional

    timeout : float, optional

    run_in_place : boolean, optional. False by default
        If False fork the process and retrieve timings from a different
        process. You shouldn't need to change this unless you are affected
        by this (http://blog.vene.ro/2012/07/04/on-why-my-memit-fails-on-osx)
        bug.

    Returns
    -------
    mm : list of integers, size less than num
        memory usage, in KB
    """
    ret = []

    if timeout is not None:
        max_iter = timeout / interval
    elif isinstance(proc, int):
        # external process and no timeout
        max_iter = 1
    else:
        # for a Python function wait until it finishes
        max_iter = float('inf')

    if str(proc).endswith('.py'):
        filename = _find_script(proc)
        with open(filename) as f:
            proc = f.read()
        raise NotImplementedError

    if isinstance(proc, (list, tuple)):

        if len(proc) == 1:
            f, args, kw = (proc[0], (), {})
        elif len(proc) == 2:
            f, args, kw = (proc[0], proc[1], {})
        elif len(proc) == 3:
            f, args, kw = (proc[0], proc[1], proc[2])
        else:
            raise ValueError
        try:
            import multiprocessing
        except ImportError:
            print ('WARNING: cannot import module `multiprocessing`. Forcing to'
                   ' run inplace.')
            # force inplace
            run_in_place = True
        if run_in_place:
            import threading
            main_thread = threading.Thread(target=f, args=args, kwargs=kw)
        else:
            main_thread = multiprocessing.Process(target=f, args=args, kwargs=kw)
        i = 0
        main_thread.start()
        pid = getattr(main_thread, 'pid', os.getpid())
        while i < max_iter and main_thread.is_alive():
            m = _get_memory(pid)
            ret.append(m)
            time.sleep(interval)
            i += 1
        main_thread.join()
    else:
        # external process
        if proc == -1:
            proc = os.getpid()
        if max_iter == -1:
            max_iter = 1
        for _ in range(max_iter):
            ret.append(_get_memory(proc))
            time.sleep(interval)
    return ret


Instruction:
Can you create a Python 3 function that
finds the script in the PATH.

Response:
def _find_script(script_name):
    """ Find the script.

    If the input is not a file, then $PATH will be searched.
    """
    if os.path.isfile(script_name):
        return script_name
    path = os.getenv('PATH', os.defpath).split(os.pathsep)
    for dir in path:
        if dir == '':
            continue
        fn = os.path.join(dir, script_name)
        if os.path.isfile(fn):
            return fn

    print >> sys.stderr, 'Could not find script {0}'.format(script_name)
    raise SystemExit(1)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def magic_memit(self, line=''):
    opts, stmt = self.parse_options(line, 'r:t:i', posix=False, strict=False)
    repeat = int(getattr(opts, 'r', 1))
    if repeat < 1:
        repeat == 1
    timeout = int(getattr(opts, 't', 0))
    if timeout <= 0:
        timeout = None
    run_in_place = hasattr(opts, 'i')

    mem_usage = memory_usage((_func_exec, (stmt, self.shell.user_ns)), timeout=timeout,
        run_in_place=run_in_place)

    if mem_usage:
        print('maximum of %d: %f MB per loop' % (repeat, max(mem_usage)))
    else:
        print('ERROR: could not read memory usage, try with a lower interval or more iterations')

Response: Measure memory usage of a Python statement.


Instruction:
Write a Python 3 function for
adding a Python function to the line profiling information.

Response:
def add_function(self, func):
        """ Record line profiling information for the given Python function.
        """
        try:
            # func_code does not exist in Python3
            code = func.__code__
        except AttributeError:
            import warnings
            warnings.warn("Could not extract a code object for the object %r"
                          % (func,))
            return
        if code not in self.code_map:
            self.code_map[code] = {}
            self.functions.append(func)


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

        def f(*args, **kwds):
            self.enable_by_count()
            try:
                result = func(*args, **kwds)
            finally:
                self.disable_by_count()
            return result
        return f

Response: Wrap a function to profile it.


Instruction:
Explain what the following Python 3 code does
def runctx(self, cmd, globals, locals):
        self.enable_by_count()
        try:
            exec(cmd, globals, locals)
        finally:
            self.disable_by_count()
        return self

Response: Profile a single executable statement in the given namespaces.


Instruction:
Can you create a Python 3 function that
profiles a single function call.

Response:
def runcall(self, func, *args, **kw):
        """ Profile a single function call.
        """
        # XXX where is this used ? can be removed ?
        self.enable_by_count()
        try:
            return func(*args, **kw)
        finally:
            self.disable_by_count()


Instruction:
Write a Python 3 function that can
disable the profiler if the number of disable requests matches the number of enable requests.

Response:
def disable_by_count(self):
        """ Disable the profiler if the number of disable requests matches the
        number of enable requests.
        """
        if self.enable_count > 0:
            self.enable_count -= 1
            if self.enable_count == 0:
                self.disable()


Instruction:
Can you write a function in Python 3 where it
parses a makefile to find commands and substitute variables. Expects a makefile with only aliases and a line return between each command and each alias. Returns a dict with a list of commands for each alias.

Response:
def parse_makefile_aliases(filepath):
    '''
    Parse a makefile to find commands and substitute variables. Expects a
    makefile with only aliases and a line return between each command.

    Returns a dict, with a list of commands for each alias.
    '''

    # -- Parsing the Makefile using ConfigParser
    # Adding a fake section to make the Makefile a valid Ini file
    ini_str = '[root]\n'
    with open(filepath, 'r') as fd:
        ini_str = ini_str + fd.read().replace('@make ', '')
    ini_fp = StringIO.StringIO(ini_str)
    # Parse using ConfigParser
    config = ConfigParser.RawConfigParser()
    config.readfp(ini_fp)
    # Fetch the list of aliases
    aliases = config.options('root')

    # -- Extracting commands for each alias
    commands = {}
    for alias in aliases:
        # strip the first line return, and then split by any line return
        commands[alias] = config.get('root', alias).lstrip('\n').split('\n')

    # -- Commands substitution
    # Loop until all aliases are substituted by their commands:
    # Check each command of each alias, and if there is one command that is to
    # be substituted by an alias, try to do it right away. If this is not
    # possible because this alias itself points to other aliases , then stop
    # and put the current alias back in the queue to be processed again later.

    # Create the queue of aliases to process
    aliases_todo = commands.keys()
    # Create the dict that will hold the full commands
    commands_new = {}
    # Loop until we have processed all aliases
    while aliases_todo:
        # Pick the first alias in the queue
        alias = aliases_todo.pop(0)
        # Create a new entry in the resulting dict
        commands_new[alias] = []
        # For each command of this alias
        for cmd in commands[alias]:
            # Ignore self-referencing (alias points to itself)
            if cmd == alias:
                pass
            # Substitute full command
            elif cmd in aliases and cmd in commands_new:
                # Append all the commands referenced by the alias
                commands_new[alias].extend(commands_new[cmd])
            # Delay substituting another alias, waiting for the other alias to
            # be substituted first
            elif cmd in aliases and cmd not in commands_new:
                # Delete the current entry to avoid other aliases
                # to reference this one wrongly (as it is empty)
                del commands_new[alias]
                aliases_todo.append(alias)
                break
            # Full command (no aliases)
            else:
                commands_new[alias].append(cmd)
    commands = commands_new
    del commands_new

    # -- Prepending prefix to avoid conflicts with standard setup.py commands
    # for alias in commands.keys():
    #     commands['make_'+alias] = commands[alias]
    #     del commands[alias]

    return commands


Instruction:
Can you implement a function in Python 3 that
hacks run function which installs the trace.

Response:
def __run(self):
    """Hacked run function, which installs the trace."""
    sys.settrace(self.globaltrace)
    self.__run_backup()
    self.run = self.__run_backup