Instruction:
Can you create a Python 3 function that
finds the VERSION_FILE and return its contents.

Response:
def get_version_from_file(path):
    """Find the VERSION_FILE and return its contents.

    Returns
    -------
    version : string or None

    """
    filename = os.path.join(path, VERSION_FILE)
    if not os.path.isfile(filename):
        # Look in the parent directory of path instead.
        filename = os.path.join(os.path.dirname(path), VERSION_FILE)
        if not os.path.isfile(filename):
            filename = ''

    if filename:
        with open(filename) as fh:
            version = fh.readline().strip()
            if version:
                return version


Instruction:
Write a Python 3 function for
normalising a version string according to PEP 440 if possible.

Response:
def normalised(version):
    """Normalise a version string according to PEP 440, if possible."""
    norm_version = pkg_resources.parse_version(version)
    if not isinstance(norm_version, tuple):
        # Let setuptools (>= 8) do the normalisation
        return str(norm_version)
    else:
        # Homegrown normalisation for older setuptools (< 8)
        public, sep, local = version.lower().partition('+')
        # Remove leading 'v' from public version
        if len(public) >= 2:
            if public[0] == 'v' and public[1] in '0123456789':
                public = public[1:]
        # Turn all chars except alphanumerics into periods in local version
        local = NON_ALPHANUMERIC.sub('.', local)
        return public + sep + local


Instruction:
Given the following Python 3 function, write the documentation
def get_version(path=None, module=None):
    # Check the module option first.
    version = get_version_from_module(module)
    if version:
        return normalised(version)

    # Turn path into a valid directory (default is current directory)
    if path is None:
        path = os.getcwd()
    path = os.path.abspath(path)
    if os.path.exists(path) and not os.path.isdir(path):
        path = os.path.dirname(path)
    if not os.path.isdir(path):
        raise ValueError('No such package source directory: %r' % (path,))

    # Check for an sdist in the process of being installed by pip.
    version = get_version_from_unpacked_sdist(path)
    if version:
        return normalised(version)

    # Check the SCM.
    scm, version = get_version_from_scm(path)
    if version:
        return normalised(version)

    # Check if there is a katversion file in the given path.
    version = get_version_from_file(path)
    if version:
        return normalised(version)

    # None of the above got a version so we will make one up based on the date.
    return normalised(date_version(scm))

Response: Returns the version string of the current version of the current version of the current version of the current version.


Instruction:
Here you have a function in Python 3, explain what it does
def _sane_version_list(version):
    v0 = str(version[0])
    if v0:
        # Test if the major is a number.
        try:
            v0 = v0.lstrip("v").lstrip("V")
            # Handle the common case where tags have v before major.
            v0 = int(v0)
        except ValueError:
            v0 = None

    if v0 is None:
        version = [0, 0] + version
    else:
        version[0] = v0

    try:
        # Test if the minor is a number.
        version[1] = int(version[1])
    except ValueError:
        # Insert Minor 0.
        version = [version[0], 0] + version[1:]

    return version

Response: Ensure the major and minor numbers are int.


Instruction:
Explain what the following Python 3 code does
def get_version_list(path=None, module=None):
    major = 0
    minor = 0
    patch = ''  # PEP440 calls this prerelease, postrelease or devrelease
    ver = get_version(path, module)
    if ver is not None:
        ver_segments = _sane_version_list(ver.split(".", 2))
        major = ver_segments[0]
        minor = ver_segments[1]
        patch = ".".join(ver_segments[2:])  # Rejoin the .

    # Return None as first field, makes substitution easier in next step.
    return [None, major, minor, patch]

Response: Return the version information as a tuple.


Instruction:
Can you create a Python 3 function that
returns the build info tuple.

Response:
def build_info(name, path=None, module=None):
    """Return the build info tuple."""
    verlist = get_version_list(path, module)
    verlist[0] = name
    return tuple(verlist)


Instruction:
Create a Python 3 function for
finding all of the indices of particles of groupid

Response:
def find(self, groupid):
        """ return all of the indices of particles of groupid """
        return self.indices[self.offset[groupid]
                :self.offset[groupid]+ self.length[groupid]]


Instruction:
How would you explain what the following Python 3 function does
def sum(self, weights=None):
        if weights is None:
            weights = self.data.weights
        return utils.bincount(self.labels, weights, self.N)

Response: return the sum of weights of each object


Instruction:
Can you create a Python 3 function that
returns the center of each object

Response:
def center(self, weights=None):
        """ return the center of each object """
        if weights is None:
            weights = self.data.weights
        mass = utils.bincount(self.labels, weights, self.N)
        cp = numpy.empty((len(mass), self.data.pos.shape[-1]), 'f8')
        for d in range(self.data.pos.shape[-1]):
            cp[..., d] = utils.bincount(self.labels, weights *
                    self.data.pos[..., d], self.N)
            cp[..., d] /= mass
        return cp


Instruction:
Given the following Python 3 function, write the documentation
def getParamValues(self, paramName=None, paramId=None, useOnlyValids=True):

        if not paramName is None:
            if not paramId is None:
                if getParameterTypeNameFromID(paramId) != paramName:
                    raise ValueError("Parameters paramId and paramName "
                                    + "passed to ParamSample.getParamValues() are incompatible.")
        else:
            if paramId is None:
                raise ValueError("At least one of the attribute paramName and paramId "
                                    + "passed to ParamSample.getParamValues() most not be None.")
            paramName = getParameterTypeNameFromID(paramId)

        df = self.sampleDF
        if useOnlyValids:
            df = df[df["isValid"] == True]

        df.loc[:, "paramNames"] = [getParameterTypeNameFromID(param.typeId) for param in df["obj_parameter"]]

        return df[df["paramNames"] == paramName]

Response: Return the rows of sampleDF that are associated to the parameter paramName.


Instruction:
Make a summary of the following Python 3 code
def interpolate(self, interpValues):
        self.__operations.append(["interpolate", interpValues])

        df = self.sampleDF
        self.interpValues = interpValues
        for interParamName, value  in interpValues.items():
            self.__report += "Interpolation of the parameters for independent variables '" \
                             + interParamName + "' at value " + str(value) + ".\n"
            for ind, (paramTrace, resType) in enumerate(zip(df["obj_parameter"], df["Result type"])):
                if resType == "numericalTrace" and interParamName in paramTrace.indepNames:
                    val = paramTrace.getInterp1dValues(value, interParamName, statsToReturn=["mean"])
                    if isinstance(val, list):
                        if len(val) == 1:
                            val = val[0]
                        else:
                            raise ValueError("This case has not been implemented yet.")
                    df.loc[ind, "Values"] = float(val)

Response: This method is called by the interpolation functions in the base class to interpolate the parameters of the independant variables.


Instruction:
Create a Python 3 function for
comparing two Psisloo objects using pointwise approximate leave - one - out cross validation.

Response:
def loo_compare(psisloo1, psisloo2):
    """
    Compares two models using pointwise approximate leave-one-out cross validation.

    For the method to be valid, the two models should have been fit on the same input data.

    Parameters
    -------------------
    psisloo1 : Psisloo object for model1
    psisloo2 : Psisloo object for model2

    Returns
    -------------------
    Dict with two values:

        diff: difference in elpd (estimated log predictive density)
                between two models, where a positive value indicates
                that model2 is a better fit than model1.

        se_diff: estimated standard error of the difference
                between model2 & model1.

    """
    ## TODO: confirm that dimensions for psisloo1 & psisloo2 are the same
    loores = psisloo1.pointwise.join(
        psisloo2.pointwise,
        lsuffix = '_m1',
        rsuffix = '_m2')

    loores['pw_diff'] = loores.pointwise_elpd_m2 - loores.pointwise_elpd_m1

    sum_elpd_diff = loores.apply(numpy.sum).pw_diff
    sd_elpd_diff = loores.apply(numpy.std).pw_diff

    elpd_diff = {
        'diff' : sum_elpd_diff,
        'se_diff' : math.sqrt(len(loores.pw_diff)) * sd_elpd_diff
        }

    return elpd_diff


Instruction:
Write a Python 3 script to
plot the pointwise importance - sampling indices of the observation units.

Response:
def plot(self):
        """ Graphical summary of pointwise pareto-k importance-sampling indices

        Pareto-k tail indices are plotted (on the y axis) for each observation unit (on the x axis)

        """
        seaborn.pointplot(
            y = self.pointwise.pareto_k,
            x = self.pointwise.index,
            join = False)


Instruction:
Implement a Python 3 function for
adding a new layer to the stack

Response:
def add_layer(self, formula='', thickness=np.NaN, density=np.NaN):
        """provide another way to define the layers (stack)

        Parameters:
        ===========
        formula: string
           ex: 'CoAg2'
           ex: 'Al'
        thickness: float (in mm)
        density: float (g/cm3)
        """
        if formula == '':
            return

        _new_stack = _utilities.formula_to_dictionary(formula=formula,
                                                      thickness=thickness,
                                                      density=density,
                                                      database=self.database)
        # check if density has been defined
        self.__lock_density_if_defined(stack=_new_stack)

        new_stack = self.__update_stack_with_isotopes_infos(stack=_new_stack)
        self.stack = {**self.stack, **new_stack}

        # calculate stack_sigma, layer density, atoms_per_cm3 ...
        self.__math_on_stack()


Instruction:
Can you tell what is the following Python 3 function doing
def get_isotopic_ratio(self, compound='', element=''):
        _stack = self.stack
        compound = str(compound)

        if compound == '':
            _list_compounds = _stack.keys()
            list_all_dict = {}
            for _compound in _list_compounds:
                _compound = str(_compound)
                _list_element = _stack[_compound]['elements']
                list_all_dict[_compound] = {}
                for _element in _list_element:
                    list_all_dict[_compound][_element] = self.get_isotopic_ratio(
                        compound=_compound,
                        element=_element)
            return list_all_dict

        # checking compound is valid
        list_compounds = _stack.keys()
        if compound not in list_compounds:
            list_compounds_joined = ', '.join(list_compounds)
            raise ValueError("Compound '{}' could not be find in {}".format(compound, list_compounds_joined))

        # checking element is valid
        if element == '':
            # we assume that the element and compounds names matched
            element = compound
        list_element = _stack[compound].keys()
        if element not in list_element:
            list_element_joined = ', '.join(list_element)
            raise ValueError("Element '{}' should be any of those elements: {}".format(element, list_element_joined))

        list_istopes = _stack[compound][element]['isotopes']['list']
        list_ratio = _stack[compound][element]['isotopes']['isotopic_ratio']
        iso_ratio = zip(list_istopes, list_ratio)

        _stoichiometric_ratio = {}
        for _iso, _ratio in iso_ratio:
            _stoichiometric_ratio[_iso] = _ratio

        return _stoichiometric_ratio

