Instruction:
Here you have a function in Python 3, explain what it does
def splitPolygon(self, polygon, coplanarFront, coplanarBack, front, back):
        COPLANAR = 0 # all the vertices are within EPSILON distance from plane
        FRONT = 1 # all the vertices are in front of the plane
        BACK = 2 # all the vertices are at the back of the plane
        SPANNING = 3 # some vertices are in front, some in the back

        # Classify each point as well as the entire polygon into one of the above
        # four classes.
        polygonType = 0
        vertexLocs = []

        numVertices = len(polygon.vertices)
        for i in range(numVertices):
            t = self.normal.dot(polygon.vertices[i].pos) - self.w
            loc = -1
            if t < -Plane.EPSILON:
                loc = BACK
            elif t > Plane.EPSILON:
                loc = FRONT
            else:
                loc = COPLANAR
            polygonType |= loc
            vertexLocs.append(loc)

        # Put the polygon in the correct list, splitting it when necessary.
        if polygonType == COPLANAR:
            normalDotPlaneNormal = self.normal.dot(polygon.plane.normal)
            if normalDotPlaneNormal > 0:
                coplanarFront.append(polygon)
            else:
                coplanarBack.append(polygon)
        elif polygonType == FRONT:
            front.append(polygon)
        elif polygonType == BACK:
            back.append(polygon)
        elif polygonType == SPANNING:
            f = []
            b = []
            for i in range(numVertices):
                j = (i+1) % numVertices
                ti = vertexLocs[i]
                tj = vertexLocs[j]
                vi = polygon.vertices[i]
                vj = polygon.vertices[j]
                if ti != BACK:
                    f.append(vi)
                if ti != FRONT:
                    if ti != BACK:
                        b.append(vi.clone())
                    else:
                        b.append(vi)
                if (ti | tj) == SPANNING:
                    # interpolation weight at the intersection point
                    t = (self.w - self.normal.dot(vi.pos)) / self.normal.dot(vj.pos.minus(vi.pos))
                    # intersection point on the plane
                    v = vi.interpolate(vj, t)
                    f.append(v)
                    b.append(v.clone())
            if len(f) >= 3:
                front.append(Polygon(f, polygon.shared))
            if len(b) >= 3:
                back.append(Polygon(b, polygon.shared))

Response: Splits a polygon into two lists of coplanar polygons.


Instruction:
Write a Python 3 script to
invert the solid space to empty space and empty space to solid space.

Response:
def invert(self):
        """
        Convert solid space to empty space and empty space to solid space.
        """
        for poly in self.polygons:
            poly.flip()
        self.plane.flip()
        if self.front:
            self.front.invert()
        if self.back:
            self.back.invert()
        temp = self.front
        self.front = self.back
        self.back = temp


Instruction:
How would you implement a function in Python 3 that
removes all polygons in this BSP tree that are inside the other BSP tree.

Response:
def clipTo(self, bsp):
        """
        Remove all polygons in this BSP tree that are inside the other BSP tree
        `bsp`.
        """
        self.polygons = bsp.clipPolygons(self.polygons)
        if self.front:
            self.front.clipTo(bsp)
        if self.back:
            self.back.clipTo(bsp)


Instruction:
Write a Python 3 function for
returning a list of all polygons in this BSP tree.

Response:
def allPolygons(self):
        """
        Return a list of all polygons in this BSP tree.
        """
        polygons = self.polygons[:]
        if self.front:
            polygons.extend(self.front.allPolygons())
        if self.back:
            polygons.extend(self.back.allPolygons())
        return polygons


Instruction:
Here you have a function in Python 3, explain what it does
def build(self, polygons):
        if len(polygons) == 0:
            return
        if not self.plane:
            self.plane = polygons[0].plane.clone()
        # add polygon to this node
        self.polygons.append(polygons[0])
        front = []
        back = []
        # split all other polygons using the first polygon's plane
        for poly in polygons[1:]:
            # coplanar front and back polygons go into self.polygons
            self.plane.splitPolygon(poly, self.polygons, self.polygons,
                                    front, back)
        # recursively build the BSP tree
        if len(front) > 0:
            if not self.front:
                self.front = BSPNode()
            self.front.build(front)
        if len(back) > 0:
            if not self.back:
                self.back = BSPNode()
            self.back.build(back)

Response: Builds a BSP tree out of a list of polygons.


Instruction:
How would you implement a function in Python 3 that
returns the rate from the default currency to currency.

Response:
def get_rate(currency):
    """Returns the rate from the default currency to `currency`."""
    source = get_rate_source()
    try:
        return Rate.objects.get(source=source, currency=currency).value
    except Rate.DoesNotExist:
        raise CurrencyConversionException(
            "Rate for %s in %s do not exists. "
            "Please run python manage.py update_rates" % (
                currency, source.name))


Instruction:
Create a Python 3 function for
getting the default Rate Source and return it.

Response:
def get_rate_source():
    """Get the default Rate Source and return it."""
    backend = money_rates_settings.DEFAULT_BACKEND()
    try:
        return RateSource.objects.get(name=backend.get_source_name())
    except RateSource.DoesNotExist:
        raise CurrencyConversionException(
            "Rate for %s source do not exists. "
            "Please run python manage.py update_rates" % backend.get_source_name())


Instruction:
Given the following Python 3 function, write the documentation
def base_convert_money(amount, currency_from, currency_to):
    source = get_rate_source()

    # Get rate for currency_from.
    if source.base_currency != currency_from:
        rate_from = get_rate(currency_from)
    else:
        # If currency from is the same as base currency its rate is 1.
        rate_from = Decimal(1)

    # Get rate for currency_to.
    rate_to = get_rate(currency_to)

    if isinstance(amount, float):
        amount = Decimal(amount).quantize(Decimal('.000001'))

    # After finishing the operation, quantize down final amount to two points.
    return ((amount / rate_from) * rate_to).quantize(Decimal("1.00"))

Response: Convert amount from currency_from to currency_to


Instruction:
Explain what the following Python 3 code does
def convert_money(amount, currency_from, currency_to):
    new_amount = base_convert_money(amount, currency_from, currency_to)
    return moneyed.Money(new_amount, currency_to)

Response: Convert amount from currency_from to currency_to and return a Money object of the converted amount.


Instruction:
Given the following Python 3 function, write the documentation
def format_date(format_string=None, datetime_obj=None):
    datetime_obj = datetime_obj or datetime.now()
    if format_string is None:
        seconds = int(datetime_obj.strftime("%s"))
        milliseconds = datetime_obj.microsecond // 1000
        return str(seconds * 1000 + milliseconds)
    else:
        formatter = SimpleDateFormat(format_string)
        return formatter.format_datetime(datetime_obj)

Response: Formats a datetime object with Java SimpleDateFormat s - like string.


Instruction:
Write a Python 3 script to
check if a given buffer is empty and returns a boolean indicating if it is full of zeros.

Response:
def allZero(buffer):
    """
    Tries to determine if a buffer is empty.

    @type buffer: str
    @param buffer: Buffer to test if it is empty.

    @rtype: bool
    @return: C{True} if the given buffer is empty, i.e. full of zeros,
        C{False} if it doesn't.
    """
    allZero = True
    for byte in buffer:
        if byte != "\x00":
            allZero = False
            break
    return allZero