Response: returns the list of isotopes for the element of the compound defined with their stoichiometric values


Instruction:
Can you generate the documentation for the following Python 3 function
def set_isotopic_ratio(self, compound='', element='', list_ratio=[]):
        _stack = self.stack

        list_compounds = _stack.keys()
        if compound not in _stack.keys():
            list_compounds_joined = ', '.join(list_compounds)
            raise ValueError("Compound '{}' could not be find in {}".format(compound, list_compounds_joined))

        if element == '':
            # we assume that the element and compounds names matched
            element = compound
        list_element = _stack[compound].keys()
        if element not in list_element:
            list_element_joined = ', '.join(list_element)
            raise ValueError("Element '{}' should be any of those elements: {}".format(element, list_element_joined))

        old_list_ratio = _stack[compound][element]['isotopes']['list']
        if not (len(old_list_ratio) == len(list_ratio)):
            raise ValueError("New list of ratio ({} elements) does not match old list size ({} elements!".format(len(
                list_ratio), len(old_list_ratio)))

        self.stack[compound][element]['isotopes']['isotopic_ratio'] = list_ratio
        self.__update_molar_mass(compound=compound, element=element)
        self.__update_density(compound=compound, element=element)

        # update entire stack
        self.__math_on_stack()

Response: This function updates the isotopic ratio of the current set of elements in the current set of elements.


Instruction:
Can you tell what is the following Python 3 function doing
def get_density(self, compound='', element=''):
        _stack = self.stack

        if compound == '':
            _list_compounds = _stack.keys()
            list_all_dict = {}
            for _compound in _list_compounds:
                _list_element = _stack[_compound]['elements']
                list_all_dict[_compound] = {}
                for _element in _list_element:
                    list_all_dict[_compound][_element] = self.get_density(
                        compound=_compound,
                        element=_element)
            return list_all_dict

        # checking compound is valid
        list_compounds = _stack.keys()
        if compound not in list_compounds:
            list_compounds_joined = ', '.join(list_compounds)
            raise ValueError("Compound '{}' could not be find in {}".format(compile, list_compounds_joined))

        # checking element is valid
        if element == '':
            # we assume that the element and compounds names matched
            element = compound
        list_element = _stack[compound].keys()
        if element not in list_element:
            list_element_joined = ', '.join(list_element)
            raise ValueError("Element '{}' should be any of those elements: {}".format(element, list_element_joined))

        return _stack[compound][element]['density']['value']

Response: returns the list of isotopes for the element of the compound defined with their density


Instruction:
Can you generate the documentation for the following Python 3 function
def __math_on_stack(self, used_lock=False):

        # populate stack_sigma (Sigma vs Energy for every element)
        self.__get_sigmas()

        # populate compound density (if none provided)
        self.__update_layer_density()

        # populate compound molar mass
        # self.__update_layer_molar_mass()  ### included in __calculate_atoms_per_cm3

        # populate atoms_per_cm3
        self.__calculate_atoms_per_cm3(used_lock=used_lock)

        # calculate transmission and attenuation
        self.__calculate_transmission_attenuation()

Response: This method is called by the module that is going to be run on the stack.


Instruction:
Implement a Python 3 function for
locking the density if the density has been defined during initialization.

Response:
def __lock_density_if_defined(self, stack: dict):
        """lock (True) the density lock if the density has been been defined during initialization
        Store the resulting dictionary into density_lock

        Parameters:
        ===========
        stack: dictionary (optional)
          if not provided, the entire stack will be used
        """

        if self.stack == {}:
            density_lock = {}
        else:
            density_lock = self.density_lock

        for _compound in stack.keys():
            _density = stack[_compound]['density']['value']
            if np.isnan(_density):
                density_lock[_compound] = False
            else:
                density_lock[_compound] = True
        self.density_lock = density_lock


Instruction:
Can you write a function in Python 3 where it
calculates for each element the atoms per cm3

Response:
def __calculate_atoms_per_cm3(self, used_lock=False):
        """calculate for each element, the atoms per cm3"""
        stack = self.stack
        _density_lock = self.density_lock

        for _name_of_compound in stack.keys():
            if used_lock and _density_lock[_name_of_compound]:
                continue
            molar_mass_layer, atoms_per_cm3_layer = _utilities.get_atoms_per_cm3_of_layer(
                compound_dict=stack[_name_of_compound])
            # Update layer molar mass
            stack[_name_of_compound]['molar_mass'] = {'value': molar_mass_layer,
                                                      'units': 'g/mol'}
            # Update atoms per cm3
            stack[_name_of_compound]['atoms_per_cm3'] = atoms_per_cm3_layer
            for _index, _name_of_ele in enumerate(stack[_name_of_compound]['elements']):
                stack[_name_of_compound][_name_of_ele]['atoms_per_cm3'] = atoms_per_cm3_layer * \
                                                                          stack[_name_of_compound][
                                                                              'stoichiometric_ratio'][_index]
        self.stack = stack


Instruction:
How would you explain what the following Python 3 function does
def __update_stack_with_isotopes_infos(self, stack: dict):
        for _key in stack:
            _elements = stack[_key]['elements']
            for _element in _elements:
                _dict = _utilities.get_isotope_dicts(element=_element, database=self.database)
                stack[_key][_element] = _dict

        stack = self.__fill_missing_keys(stack=stack)
        return stack

Response: update the stack with isotopes file names mass and atomic_ratio from each element in stack


Instruction:
Can you tell what is the following Python 3 function doing
def __update_layer_density(self):
        _stack = self.stack
        _density_lock = self.density_lock
        list_compound = _stack.keys()
        for _key in list_compound:
            if _density_lock[_key]:
                continue

            _list_ratio = _stack[_key]['stoichiometric_ratio']
            _list_density = []
            for _element in _stack[_key]['elements']:
                _list_density.append(_stack[_key][_element]['density']['value'])
                _compound_density = _utilities.get_compound_density(list_density=_list_density,
                                                                    list_ratio=_list_ratio)

            _stack[_key]['density']['value'] = _compound_density
        self.stack = _stack

Response: calculate or update the layer density


Instruction:
Can you tell what is the following Python 3 function doing
def __update_molar_mass(self, compound='', element=''):
        _molar_mass_element = 0
        list_ratio = self.stack[compound][element]['isotopes']['isotopic_ratio']
        list_mass = self.stack[compound][element]['isotopes']['mass']['value']
        ratio_mass = zip(list_ratio, list_mass)
        for _ratio, _mass in ratio_mass:
            _molar_mass_element += np.float(_ratio) * np.float(_mass)
        self.stack[compound][element]['molar_mass']['value'] = _molar_mass_element

Response: Update the molar mass of the element given due to stoichiometric changes


Instruction:
Given the following Python 3 function, write the documentation
def plot(self, y_axis='attenuation', x_axis='energy',
             logx=False, logy=False,
             mixed=True, all_layers=False, all_elements=False,
             all_isotopes=False, items_to_plot=None,
             time_unit='us', offset_us=0., source_to_detector_m=16.,
             time_resolution_us=0.16, t_start_us=1,
             plotly=False, ax_mpl=None,
             fmt='-', ms='2', lw='1.5', alpha=1):
        # offset delay values is normal 2.99 us with NONE actual MCP delay settings
        """display the transmission or attenuation of compound, element and/or isotopes specified

        Parameters:
        ===========
        :param x_axis: x type for export. Must be either ['energy'|'lambda'|'time'|'number']
        :type x_axis: str
        :param y_axis: y type for export. Must be either ['transmission'|'attenuation'|'sigma'|'sigma_raw'|'miu_per_cm']
        :type y_axis: str
        :param logx: True -> display x in log scale
        :type logx: boolean.
        :param logy: True -> display y in log scale
        :type logy: boolean.
        :param mixed: boolean. True -> display the total of each layer
                               False -> not displayed
        :param all_layers: boolean. True -> display all layers
                                    False -> not displayed
        :param all_elements: boolean. True -> display all elements signal
                                      False -> not displayed
        :param all_isotopes: boolean. True -> display all isotopes signal
                                      False -> not displayed
        :param items_to_plot: array that describes what to plot
            ex:
                [['CoAg','Ag','107-Ag'], ['CoAg']]
            if the dictionary is empty, everything is exported
        :param time_unit: string. Must be either ['s'|'us'|'ns']
               Note: this will be used only when x_axis='time'
        :param offset_us: default: 0
               Note: only used when x_axis='number' or 'time'
        :param source_to_detector_m: Note: this will be used only when x_axis='number' or 'time'
        :param time_resolution_us: Note: this will be used only when x_axis='number'
        :param t_start_us: when is the first acquisition occurred. default: 1
               Note: this will be used only when x_axis='number'
        :param plotly: control to use plotly to display or not.
        :type plotly: bool
        :param ax_mpl: matplotlib.axes to plot against
        :type ax_mpl: matplotlib.axes
        :param fmt: matplotlib.axes.plot kwargs
        :type fmt: str
        :param ms: matplotlib.axes.plot kwargs
        :type ms: float
        :param lw: matplotlib.axes.plot kwargs
        :type lw: float
        :param alpha: matplotlib.axes.plot kwargs
        :type alpha: float

        """
        if x_axis not in x_type_list:
            raise ValueError("Please specify the x-axis type using one from '{}'.".format(x_type_list))
        if time_unit not in time_unit_list:
            raise ValueError("Please specify the time unit using one from '{}'.".format(time_unit_list))
        if y_axis not in y_type_list:
            raise ValueError("Please specify the y-axis type using one from '{}'.".format(y_type_list))
        # figure size
        # plt.figure(figsize=(8, 8))

        # stack from self
        _stack_signal = self.stack_signal
        _stack = self.stack

        _stack_sigma = self.stack_sigma
        _x_axis = self.total_signal['energy_eV']
        x_axis_label = None

        # Creating the matplotlib graph..
        if ax_mpl is None:
            fig_mpl, ax_mpl = plt.subplots()

        """X-axis"""
        # determine values and labels for x-axis with options from
        # 'energy(eV)' & 'lambda(A)' & 'time(us)' & 'image number(#)'
        if x_axis == 'energy':
            x_axis_label = 'Energy (eV)'
        if x_axis == 'lambda':
            x_axis_label = u"Wavelength (\u212B)"
            _x_axis = _utilities.ev_to_angstroms(array=_x_axis)
        if x_axis == 'time':
            if time_unit == 's':
                x_axis_label = 'Time (s)'
                _x_axis = _utilities.ev_to_s(array=_x_axis,
                                             source_to_detector_m=source_to_detector_m,
                                             offset_us=offset_us)
            if time_unit == 'us':
                x_axis_label = 'Time (us)'
                _x_axis = 1e6 * _utilities.ev_to_s(array=_x_axis,
                                                   source_to_detector_m=source_to_detector_m,
                                                   offset_us=offset_us)
            if time_unit == 'ns':
                x_axis_label = 'Time (ns)'
                _x_axis = 1e9 * _utilities.ev_to_s(array=_x_axis,
                                                   source_to_detector_m=source_to_detector_m,
                                                   offset_us=offset_us)
            print("'{}' was obtained with the following:\nsource_to_detector_m={}\noffset_us={}"
                  .format(x_axis_label, source_to_detector_m, offset_us))

        if x_axis == 'number':
            x_axis_label = 'Image number (#)'
            _x_axis = _utilities.ev_to_image_number(array=_x_axis,
                                                    source_to_detector_m=source_to_detector_m,
                                                    offset_us=offset_us,
                                                    time_resolution_us=time_resolution_us,
                                                    t_start_us=t_start_us)
            print("'{}' was obtained with the following:\nsource_to_detector_m={}\noffset_us={}\ntime_resolution_us={}"
                  .format(x_axis_label, source_to_detector_m, offset_us, time_resolution_us))
        if x_axis_label is None:
            raise ValueError("x_axis_label does NOT exist, please check.")

        """Y-axis"""
        # determine to plot transmission or attenuation
        # determine to put transmission or attenuation words for y-axis
        y_axis_tag = y_axis
        if y_axis == 'transmission':
            y_axis_label = 'Neutron Transmission'
        elif y_axis == 'attenuation':
            y_axis_label = 'Neutron Attenuation'
        elif y_axis == 'sigma':
            y_axis_tag = 'sigma_b'
            y_axis_label = 'Cross-section (barns)'
        elif y_axis == 'sigma_raw':
            y_axis_tag = 'sigma_b_raw'
            y_axis_label = 'Cross-section (barns)'
        else:
            y_axis_tag = 'miu_per_cm'
            y_axis_label = "Attenuation coefficient (cm\u207B\u00B9)"

        if y_axis_tag[:5] == 'sigma':
            mixed = False
            all_layers = False
            print("'y_axis='sigma'' is selected. Auto force 'mixed=False', 'all_layers=False'")
            if y_axis_tag[-3:] == 'raw':
                all_elements = False
                print("'y_axis='sigma_raw'' is selected. Auto force 'all_elements=False'")

        if y_axis_tag == 'miu_per_cm':
            mixed = False
            print("'y_axis='miu_per_cm'' is selected. Auto force 'mixed=False'")

        # Plotting begins
        if mixed:
            _y_axis = self.total_signal[y_axis_tag]
            ax_mpl.plot(_x_axis, _y_axis, fmt, ms=ms, lw=lw, alpha=alpha, label="Total")

        if all_layers:
            for _compound in _stack.keys():
                _y_axis = _stack_signal[_compound][y_axis_tag]
                ax_mpl.plot(_x_axis, _y_axis, fmt, ms=ms, lw=lw, alpha=alpha, label=_compound)

        if all_elements:
            for _compound in _stack.keys():
                for _element in _stack[_compound]['elements']:
                    if y_axis_tag[:5] != 'sigma':
                        _y_axis = _stack_signal[_compound][_element][y_axis_tag]
                        ax_mpl.plot(_x_axis, _y_axis, fmt, ms=ms, lw=lw, alpha=alpha,
                                    label="{}/{}".format(_compound, _element))
                    else:
                        _y_axis = _stack_sigma[_compound][_element]['sigma_b']
                        ax_mpl.plot(_x_axis, _y_axis, fmt, ms=ms, lw=lw, alpha=alpha,
                                    label="{}/{}".format(_compound, _element))

        if all_isotopes:
            for _compound in _stack.keys():
                for _element in _stack[_compound]['elements']:
                    for _isotope in _stack[_compound][_element]['isotopes']['list']:
                        if y_axis_tag[:5] != 'sigma':
                            _y_axis = _stack_signal[_compound][_element][_isotope][y_axis_tag]
                            ax_mpl.plot(_x_axis, _y_axis, fmt, ms=ms, lw=lw, alpha=alpha,
                                        label="{}/{}/{}".format(_compound, _element, _isotope))
                        else:
                            _y_axis = _stack_sigma[_compound][_element][_isotope][y_axis_tag]
                            ax_mpl.plot(_x_axis, _y_axis, fmt, ms=ms, lw=lw, alpha=alpha,
                                        label="{}/{}/{}".format(_compound, _element, _isotope))

        """Y-axis for specified items_to_plot"""
        if items_to_plot is not None:
            for _path_to_plot in items_to_plot:
                _path_to_plot = list(_path_to_plot)
                if y_axis_tag[:5] != 'sigma':
                    _live_path = _stack_signal
                else:
                    _len_of_path = len(_path_to_plot)
                    if y_axis_tag[-3:] == 'raw':
                        if _len_of_path < 3:
                            raise ValueError("'y_axis={}' is not supported for layer or element levels '{}'.".format(
                                y_axis_tag, _path_to_plot[-1]))
                    else:
                        if _len_of_path < 2:
                            raise ValueError("'y_axis={}' is not supported for layer level '{}'.".format(
                                y_axis_tag, _path_to_plot[-1]))

                    _live_path = _stack_sigma
                _label = "/".join(_path_to_plot)
                while _path_to_plot:
                    _item = _path_to_plot.pop(0)
                    _live_path = _live_path[_item]
                _y_axis = _live_path[y_axis_tag]
                ax_mpl.plot(_x_axis, _y_axis, fmt, ms=ms, lw=lw, alpha=alpha, label=_label)

        if y_axis_tag[:5] != 'sigma' and y_axis_tag != 'miu_per_cm':
            ax_mpl.set_ylim(-0.01, 1.01)
        if logy is True:
            ax_mpl.set_yscale('log')
        if logx is True:
            ax_mpl.set_xscale('log')
        ax_mpl.set_xlabel(x_axis_label)
        ax_mpl.set_ylabel(y_axis_label)
        if not plotly:
            ax_mpl.legend(loc='best')
            # plt.tight_layout()
            return ax_mpl
        else:
            fig_mpl = ax_mpl.get_figure()
            plotly_fig = tls.mpl_to_plotly(fig_mpl)
            plotly_fig.layout.showlegend = True
            return plotly_fig