Instruction:
Write a Python 3 function that can
write a byte into the L { WriteData object object.

Response:
def writeByte(self, byte):
        """
        Writes a byte into the L{WriteData} stream object.

        @type byte: int
        @param byte: Byte value to write into the stream.
        """
        self.data.write(pack("B" if not self.signed else "b", byte))


Instruction:
How would you implement a function in Python 3 that
writes a word into the L { WriteData object object.

Response:
def writeWord(self, word):
        """
        Writes a word value into the L{WriteData} stream object.

        @type word: int
        @param word: Word value to write into the stream.
        """
        self.data.write(pack(self.endianness + ("H" if not self.signed else "h"), word))


Instruction:
Make a summary of the following Python 3 code
def writeDword(self, dword):
        self.data.write(pack(self.endianness + ("L" if not self.signed else "l"), dword))

Response: Writes a dword value into the L { WriteData object object.


Instruction:
Create a Python 3 function for
writing a qword value into the L { WriteData object object.

Response:
def writeQword(self, qword):
        """
        Writes a qword value into the L{WriteData} stream object.

        @type qword: int
        @param qword: Qword value to write into the stream.
        """
        self.data.write(pack(self.endianness + ("Q" if not self.signed else "q"),  qword))


Instruction:
Explain what the following Python 3 code does
def setOffset(self, value):
        if value >= len(self.data.getvalue()):
            raise excep.WrongOffsetValueException("Wrong offset value. Must be less than %d" % len(self.data))
        self.data.seek(value)

Response: Sets the offset of the data in which the data is written.


Instruction:
Given the following Python 3 function, write the documentation
def skipBytes(self, nroBytes):
        self.data.seek(nroBytes + self.data.tell())

Response: Skips the specified number of bytes from the current value of the APK.


Instruction:
Make a summary of the following Python 3 code
def readDword(self):
        dword = unpack(self.endianness + ('L' if not self.signed else 'l'), self.readAt(self.offset,  4))[0]
        self.offset += 4
        return dword

Response: Reads a dword value from the L { ReadData } stream object.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def readWord(self):
        word = unpack(self.endianness + ('H' if not self.signed else 'h'), self.readAt(self.offset, 2))[0]
        self.offset += 2
        return word

Response: Reads a word value from the L { ReadData object object.


Instruction:
Given the following Python 3 function, write the documentation
def readByte(self):
        byte = unpack('B' if not self.signed else 'b', self.readAt(self.offset, 1))[0]
        self.offset += 1
        return byte

Response: Reads a byte value from the L { ReadData } stream object.


Instruction:
How would you explain what the following Python 3 function does
def readQword(self):
        qword = unpack(self.endianness + ('Q' if not self.signed else 'b'),  self.readAt(self.offset, 8))[0]
        self.offset += 8
        return qword

Response: Reads a qword value from the L { ReadData } stream object.


Instruction:
Create a Python 3 function for
reading an ASCII string from the stream object.

Response:
def readString(self):
        """
        Reads an ASCII string from the L{ReadData} stream object.

        @rtype: str
        @return: An ASCII string read form the stream.
        """
        resultStr = ""
        while self.data[self.offset] != "\x00":
            resultStr += self.data[self.offset]
            self.offset += 1
        return resultStr


Instruction:
Can you implement a function in Python 3 that
reads an ASCII string aligned to the next align - bytes boundary.

Response:
def readAlignedString(self, align = 4):
        """
        Reads an ASCII string aligned to the next align-bytes boundary.

        @type align: int
        @param align: (Optional) The value we want the ASCII string to be aligned.

        @rtype: str
        @return: A 4-bytes aligned (default) ASCII string.
        """
        s = self.readString()
        r = align - len(s) % align
        while r:
            s += self.data[self.offset]
            self.offset += 1
            r -= 1
        return s.rstrip("\x00")


Instruction:
Given the following Python 3 function, write the documentation
def read(self, nroBytes):
        if nroBytes > self.length - self.offset:
            if self.log:
                print "Warning: Trying to read: %d bytes - only %d bytes left" % (nroBytes,  self.length - self.offset)
            nroBytes = self.length - self.offset

        resultStr = self.data[self.offset:self.offset + nroBytes]
        self.offset += nroBytes
        return resultStr

Response: Reads the contents of the n - bytes in the object at the current offset and returns it as a string.


Instruction:
Given the following Python 3 function, write the documentation
def readAt(self, offset, size):
        if offset > self.length:
            if self.log:
                print "Warning: Trying to read: %d bytes - only %d bytes left" % (nroBytes,  self.length - self.offset)
            offset = self.length - self.offset
        tmpOff = self.tell()
        self.setOffset(offset)
        r = self.read(size)
        self.setOffset(tmpOff)
        return r

Response: Reads as many bytes as possible from the specified offset.


Instruction:
How would you code a function in Python 3 to
send a message to a specific channel.

Response:
def send(self, message, channel_name=None, fail_silently=False,
             options=None):
        # type: (Text, Optional[str], bool, Optional[SendOptions]) -> None
        """Send a notification to channels

        :param message: A message
        """
        if channel_name is None:
            channels = self.settings["CHANNELS"]
        else:
            try:
                channels = {
                    "__selected__": self.settings["CHANNELS"][channel_name]
                }
            except KeyError:
                raise Exception("channels does not exist %s", channel_name)

        for _, config in channels.items():
            if "_backend" not in config:
                raise ImproperlyConfigured(
                    "Specify the backend class in the channel configuration")

            backend = self._load_backend(config["_backend"])  # type: Any
            config = deepcopy(config)
            del config["_backend"]
            channel = backend(**config)
            channel.send(message, fail_silently=fail_silently, options=options)


Instruction:
Write a Python 3 script to
send an HTTP request to the specified path. Returns the HTTPResponse object.

Response:
def request(self, method, path,
                params=None, headers=None, cookies=None, data=None, json=None, allow_redirects=None, timeout=None):
        """
        Prepares and sends an HTTP request. Returns the HTTPResponse object.

        :param method: str
        :param path: str
        :return: response
        :rtype: HTTPResponse
        """
        headers = headers or {}
        timeout = timeout if timeout is not None else self._timeout
        allow_redirects = allow_redirects if allow_redirects is not None else self._allow_redirects

        if self._keep_alive and self.__session is None:
            self.__session = requests.Session()

        if self.__session is not None and not self._use_cookies:
            self.__session.cookies.clear()

        address = self._bake_address(path)
        req_headers = copy.deepcopy(self._additional_headers)
        req_headers.update(headers)

        response = http.request(method, address, session=self.__session,
                                params=params, headers=headers, cookies=cookies, data=data, json=json,
                                allow_redirects=allow_redirects, timeout=timeout)
        if self._auto_assert_ok:
            response.assert_ok()
        return response


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

    anchor_pos_and_functionality = {}
    anchor_pos_file = open(anchor_pos_file_name, 'r')

    first_line = True
    for line in anchor_pos_file:
        if first_line:
            first_line = False
            continue

        split_line = line.split(',')
        split_line = [x.strip() for x in split_line]
        anchor_pos_and_functionality[split_line[0]] = [int(split_line[1]), split_line[2].strip().strip('()')]

    return anchor_pos_and_functionality

Response: Reads the CDR3 anchor position and functionality from file.


Instruction:
Explain what the following Python 3 code does
def read_igor_V_gene_parameters(params_file_name):
    params_file = open(params_file_name, 'r')

    V_gene_info = {}

    in_V_gene_sec = False
    for line in params_file:
        if line.startswith('#GeneChoice;V_gene;'):
            in_V_gene_sec = True
        elif in_V_gene_sec:
            if line[0] == '%':
                split_line = line[1:].split(';')
                V_gene_info[split_line[0]] = [split_line[1] , int(split_line[2])]
            else:
                break
    params_file.close()

    genV = [[]]*len(V_gene_info.keys())

    for V_gene in V_gene_info.keys():
        genV[V_gene_info[V_gene][1]] = [V_gene, '', V_gene_info[V_gene][0]]

    return genV

Response: Load raw genV from IGOR parameter file.


Instruction:
Create a Python 3 function to
load genD from IGOR parameter file.

Response:
def read_igor_D_gene_parameters(params_file_name):
    """Load genD from file.

    genD is a list of genomic D information. Each element is a list of the name
    of the D allele and the germline sequence.

    Parameters
    ----------
    params_file_name : str
        File name for a IGOR parameter file.

    Returns
    -------
    genD : list
        List of genomic D information.

    """
    params_file = open(params_file_name, 'r')

    D_gene_info = {}

    in_D_gene_sec = False
    for line in params_file:
        if line.startswith('#GeneChoice;D_gene;'):
            in_D_gene_sec = True
        elif in_D_gene_sec:
            if line[0] == '%':
                split_line = line[1:].split(';')
                D_gene_info[split_line[0]] = [split_line[1] , int(split_line[2])]
            else:
                break
    params_file.close()

    genD = [[]]*len(D_gene_info.keys())

    for D_gene in D_gene_info.keys():
        genD[D_gene_info[D_gene][1]] = [D_gene, D_gene_info[D_gene][0]]

    return genD


Instruction:
Can you tell what is the following Python 3 function doing
def read_igor_J_gene_parameters(params_file_name):
    params_file = open(params_file_name, 'r')

    J_gene_info = {}

    in_J_gene_sec = False
    for line in params_file:
        if line.startswith('#GeneChoice;J_gene;'):
            in_J_gene_sec = True
        elif in_J_gene_sec:
            if line[0] == '%':
                split_line = line[1:].split(';')
                J_gene_info[split_line[0]] = [split_line[1] , int(split_line[2])]
            else:
                break
    params_file.close()

    genJ = [[]]*len(J_gene_info.keys())

    for J_gene in J_gene_info.keys():
        genJ[J_gene_info[J_gene][1]] = [J_gene, '', J_gene_info[J_gene][0]]

    return genJ

Response: Load raw genJ from IGOR parameter file.


Instruction:
How would you implement a function in Python 3 that
reads a GOR model marginals file and returns a dictionary with model parameters and dimension names.

Response:
def read_igor_marginals_txt(marginals_file_name , dim_names=False):
    """Load raw IGoR model marginals.

    Parameters
    ----------
    marginals_file_name : str
        File name for a IGOR model marginals file.

    Returns
    -------
    model_dict : dict
        Dictionary with model marginals.
    dimension_names_dict : dict
        Dictionary that defines IGoR model dependecies.

    """
    with open(marginals_file_name,'r') as file:
        #Model parameters are stored inside a dictionary of ndarrays
        model_dict = {}
        dimension_names_dict = {}
        element_name=""
        first = True
        first_dim_line = False
        element_marginal_array = []
        indices_array = []

        for line in file:
            strip_line = line.rstrip('\n') #Remove end of line character
            if strip_line[0]=='@':
                first_dim_line = True
                if not(first):
                    #Add the previous to the dictionnary
                    model_dict[element_name] = element_marginal_array
                else:
                    first = False

                element_name = strip_line[1:]

            if strip_line[0]=='$':
                #define array dimensions
                coma_index = strip_line.find(',')
                dimensions = []

                #Get rid of $Dim[
                previous_coma_index = 4
                while coma_index != -1:
                    dimensions.append(int(strip_line[previous_coma_index+1:coma_index]))
                    previous_coma_index = coma_index
                    coma_index = strip_line.find(',',coma_index+1)

                #Add last dimension and get rid of the closing bracket
                dimensions.append(int(strip_line[previous_coma_index+1:-1]))

                element_marginal_array = np.ndarray(shape=dimensions)

            if strip_line[0]=='#':
                if first_dim_line:
                    dimensions_names = []
                    if len(dimensions) > 1:
                        comma_index = strip_line.find(',')
                        opening_bracket_index = strip_line.find('[')
                        while opening_bracket_index != -1:
                            dimensions_names.append(strip_line[opening_bracket_index+1:comma_index])
                            opening_bracket_index = strip_line.find('[',comma_index)
                            comma_index = strip_line.find(',',opening_bracket_index)
                    first_dim_line = False
                    dimensions_names.append(element_name)
                    dimension_names_dict[element_name] = dimensions_names

                #update indices
                indices_array = []
                if len(dimensions) > 1:
                    comma_index = strip_line.find(',')
                    closing_brack_index = strip_line.find(']')
                    while closing_brack_index != -1:
                        indices_array.append(int(strip_line[comma_index+1:closing_brack_index]))
                        opening_bracket_index = strip_line.find('[',closing_brack_index)
                        comma_index = strip_line.find(',',opening_bracket_index)
                        closing_brack_index = strip_line.find(']',closing_brack_index+1)

            if strip_line[0]=='%':
                #read doubles
                coma_index = strip_line.find(',')
                marginals_values = []

                #Get rid of the %
                previous_coma_index = 0
                while coma_index != -1:
                    marginals_values.append(float(strip_line[previous_coma_index+1:coma_index]))
                    previous_coma_index = coma_index
                    coma_index = strip_line.find(',',coma_index+1)

                #Add last dimension and get rid of the closing bracket
                marginals_values.append(float(strip_line[previous_coma_index+1:]))
                if len(marginals_values)!=dimensions[-1]:
                    print "problem"
                element_marginal_array[tuple(indices_array)] = marginals_values
        model_dict[element_name] = element_marginal_array

    return [model_dict,dimension_names_dict]


Instruction:
Explain what the following Python 3 code does
def anchor_and_curate_genV_and_genJ(self, V_anchor_pos_file, J_anchor_pos_file):

        V_anchor_pos = load_genomic_CDR3_anchor_pos_and_functionality(V_anchor_pos_file)
        J_anchor_pos = load_genomic_CDR3_anchor_pos_and_functionality(J_anchor_pos_file)

        for V in self.genV:
            try:
                if V_anchor_pos[V[0]][0] > 0 and V_anchor_pos[V[0]][1] == 'F': #Check for functionality
                    V[1] = V[2][V_anchor_pos[V[0]][0]:]
                else:
                    V[1] = ''
            except KeyError:
                V[1] = ''

        for J in self.genJ:
            try:
                if J_anchor_pos[J[0]][0] > 0 and J_anchor_pos[J[0]][1] == 'F': #Check for functionality
                    J[1] = J[2][:J_anchor_pos[J[0]][0]+3]
                else:
                    J[1] = ''
            except KeyError:
                J[1] = ''

Response: Trim V and J germline sequences to the CDR3 region.


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

        max_palindrome = self.max_delV_palindrome

        self.cutV_genomic_CDR3_segs = []
        for CDR3_V_seg in [x[1] for x in self.genV]:
            if len(CDR3_V_seg) < max_palindrome:
                self.cutV_genomic_CDR3_segs += [cutR_seq(CDR3_V_seg, 0, len(CDR3_V_seg))]
            else:
                self.cutV_genomic_CDR3_segs += [cutR_seq(CDR3_V_seg, 0, max_palindrome)]

Response: Add palindromic inserted nucleotides to the genV attribute.


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

        max_palindrome = self.max_delJ_palindrome
        self.cutJ_genomic_CDR3_segs = []
        for CDR3_J_seg in [x[1] for x in self.genJ]:
            if len(CDR3_J_seg) < max_palindrome:
                self.cutJ_genomic_CDR3_segs += [cutL_seq(CDR3_J_seg, 0, len(CDR3_J_seg))]
            else:
                self.cutJ_genomic_CDR3_segs += [cutL_seq(CDR3_J_seg, 0, max_palindrome)]

Response: Add palindromic inserted nucleotides to the genJ sequence.


Instruction:
How would you implement a function in Python 3 that
loads in genomic data from IGOR parameter file.

Response:
def load_igor_genomic_data(self, params_file_name, V_anchor_pos_file, J_anchor_pos_file):
        """Set attributes by loading in genomic data from IGoR parameter file.

        Sets attributes genV,  max_delV_palindrome, cutV_genomic_CDR3_segs,
        genD, max_delDl_palindrome, max_delDr_palindrome,
        cutD_genomic_CDR3_segs, genJ, max_delJ_palindrome, and
        cutJ_genomic_CDR3_segs.

        Parameters
        ----------
        params_file_name : str
            File name for a IGOR parameter file.
        V_anchor_pos_file_name : str
            File name for the conserved residue (C) locations and functionality
            of each V genomic sequence.
        J_anchor_pos_file_name : str
            File name for the conserved residue (F/W) locations and
            functionality of each J genomic sequence.

        """

        self.genV = read_igor_V_gene_parameters(params_file_name)
        self.genD = read_igor_D_gene_parameters(params_file_name)
        self.genJ = read_igor_J_gene_parameters(params_file_name)

        self.anchor_and_curate_genV_and_genJ(V_anchor_pos_file, J_anchor_pos_file)

        self.read_VDJ_palindrome_parameters(params_file_name) #Need palindrome info before generating cut_genomic_CDR3_segs

        self.generate_cutV_genomic_CDR3_segs()
        self.generate_cutD_genomic_CDR3_segs()
        self.generate_cutJ_genomic_CDR3_segs()


Instruction:
How would you code a function in Python 3 to
add palindromic inserted nucleotides to germline V sequences.

Response:
def generate_cutD_genomic_CDR3_segs(self):
        """Add palindromic inserted nucleotides to germline V sequences.

        The maximum number of palindromic insertions are appended to the
        germline D segments so that delDl and delDr can index directly for number
        of nucleotides to delete from a segment.

        Sets the attribute cutV_genomic_CDR3_segs.

        """
        max_palindrome_L = self.max_delDl_palindrome
        max_palindrome_R = self.max_delDr_palindrome

        self.cutD_genomic_CDR3_segs = []
        for CDR3_D_seg in [x[1] for x in self.genD]:
            if len(CDR3_D_seg) < min(max_palindrome_L, max_palindrome_R):
                self.cutD_genomic_CDR3_segs += [cutR_seq(cutL_seq(CDR3_D_seg, 0, len(CDR3_D_seg)), 0, len(CDR3_D_seg))]
            else:
                self.cutD_genomic_CDR3_segs += [cutR_seq(cutL_seq(CDR3_D_seg, 0, max_palindrome_L), 0, max_palindrome_R)]


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

        params_file = open(params_file_name, 'r')

        in_delV = False
        in_delDl = False
        in_delDr = False
        in_delJ = False

        for line in params_file:
            if line.startswith('#Deletion;V_gene;'):
                in_delV = True
                in_delDl = False
                in_delDr = False
                in_delJ = False
            elif line.startswith('#Deletion;D_gene;Three_prime;'):
                in_delV = False
                in_delDl = False
                in_delDr = True
                in_delJ = False
            elif line.startswith('#Deletion;D_gene;Five_prime;'):
                in_delV = False
                in_delDl = True
                in_delDr = False
                in_delJ = False
            elif line.startswith('#Deletion;J_gene;'):
                in_delV = False
                in_delDl = False
                in_delDr = False
                in_delJ = True
            elif any([in_delV, in_delDl, in_delDr, in_delJ]) and line.startswith('%'):
                if int(line.split(';')[-1]) == 0:
                    if in_delV:
                        self.max_delV_palindrome = np.abs(int(line.lstrip('%').split(';')[0]))
                    elif in_delDl:
                        self.max_delDl_palindrome = np.abs(int(line.lstrip('%').split(';')[0]))
                    elif in_delDr:
                        self.max_delDr_palindrome = np.abs(int(line.lstrip('%').split(';')[0]))
                    elif in_delJ:
                        self.max_delJ_palindrome = np.abs(int(line.lstrip('%').split(';')[0]))
            else:
                in_delV = False
                in_delDl = False
                in_delDr = False
                in_delJ = False

Response: Reads the VDJ and D and J palindrome parameters from a file.


Instruction:
How would you explain what the following Python 3 function does
def load_igor_genomic_data(self, params_file_name, V_anchor_pos_file, J_anchor_pos_file):

        self.genV = read_igor_V_gene_parameters(params_file_name)
        self.genJ = read_igor_J_gene_parameters(params_file_name)

        self.anchor_and_curate_genV_and_genJ(V_anchor_pos_file, J_anchor_pos_file)

        self.read_igor_VJ_palindrome_parameters(params_file_name)

        self.generate_cutV_genomic_CDR3_segs()
        self.generate_cutJ_genomic_CDR3_segs()

Response: Load in genomic data from IGOR parameter file.


Instruction:
Given the following Python 3 function, write the documentation
def read_igor_VJ_palindrome_parameters(self, params_file_name):
        params_file = open(params_file_name, 'r')

        in_delV = False
        in_delJ = False

        for line in params_file:
            if line.startswith('#Deletion;V_gene;'):
                in_delV = True
                in_delJ = False
            elif line.startswith('#Deletion;J_gene;'):
                in_delV = False
                in_delJ = True
            elif any([in_delV, in_delJ]) and line.startswith('%'):
                if int(line.split(';')[-1]) == 0:
                    if in_delV:
                        self.max_delV_palindrome = np.abs(int(line.lstrip('%').split(';')[0]))
                    elif in_delJ:
                        self.max_delJ_palindrome = np.abs(int(line.lstrip('%').split(';')[0]))
            else:
                in_delV = False
                in_delJ = False

Response: Reads the V and J palindrome parameters from a file.


Instruction:
Here you have a function in Python 3, explain what it does
def load_and_process_igor_model(self, marginals_file_name):

        raw_model = read_igor_marginals_txt(marginals_file_name)

        self.PV = raw_model[0]['v_choice']
        self.PinsVD = raw_model[0]['vd_ins']
        self.PinsDJ = raw_model[0]['dj_ins']
        self.PdelV_given_V = raw_model[0]['v_3_del'].T
        self.PdelJ_given_J = raw_model[0]['j_5_del'].T

        #While this class assumes P(V, D, J) factorizes into P(V)*P(D, J), the B cell model
        #infers allowing for the full correlation. Most of the correlation information is due to
        #chromosomal correlation of alleles (i.e. what chromosome each allele is found on).
        #While this information can be interesting for inference purposes, it is discarded here
        #as generally these models may be use for CDR3s from individuals the models weren't inferred
        #from (and thus the chromosomal correlations are incorrect). This also equates the T and B cell
        #models. To reintroduce the chromosomal correlations use V and J usage masks after inferring the
        #allele identities on each chromosome.

        if raw_model[1]['d_gene'] == ['j_choice', 'd_gene']:
        #Factorized P(V, D, J) = P(V)*P(D, J) --- correct for T cell models
            self.PDJ = np.multiply(raw_model[0]['d_gene'].T, raw_model[0]['j_choice'])
        elif raw_model[1]['d_gene'] == ['v_choice', 'j_choice', 'd_gene']:
        #Full P(V, D, J) for B cells --- need to compute the marginal P(D, J)
            PVJ = np.multiply(raw_model[0]['j_choice'].T, raw_model[0]['v_choice']).T
            PVDJ = np.zeros([raw_model[0]['d_gene'].shape[0], raw_model[0]['d_gene'].shape[2], raw_model[0]['d_gene'].shape[1]])
            for v_in in range(raw_model[0]['d_gene'].shape[0]):
                for j_in in range(raw_model[0]['d_gene'].shape[1]):
                    PVDJ[v_in, :, j_in] = PVJ[v_in, j_in]*raw_model[0]['d_gene'][v_in, j_in, :]
            self.PDJ = np.sum(PVDJ, 0)
        else:
            print 'Unrecognized model structure -- need to construct P(D, J)'
            return 0

        self.PdelDldelDr_given_D = np.transpose(np.multiply(np.transpose(raw_model[0]['d_3_del'], (2, 0, 1)), raw_model[0]['d_5_del']), (2, 0 , 1))
        Rvd_raw = raw_model[0]['vd_dinucl'].reshape((4, 4)).T
        self.Rvd = np.multiply(Rvd_raw, 1/np.sum(Rvd_raw, axis = 0))
        Rdj_raw = raw_model[0]['dj_dinucl'].reshape((4, 4)).T
        self.Rdj = np.multiply(Rdj_raw, 1/np.sum(Rdj_raw, axis = 0))

Response: Load and process a generative model from the IGoR Marginal File.


Instruction:
Can you write a function in Python 3 where it
loads attributes by reading a generative model from IGoR marginal file.

Response:
def load_and_process_igor_model(self, marginals_file_name):
        """Set attributes by reading a generative model from IGoR marginal file.

        Sets attributes PVJ, PdelV_given_V, PdelJ_given_J, PinsVJ, and Rvj.

        Parameters
        ----------
        marginals_file_name : str
            File name for a IGoR model marginals file.

        """

        raw_model = read_igor_marginals_txt(marginals_file_name)

        self.PinsVJ = raw_model[0]['vj_ins']
        self.PdelV_given_V = raw_model[0]['v_3_del'].T
        self.PdelJ_given_J = raw_model[0]['j_5_del'].T
        self.PVJ = np.multiply( raw_model[0]['j_choice'].T, raw_model[0]['v_choice']).T
        Rvj_raw = raw_model[0]['vj_dinucl'].reshape((4, 4)).T
        self.Rvj = np.multiply(Rvj_raw, 1/np.sum(Rvj_raw, axis = 0))


Instruction:
Write a Python 3 function for
parsing the contents of the object with the corresponding data.

Response:
def parse(readDataInstance):
        """
        Returns a new L{ImageBoundForwarderRefEntry} object.

        @type readDataInstance: L{ReadData}
        @param readDataInstance: A L{ReadData} object with the corresponding data to generate a new L{ImageBoundForwarderRefEntry} object.

        @rtype: L{ImageBoundForwarderRefEntry}
        @return: A new L{ImageBoundForwarderRefEntry} object.
        """
        boundForwarderEntry = ImageBoundForwarderRefEntry()
        boundForwarderEntry.timeDateStamp.value = readDataInstance.readDword()
        boundForwarderEntry.offsetModuleName.value = readDataInstance.readWord()
        boundForwarderEntry.reserved.value = readDataInstance.readWord()
        return boundForwarderEntry


Instruction:
Write a Python 3 script to
parse the contents of a L { ImageBoundForwarderRefEntry } object.

Response:
def parse(readDataInstance,  numberOfEntries):
        """
        Returns a L{ImageBoundForwarderRef} array where every element is a L{ImageBoundForwarderRefEntry} object.

        @type readDataInstance: L{ReadData}
        @param readDataInstance: A L{ReadData} object with the corresponding data to generate a new L{ImageBoundForwarderRef} object.

        @type numberOfEntries: int
        @param numberOfEntries: The number of C{IMAGE_BOUND_FORWARDER_REF} entries in the array.

        @rtype: L{ImageBoundForwarderRef}
        @return: A new L{ImageBoundForwarderRef} object.

        @raise DataLengthException: If the L{ReadData} instance has less data than C{NumberOfEntries} * sizeof L{ImageBoundForwarderRefEntry}.
        """
        imageBoundForwarderRefsList = ImageBoundForwarderRef()
        dLength = len(readDataInstance)
        entryLength = ImageBoundForwarderRefEntry().sizeof()
        toRead = numberOfEntries * entryLength

        if dLength >= toRead:
            for i in range(numberOfEntries):
                entryData = readDataInstance.read(entryLength)
                rd = utils.ReadData(entryData)
                imageBoundForwarderRefsList.append(ImageBoundForwarderRefEntry.parse(rd))
        else:
            raise excep.DataLengthException("Not enough bytes to read.")

        return imageBoundForwarderRefsList


Instruction:
Given the following Python 3 function, write the documentation
def parse(readDataInstance):
        ibd = ImageBoundImportDescriptor()

        entryData = readDataInstance.read(consts.SIZEOF_IMAGE_BOUND_IMPORT_ENTRY32)
        readDataInstance.offset = 0
        while not utils.allZero(entryData):
            prevOffset = readDataInstance.offset

            boundEntry = ImageBoundImportDescriptorEntry.parse(readDataInstance)

            # if the parsed entry has numberOfModuleForwarderRefs we must adjust the value in the readDataInstance.offset field
            # in order to point after the last ImageBoundForwarderRefEntry.
            if boundEntry.numberOfModuleForwarderRefs.value:
                readDataInstance.offset = prevOffset + (consts.SIZEOF_IMAGE_BOUND_FORWARDER_REF_ENTRY32 * boundEntry.numberOfModuleForwarderRefs.value)
            else:
                readDataInstance.offset = prevOffset

            ibd.append(boundEntry)
            entryData = readDataInstance.read(consts.SIZEOF_IMAGE_BOUND_IMPORT_ENTRY32)

        return ibd

Response: Parses the contents of the image bound forwarder file.


Instruction:
Explain what the following Python 3 code does
def parse(readDataInstance):
        boundEntry = ImageBoundImportDescriptorEntry()
        boundEntry.timeDateStamp.value = readDataInstance.readDword()
        boundEntry.offsetModuleName.value = readDataInstance.readWord()
        boundEntry.numberOfModuleForwarderRefs.value = readDataInstance.readWord()

        numberOfForwarderRefsEntries = boundEntry.numberOfModuleForwarderRefs .value
        if numberOfForwarderRefsEntries:
            bytesToRead = numberOfForwarderRefsEntries * ImageBoundForwarderRefEntry().sizeof()
            rd = utils.ReadData(readDataInstance.read(bytesToRead))
            boundEntry.forwarderRefsList = ImageBoundForwarderRef.parse(rd,  numberOfForwarderRefsEntries)

        return boundEntry

Response: Parses the contents of an image - bound import descriptor entry.


Instruction:
Write a Python 3 function for
parsing the contents of a NIC - level directory into a new object.

Response:
def parse(readDataInstance):
        """
        Returns a new L{TLSDirectory} object.

        @type readDataInstance: L{ReadData}
        @param readDataInstance: A L{ReadData} object containing data to create a new L{TLSDirectory} object.

        @rtype: L{TLSDirectory}
        @return: A new {TLSDirectory} object.
        """
        tlsDir = TLSDirectory()

        tlsDir.startAddressOfRawData.value = readDataInstance.readDword()
        tlsDir.endAddressOfRawData.value = readDataInstance.readDword()
        tlsDir.addressOfIndex.value = readDataInstance.readDword()
        tlsDir.addressOfCallbacks.value = readDataInstance.readDword()
        tlsDir.sizeOfZeroFill.value = readDataInstance.readDword()
        tlsDir.characteristics.value = readDataInstance.readDword()
        return tlsDir


Instruction:
How would you implement a function in Python 3 that
parses the contents of a L{TLSDirectory64} object.

Response:
def parse(readDataInstance):
        """
        Returns a new L{TLSDirectory64} object.

        @type readDataInstance: L{ReadData}
        @param readDataInstance: A L{ReadData} object containing data to create a new L{TLSDirectory64} object.

        @rtype: L{TLSDirectory64}
        @return: A new L{TLSDirectory64} object.
        """
        tlsDir = TLSDirectory64()

        tlsDir.startAddressOfRawData.value = readDataInstance.readQword()
        tlsDir.endAddressOfRawData.value = readDataInstance.readQword()
        tlsDir.addressOfIndex.value = readDataInstance.readQword()
        tlsDir.addressOfCallbacks.value = readDataInstance.readQword()
        tlsDir.sizeOfZeroFill.value = readDataInstance.readDword()
        tlsDir.characteristics.value = readDataInstance.readDword()
        return tlsDir


Instruction:
Write a Python 3 script to
parse the contents of a ReadData object containing the contents of a new ImageLoadConfigDirectory64 object.

Response:
def parse(readDataInstance):
        """
        Returns a new L{ImageLoadConfigDirectory64} object.

        @type readDataInstance: L{ReadData}
        @param readDataInstance: A L{ReadData} object containing data to create a new L{ImageLoadConfigDirectory64} object.

        @rtype: L{ImageLoadConfigDirectory64}
        @return: A new L{ImageLoadConfigDirectory64} object.
        """
        configDir = ImageLoadConfigDirectory64()

        configDir.size.value = readDataInstance.readDword()
        configDir.timeDateStamp.value = readDataInstance.readDword()
        configDir.majorVersion.value = readDataInstance.readWord()
        configDir.minorVersion.value = readDataInstance.readWord()
        configDir.globalFlagsClear.value = readDataInstance.readDword()
        configDir.globalFlagsSet.value = readDataInstance.readDword()
        configDir.criticalSectionDefaultTimeout.value = readDataInstance.readDword()
        configDir.deCommitFreeBlockThreshold.value = readDataInstance.readQword()
        configDir.deCommitTotalFreeThreshold.value = readDataInstance.readQword()
        configDir.lockPrefixTable.value = readDataInstance.readQword()
        configDir.maximumAllocationSize.value = readDataInstance.readQword()
        configDir.virtualMemoryThreshold.value = readDataInstance.readQword()
        configDir.processAffinityMask.value = readDataInstance.readQword()
        configDir.processHeapFlags.value = readDataInstance.readDword()
        configDir.cdsVersion.value = readDataInstance.readWord()
        configDir.reserved1.value = readDataInstance.readWord()
        configDir.editList.value = readDataInstance.readQword()
        configDir.securityCookie.value = readDataInstance.readQword()
        configDir.SEHandlerTable.value = readDataInstance.readQword()
        configDir.SEHandlerCount.value = readDataInstance.readQword()

        # Fields for Control Flow Guard
        configDir.GuardCFCheckFunctionPointer.value = readDataInstance.readQword() # VA
        configDir.Reserved2.value = readDataInstance.readQword()
        configDir.GuardCFFunctionTable.value = readDataInstance.readQword() # VA
        configDir.GuardCFFunctionCount.value = readDataInstance.readQword()
        configDir.GuardFlags.value = readDataInstance.readQword()
        return configDir


Instruction:
Explain what the following Python 3 code does
def parse(readDataInstance):
        reloc = ImageBaseRelocationEntry()
        reloc.virtualAddress.value = readDataInstance.readDword()
        reloc.sizeOfBlock.value = readDataInstance.readDword()
        toRead = (reloc.sizeOfBlock.value - 8) / len(datatypes.WORD(0))
        reloc.items = datatypes.Array.parse(readDataInstance,  datatypes.TYPE_WORD,  toRead)
        return reloc

Response: Parses the contents of the object as a list of bytes.


Instruction:
How would you code a function in Python 3 to
parse the contents of the given object and returns a new object with the contents.

Response:
def parse(readDataInstance):
        """
        Returns a new L{ImageDebugDirectory} object.

        @type readDataInstance: L{ReadData}
        @param readDataInstance: A new L{ReadData} object with data to be parsed as a L{ImageDebugDirectory} object.

        @rtype: L{ImageDebugDirectory}
        @return: A new L{ImageDebugDirectory} object.
        """
        dbgDir = ImageDebugDirectory()

        dbgDir.characteristics.value = readDataInstance.readDword()
        dbgDir.timeDateStamp.value = readDataInstance.readDword()
        dbgDir.majorVersion.value = readDataInstance.readWord()
        dbgDir.minorVersion.value = readDataInstance.readWord()
        dbgDir.type.value = readDataInstance.readDword()
        dbgDir.sizeOfData.value = readDataInstance.readDword()
        dbgDir.addressOfData.value = readDataInstance.readDword()
        dbgDir.pointerToRawData.value = readDataInstance.readDword()

        return dbgDir


Instruction:
Explain what the following Python 3 code does
def parse(readDataInstance,  nDebugEntries):
        dbgEntries = ImageDebugDirectories()

        dataLength = len(readDataInstance)
        toRead = nDebugEntries * consts.SIZEOF_IMAGE_DEBUG_ENTRY32
        if dataLength >= toRead:
            for i in range(nDebugEntries):
                dbgEntry = ImageDebugDirectory.parse(readDataInstance)
                dbgEntries.append(dbgEntry)
        else:
            raise excep.DataLengthException("Not enough bytes to read.")

        return dbgEntries

Response: Parses the contents of a L { ImageDebugDirectory } object.


Instruction:
Can you tell what is the following Python 3 function doing
def parse(readDataInstance):
        iid = ImageImportDescriptorEntry()
        iid.originalFirstThunk.value = readDataInstance.readDword()
        iid.timeDateStamp.value = readDataInstance.readDword()
        iid.forwarderChain.value = readDataInstance.readDword()
        iid.name.value = readDataInstance.readDword()
        iid.firstThunk.value = readDataInstance.readDword()
        return iid

Response: Parses the contents of the given object and returns a new object.


Instruction:
How would you implement a function in Python 3 that
parses the contents of a C { readDataInstance} object and returns a new object with the contents.

Response:
def parse(readDataInstance,  nEntries):
        """
        Returns a new L{ImageImportDescriptor} object.

        @type readDataInstance: L{ReadData}
        @param readDataInstance: A L{ReadData} object with data to be parsed as a L{ImageImportDescriptor} object.

        @type nEntries: int
        @param nEntries: The number of L{ImageImportDescriptorEntry} objects in the C{readDataInstance} object.

        @rtype: L{ImageImportDescriptor}
        @return: A new L{ImageImportDescriptor} object.

        @raise DataLengthException: If not enough data to read.
        """
        importEntries = ImageImportDescriptor()

        dataLength = len(readDataInstance)
        toRead = nEntries * consts.SIZEOF_IMAGE_IMPORT_ENTRY32
        if dataLength >= toRead:
            for i in range(nEntries):
                importEntry = ImageImportDescriptorEntry.parse(readDataInstance)
                importEntries.append(importEntry)
        else:
            raise excep.DataLengthException("Not enough bytes to read.")

        return importEntries


Instruction:
Can you create a Python 3 function that
parses the contents of the n - tuple in a L{ExportTableEntry } object.

Response:
def parse(readDataInstance):
        """
        Returns a new L{ExportTableEntry} object.

        @type readDataInstance: L{ReadData}
        @param readDataInstance: A L{ReadData} object with data to be parsed as a L{ExportTableEntry} object.

        @rtype: L{ExportTableEntry}
        @return: A new L{ExportTableEntry} object.
        """
        exportEntry = ExportTableEntry()

        exportEntry.functionRva.value = readDataInstance.readDword()
        exportEntry.nameOrdinal.value = readDataInstance.readWord()
        exportEntry.nameRva.value = readDataInstance.readDword()
        exportEntry.name.value = readDataInstance.readString()
        return exportEntry


Instruction:
Write a Python 3 script for
parsing the contents of the given object and returns a new object.

Response:
def parse(readDataInstance):
        """
        Returns a new L{ImageExportTable} object.

        @type readDataInstance: L{ReadData}
        @param readDataInstance: A L{ReadData} object with data to be parsed as a L{ImageExportTable} object.

        @rtype: L{ImageExportTable}
        @return: A new L{ImageExportTable} object.
        """
        et = ImageExportTable()

        et.characteristics.value = readDataInstance.readDword()
        et.timeDateStamp.value = readDataInstance.readDword()
        et.majorVersion.value = readDataInstance.readWord()
        et.minorVersion.value = readDataInstance.readWord()
        et.name.value = readDataInstance.readDword()
        et.base.value = readDataInstance.readDword()
        et.numberOfFunctions.value = readDataInstance.readDword()
        et.numberOfNames.value = readDataInstance.readDword()
        et.addressOfFunctions.value = readDataInstance.readDword()
        et.addressOfNames.value = readDataInstance.readDword()
        et.addressOfNameOrdinals.value = readDataInstance.readDword()
        return et


Instruction:
Given the following Python 3 function, write the documentation
def parse(readDataInstance):
        nd = NETDirectory()

        nd.directory = NetDirectory.parse(readDataInstance)
        nd.netMetaDataHeader = NetMetaDataHeader.parse(readDataInstance)
        nd.netMetaDataStreams = NetMetaDataStreams.parse(readDataInstance)
        return nd

Response: Parses the specified data instance into a new object.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def parse(readDataInstance):
        nd = NetDirectory()

        nd.cb.value = readDataInstance.readDword()
        nd.majorRuntimeVersion.value= readDataInstance.readWord()
        nd.minorRuntimeVersion.value = readDataInstance.readWord()

        nd.metaData.rva.value = readDataInstance.readDword()
        nd.metaData.size.value = readDataInstance.readDword()
        nd.metaData.name.value = "MetaData"

        nd.flags.value = readDataInstance.readDword()
        nd.entryPointToken.value = readDataInstance.readDword()

        nd.resources.rva.value = readDataInstance.readDword()
        nd.resources.size.value = readDataInstance.readDword()
        nd.resources.name.value = "Resources"

        nd.strongNameSignature.rva.value = readDataInstance.readDword()
        nd.strongNameSignature.size.value = readDataInstance.readDword()
        nd.strongNameSignature.name.value = "StrongNameSignature"

        nd.codeManagerTable.rva.value = readDataInstance.readDword()
        nd.codeManagerTable.size.value = readDataInstance.readDword()
        nd.codeManagerTable.name.value = "CodeManagerTable"

        nd.vTableFixups.rva.value = readDataInstance.readDword()
        nd.vTableFixups.size.value = readDataInstance.readDword()
        nd.vTableFixups.name.value = "VTableFixups"

        nd.exportAddressTableJumps.rva.value = readDataInstance.readDword()
        nd.exportAddressTableJumps.size.value = readDataInstance.readDword()
        nd.exportAddressTableJumps.name.value = "ExportAddressTableJumps"

        nd.managedNativeHeader.rva.value = readDataInstance.readDword()
        nd.managedNativeHeader.size.value = readDataInstance.readDword()
        nd.managedNativeHeader.name.value = "ManagedNativeHeader"

        return nd

Response: Parses the contents of a single entry point into a new object.


Instruction:
Write a Python 3 function that can
parse the contents of the n - tuple containing the key - value pairs of the key - value pairs and returns a new object.

Response:
def parse(readDataInstance):
        """
        Returns a new L{NetMetaDataHeader} object.

        @type readDataInstance: L{ReadData}
        @param readDataInstance: A L{ReadData} object with data to be parsed as a L{NetMetaDataHeader} object.

        @rtype: L{NetMetaDataHeader}
        @return: A new L{NetMetaDataHeader} object.
        """
        nmh = NetMetaDataHeader()

        nmh.signature.value = readDataInstance.readDword()
        nmh.majorVersion.value = readDataInstance.readWord()
        nmh.minorVersion.value = readDataInstance.readWord()
        nmh.reserved.value = readDataInstance.readDword()
        nmh.versionLength.value = readDataInstance.readDword()
        nmh.versionString.value = readDataInstance.readAlignedString()
        nmh.flags.value = readDataInstance.readWord()
        nmh.numberOfStreams.value = readDataInstance.readWord()
        return nmh


Instruction:
Can you generate a brief explanation for the following Python 3 code
def parse(readDataInstance):
        n = NetMetaDataStreamEntry()
        n.offset.value = readDataInstance.readDword()
        n.size.value = readDataInstance.readDword()
        n.name.value = readDataInstance.readAlignedString()
        return n

Response: Parses the n - tuple of the data contained in readDataInstance and returns a new object with the contents of the n - tuple.


Instruction:
Can you write a function in Python 3 where it
parses the n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n - bit n

Response:
def parse(readDataInstance,  nStreams):
        """
        Returns a new L{NetMetaDataStreams} object.

        @type readDataInstance: L{ReadData}
        @param readDataInstance: A L{ReadData} object with data to be parsed as a L{NetMetaDataStreams} object.

        @type nStreams: int
        @param nStreams: The number of L{NetMetaDataStreamEntry} objects in the C{readDataInstance} object.

        @rtype: L{NetMetaDataStreams}
        @return: A new L{NetMetaDataStreams} object.
        """
        streams = NetMetaDataStreams()

        for i in range(nStreams):
            streamEntry = NetMetaDataStreamEntry()

            streamEntry.offset.value = readDataInstance.readDword()
            streamEntry.size.value = readDataInstance.readDword()
            streamEntry.name.value = readDataInstance.readAlignedString()

            #streams.append(streamEntry)
            streams.update({ i: streamEntry, streamEntry.name.value: streamEntry })

        return streams


Instruction:
Create a Python 3 function for
parsing the object containing the n - tuple of keys and returns a new object.

Response:
def parse(readDataInstance):
        """
        Returns a new L{NetMetaDataTableHeader} object.

        @type readDataInstance: L{ReadData}
        @param readDataInstance: A L{ReadData} object with data to be parsed as a L{NetMetaDataTableHeader} object.

        @rtype: L{NetMetaDataTableHeader}
        @return: A new L{NetMetaDataTableHeader} object.
        """
        th = NetMetaDataTableHeader()

        th.reserved_1.value = readDataInstance.readDword()
        th.majorVersion.value = readDataInstance.readByte()
        th.minorVersion.value = readDataInstance.readByte()
        th.heapOffsetSizes.value = readDataInstance.readByte()
        th.reserved_2.value = readDataInstance.readByte()
        th.maskValid.value = readDataInstance.readQword()
        th.maskSorted.value = readDataInstance.readQword()

        return th


Instruction:
Write a Python 3 script for
parsing the data from readDataInstance and returns a new object with the parsed data.

Response:
def parse(readDataInstance, netMetaDataStreams):
        """
        Returns a new L{NetMetaDataTables} object.

        @type readDataInstance: L{ReadData}
        @param readDataInstance: A L{ReadData} object with data to be parsed as a L{NetMetaDataTables} object.

        @rtype: L{NetMetaDataTables}
        @return: A new L{NetMetaDataTables} object.
        """
        dt = NetMetaDataTables()
        dt.netMetaDataTableHeader = NetMetaDataTableHeader.parse(readDataInstance)
        dt.tables = {}

        metadataTableDefinitions = dotnet.MetadataTableDefinitions(dt, netMetaDataStreams)

        for i in xrange(64):
            dt.tables[i] = { "rows": 0 }
            if dt.netMetaDataTableHeader.maskValid.value >> i & 1:
                dt.tables[i]["rows"] = readDataInstance.readDword()
            if i in dotnet.MetadataTableNames:
                dt.tables[dotnet.MetadataTableNames[i]] = dt.tables[i]

        for i in xrange(64):
            dt.tables[i]["data"] = []
            for j in range(dt.tables[i]["rows"]):
                row = None
                if i in metadataTableDefinitions:
                    row = readDataInstance.readFields(metadataTableDefinitions[i])
                dt.tables[i]["data"].append(row)

        for i in xrange(64):
            if i in dotnet.MetadataTableNames:
                dt.tables[dotnet.MetadataTableNames[i]] = dt.tables[i]["data"]
            dt.tables[i] = dt.tables[i]["data"]

        return dt


Instruction:
Create a Python 3 function to
parse the contents of a NetResources object.

Response:
def parse(readDataInstance):
        """
        Returns a new L{NetResources} object.

        @type readDataInstance: L{ReadData}
        @param readDataInstance: A L{ReadData} object with data to be parsed as a L{NetResources} object.

        @rtype: L{NetResources}
        @return: A new L{NetResources} object.
        """
        r = NetResources()

        r.signature = readDataInstance.readDword()
        if r.signature != 0xbeefcace:
            return r

        r.readerCount = readDataInstance.readDword()
        r.readerTypeLength = readDataInstance.readDword()
        r.readerType = utils.ReadData(readDataInstance.read(r.readerTypeLength)).readDotNetBlob()
        r.version = readDataInstance.readDword()
        r.resourceCount = readDataInstance.readDword()
        r.resourceTypeCount = readDataInstance.readDword()

        r.resourceTypes = []
        for i in xrange(r.resourceTypeCount):
            r.resourceTypes.append(readDataInstance.readDotNetBlob())

        # aligned to 8 bytes
        readDataInstance.skipBytes(8 - readDataInstance.tell() & 0x7)

        r.resourceHashes = []
        for i in xrange(r.resourceCount):
            r.resourceHashes.append(readDataInstance.readDword())

        r.resourceNameOffsets = []
        for i in xrange(r.resourceCount):
            r.resourceNameOffsets.append(readDataInstance.readDword())

        r.dataSectionOffset = readDataInstance.readDword()

        r.resourceNames = []
        r.resourceOffsets = []
        base = readDataInstance.tell()
        for i in xrange(r.resourceCount):
            readDataInstance.setOffset(base + r.resourceNameOffsets[i])
            r.resourceNames.append(readDataInstance.readDotNetUnicodeString())
            r.resourceOffsets.append(readDataInstance.readDword())

        r.info = {}
        for i in xrange(r.resourceCount):
            readDataInstance.setOffset(r.dataSectionOffset + r.resourceOffsets[i])
            r.info[i] = readDataInstance.read(len(readDataInstance))
            r.info[r.resourceNames[i]] = r.info[i]

        return r


Instruction:
Given the following Python 3 function, write the documentation
def verify_and_fill_address_paths_from_bip32key(address_paths, master_key, network):
    '''
    Take address paths and verifies their accuracy client-side.

    Also fills in all the available metadata (WIF, public key, etc)
    '''

    assert network, network

    wallet_obj = Wallet.deserialize(master_key, network=network)

    address_paths_cleaned = []

    for address_path in address_paths:
        path = address_path['path']
        input_address = address_path['address']
        child_wallet = wallet_obj.get_child_for_path(path)

        if child_wallet.to_address() != input_address:
            err_msg = 'Client Side Verification Fail for %s on %s:\n%s != %s' % (
                    path,
                    master_key,
                    child_wallet.to_address(),
                    input_address,
                    )
            raise Exception(err_msg)

        pubkeyhex = child_wallet.get_public_key_hex(compressed=True)

        server_pubkeyhex = address_path.get('public')
        if server_pubkeyhex and server_pubkeyhex != pubkeyhex:
            err_msg = 'Client Side Verification Fail for %s on %s:\n%s != %s' % (
                    path,
                    master_key,
                    pubkeyhex,
                    server_pubkeyhex,
                    )
            raise Exception(err_msg)

        address_path_cleaned = {
            'pub_address': input_address,
            'path': path,
            'pubkeyhex': pubkeyhex,
            }

        if child_wallet.private_key:
            privkeyhex = child_wallet.get_private_key_hex()
            address_path_cleaned['wif'] = child_wallet.export_to_wif()
            address_path_cleaned['privkeyhex'] = privkeyhex
        address_paths_cleaned.append(address_path_cleaned)

    return address_paths_cleaned

Response: Takes a list of address paths and verifies their accuracy client - side.


Instruction:
Explain what the following Python 3 code does
def solution_path(self):
        '''Follows the solution path of the generalized lasso to find the best lambda value.'''
        lambda_grid = np.exp(np.linspace(np.log(self.max_lambda), np.log(self.min_lambda), self.lambda_bins))
        aic_trace = np.zeros((len(self.bins),lambda_grid.shape[0])) # The AIC score for each lambda value
        aicc_trace = np.zeros((len(self.bins),lambda_grid.shape[0])) # The AICc score for each lambda value (correcting for finite sample size)
        bic_trace = np.zeros((len(self.bins),lambda_grid.shape[0])) # The BIC score for each lambda value
        dof_trace = np.zeros((len(self.bins),lambda_grid.shape[0])) # The degrees of freedom of each final solution
        log_likelihood_trace = np.zeros((len(self.bins),lambda_grid.shape[0]))
        bic_best_idx = [None for _ in self.bins]
        aic_best_idx = [None for _ in self.bins]
        aicc_best_idx = [None for _ in self.bins]
        bic_best_betas = [None for _ in self.bins]
        aic_best_betas = [None for _ in self.bins]
        aicc_best_betas = [None for _ in self.bins]
        if self.k == 0 and self.trails is not None:
            betas = [np.zeros(self.num_nodes, dtype='double') for _ in self.bins]
            zs = [np.zeros(self.breakpoints[-1], dtype='double') for _ in self.bins]
            us = [np.zeros(self.breakpoints[-1], dtype='double') for _ in self.bins]
        else:
            betas = [np.zeros(self.num_nodes, dtype='double') for _ in self.bins]
            us = [np.zeros(self.Dk.shape[0], dtype='double') for _ in self.bins]
        for i, _lambda in enumerate(lambda_grid):
            if self.verbose:
                print('\n#{0} Lambda = {1}'.format(i, _lambda))

            # Run the graph fused lasso over each bin with the current lambda value
            initial_values = (betas, zs, us) if self.k == 0 and self.trails is not None else (betas, us)
            self.run(_lambda, initial_values=initial_values)

            if self.verbose > 1:
                print('\tCalculating degrees of freedom and information criteria')

            for b, beta in enumerate(betas):
                if self.bins_allowed is not None and b not in self.bins_allowed:
                    continue

                # Count the number of free parameters in the grid (dof)
                # TODO: this is not really the true DoF, since a change in a higher node multiplies
                # the DoF in the lower nodes
                # dof_trace[b,i] = len(self.calc_plateaus(beta))
                dof_vals = self.Dk_minus_one.dot(beta) if self.k > 0 else beta
                plateaus = calc_plateaus(dof_vals, self.edges, rel_tol=0.01) if (self.k % 2) == 0 else nearly_unique(dof_vals, rel_tol=0.03)
                #plateaus = calc_plateaus(dof_vals, self.edges, rel_tol=1e-5) if (self.k % 2) == 0 else nearly_unique(dof_vals, rel_tol=1e-5)
                dof_trace[b,i] = max(1,len(plateaus)) #* (k+1)

                # Get the negative log-likelihood
                log_likelihood_trace[b,i] = self.data_log_likelihood(self.bins[b][-1], self.bins[b][-2], beta)

                # Calculate AIC = 2k - 2ln(L)
                aic_trace[b,i] = 2. * dof_trace[b,i] - 2. * log_likelihood_trace[b,i]

                # Calculate AICc = AIC + 2k * (k+1) / (n - k - 1)
                aicc_trace[b,i] = aic_trace[b,i] + 2 * dof_trace[b,i] * (dof_trace[b,i]+1) / (self.num_nodes - dof_trace[b,i] - 1.)

                # Calculate BIC = -2ln(L) + k * (ln(n) - ln(2pi))
                bic_trace[b,i] = -2 * log_likelihood_trace[b,i] + dof_trace[b,i] * (np.log(self.num_nodes) - np.log(2 * np.pi))

                # Track the best model thus far
                if aic_best_idx[b] is None or aic_trace[b,i] < aic_trace[b,aic_best_idx[b]]:
                    aic_best_idx[b] = i
                    aic_best_betas[b] = np.array(beta)

                # Track the best model thus far
                if aicc_best_idx[b] is None or aicc_trace[b,i] < aicc_trace[b,aicc_best_idx[b]]:
                    aicc_best_idx[b] = i
                    aicc_best_betas[b] = np.array(beta)

                # Track the best model thus far
                if bic_best_idx[b] is None or bic_trace[b,i] < bic_trace[b,bic_best_idx[b]]:
                    bic_best_idx[b] = i
                    bic_best_betas[b] = np.array(beta)

                if self.verbose and self.bins_allowed is not None:
                    print('\tBin {0} Log-Likelihood: {1} DoF: {2} AIC: {3} AICc: {4} BIC: {5}'.format(b, log_likelihood_trace[b,i], dof_trace[b,i], aic_trace[b,i], aicc_trace[b,i], bic_trace[b,i]))

            if self.verbose and self.bins_allowed is None:
                print('Overall Log-Likelihood: {0} DoF: {1} AIC: {2} AICc: {3} BIC: {4}'.format(log_likelihood_trace[:,i].sum(), dof_trace[:,i].sum(), aic_trace[:,i].sum(), aicc_trace[:,i].sum(), bic_trace[:,i].sum()))

        if self.verbose:
            print('')
            print('Best settings per bin:')
            for b, (aic_idx, aicc_idx, bic_idx) in enumerate(zip(aic_best_idx, aicc_best_idx, bic_best_idx)):
                if self.bins_allowed is not None and b not in self.bins_allowed:
                    continue
                left, mid, right, trials, successes = self.bins[b]
                print('\tBin #{0} ([{1}, {2}], split={3}) lambda: AIC={4:.2f} AICC={5:.2f} BIC={6:.2f} DoF: AIC={7:.0f} AICC={8:.0f} BIC={9:.0f}'.format(
                        b, left, right, mid,
                        lambda_grid[aic_idx], lambda_grid[aicc_idx], lambda_grid[bic_idx],
                        dof_trace[b,aic_idx], dof_trace[b,aicc_idx], dof_trace[b,bic_idx]))
            print('')

        if self.bins_allowed is None:
            if self.verbose:
                print('Creating densities from betas...')
            bic_density = self.density_from_betas(bic_best_betas)
            aic_density = self.density_from_betas(aic_best_betas)
            aicc_density = self.density_from_betas(aicc_best_betas)
            self.map_density = bic_density
        else:
            aic_density, aicc_density, bic_density = None, None, None

        self.map_betas = bic_best_betas

        return {'aic': aic_trace,
                'aicc': aicc_trace,
                'bic': bic_trace,
                'dof': dof_trace,
                'loglikelihood': log_likelihood_trace,
                'lambdas': lambda_grid,
                'aic_betas': aic_best_betas,
                'aicc_betas': aicc_best_betas,
                'bic_betas': bic_best_betas,
                'aic_best_idx': aic_best_idx,
                'aicc_best_idx': aicc_best_idx,
                'bic_best_idx': bic_best_idx,
                'aic_densities': aic_density.reshape(self.data_shape),
                'aicc_densities': aicc_density.reshape(self.data_shape),
                'bic_densities': bic_density.reshape(self.data_shape)}

Response: Follows the solution path of the generalized lasso to find the best lambda value.


Instruction:
Explain what the following Python 3 code does
def run(self, lam, initial_values=None):
        '''Run the graph-fused logit lasso with a fixed lambda penalty.'''
        if initial_values is not None:
            if self.k == 0 and self.trails is not None:
                betas, zs, us = initial_values
            else:
                betas, us = initial_values
        else:
            if self.k == 0 and self.trails is not None:
                betas = [np.zeros(self.num_nodes, dtype='double') for _ in self.bins]
                zs = [np.zeros(self.breakpoints[-1], dtype='double') for _ in self.bins]
                us = [np.zeros(self.breakpoints[-1], dtype='double') for _ in self.bins]
            else:
                betas = [np.zeros(self.num_nodes, dtype='double') for _ in self.bins]
                us = [np.zeros(self.Dk.shape[0], dtype='double') for _ in self.bins]

        for j, (left, mid, right, trials, successes) in enumerate(self.bins):
            if self.bins_allowed is not None and j not in self.bins_allowed:
                continue

            if self.verbose > 2:
                print('\tBin #{0} [{1},{2},{3}]'.format(j, left, mid, right))
            # if self.verbose > 3:
            #     print 'Trials:\n{0}'.format(pretty_str(trials))
            #     print ''
            #     print 'Successes:\n{0}'.format(pretty_str(successes))

            beta = betas[j]
            u = us[j]

            if self.k == 0 and self.trails is not None:
                z = zs[j]
                # Run the graph-fused lasso algorithm
                self.graphfl(len(beta), trials, successes,
                             self.ntrails, self.trails, self.breakpoints,
                             lam, self.alpha, self.inflate,
                             self.max_steps, self.converge,
                             beta, z, u)
            else:
                # Run the graph trend filtering algorithm
                self.graphtf(len(beta), trials, successes, lam,
                                 self.Dk.shape[0], self.Dk.shape[1], self.Dk.nnz,
                                 self.Dk.row.astype('int32'), self.Dk.col.astype('int32'), self.Dk.data.astype('double'),
                                 self.max_steps, self.converge,
                                 beta, u)
                beta = np.clip(beta, 1e-12, 1-1e-12) # numerical stability
                betas[j] = -np.log(1./beta - 1.) # convert back to natural parameter form

        return (betas, zs, us) if self.k == 0 and self.trails is not None else (betas, us)

Response: Run the graph - fused logit lasso with a fixed lambda penalty.


Instruction:
Make a summary of the following Python 3 code
def data_log_likelihood(self, successes, trials, beta):
        '''Calculates the log-likelihood of a Polya tree bin given the beta values.'''
        return binom.logpmf(successes, trials, 1.0 / (1 + np.exp(-beta))).sum()

Response: Calculates the log - likelihood of a Polya tree bin given the beta values.


Instruction:
Write a Python 3 script for
running the nose test

Response:
def run_nose(self, params):
        """
        :type params: Params
        """
        thread.set_index(params.thread_index)
        log.debug("[%s] Starting nose iterations: %s", params.worker_index, params)
        assert isinstance(params.tests, list)
        # argv.extend(['--with-apiritif', '--nocapture', '--exe', '--nologcapture'])

        end_time = self.params.ramp_up + self.params.hold_for
        end_time += time.time() if end_time else 0
        time.sleep(params.delay)

        plugin = ApiritifPlugin(self._writer)
        self._writer.concurrency += 1

        config = Config(env=os.environ, files=all_config_files(), plugins=DefaultPluginManager())
        config.plugins.addPlugins(extraplugins=[plugin])
        config.testNames = params.tests
        config.verbosity = 3 if params.verbose else 0
        if params.verbose:
            config.stream = open(os.devnull, "w")  # FIXME: use "with", allow writing to file/log

        iteration = 0
        try:
            while True:
                log.debug("Starting iteration:: index=%d,start_time=%.3f", iteration, time.time())
                thread.set_iteration(iteration)
                ApiritifTestProgram(config=config)
                log.debug("Finishing iteration:: index=%d,end_time=%.3f", iteration, time.time())

                iteration += 1

                # reasons to stop
                if plugin.stop_reason:
                    log.debug("[%s] finished prematurely: %s", params.worker_index, plugin.stop_reason)
                elif iteration >= params.iterations:
                    log.debug("[%s] iteration limit reached: %s", params.worker_index, params.iterations)
                elif 0 < end_time <= time.time():
                    log.debug("[%s] duration limit reached: %s", params.worker_index, params.hold_for)
                else:
                    continue  # continue if no one is faced

                break
        finally:
            self._writer.concurrency -= 1

            if params.verbose:
                config.stream.close()


Instruction:
Given the following Python 3 function, write the documentation
def _write_single_sample(self, sample):
        bytes = sample.extras.get("responseHeadersSize", 0) + 2 + sample.extras.get("responseBodySize", 0)

        message = sample.error_msg
        if not message:
            message = sample.extras.get("responseMessage")
        if not message:
            for sample in sample.subsamples:
                if sample.error_msg:
                    message = sample.error_msg
                    break
                elif sample.extras.get("responseMessage"):
                    message = sample.extras.get("responseMessage")
                    break
        self.writer.writerow({
            "timeStamp": int(1000 * sample.start_time),
            "elapsed": int(1000 * sample.duration),
            "Latency": 0,  # TODO
            "label": sample.test_case,

            "bytes": bytes,

            "responseCode": sample.extras.get("responseCode"),
            "responseMessage": message,
            "allThreads": self.concurrency,  # TODO: there will be a problem aggregating concurrency for rare samples
            "success": "true" if sample.status == "PASSED" else "false",
        })
        self.out_stream.flush()

Response: Writes a single sample to the output stream.


Instruction:
Make a summary of the following Python 3 code
def addError(self, test, error):
        # test_dict will be None if startTest wasn't called (i.e. exception in setUp/setUpClass)
        # status=BROKEN
        if self.current_sample is not None:
            assertion_name = error[0].__name__
            error_msg = str(error[1]).split('\n')[0]
            error_trace = self._get_trace(error)
            self.current_sample.add_assertion(assertion_name)
            self.current_sample.set_assertion_failed(assertion_name, error_msg, error_trace)

Response: addError - Called by the test when a test raises an uncaught exception


Instruction:
Given the following Python 3 function, write the documentation
def parse(readDataInstance):
        d = Directory()
        d.rva.value = readDataInstance.readDword()
        d.size.value = readDataInstance.readDword()
        return d

Response: Parses a NDEF - like object.


Instruction:
Explain what the following Python 3 code does
def parse(readDataInstance):
        if len(readDataInstance) == consts.IMAGE_NUMBEROF_DIRECTORY_ENTRIES * 8:
            newDataDirectory = DataDirectory()
            for i in range(consts.IMAGE_NUMBEROF_DIRECTORY_ENTRIES):
                newDataDirectory[i].name.value = dirs[i]
                newDataDirectory[i].rva.value = readDataInstance.readDword()
                newDataDirectory[i].size.value = readDataInstance.readDword()
        else:
            raise excep.DirectoryEntriesLengthException("The IMAGE_NUMBEROF_DIRECTORY_ENTRIES does not match with the length of the passed argument.")
        return newDataDirectory

Response: Parses the IMAGE_NUMBEROF_DIRECTORY_ENTRIES and returns a DataDirectory object containing the names rva and size.