Response: Plot the transmission or attenuation of the compound element and or isotope of the current language.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def export(self, output_type='df', filename=None, x_axis='energy', y_axis='attenuation', mixed=True,
               all_layers=False, all_elements=False, all_isotopes=False, items_to_export=None,
               offset_us=0., source_to_detector_m=16.,
               t_start_us=1, time_resolution_us=0.16, time_unit='us'):
        """
        output x and y values to clipboard or .csv file
        output the transmission or attenuation or sigma of compound, element and/or isotopes specified
        'sigma_b' exported for each isotope is the product resulted from (sigma * isotopic ratio)
        'atoms_per_cm3' of each element is also exported in 'sigma' mode based on molar mass within stack.

        :param output_type: export type : ['df', 'csv', 'clip']
        :type output_type: str
        :param mixed: True -> display the total of each layer
                               False -> not displayed
        :type mixed: boolean
        :param filename: string. filename (with .csv suffix) you would like to save as
                                None -> export to clipboard
        :type filename: string
        :param x_axis: string. x type for export. Must in ['energy', 'lambda', 'time', 'number']
        :param y_axis: string. y type for export. Must in ['transmission', 'attenuation', 'sigma', 'sigma_raw', 'miu_per_cm']
        :param all_layers: boolean. True -> export all layers
                                    False -> not export
        :param all_elements: boolean. True -> export all elements signal
                                      False -> not export
        :param all_isotopes: boolean. True -> export all isotopes signal
                                      False -> not export
        :param items_to_export: array that describes what to export
            ex:
                [['CoAg','Ag','107-Ag'], ['CoAg']]
            if the dictionary is empty, everything is exported
        :param time_unit: string. Must be either 's' or 'us' or 'ns'
               Note: this will be used only when x_axis='time'
        :param offset_us: default: 0
               Note: only used when x_axis='number' or 'time'
        :param source_to_detector_m: Note: this will be used only when x_axis='number' or 'time'
        :param time_resolution_us: Note: this will be used only when x_axis='number'
        :param t_start_us: when is the first acquisition occurred. default: 1
               Note: this will be used only when x_axis='number'

        :return: simulated resonance signals or sigma in the form of 'clipboard' or '.csv file' or 'pd.DataFrame'
        """
        if x_axis not in x_type_list:
            raise ValueError("Please specify the x-axis type using one from '{}'.".format(x_type_list))
        if time_unit not in time_unit_list:
            raise ValueError("Please specify the time unit using one from '{}'.".format(time_unit_list))
        if y_axis not in y_type_list:
            raise ValueError("Please specify the y-axis type using one from '{}'.".format(y_type_list))
        if output_type not in export_type_list:
            raise ValueError("Please specify export type using one from '{}'.".format(export_type_list))
        # stack from self
        _stack_signal = self.stack_signal
        _stack = self.stack

        _x_axis = self.total_signal['energy_eV']
        x_axis_label = None
        df = pd.DataFrame()

        """X-axis"""
        # determine values and labels for x-axis with options from
        # 'energy(eV)' & 'lambda(A)' & 'time(us)' & 'image number(#)'
        if x_axis == 'energy':
            x_axis_label = 'Energy (eV)'
        if x_axis == 'lambda':
            x_axis_label = u"Wavelength (\u212B)"
            _x_axis = _utilities.ev_to_angstroms(array=_x_axis)
        if x_axis == 'time':
            if time_unit == 's':
                x_axis_label = 'Time (s)'
                _x_axis = _utilities.ev_to_s(array=_x_axis,
                                             source_to_detector_m=source_to_detector_m,
                                             offset_us=offset_us)
            if time_unit == 'us':
                x_axis_label = 'Time (us)'
                _x_axis = 1e6 * _utilities.ev_to_s(array=_x_axis,
                                                   source_to_detector_m=source_to_detector_m,
                                                   offset_us=offset_us)
            if time_unit == 'ns':
                x_axis_label = 'Time (ns)'
                _x_axis = 1e9 * _utilities.ev_to_s(array=_x_axis,
                                                   source_to_detector_m=source_to_detector_m,
                                                   offset_us=offset_us)
            print("'{}' was obtained with the following:\nsource_to_detector_m={}\noffset_us={}"
                  .format(x_axis_label, source_to_detector_m, offset_us))

        if x_axis == 'number':
            x_axis_label = 'Image number (#)'
            _x_axis = _utilities.ev_to_image_number(array=_x_axis,
                                                    source_to_detector_m=source_to_detector_m,
                                                    offset_us=offset_us,
                                                    time_resolution_us=time_resolution_us,
                                                    t_start_us=t_start_us)
            print("'{}' was obtained with the following:\nsource_to_detector_m={}\noffset_us={}\ntime_resolution_us={}"
                  .format(x_axis_label, source_to_detector_m, offset_us, time_resolution_us))

        if x_axis_label is None:
            raise ValueError("x_axis_label does NOT exist, please check.")
        df[x_axis_label] = _x_axis

        """Y-axis"""
        if y_axis[:5] != 'sigma':
            # export transmission or attenuation or miu_per_cm
            y_axis_tag = y_axis
            if y_axis_tag == 'miu_per_cm':
                mixed = False
                print("'y_axis='miu_per_cm'' is selected. Auto force 'mixed=False'")
            if mixed:
                _y_axis = self.total_signal[y_axis_tag]
                df['Total_' + y_axis_tag] = _y_axis
            if items_to_export is None:
                # export based on specified level : layer|element|isotope
                if all_layers:
                    for _compound in _stack.keys():
                        _y_axis = _stack_signal[_compound][y_axis_tag]
                        df[_compound] = _y_axis

                if all_elements:
                    for _compound in _stack.keys():
                        for _element in _stack[_compound]['elements']:
                            _y_axis = _stack_signal[_compound][_element][y_axis_tag]
                            df[_compound + '/' + _element] = _y_axis

                if all_isotopes:
                    for _compound in _stack.keys():
                        for _element in _stack[_compound]['elements']:
                            for _isotope in _stack[_compound][_element]['isotopes']['list']:
                                _y_axis = _stack_signal[_compound][_element][_isotope][y_axis_tag]
                                df[_compound + '/' + _element + '/' + _isotope] = _y_axis
            else:
                # export specified transmission or attenuation
                for _path_to_export in items_to_export:
                    _path_to_export = list(_path_to_export)
                    _live_path = _stack_signal
                    _label = "/".join(_path_to_export)
                    while _path_to_export:
                        _item = _path_to_export.pop(0)
                        _live_path = _live_path[_item]
                    _y_axis = _live_path[y_axis_tag]
                    df[_label] = _y_axis
        else:
            # export sigma
            if y_axis == 'sigma':
                y_axis_tag = 'sigma_b'
            else:
                y_axis_tag = 'sigma_b_raw'
            # y_axis_tag = 'sigma_b_raw'
            _stack_sigma = self.stack_sigma
            if items_to_export is None:
                for _compound in _stack.keys():
                    for _element in _stack[_compound]['elements']:
                        _y_axis = _stack_sigma[_compound][_element]['sigma_b']  # No 'sigma_b_raw' at this level
                        df[_compound + '/' + _element + '/atoms_per_cm3'] = _stack[_compound][_element]['atoms_per_cm3']
                        df[_compound + '/' + _element] = _y_axis
                        if all_isotopes:
                            for _isotope in _stack[_compound][_element]['isotopes']['list']:
                                _y_axis = _stack_sigma[_compound][_element][_isotope][y_axis_tag]
                                df[_compound + '/' + _element + '/' + _isotope] = _y_axis
            else:
                # export specified sigma
                for _path_to_export in items_to_export:
                    if y_axis_tag[-3:] == 'raw':
                        if len(_path_to_export) < 3:
                            raise ValueError(
                                "Getting raw sigma of '{}' at layer or element level is not supported. "
                                "If it is a single element layer, please follow "
                                "['layer', 'element', 'isotope'] format.".format(_path_to_export[0]))
                    else:
                        if len(_path_to_export) < 2:
                            raise ValueError(
                                "Getting weighted sigma of '{}' at layer level is not supported. "
                                "If it is a single element layer, please follow "
                                "['layer', 'element'] format.".format(_path_to_export[0]))
                    _path_to_export = list(_path_to_export)
                    _live_path = _stack_sigma
                    _label = "/".join(_path_to_export)
                    while _path_to_export:
                        _item = _path_to_export.pop(0)
                        _live_path = _live_path[_item]
                    _y_axis = _live_path[y_axis_tag]
                    df[_label] = _y_axis

        if len(df.columns) <= 1:
            raise ValueError("No y values have been selected to export!")
        if output_type == 'csv':
            if filename is None:
                filename = 'data.csv'
            if '.csv' not in filename:
                filename += '.csv'
            df.to_csv(filename, index=False)
            print("Exporting to file ('./{}') completed.".format(filename))
        elif output_type == 'clip':
            df.to_clipboard(excel=True, index=False)
            print('Exporting to clipboard completed.')
        else:  # output_type == 'df'
            return df

Response: export x and y values to clipboard or. csv file


Instruction:
Implement a Python 3 function for
building a yaml file

Response:
def main():
    """Builds a yaml file"""
    parser = argparse.ArgumentParser(description='Compose a yaml file.')
    parser.add_argument(
        'root',
        type=argparse.FileType('r'),
        help='The root yaml file to compose.'
    )

    args = parser.parse_args()

    result = yaml.load(args.root, Loader=ComposeLoader)

    print(yaml.dump(result))


Instruction:
How would you explain what the following Python 3 function does
def _parse(self, text, mode):
        if text is None or not text.strip():
            return None

        # first try to parse as an ISO8601 date, if it doesn't work we'll try other options
        if len(text) >= 16:
            try:
                parsed = iso8601.parse_date(text, default_timezone=None)
                if not parsed.tzinfo:
                    parsed = self._timezone.localize(parsed)

                return parsed
            except iso8601.ParseError:
                pass

        # split the text into numerical and text tokens
        tokens = regex.findall(r'([0-9]+|[^\W\d]+)', text, flags=regex.MULTILINE | regex.UNICODE | regex.V0)

        # get the possibilities for each token
        token_possibilities = []
        for token in tokens:
            possibilities = self._get_token_possibilities(token, mode)
            if len(possibilities) > 0:
                token_possibilities.append(possibilities)

        # see what valid sequences we can make
        sequences = self._get_possible_sequences(mode, len(token_possibilities), self._date_style)

        for sequence in sequences:
            match = OrderedDict()

            for c in range(len(sequence)):
                component = sequence[c]
                value = token_possibilities[c].get(component, None)
                match[component] = value

                if value is None:
                    break
            else:
                # try to make a valid result from this and return if successful
                obj = self._make_result(match, self._now, self._timezone)
                if obj is not None:
                    return obj

        return None

Response: Parses the text into a date datetime or time depending on what information is available.


Instruction:
Given the following Python 3 function, write the documentation
def _get_possible_sequences(cls, mode, length, date_style):
        sequences = []
        date_sequences = cls.DATE_SEQUENCES_DAY_FIRST if date_style == DateStyle.DAY_FIRST else cls.DATE_SEQUENCES_MONTH_FIRST

        if mode == Mode.DATE or mode == Mode.AUTO:
            for seq in date_sequences:
                if len(seq) == length:
                    sequences.append(seq)

        elif mode == Mode.TIME:
            for seq in cls.TIME_SEQUENCES:
                if len(seq) == length:
                    sequences.append(seq)

        if mode == Mode.DATETIME or mode == Mode.AUTO:
            for date_seq in date_sequences:
                for time_seq in cls.TIME_SEQUENCES:
                    if len(date_seq) + len(time_seq) == length:
                        sequences.append(date_seq + time_seq)

        return sequences

Response: Gets the possible component sequences in the given mode.


Instruction:
Explain what the following Python 3 code does
def _make_result(cls, values, now, timezone):
        date = None
        time = None

        if Component.MONTH in values:
            year = cls._year_from_2digits(values.get(Component.YEAR, now.year), now.year)
            month = values[Component.MONTH]
            day = values.get(Component.DAY, 1)
            try:
                date = datetime.date(year, month, day)
            except ValueError:
                return None  # not a valid date

        if (Component.HOUR in values and Component.MINUTE in values) or Component.HOUR_AND_MINUTE in values:
            if Component.HOUR_AND_MINUTE in values:
                combined = values[Component.HOUR_AND_MINUTE]
                hour = combined // 100
                minute = combined - (hour * 100)
                second = 0
                nano = 0
            else:
                hour = values[Component.HOUR]
                minute = values[Component.MINUTE]
                second = values.get(Component.SECOND, 0)
                nano = values.get(Component.NANO, 0)

                if hour < 12 and values.get(Component.AM_PM) == cls.PM:
                    hour += 12
                elif hour == 12 and values.get(Component.AM_PM) == cls.AM:
                    hour -= 12

            try:
                time = datetime.time(hour, minute, second, microsecond=nano // 1000)
            except ValueError:
                return None  # not a valid time

        if Component.OFFSET in values:
            timezone = pytz.FixedOffset(values[Component.OFFSET] // 60)

        if date is not None and time is not None:
            return timezone.localize(datetime.datetime.combine(date, time))
        elif date is not None:
            return date
        elif time is not None:
            return time
        else:
            return None

Response: Makes a date datetime or time object from a map of component values.


Instruction:
How would you explain what the following Python 3 function does
def _year_from_2digits(short_year, current_year):
        if short_year < 100:
            short_year += current_year - (current_year % 100)
            if abs(short_year - current_year) >= 50:
                if short_year < current_year:
                    return short_year + 100
                else:
                    return short_year - 100
        return short_year

Response: Converts a relative 2 - digit year to an absolute 4 - digit year.


Instruction:
Can you create a Python 3 function that
prepares the black list and white lists for a given project and patchset.

Response:
def prepare_patchset(project, patchset, binaries, ips, urls):
    """ Create black/white lists and default / project waivers
        and iterates over patchset file """
    # Get Various Lists / Project Waivers
    lists = get_lists.GetLists()

    # Get file name black list and project waivers
    file_audit_list, file_audit_project_list = lists.file_audit_list(project)

    # Get file content black list and project waivers
    flag_list, ignore_list = lists.file_content_list(project)

    # Get URL Ignore Lists
    url_ignore = lists.url_ignore(project)

    # Get URL Ignore Lists
    ip_ignore = lists.ip_ignore(project)

    # Get File Ignore Lists
    file_ignore = lists.file_ignore()

    # Get Directory Ignore Lists
    ignore_directories = lists.ignore_directories(project)

    if binaries or ips or urls:
        try:
            apikey = os.environ["VT_KEY"]
        except KeyError:
            logger.error("Please set your virustotal.com API key as an environment variable")
            sys.exit(1)
        try:
            vt_rate_type = config.get('config', 'vt_rate_type')
        except six.moves.configparser.NoSectionError:
            logger.error("A config section is required for vt_rate_type with a public | private option ")
            sys.exit(1)

        patten = re.compile(r'\bpublic\b|\bprivate\b')
        if not patten.match(vt_rate_type):
            logger.error("Unrecognized %s option for vt_rate_type", vt_rate_type)
            sys.exit(1)
    else:
        apikey = ""

    # Open patch set to get file list
    try:
        fo = open(patchset, 'r')
        lines = fo.readlines()
    except IOError:
        logger.error('%s does not exist', patchset)
        sys.exit(1)

    for line in lines:
        patch_file = line.strip('\n')
        # Perform binary and file / content checks
        scan_patch(project, patch_file, binaries, ips, urls, file_audit_list,
                   file_audit_project_list, flag_list, ignore_list,
                   file_ignore, ignore_directories, url_ignore, ip_ignore, apikey)

    # Process final result
    process_failure(project)


Instruction:
Can you implement a function in Python 3 that
scans patch file for changes and return a list of successive items

Response:
def scan_patch(project, patch_file, binaries, ips, urls, file_audit_list,
               file_audit_project_list, flag_list, ignore_list, file_ignore,
               ignore_directories, url_ignore, ip_ignore, apikey):

    """
    Scan actions for each commited file in patch set
    """
    global failure
    split_path = patch_file.split(project + '/', 1)[-1]

    if not any(x in split_path for x in ignore_directories):
        if is_binary(patch_file) and binaries:
            hashlist = get_lists.GetLists()
            binary_hash = hashlist.binary_hash(project, split_path)

            with open(patch_file, 'rb') as afile:
                hasher = hashlib.sha256()
                buf = afile.read()
                hasher.update(buf)
                sha256hash = hasher.hexdigest()

            if sha256hash in binary_hash:

                logger.info('Found matching file hash for: %s',
                            patch_file)
            else:
                logger.info('sha256hash: %s', sha256hash)
                logger.error('Non Whitelisted Binary file: %s',
                             patch_file)

                scan_binary(patch_file, project, sha256hash, apikey)

                failure = True

            with open(reports_dir + "binaries-" + project + ".log", "a") \
                    as gate_report:
                gate_report.write('Non Whitelisted Binary file: {0}\n'.
                                  format(patch_file))
        else:
            # Check file names / extensions
            if file_audit_list.search(patch_file) and not \
                    file_audit_project_list.search(patch_file):
                match = file_audit_list.search(patch_file)
                logger.error('Blacklisted file: %s', patch_file)
                logger.error('Matched String: %s', match.group())
                failure = True
                with open(reports_dir + "file-names_" + project + ".log", "a") \
                        as gate_report:
                    gate_report.write('Blacklisted file: {0}\n'.
                                      format(patch_file))
                    gate_report.write('Matched String: {0}'.
                                      format(match.group()))

            # Open file to check for blacklisted content
            if not is_binary(patch_file):
                try:
                    fo = open(patch_file, 'r')
                    lines = fo.readlines()
                    file_exists = True
                except IOError:
                    file_exists = False

                if file_exists and not patch_file.endswith(tuple(file_ignore)):
                    for line in lines:
                        # Find IP Addresses and send for report to Virus Total
                        if ips:
                            ipaddr = re.findall(r'(?:\d{1,3}\.)+(?:\d{1,3})', line)
                            if ipaddr:
                                ipaddr = ipaddr[0]
                                if re.search(ip_ignore, ipaddr):
                                        logger.info('%s is in IP ignore list.', ipaddr)
                                else:
                                    try:
                                        ipaddress.ip_address(ipaddr).is_global
                                        scan_ipaddr(ipaddr, apikey)
                                    except:
                                        pass  # Ok to pass here, as this captures the odd string which is not an IP Address

                        #  Check for URLs and send for report to Virus Total
                        if urls:
                            url = re.search("(?P<url>https?://[^\s]+)", line) or re.search("(?P<url>www[^\s]+)", line)
                            if url:
                                url = url.group("url")
                                if re.search(url_ignore, url):
                                    logger.info('%s is in URL ignore list.', url)
                                else:
                                    scan_url(url, apikey)

                        #  Perform search within text files
                        for key, value in flag_list.items():
                            regex = value['regex']
                            desc = value['desc']
                            if re.search(regex, line) and not re.search(
                                    ignore_list, line):
                                logger.error('File contains violation: %s', patch_file)
                                logger.error('Flagged Content: %s', line.rstrip())
                                logger.error('Rationale: %s', desc.rstrip())
                                failure = True
                                with open(reports_dir + "contents_" + project + ".log", "a") as gate_report:
                                    gate_report.write('File contains violation: {0}\n'.format(patch_file))
                                    gate_report.write('Flagged Content: {0}'.format(line))
                                    gate_report.write('Matched Regular Exp: {0}\n'.format(regex))
                                    gate_report.write('Rationale: {0}\n'.format(desc.rstrip()))


Instruction:
Can you write a function in Python 3 where it
scans binary files for a given project and sha256hash.

Response:
def scan_binary(patch_file, project, sha256hash, apikey):
    """
    Sends Binary (sha256hash) to Virus Total API
    """
    v_api = virus_total.VirusTotal()

    while True:
        binary_report = v_api.binary_report(sha256hash, apikey)
        response_code = binary_report['response_code']

        # report does not exist, need to scan
        if response_code == 0:
            logger.info('Performing new scan of %s.', patch_file)
            scan_file = v_api.scan_file(patch_file, apikey)
            logger.info('VirusTotal Response: %s', scan_file['verbose_msg'])
            logger.info('Report will be rendered at: %s', scan_file['permalink'])
            binary_report = v_api.binary_report(sha256hash, apikey)

        # Item is still queued
        if response_code == -2:
            logger.info('Report job still queued..')

        if response_code == 1:
            logger.info('Report found, job complete.')
            break

    positives = binary_report['positives']

    if positives == 0:
        negative_report(binary_report, sha256hash, project, patch_file)
    else:
        positive_report(binary_report, sha256hash, project, patch_file)


Instruction:
Can you tell what is the following Python 3 function doing
def negative_report(binary_report, sha256hash, project, patch_file):
    report_url = binary_report['permalink']
    scan_date = binary_report['scan_date']
    logger.info('File scan date for %s shows a clean status on: %s', patch_file, scan_date)
    logger.info('Full report avaliable here: %s', report_url)
    logger.info('The following sha256 hash can be used in your %s.yaml file to suppress this scan:', project)
    logger.info('%s', sha256hash)
    with open(reports_dir + "binaries-" + project + ".log", "a") as gate_report:
                gate_report.write('Non Whitelisted Binary: {}\n'.format(patch_file))
                gate_report.write('File scan date for {} shows a clean status on {}\n'.format(patch_file, scan_date))
                gate_report.write('The following sha256 hash can be used in your {}.yaml file to suppress this scan:\n'.format(project))
                gate_report.write('{}\n'.format(sha256hash))

Response: Write negative binary report to log file.


Instruction:
Can you generate the documentation for the following Python 3 function
def positive_report(binary_report, sha256hash, project, patch_file):
    failure = True
    report_url = binary_report['permalink']
    scan_date = binary_report['scan_date']
    logger.error("Virus Found!")
    logger.info('File scan date for %s shows a infected status on: %s', patch_file, scan_date)
    logger.info('Full report avaliable here: %s', report_url)

Response: Check if a Positive match is found and if so show a full report


Instruction:
Can you generate the documentation for the following Python 3 function
def scan_ipaddr(ipaddr, apikey):
    logger.info('Query VirusTotal API for Public IP Found: %s', ipaddr)
    v_api = virus_total.VirusTotal()
    scan_ip = v_api.send_ip(ipaddr, apikey)
    response_code = scan_ip['response_code']
    verbose_msg = scan_ip['verbose_msg']
    urls = scan_ip['detected_urls']

    if urls:
        failure = True
        logger.error('%s has been known to resolve to malicious urls', ipaddr)
        for url in urls:
            logger.error('%s on date: %s', url['url'], url['scan_date'])
    else:
        logger.info('%s has no record of resolving to malicious urls', ipaddr)

Response: Scan VirusTotal API for a given IP Address.


Instruction:
Write a Python 3 script for
scanning a URL for VirusTotal reports.

Response:
def scan_url(url, apikey):
    """
    If URL is found, scan it
    """
    logger.info('Found what I believe is a URL: %s', url)
    v_api = virus_total.VirusTotal()
    while True:
        url_report = v_api.url_report(url, apikey)
        response_code = url_report['response_code']

        # report does not exist, need to scan
        if response_code == -2:
            logger.info('Report job still queued..')

        if response_code == 0:
            logger.info('No report  for %s', url)
            break

        if response_code == 1:
            logger.info('Report found, job complete for %s.', url)
            break

    try:
        positives = url_report['positives']
        if positives > 0:
            for site, results in url_report['scans'].items():
                if results['detected']:
                    detected = True
                    failure = True
                    logger.error("%s is recorded as a %s by %s", url, results['result'], site)
            if detected:
                logger.error("Full report available here: %s", url_report['permalink'])
        else:
            logger.info("%s is recorded as a clean", url)
    except:
        pass


Instruction:
Here you have a function in Python 3, explain what it does
def process_failure(project):
    if failure:
        lists = get_lists.GetLists()
        report_url = lists.report_url(project)
        if report_url:
            print(report_url)
        sys.exit(1)

Response: Exit if a failure occurs.


Instruction:
Can you write a function in Python 3 where it
computes average gradient norm of an image

Response:
def average_gradient(data, *kwargs):
    """ Compute average gradient norm of an image
    """
    return np.average(np.array(np.gradient(data))**2)


Instruction:
Given the following Python 3 function, write the documentation
def contrast_rms(data, *kwargs):
    av = np.average(data, *kwargs)
    mal = 1 / (data.shape[0] * data.shape[1])
    return np.sqrt(mal * np.sum(np.square(data - av)))

Response: Compute the RMS contrast norm of an image


Instruction:
Write a Python 3 function that can
compute spectral contrast of image

Response:
def spectral(data, lambd, *kwargs):
    """ Compute spectral contrast of image

    Performs bandpass filtering in Fourier space according to optical
    limit of detection system, approximated by twice the wavelength.

    Parameters
    ----------
    data : 2d ndarray
        the image to compute the norm from
    lambd : float
        wavelength of the light in pixels

    """
    # Set up fast fourier transform
    # if not data.dtype == np.dtype(np.complex):
    #    data = np.array(data, dtype=np.complex)
    # fftplan = fftw3.Plan(data.copy(), None, nthreads = _ncores,
    #                     direction="forward", flags=_fftwflags)
    # fftdata = np.zeros(data.shape, dtype=np.complex)
    # fftplan.guru_execute_dft(data, fftdata)
    # fftw.destroy_plan(fftplan)
    fftdata = np.fft.fftn(data)

    # Filter Fourier transform
    fftdata[0, 0] = 0
    kx = 2 * np.pi * np.fft.fftfreq(data.shape[0]).reshape(1, -1)
    ky = 2 * np.pi * np.fft.fftfreq(data.shape[1]).reshape(-1, 1)
    kmax = (2 * np.pi) / (2 * lambd)
    fftdata[np.where(kx**2 + ky**2 > kmax**2)] = 0

    spec = np.sum(np.log(1 + np.abs(fftdata))) / np.sqrt(np.prod(data.shape))

    return spec


Instruction:
Can you implement a function in Python 3 that
parses file extension. - *.json: uncompressed, utf-8 encode json file - *.gz: compressed, utf-8 encode json file

Response:
def is_gzip_file(abspath):
    """Parse file extension.

    - *.json: uncompressed, utf-8 encode json file
    - *.gz: compressed, utf-8 encode json file
    """
    abspath = abspath.lower()
    _, ext = os.path.splitext(abspath)
    if ext in [".gz", ".zip"]:
        is_gzip = True
    else:
        is_gzip = False
    return is_gzip


Instruction:
Write a Python 3 function that can
write string to text file.

Response:
def write(s, path, encoding="utf-8"):
    """Write string to text file.
    """
    is_gzip = is_gzip_file(path)

    with open(path, "wb") as f:
        if is_gzip:
            f.write(zlib.compress(s.encode(encoding)))
        else:
            f.write(s.encode(encoding))


Instruction:
Can you tell what is the following Python 3 function doing
def read(path, encoding="utf-8"):
    is_gzip = is_gzip_file(path)

    with open(path, "rb") as f:
        if is_gzip:
            return zlib.decompress(f.read()).decode(encoding)
        else:
            return f.read().decode(encoding)

Response: Read string from text file.


Instruction:
How would you explain what the following Python 3 function does
def smartread(path):
    with open(path, "rb") as f:
        content = f.read()
        result = chardet.detect(content)
        return content.decode(result["encoding"])

Response: Read text from file automatically detect encoding. Chardet is required.


Instruction:
Can you generate the documentation for the following Python 3 function
def to_utf8(path, output_path=None):
    if output_path is None:
        basename, ext = os.path.splitext(path)
        output_path = basename + "-UTF8Encode" + ext

    text = smartread(path)
    write(text, output_path)

Response: Convert any text file to utf8 encoding.


Instruction:
Write a Python 3 function that can
yield a generator of lines from file

Response:
def readlines(path, encoding="utf-8", skiplines=None, nlines=None, strip='right'):
    """skip n lines and fetch the next n lines.

    :param skiplines: default None, skip first n lines
    :param nlines: default None, yield next n lines
    :param strip: default None, available option 'left', 'right', 'both'

    **中文文档**

    跳过前#skiplines行, 然后读取#nlines行。可对字符串进行strip预处理。
    """
    strip_method = str(strip).lower()
    if strip_method in _strip_method_mapping:
        strip_func = _strip_method_mapping[strip_method]
    else:
        raise ValueError("'strip' keyword has to be one of "
                         "None, 'left', 'right', 'both'.")

    with open(path, "rb") as file:
        if skiplines:
            for _ in range(skiplines):
                next(file)

        if nlines:
            for _ in range(nlines):
                yield strip_func(next(file).decode(encoding))
        else:
            for line in file:
                yield strip_func(line.decode(encoding))


Instruction:
Implement a Python 3 function for
reading a file into chunks

Response:
def readchunks(path, encoding="utf-8", skiplines=None, chunksize=None, strip='right'):
    """skip n lines and fetch the next n lines as a chunk, and repeat fetching.

    :param skiplines: default None, skip first n lines
    :param chunksize: default None (size-1 chunk), lines chunk size
    :param strip: default None, avaliable option 'left', 'right', 'both'

    **中文文档**

    跳过前#skiplines行, 每次读取#chunksize行yield。可对字符串进行strip预处理。
    """
    strip_method = str(strip).lower()
    if strip_method in _strip_method_mapping:
        strip_func = _strip_method_mapping[strip_method]
    else:
        raise ValueError("'strip' keyword has to be one of "
                         "None, 'left', 'right', 'both'.")

    with open(path, "rb") as file:
        if skiplines:
            for _ in range(skiplines):
                next(file)

        if chunksize is None:
            chunksize = 1
        elif not isinstance(chunksize, int_type):
            raise ValueError("'chunksize' has to be None or an integer.")

        chunk = list()
        while 1:
            for _ in range(chunksize):
                chunk.append(strip_func(next(file).decode(encoding)))
            if len(chunk) < chunksize:
                break
            yield chunk
            chunk = list()
        yield chunk


Instruction:
Can you tell what is the following Python 3 function doing
def _coerce_to_supported_type(cls, value):
        if value is None:
            return ""  # empty string rather than none
        elif isinstance(value, dict):
            if '*' in value:
                return value['*']
            elif '__default__' in value:
                return value['__default__']
            else:
                return json.dumps(value, separators=(',', ':'))  # return serialized JSON if no default
        elif isinstance(value, bool):
            return value
        elif isinstance(value, float) or isinstance(value, int):
            return Decimal(value)
        else:
            return value

Response: Coerce the value to a supported data type.


Instruction:
Write a Python 3 script to
evaluate a template string and returns a tuple of the evaluated template string and a list of evaluation errors

Response:
def evaluate_template(self, template, context, url_encode=False, strategy=EvaluationStrategy.COMPLETE):
        """
        Evaluates a template string, e.g. "Hello @contact.name you have @(contact.reports * 2) reports"
        :param template: the template string
        :param context: the evaluation context
        :param url_encode: whether or not values should be URL encoded
        :return: a tuple of the evaluated template and a list of evaluation errors
        """
        input_chars = list(template)
        output_chars = []
        errors = []
        state = State.BODY
        current_expression_chars = []
        current_expression_terminated = False
        parentheses_level = 0

        # determines whether the given character is a word character, i.e. \w in a regex
        is_word_char = lambda c: c and (c.isalnum() or c == '_')

        for pos, ch in enumerate(input_chars):
            # in order to determine if the b in a.b terminates an identifier, we have to peek two characters ahead as it
            # could be a.b. (b terminates) or a.b.c (b doesn't terminate)
            next_ch = input_chars[pos + 1] if (pos < (len(input_chars) - 1)) else None
            next_next_ch = input_chars[pos + 2] if (pos < (len(input_chars) - 2)) else None

            if state == State.BODY:
                if ch == self._expression_prefix and (is_word_char(next_ch) or next_ch == '('):
                    state = State.PREFIX
                    current_expression_chars = [ch]
                elif ch == self._expression_prefix and next_ch == self._expression_prefix:
                    state = State.ESCAPED_PREFIX
                else:
                    output_chars.append(ch)

            elif state == State.PREFIX:
                if is_word_char(ch):
                    # we're parsing an expression like @XXX
                    state = State.IDENTIFIER
                elif ch == '(':
                    # we're parsing an expression like @(1 + 2)
                    state = State.BALANCED
                    parentheses_level += 1

                current_expression_chars.append(ch)

            elif state == State.IDENTIFIER:
                current_expression_chars.append(ch)

            elif state == State.BALANCED:
                if ch == '(':
                    parentheses_level += 1
                elif ch == ')':
                    parentheses_level -= 1
                elif ch == '"':
                    state = State.STRING_LITERAL

                current_expression_chars.append(ch)

                # expression terminates if parentheses balance
                if parentheses_level == 0:
                    current_expression_terminated = True

            elif state == State.STRING_LITERAL:
                if ch == '"':
                    state = State.BALANCED
                current_expression_chars.append(ch)

            elif state == State.ESCAPED_PREFIX:
                state = State.BODY
                output_chars.append(ch)

            # identifier can terminate expression in 3 ways:
            #  1. next char is null (i.e. end of the input)
            #  2. next char is not a word character or period
            #  3. next char is a period, but it's not followed by a word character
            if state == State.IDENTIFIER:
                if not next_ch or (not is_word_char(next_ch) and next_ch != '.') or (next_ch == '.' and not is_word_char(next_next_ch)):
                    current_expression_terminated = True

            if current_expression_terminated:
                expression = ''.join(current_expression_chars)
                output_chars.append(self._resolve_expression_block(expression, context, url_encode, strategy, errors))
                current_expression_chars = []
                current_expression_terminated = False
                state = State.BODY

        # if last expression didn't terminate - add to output as is
        if not current_expression_terminated and current_expression_chars:
            output_chars.append(''.join(current_expression_chars))

        output = ''.join(output_chars)  # joining is fastest way to build strings in Python
        return output, errors


Instruction:
Given the following Python 3 function, write the documentation
def _resolve_expression_block(self, expression, context, url_encode, strategy, errors):
        try:
            body = expression[1:]  # strip prefix

            # if expression doesn't start with ( then check it's an allowed top level context reference
            if not body.startswith('('):
                top_level = body.split('.')[0].lower()
                if top_level not in self._allowed_top_levels:
                    return expression

            evaluated = self.evaluate_expression(body, context, strategy)

            # convert result to string
            result = conversions.to_string(evaluated, context)

            return urlquote(result) if url_encode else result
        except EvaluationError as e:
            logger.debug("EvaluationError: %s" % str(e))

            # if we can't evaluate expression, include it as is in the output
            errors.append(str(e))
            return expression

Response: Resolves an expression block found in the template e. g. @(\. \ d + \. \ d + \. \ d + \. \ d + \. \ d + \. \ d + \. \ d + \. \ d + \. \ d + \. \ d + \. \ d + \. \ d + \. \ d + \. \ d + \. \ d + \. \ d + \. \ d + \.


Instruction:
Create a Python 3 function to
evaluate a single expression and returns the value of the expression.

Response:
def evaluate_expression(self, expression, context, strategy=EvaluationStrategy.COMPLETE):
        """
        Evaluates a single expression, e.g. "contact.reports * 2"
        :param expression: the expression string
        :param context: the evaluation context
        :param strategy: the evaluation strategy
        :return: the evaluated expression value
        """
        from .gen.ExcellentLexer import ExcellentLexer
        from .gen.ExcellentParser import ExcellentParser

        stream = InputStream(expression)
        lexer = ExcellentLexer(stream)
        tokens = CommonTokenStream(lexer)

        parser = ExcellentParser(tokens)
        parser._errHandler = BailErrorStrategy()

        try:
            tree = parser.parse()

            if logger.isEnabledFor(logging.DEBUG):
                logger.debug("Expression '%s' parsed as %s" % (expression, tree.toStringTree()))
        except ParseCancellationException as ex:
            message = None
            if ex.args and isinstance(ex.args[0], NoViableAltException):
                token = ex.args[0].offendingToken
                if token is not None and token.type != ExcellentParser.EOF:
                    message = "Expression error at: %s" % token.text

            if message is None:
                message = "Expression is invalid"

            raise EvaluationError(message, ex)

        if strategy == EvaluationStrategy.RESOLVE_AVAILABLE:
            resolved = self._resolve_available(tokens, context)
            if resolved is not None:
                return resolved

        visitor = ExcellentVisitor(self._function_manager, context)
        return visitor.visit(tree)


Instruction:
Can you tell what is the following Python 3 function doing
def _resolve_available(self, tokens, context):
        from .gen.ExcellentParser import ExcellentParser

        has_missing = False
        output_components = []

        for t in range(len(tokens.tokens) - 1):  # we can ignore the final EOF token
            token = tokens.get(t)
            next_token = tokens.get(t + 1)

            # if token is a NAME not followed by ( then it's a context reference
            if token.type == ExcellentParser.NAME and next_token.type != ExcellentParser.LPAREN:
                try:
                    output_components.append(context.resolve_variable(token.text))
                except EvaluationError:
                    has_missing = True
                    output_components.append(token)
            else:
                output_components.append(token)

        # if we don't have missing context references, perform evaluation as normal
        if not has_missing:
            return None

        # re-combine the tokens and context values back into an expression
        output = [self._expression_prefix]

        for output_component in output_components:
            if isinstance(output_component, Token):
                comp_val = output_component.text
            else:
                comp_val = conversions.to_repr(output_component, context)
            output.append(comp_val)

        return ''.join(output)

Response: Resolves the available context references and returns a partially evaluated expression.


Instruction:
Implement a function in Python 3 to
return an expression that is the result of evaluating the function.

Response:
def visitFunctionCall(self, ctx):
        """
        expression : fnname LPAREN parameters? RPAREN
        """
        func_name = ctx.fnname().getText()

        if ctx.parameters() is not None:
            parameters = self.visit(ctx.parameters())
        else:
            parameters = []

        return self._functions.invoke_function(self._eval_context, func_name, parameters)


Instruction:
Can you create a Python 3 function that
returns a negative integer representation of the expression.

Response:
def visitNegation(self, ctx):
        """
        expression: MINUS expression
        """
        return -conversions.to_decimal(self.visit(ctx.expression()), self._eval_context)


Instruction:
Can you write a function in Python 3 where it
returns the value of the expression EXPONENT expression as a decimal number.

Response:
def visitExponentExpression(self, ctx):
        """
        expression: expression EXPONENT expression
        """
        arg1 = conversions.to_decimal(self.visit(ctx.expression(0)), self._eval_context)
        arg2 = conversions.to_decimal(self.visit(ctx.expression(1)), self._eval_context)
        return conversions.to_decimal(decimal_pow(arg1, arg2), ctx)


Instruction:
Explain what the following Python 3 code does
def visitMultiplicationOrDivisionExpression(self, ctx):
        is_mul = ctx.TIMES() is not None

        arg1 = conversions.to_decimal(self.visit(ctx.expression(0)), self._eval_context)
        arg2 = conversions.to_decimal(self.visit(ctx.expression(1)), self._eval_context)

        if not is_mul and arg2 == Decimal(0):
            raise EvaluationError("Division by zero")

        return arg1 * arg2 if is_mul else arg1 / arg2

Response: Multiplication or division expression


Instruction:
Write a Python 3 function that can
return the value of the addition or subtraction expression as a number.

Response:
def visitAdditionOrSubtractionExpression(self, ctx):
        """
        expression: expression (PLUS | MINUS) expression
        """
        is_add = ctx.PLUS() is not None
        arg1 = self.visit(ctx.expression(0))
        arg2 = self.visit(ctx.expression(1))

        # first try as decimals
        try:
            _arg1 = conversions.to_decimal(arg1, self._eval_context)
            _arg2 = conversions.to_decimal(arg2, self._eval_context)
            return _arg1 + _arg2 if is_add else _arg1 - _arg2
        except EvaluationError:
            pass

        # then as date + something
        try:
            _arg1 = conversions.to_date_or_datetime(arg1, self._eval_context)

            if isinstance(arg2, datetime.time):
                # upgrade our date to datetime
                _arg1 = conversions.to_datetime(_arg1, self._eval_context)

                # convert time value to a duration
                _arg2 = datetime.timedelta(hours=arg2.hour, minutes=arg2.minute, seconds=arg2.second, microseconds=arg2.microsecond)
            else:
                _arg2 = datetime.timedelta(days=conversions.to_integer(arg2, self._eval_context))

            return _arg1 + _arg2 if is_add else _arg1 - _arg2

        except EvaluationError as ex:
            raise EvaluationError("Expression could not be evaluated as decimal or date arithmetic", ex)


Instruction:
How would you code a function in Python 3 to
return a boolean indicating if the value of a comparison expression is greater than or equal to a value of a key.

Response:
def visitComparisonExpression(self, ctx):
        """
        expression: expression (LTE | LT | GTE | GT) expression
        """
        arg1, arg2 = conversions.to_same(self.visit(ctx.expression(0)), self.visit(ctx.expression(1)), self._eval_context)

        if isinstance(arg1, str):
            # string comparison is case-insensitive
            compared = (arg1.lower() > arg2.lower()) - (arg1.lower() < arg2.lower())
        else:
            compared = (arg1 > arg2) - (arg1 < arg2)

        if ctx.LTE() is not None:
            return compared <= 0
        elif ctx.LT() is not None:
            return compared < 0
        elif ctx.GTE() is not None:
            return compared >= 0
        else:  # GT
            return compared > 0


Instruction:
Can you tell what is the following Python 3 function doing
def visitConcatenation(self, ctx):
        arg1 = conversions.to_string(self.visit(ctx.expression(0)), self._eval_context)
        arg2 = conversions.to_string(self.visit(ctx.expression(1)), self._eval_context)
        return arg1 + arg2

Response: return an expression AMPERSAND expression


Instruction:
Write a Python 3 script for
returning the value of the variable referenced by the given context identifier.

Response:
def visitContextReference(self, ctx):
        """
        expression: NAME
        """
        identifier = ctx.NAME().getText()
        return self._eval_context.resolve_variable(identifier)


Instruction:
Make a summary of the following Python 3 code
def load_cache(self):
        with open(self.cache_path, "rb") as f:
            print("Loading cached Zotero data...")
            cache = pickle.load(f)
            self._references = cache[self.CACHE_REFERENCE_LIST]
            self.reference_types = cache[self.CACHE_REFERENCE_TYPES]
            self.reference_templates = cache[self.CACHE_REFERENCE_TEMPLATES]
            print("Cached Zotero data loaded.")

Response: Load the cached Zotero data.


Instruction:
Create a Python 3 function to
load the distant Zotero data.

Response:
def load_distant(self):
        """Load the distant Zotero data."""
        print("Loading distant Zotero data...")
        self._references = self.get_references()
        self.reference_types = self.get_reference_types()
        self.reference_templates = self.get_reference_templates(self.reference_types)
        print("Distant Zotero data loaded.")
        self.cache()


Instruction:
Make a summary of the following Python 3 code
def cache(self):
        with open(self.cache_path, "wb") as f:
            cache = {self.CACHE_REFERENCE_LIST: self._references,
                     self.CACHE_REFERENCE_TYPES: self.reference_types,
                     self.CACHE_REFERENCE_TEMPLATES: self.reference_templates}
            pickle.dump(cache, f)

Response: Cache the Zotero data.


Instruction:
Write a Python 3 function that can
validate and create the reference in Zotero and return the created item.

Response:
def create_distant_reference(self, ref_data):
        """Validate and create the reference in Zotero and return the created item."""
        self.validate_reference_data(ref_data)
        creation_status = self._zotero_lib.create_items([ref_data])
        try:
            created_item = creation_status["successful"]["0"]
            return created_item
        except KeyError as e:
            print(creation_status)
            raise CreateZoteroItemError from e


Instruction:
Create a Python 3 function to
replace the reference in the reference list with the reference ref.

Response:
def update_local_reference(self, index, ref):
        """Replace the reference in the reference list and cache it."""
        self._references[index] = ref
        self.cache()


Instruction:
Write a Python 3 script to
validate and update the distant reference in Zotero.

Response:
def update_distant_reference(self, ref):
        """Validate and update the reference in Zotero.

        Existing fields not present will be left unmodified.
        """
        self.validate_reference_data(ref["data"])
        self._zotero_lib.update_item(ref)


Instruction:
Can you write a function in Python 3 where it
validates the reference data.

Response:
def validate_reference_data(self, ref_data):
        """Validate the reference data.

        Zotero.check_items() caches data after the first API call.
        """
        try:
            self._zotero_lib.check_items([ref_data])
        except InvalidItemFields as e:
            raise InvalidZoteroItemError from e


Instruction:
How would you explain what the following Python 3 function does
def get_reference_types(self):
        item_types = self._zotero_lib.item_types()
        return sorted([x["itemType"] for x in item_types])

Response: Return the reference types.


Instruction:
Can you implement a function in Python 3 that
returns the reference templates for the types as an ordered dictionary.

Response:
def get_reference_templates(self, ref_types):
        """Return the reference templates for the types as an ordered dictionary."""
        return OrderedDict([(x, self.get_reference_template(x)) for x in ref_types])


Instruction:
How would you code a function in Python 3 to
return the reference template for the type as an ordered dictionary.

Response:
def get_reference_template(self, ref_type):
        """Return the reference template for the type as an ordered dictionary.

        Zotero.item_template() caches data after the first API call.
        """
        template = self._zotero_lib.item_template(ref_type)
        return OrderedDict(sorted(template.items(), key=lambda x: x[0]))


Instruction:
Write a Python 3 script to
return the value of the field in extra otherwise empty.

Response:
def reference_extra_field(self, field, index):
        """Return the value of the field in 'extra', otherwise ''."""
        ref_data = self.reference_data(index)
        extra_fields = ref_data["extra"].split("\n")
        field_id = field + ":"
        matched = next((x for x in extra_fields if x.startswith(field_id)), None)
        if matched:
            return matched.replace(field_id, "", 1).strip()
        else:
            return ""


Instruction:
Write a Python 3 function that can
return the reference ID for the specified index.

Response:
def reference_id(self, index):
        """Return the reference ID (locally defined)."""
        # TODO Include ISBN and ISSN?
        doi = self.reference_doi(index)
        if doi:
            return doi
        else:
            pmid = self.reference_pmid(index)
            if pmid:
                return "PMID_" + pmid
            else:
                unpublished_id = self.reference_unpublished_id(index)
                if unpublished_id:
                    return "UNPUBLISHED_" + unpublished_id
        return ""


Instruction:
Can you implement a function in Python 3 that
returns the reference DOI.

Response:
def reference_doi(self, index):
        """Return the reference DOI."""
        return self.reference_data(index).get("DOI", self.reference_extra_field("DOI", index))


Instruction:
How would you code a function in Python 3 to
return as a list the surnames of the reference creators ( locally defined.

Response:
def reference_creator_surnames(self, index):
        """Return as a list the surnames of the reference creators (locally defined)."""
        # TODO Not true, ex: ISBN 978-1-4398-3778-8. Return all creator types?
        # Academic books published as a collection of chapters contributed by
        # different authors have editors but not authors at the level of the
        # book (as opposed to the level of a chapter).
        creators = self.reference_data(index)["creators"]
        creator_types = [x["creatorType"] for x in creators]
        # 'name' (not split) might be used instead of 'firstName' and 'lastName'.
        try:
            if "author" in creator_types:
                return [x["lastName"] for x in creators if x["creatorType"] == "author"]
            else:
                return [x["lastName"] for x in creators]
        except KeyError:
            return []


Instruction:
Can you create a Python 3 function that
returns the reference publication year.

Response:
def reference_year(self, index):
        """Return the reference publication year."""
        # TODO Use meta:parsedDate field instead?
        ref_date = self.reference_date(index)
        try:
            # NB: datetime.year returns an int.
            return parse(ref_date).year
        except ValueError:
            matched = re.search(r"\d{4}", ref_date)
            if matched:
                return int(matched.group())
            else:
                return ""


Instruction:
Given the following Python 3 function, write the documentation
def reference_journal(self, index):
        # TODO Change the column name 'Journal' to an other?
        ref_type = self.reference_type(index)
        if ref_type == "journalArticle":
            return self.reference_data(index)["publicationTitle"]
        else:
            return "({})".format(ref_type)

Response: Return the reference journal name.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def reference_index(self, ref_id):
        try:
            indexes = range(self.reference_count())
            return next(i for i in indexes if self.reference_id(i) == ref_id)
        except StopIteration as e:
            raise ReferenceNotFoundError("ID: " + ref_id) from e

Response: Return the first reference with this ID.


Instruction:
Write a Python 3 function for
returning for citation the creator surnames and the publication year.

Response:
def reference_creators_citation(self, ref_id):
        """Return for citation the creator surnames (locally defined) and the publication year."""
        # FIXME Delayed refactoring. Use an index instead of an ID.
        index = self.reference_index(ref_id)
        creators = self.reference_creator_surnames(index)
        creator_count = len(creators)
        if creator_count == 0:
            return ""
        year = self.reference_year(index)
        if creator_count == 1:
            return "{} ({})".format(creators[0], year)
        elif creator_count == 2:
            return "{} and {} ({})".format(creators[0], creators[1], year)
        else:
            return "{} et al. ({})".format(creators[0], year)


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

Response:
def computePDFSimilarity(paperId, userPDF):
    if not isPDFInDb(paperId):
         return None

    userPDF.save("temp.pdf")
    # check_call is blocking
    check_call(['pdftotext', '-enc', 'UTF-8', "temp.pdf", "temp.txt"])
    os.remove("temp.pdf")

    a = open("temp.txt", 'r').read()
    b = open(join(dbPath, paperId) + ".txt", 'r').read()

    import nltk, string
    from sklearn.feature_extraction.text import TfidfVectorizer

    stemmer = nltk.stem.porter.PorterStemmer()
    remove_punctuation_map = dict((ord(char), None) for char in string.punctuation)

    def stem_tokens(tokens):
        return [stemmer.stem(item) for item in tokens]

    '''remove punctuation, lowercase, stem'''
    def normalize(text):
        return stem_tokens(nltk.word_tokenize(text.lower().translate(remove_punctuation_map)))

    vectorizer = TfidfVectorizer(tokenizer=normalize, stop_words='english')

    def cosine_sim(text1, text2):
        tfidf = vectorizer.fit_transform([text1, text2])
        return ((tfidf * tfidf.T).A)[0,1]

    similarity = cosine_sim(a, b)

    os.remove("temp.txt")

    return similarity


Instruction:
Explain what the following Python 3 code does
def getChildren(root_id, maxDepth=100, relationshipType="subClassOf",
                 alwaysFetch=False):
    """
     Accessing web-based ontology service is too long, so we cache the
     information in a pickle file and query the services only if the info
     has not already been cached.
    """
    childrenDic = {}
    fileName = os.path.join(os.path.dirname(__file__), "children.bin")

    #### CHECK FOR CASE OF BBP TAGS
    if root_id[:4] == "BBP_":

        if not alwaysFetch:
            try:
                with open(fileName, "rb") as childrenFile:
                    childrenDic = pickle.load(childrenFile)

                if root_id in childrenDic:
                    return childrenDic[root_id]
            except:
                pass

        childrenDic[root_id] = getBBPChildren(root_id)

        if not alwaysFetch:
            try:
                with open(fileName, "wb") as childrenFile:
                    pickle.dump(childrenDic, childrenFile)
            except:
                pass

        return childrenDic[root_id]

    ## TODO: should also check for BBP children of online onto terms

    if root_id in nlx2ks:
        root_id = nlx2ks[root_id]

    if not alwaysFetch:
        try:
            with open(fileName, "rb") as childrenFile:
                childrenDic = pickle.load(childrenFile)

            if root_id in childrenDic:
                return childrenDic[root_id]
        except:
            pass

    direction="INCOMING"
    #neighbors = graph.getNeighbors(root_id, depth=maxDepth,
    #                               relationshipType=relationshipType,
    #                               direction=direction)

    baseKS  = "http://matrix.neuinfo.org:9000"
    response = requests.get(baseKS + "/scigraph/graph/neighbors/" +
                            root_id + "?direction=" + direction +
                            "&depth=" + str(maxDepth) +
                            "&project=%2A&blankNodes=false&relationshipType="
                            + relationshipType)

    if not response.ok:
        return {}

    neighbors = response.json()

    if neighbors is None:
        return {}

    nodes = neighbors["nodes"]
    #for node in np.array(nodes):
    #    node["lbl"] = node["lbl"].encode('utf-8').decode('utf-8')

    #try:
    #    assert(np.all([not node["lbl"] is None for node in np.array(nodes)]))
    #except AssertionError:
    #    for node in np.array(nodes):
    #        if node["lbl"] is None:
    #            print(node["id"])
    #    raise

    # TODO: replace by the commented line below. This patch is only to
    #       accomodate for a current issue with the knowledge-space endpoint.
    #childrenDic[root_id]  = OntoDic({node["id"]:node["lbl"] for node in np.array(nodes)})
    childrenDic[root_id]  = OntoDic({node["id"]:node["lbl"] for node in np.array(nodes) if not node["lbl"] is None})

    if not alwaysFetch:
        try:
            with open(fileName, "wb") as childrenFile:
                pickle.dump(childrenDic, childrenFile)
        except:
            pass

    return childrenDic[root_id]

Response: Get the list of children of a given root_id.


Instruction:
Here you have a function in Python 3, explain what it does
def install_plugins(plugins, app, install_type, config):
    try:
        disable_plugins = config.disable_plugins
        if not disable_plugins:
            disable_plugins = []
    except AttributeError:
        disable_plugins = []
    for plugin_name in plugins:
        plugin_group = f'backendai_{plugin_name}_v10'
        registry = PluginRegistry(plugin_name)
        for entrypoint in pkg_resources.iter_entry_points(plugin_group):
            if entrypoint.name in disable_plugins:
                continue
            log.info('Installing plugin: {}.{}', plugin_group, entrypoint.name)
            plugin_module = entrypoint.load()
            plugin = getattr(plugin_module, 'get_plugin')(config)
            registry.register(plugin)
            if install_type == 'attr':
                setattr(app, plugin_name, registry)
            elif install_type == 'dict':
                assert isinstance(app, typing.MutableMapping), \
                    (f"app must be an instance of MutableMapping "
                     f"for 'dict' install_type.")
                app[plugin_name] = registry
            else:
                raise ValueError(f'Invalid install type: {install_type}')

Response: Automatically installs plugins into the app.


Instruction:
Implement a Python 3 function for
adding a new element to the set.

Response:
def addElement(self,etype='hex8',corners=[-1.0,-1.0,-1.0,1.,-1.0,-1.0,1.0,1.0,-1.0,-1.0,1.0,-1.0,-1.0,-1.0,1.0,1.0,-1.0,1.0,1.0,1.0,1.0,-1.0,1.0,1.0],name='new_elem'):
        '''
        corners - list of nodal coordinates properly ordered for element type (counter clockwise)
        '''
        lastelm = self.elements[-1][1]
        lastnode = self.nodes[-1][0]
        elm = [etype,lastelm+1]
        for i in range(old_div(len(corners),3)):
            elm.append(lastnode+1+i)

        self.elements.append(elm)
        self.elsets['e'+name] = {}
        self.elsets['e'+name][int(elm[1])] = True

        cnt = 1
        self.nsets['n'+name] = []
        for i in range(0,len(corners),3):
            self.nodes.append([lastnode+cnt, corners[i], corners[i+1], corners[i+2]])
            self.nsets['n'+name].append(lastnode+cnt)
            cnt += 1

        # if this is a quad4 or tri3 element make a surface set
        if etype == 'quad4' or etype == 'tri3':
            self.fsets['f'+name] = [[etype, MeshDef.facetID, lastnode+1, lastnode+2, lastnode+3, lastnode+4]]
            MeshDef.facetID += 1


Instruction:
Can you generate the documentation for the following Python 3 function
def getEdges(self, type, entail='true', limit=100, skip=0, callback=None, output='application/json'):

        kwargs = {'type':type, 'entail':entail, 'limit':limit, 'skip':skip, 'callback':callback}
        kwargs = {k:dumps(v) if type(v) is dict else v for k, v in kwargs.items()}
        param_rest = self._make_rest('type', **kwargs)
        url = self._basePath + ('/graph/edges/{type}').format(**kwargs)
        requests_params = {k:v for k, v in kwargs.items() if k != 'type'}
        return self._get('GET', url, requests_params, output)

Response: Get the nodes connected by an edge type from the graph.