Instruction:
Can you generate a brief explanation for the following Python 3 code
def make_fileitem_peinfo_importedmodules_module_importedfunctions_string(imported_function, condition='is',
                                                                         negate=False, preserve_case=False):
    """
    Create a node for FileItem/PEInfo/ImportedModules/Module/ImportedFunctions/string

    :return: A IndicatorItem represented as an Element node
    """
    document = 'FileItem'
    search = 'FileItem/PEInfo/ImportedModules/Module/ImportedFunctions/string'
    content_type = 'string'
    content = imported_function
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node

Response: Creates a IndicatorItem node for FileItem / PEInfo / ImportedModules / Module / ImportedFunctions / string


Instruction:
Can you create a Python 3 function that
creates a IndicatorItem node for FileItem / PEInfo / ImportedModules / Module / Name

Response:
def make_fileitem_peinfo_importedmodules_module_name(imported_module, condition='is', negate=False,
                                                     preserve_case=False):
    """
    Create a node for FileItem/PEInfo/ImportedModules/Module/Name

    :return: A IndicatorItem represented as an Element node
    """
    document = 'FileItem'
    search = 'FileItem/PEInfo/ImportedModules/Module/Name'
    content_type = 'string'
    content = imported_module
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node


Instruction:
Write a Python 3 function that can
create a IndicatorItem node for FileItem / PEInfo / PETimeStamp

Response:
def make_fileitem_peinfo_petimestamp(compile_time, condition='is', negate=False):
    """
    Create a node for FileItem/PEInfo/PETimeStamp

    :return: A IndicatorItem represented as an Element node
    """
    document = 'FileItem'
    search = 'FileItem/PEInfo/PETimeStamp'
    content_type = 'date'
    content = compile_time
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate)
    return ii_node


Instruction:
Create a Python 3 function for
creating a IndicatorItem node for FileItem / PEInfo / ResourceInfoItem / Name

Response:
def make_fileitem_peinfo_resourceinfolist_resourceinfoitem_name(resource_name, condition='is', negate=False,
                                                                preserve_case=False):
    """
    Create a node for FileItem/PEInfo/ResourceInfoList/ResourceInfoItem/Name

    :return: A IndicatorItem represented as an Element node
    """
    document = 'FileItem'
    search = 'FileItem/PEInfo/ResourceInfoList/ResourceInfoItem/Name'
    content_type = 'string'
    content = resource_name
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node


Instruction:
Can you generate a brief explanation for the following Python 3 code
def make_fileitem_peinfo_type(petype, condition='is', negate=False, preserve_case=False):
    document = 'FileItem'
    search = 'FileItem/PEInfo/Type'
    content_type = 'string'
    content = petype
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node

Response: Create a IndicatorItem node for FileItem / PEInfo / Type


Instruction:
Create a Python 3 function for
creating a IndicatorItem node for FileItem / SizeInBytes

Response:
def make_fileitem_sizeinbytes(filesize, condition='is', negate=False):
    """
    Create a node for FileItem/SizeInBytes

    :return: A IndicatorItem represented as an Element node
    """
    document = 'FileItem'
    search = 'FileItem/SizeInBytes'
    content_type = 'int'
    content = filesize
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate)
    return ii_node


Instruction:
Can you generate a brief explanation for the following Python 3 code
def make_fileitem_streamlist_stream_name(stream_name, condition='is', negate=False, preserve_case=False):
    document = 'FileItem'
    search = 'FileItem/StreamList/Stream/Name'
    content_type = 'string'
    content = stream_name
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node

Response: Create a IndicatorItem node for FileItem / StreamList / Stream / Name


Instruction:
Here you have a function in Python 3, explain what it does
def make_fileitem_stringlist_string(file_string, condition='contains', negate=False, preserve_case=False):
    document = 'FileItem'
    search = 'FileItem/StringList/string'
    content_type = 'string'
    content = file_string
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node

Response: Create a IndicatorItem node for FileItem / StringList / string


Instruction:
Can you implement a function in Python 3 that
creates a IndicatorItem node for FileItem / Username

Response:
def make_fileitem_username(file_owner, condition='is', negate=False, preserve_case=False):
    """
    Create a node for FileItem/Username

    :return: A IndicatorItem represented as an Element node
    """
    document = 'FileItem'
    search = 'FileItem/Username'
    content_type = 'string'
    content = file_owner
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node


Instruction:
Can you write a function in Python 3 where it
creates a IndicatorItem node for a HookItem / HookedFunction node

Response:
def make_hookitem_hookedfunction(hooked_function, condition='is', negate=False, preserve_case=False):
    """
    Create a node for HookItem/HookedFunction

    :return: A IndicatorItem represented as an Element node
    """
    document = 'HookItem'
    search = 'HookItem/HookedFunction'
    content_type = 'string'
    content = hooked_function
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node


Instruction:
Can you implement a function in Python 3 that
creates a IndicatorItem node for HookItem / HookingModule

Response:
def make_hookitem_hookingmodule(hooking_module, condition='contains', negate=False, preserve_case=False):
    """
    Create a node for HookItem/HookingModule

    :return: A IndicatorItem represented as an Element node
    """
    document = 'HookItem'
    search = 'HookItem/HookingModule'
    content_type = 'string'
    content = hooking_module
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node


Instruction:
Make a summary of the following Python 3 code
def make_portitem_remoteport(remote_port, condition='is', negate=False):
    document = 'PortItem'
    search = 'PortItem/remotePort'
    content_type = 'int'
    content = remote_port
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate)
    return ii_node

Response: Create a IndicatorItem node for PortItem and RemotePort


Instruction:
Can you generate the documentation for the following Python 3 function
def make_prefetchitem_accessedfilelist_accessedfile(accessed_file, condition='contains', negate=False,
                                                    preserve_case=False):
    """
    Create a node for PrefetchItem/AccessedFileList/AccessedFile

    :return: A IndicatorItem represented as an Element node
    """
    document = 'PrefetchItem'
    search = 'PrefetchItem/AccessedFileList/AccessedFile'
    content_type = 'string'
    content = accessed_file
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node

Response: Create a IndicatorItem node for PrefetchItem / AccessedFileList


Instruction:
Can you create a Python 3 function that
creates a IndicatorItem node for PrefetchItem and ApplicationFileName

Response:
def make_prefetchitem_applicationfilename(application_filename, condition='is', negate=False, preserve_case=False):
    """
    Create a node for PrefetchItem/ApplicationFileName

    :return: A IndicatorItem represented as an Element node
    """
    document = 'PrefetchItem'
    search = 'PrefetchItem/ApplicationFileName'
    content_type = 'string'
    content = application_filename
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node


Instruction:
Can you generate a brief explanation for the following Python 3 code
def make_prefetchitem_applicationfullpath(application_fullpath, condition='contains', negate=False,
                                          preserve_case=False):
    """
    Create a node for PrefetchItem/ApplicationFullPath

    :return: A IndicatorItem represented as an Element node
    """
    document = 'PrefetchItem'
    search = 'PrefetchItem/ApplicationFullPath'
    content_type = 'string'
    content = application_fullpath
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node

Response: Create a IndicatorItem node for PrefetchItem and ApplicationFullPath


Instruction:
Write a Python 3 function that can
create a IndicatorItem node for a ProcessItem / HandleList / Handle / Name node

Response:
def make_processitem_handlelist_handle_name(handle_name, condition='contains', negate=False, preserve_case=False):
    """
    Create a node for ProcessItem/HandleList/Handle/Name

    :return: A IndicatorItem represented as an Element node
    """
    document = 'ProcessItem'
    search = 'ProcessItem/HandleList/Handle/Name'
    content_type = 'string'
    content = handle_name
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node


Instruction:
Here you have a function in Python 3, explain what it does
def make_processitem_portlist_portitem_remoteip(remote_ip, condition='is', negate=False):
    document = 'ProcessItem'
    search = 'ProcessItem/PortList/PortItem/remoteIP'
    content_type = 'IP'
    content = remote_ip
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate)
    return ii_node

Response: Create a IndicatorItem node for a ProcessItem / PortList / PortItem / remoteIP


Instruction:
Make a summary of the following Python 3 code
def make_processitem_sectionlist_memorysection_name(section_name, condition='contains', negate=False,
                                                    preserve_case=False):
    """
    Create a node for ProcessItem/SectionList/MemorySection/Name

    :return: A IndicatorItem represented as an Element node
    """
    document = 'ProcessItem'
    search = 'ProcessItem/SectionList/MemorySection/Name'
    content_type = 'string'
    content = section_name
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node

Response: Create a IndicatorItem node for a ProcessItem section list memory section name


Instruction:
Create a Python 3 function to
create a IndicatorItem node for Exported Functions in Memory Section List

Response:
def make_processitem_sectionlist_memorysection_peinfo_exports_exportedfunctions_string(export_function, condition='is',
                                                                                negate=False,
                                                                                preserve_case=False):
    """
    Create a node for ProcessItem/SectionList/MemorySection/PEInfo/Exports/ExportedFunctions/string

    :return: A IndicatorItem represented as an Element node
    """
    document = 'ProcessItem'
    search = 'ProcessItem/SectionList/MemorySection/PEInfo/Exports/ExportedFunctions/string'
    content_type = 'string'
    content = export_function
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node


Instruction:
Can you tell what is the following Python 3 function doing
def make_processitem_stringlist_string(string, condition='contains', negate=False, preserve_case=False):
    document = 'ProcessItem'
    search = 'ProcessItem/StringList/string'
    content_type = 'string'
    content = string
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node

Response: Create a IndicatorItem node for a ProcessItem / StringList


Instruction:
Given the following Python 3 function, write the documentation
def make_processitem_username(username, condition='contains', negate=False, preserve_case=False):
    document = 'ProcessItem'
    search = 'ProcessItem/Username'
    content_type = 'string'
    content = username
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node

Response: Create a IndicatorItem node for a ProcessItem with the given username


Instruction:
Can you tell what is the following Python 3 function doing
def make_processitem_arguments(arguments, condition='contains', negate=False, preserve_case=False):
    document = 'ProcessItem'
    search = 'ProcessItem/arguments'
    content_type = 'string'
    content = arguments
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node

Response: Create a node for ProcessItem / arguments


Instruction:
Here you have a function in Python 3, explain what it does
def make_processitem_path(path, condition='contains', negate=False, preserve_case=False):
    document = 'ProcessItem'
    search = 'ProcessItem/path'
    content_type = 'string'
    content = path
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node

Response: Create a IndicatorItem node for a ProcessItem with a given path


Instruction:
How would you explain what the following Python 3 function does
def make_registryitem_keypath(keypath, condition='contains', negate=False, preserve_case=False):
    document = 'RegistryItem'
    search = 'RegistryItem/KeyPath'
    content_type = 'string'
    content = keypath
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node

Response: Create a IndicatorItem node for RegistryItem and KeyPath


Instruction:
Write a Python 3 script to
create a node for RegistryItem and Text node for RegistryItem

Response:
def make_registryitem_text(text, condition='contains', negate=False, preserve_case=False):
    """
    Create a node for RegistryItem/Text

    :return: A IndicatorItem represented as an Element node
    """
    document = 'RegistryItem'
    search = 'RegistryItem/Text'
    content_type = 'string'
    content = text
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node


Instruction:
Write a Python 3 function for
creating a IndicatorItem node for RegistryItem ValueName

Response:
def make_registryitem_valuename(valuename, condition='is', negate=False, preserve_case=False):
    """
    Create a node for RegistryItem/ValueName

    :return: A IndicatorItem represented as an Element node
    """
    document = 'RegistryItem'
    search = 'RegistryItem/ValueName'
    content_type = 'string'
    content = valuename
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node


Instruction:
Explain what the following Python 3 code does
def make_serviceitem_description(description, condition='contains', negate=False, preserve_case=False):
    document = 'ServiceItem'
    search = 'ServiceItem/description'
    content_type = 'string'
    content = description
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node

Response: Create a IndicatorItem node for ServiceItem and description


Instruction:
Can you create a Python 3 function that
creates a IndicatorItem node for ServiceItem and descriptiveName

Response:
def make_serviceitem_descriptivename(descriptive_name, condition='is', negate=False, preserve_case=False):
    """
    Create a node for ServiceItem/descriptiveName

    :return: A IndicatorItem represented as an Element node
    """
    document = 'ServiceItem'
    search = 'ServiceItem/descriptiveName'
    content_type = 'string'
    content = descriptive_name
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node


Instruction:
Write a Python 3 script for
creating a IndicatorItem node for ServiceItem and name

Response:
def make_serviceitem_name(name, condition='is', negate=False, preserve_case=False):
    """
    Create a node for ServiceItem/name

    :return: A IndicatorItem represented as an Element node
    """
    document = 'ServiceItem'
    search = 'ServiceItem/name'
    content_type = 'string'
    content = name
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node


Instruction:
Here you have a function in Python 3, explain what it does
def make_serviceitem_pathmd5sum(path_md5, condition='is', negate=False):
    document = 'ServiceItem'
    search = 'ServiceItem/pathmd5sum'
    content_type = 'md5'
    content = path_md5
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate)
    return ii_node

Response: Create a IndicatorItem node for ServiceItem / pathmd5sum


Instruction:
Can you write a function in Python 3 where it
creates a IndicatorItem node for ServiceItem and serviceDLL

Response:
def make_serviceitem_servicedll(servicedll, condition='contains', negate=False, preserve_case=False):
    """
    Create a node for ServiceItem/serviceDLL

    :return: A IndicatorItem represented as an Element node
    """
    document = 'ServiceItem'
    search = 'ServiceItem/serviceDLL'
    content_type = 'string'
    content = servicedll
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node


Instruction:
Explain what the following Python 3 code does
def make_serviceitem_servicedllsignatureexists(dll_sig_exists, condition='is', negate=False):
    document = 'ServiceItem'
    search = 'ServiceItem/serviceDLLSignatureExists'
    content_type = 'bool'
    content = dll_sig_exists
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate)
    return ii_node

Response: Create a IndicatorItem node for ServiceItem / serviceDLLSignatureExists


Instruction:
Can you tell what is the following Python 3 function doing
def make_serviceitem_servicedllsignatureverified(dll_sig_verified, condition='is', negate=False):
    document = 'ServiceItem'
    search = 'ServiceItem/serviceDLLSignatureVerified'
    content_type = 'bool'
    content = dll_sig_verified
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate)
    return ii_node

Response: Create a IndicatorItem node for ServiceItem / serviceDLLSignatureVerified


Instruction:
Explain what the following Python 3 code does
def make_serviceitem_servicedllmd5sum(servicedll_md5, condition='is', negate=False):
    document = 'ServiceItem'
    search = 'ServiceItem/serviceDLLmd5sum'
    content_type = 'md5'
    content = servicedll_md5
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate)
    return ii_node

Response: Create a IndicatorItem node for ServiceItem / serviceDLLmd5sum


Instruction:
How would you implement a function in Python 3 that
creates a IndicatorItem node for a SystemInfoItem with the given hostname

Response:
def make_systeminfoitem_hostname(hostname, condition='contains', negate=False, preserve_case=False):
    """
    Create a node for SystemInfoItem/hostname

    :return: A IndicatorItem represented as an Element node
    """
    document = 'SystemInfoItem'
    search = 'SystemInfoItem/hostname'
    content_type = 'string'
    content = hostname
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node


Instruction:
Explain what the following Python 3 code does
def make_systemrestoreitem_originalfilename(original_filename, condition='contains', negate=False, preserve_case=False):
    document = 'SystemRestoreItem'
    search = 'SystemRestoreItem/OriginalFileName'
    content_type = 'string'
    content = original_filename
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node

Response: Create a IndicatorItem node for a SystemRestoreItem with the original filename.


Instruction:
Given the following Python 3 function, write the documentation
def make_fileitem_peinfo_versioninfoitem(key, value, condition='is', negate=False, preserve_case=False):
    document = 'FileItem'
    search = 'FileItem/PEInfo/VersionInfoList/VersionInfoItem/' + key  # XXX: No validation of key done.
    content_type = 'string'
    content = value
    ii_node = ioc_api.make_indicatoritem_node(condition, document, search, content_type, content,
                                              negate=negate, preserve_case=preserve_case)
    return ii_node

Response: Create a IndicatorItem node for FileItem / PEInfo / VersionInfoList / VersionInfoItem


Instruction:
Can you write a function in Python 3 where it
fixes the ordering of children under the criteria node to ensure that IndicatorItem or Indicator order is preserved as per XML Schema.

Response:
def fix_schema_node_ordering(parent):
        """
        Fix the ordering of children under the criteria node to ensure that IndicatorItem/Indicator order
         is preserved, as per XML Schema.
        :return:
        """
        children = parent.getchildren()
        i_nodes = [node for node in children if node.tag == 'IndicatorItem']
        ii_nodes = [node for node in children if node.tag == 'Indicator']
        if not ii_nodes:
            return
        # Remove all the children
        for node in children:
            parent.remove(node)
        # Add the Indicator nodes back
        for node in i_nodes:
            parent.append(node)
        # Now add the IndicatorItem nodes back
        for node in ii_nodes:
            parent.append(node)
        # Now recurse
        for node in ii_nodes:
            fix_schema_node_ordering(node)


Instruction:
Here you have a function in Python 3, explain what it does
def make_indicator_node(operator, nid=None):
    if operator.upper() not in VALID_INDICATOR_OPERATORS:
        raise ValueError('Indicator operator must be in [{}].'.format(VALID_INDICATOR_OPERATORS))
    i_node = et.Element('Indicator')
    if nid:
        i_node.attrib['id'] = nid
    else:
        i_node.attrib['id'] = ioc_et.get_guid()
    i_node.attrib['operator'] = operator.upper()
    return i_node

Response: This function makes a Indicator node element.


Instruction:
Make a summary of the following Python 3 code
def make_indicatoritem_node(condition,
                            document,
                            search,
                            content_type,
                            content,
                            preserve_case=False,
                            negate=False,
                            context_type='mir',
                            nid=None):
    """
    This makes a IndicatorItem element.  This contains the actual threat intelligence in the IOC.

    :param condition: This is the condition of the item ('is', 'contains', 'matches', etc). The following contants in ioc_api may be used:
==================== =====================================================
Constant             Meaning
==================== =====================================================
ioc_api.IS           Exact String match.
ioc_api.CONTAINS     Substring match.
ioc_api.MATCHES      Regex match.
ioc_api.STARTS_WITH  String match at the beginning of a string.
ioc_api.ENDS_WITH    String match at the end of a string.
ioc_api.GREATER_THAN Integer match indicating a greater than (>) operation.
ioc_api.LESS_THAN    Integer match indicator a less than (<) operation.
==================== =====================================================

    :param document: Denotes the type of document to look for the encoded artifact in.
    :param search: Specifies what attribute of the document type the encoded value is.
    :param content_type: This is the display type of the item. This is normally derived from the iocterm for the search value.
    :param content: The threat intelligence that is being encoded.
    :param preserve_case: Specifiy that the content should be treated in a case sensitive manner.
    :param negate: Specifify that the condition is negated. An example of this is:
       @condition = 'is' & @negate = 'true' would be equal to the
       @condition = 'isnot' in OpenIOC 1.0.
    :param context_type: Gives context to the document/search information.
    :param nid: This is used to provide a GUID for the IndicatorItem. The ID should NOT be specified under normal
     circumstances.
    :return: an elementTree Element item
    """
    # validate condition
    if condition not in VALID_INDICATORITEM_CONDITIONS:
        raise ValueError('Invalid IndicatorItem condition [{}]'.format(condition))
    ii_node = et.Element('IndicatorItem')
    if nid:
        ii_node.attrib['id'] = nid
    else:
        ii_node.attrib['id'] = ioc_et.get_guid()
    ii_node.attrib['condition'] = condition
    if preserve_case:
        ii_node.attrib['preserve-case'] = 'true'
    else:
        ii_node.attrib['preserve-case'] = 'false'
    if negate:
        ii_node.attrib['negate'] = 'true'
    else:
        ii_node.attrib['negate'] = 'false'
    context_node = ioc_et.make_context_node(document, search, context_type)
    content_node = ioc_et.make_content_node(content_type, content)
    ii_node.append(context_node)
    ii_node.append(content_node)
    return ii_node

Response: This function creates an IndicatorItem element.


Instruction:
Here you have a function in Python 3, explain what it does
def get_top_level_indicator_node(root_node):
    if root_node.tag != 'OpenIOC':
        raise IOCParseError('Root tag is not "OpenIOC" [{}].'.format(root_node.tag))
    elems = root_node.xpath('criteria/Indicator')
    if len(elems) == 0:
        log.warning('No top level Indicator node found.')
        return None
    elif len(elems) > 1:
        log.warning('Multiple top level Indicator nodes found.  This is not a valid MIR IOC.')
        return None
    else:
        top_level_indicator_node = elems[0]
    if top_level_indicator_node.get('operator').lower() != 'or':
        log.warning('Top level Indicator/@operator attribute is not "OR".  This is not a valid MIR IOC.')
    return top_level_indicator_node

Response: This returns the first top level Indicator node under the criteria node.


Instruction:
How would you implement a function in Python 3 that
serializes an IOC as defined by a set of etree Elements to a. IOC file.

Response:
def write_ioc(root, output_dir=None, force=False):
    """
    Serialize an IOC, as defined by a set of etree Elements, to a .IOC file.

    :param root: etree Element to write out.  Should have the tag 'OpenIOC'
    :param output_dir: Directory to write the ioc out to.  default is current working directory.
    :param force: If set, skip the root node tag check.
    :return: True, unless an error occurs while writing the IOC.
    """
    root_tag = 'OpenIOC'
    if not force and root.tag != root_tag:
        raise ValueError('Root tag is not "{}".'.format(root_tag))
    default_encoding = 'utf-8'
    tree = root.getroottree()
    # noinspection PyBroadException
    try:
        encoding = tree.docinfo.encoding
    except:
        log.debug('Failed to get encoding from docinfo')
        encoding = default_encoding
    ioc_id = root.attrib['id']
    fn = ioc_id + '.ioc'
    if output_dir:
        fn = os.path.join(output_dir, fn)
    else:
        fn = os.path.join(os.getcwd(), fn)
    try:
        with open(fn, 'wb') as fout:
            fout.write(et.tostring(tree, encoding=encoding, xml_declaration=True, pretty_print=True))
    except (IOError, OSError):
        log.exception('Failed to write out IOC')
        return False
    except:
        raise
    return True


Instruction:
Can you tell what is the following Python 3 function doing
def write_ioc_string(root, force=False):
    root_tag = 'OpenIOC'
    if not force and root.tag != root_tag:
        raise ValueError('Root tag is not "{}".'.format(root_tag))
    default_encoding = 'utf-8'
    tree = root.getroottree()
    # noinspection PyBroadException
    try:
        encoding = tree.docinfo.encoding
    except:
        log.debug('Failed to get encoding from docinfo')
        encoding = default_encoding
    return et.tostring(tree, encoding=encoding, xml_declaration=True, pretty_print=True)

Response: Serialize an IOC to a string.


Instruction:
Write a Python 3 function for
opening an IOC file or XML string and returns the root element top level Indicator element and Parameters element.

Response:
def open_ioc(fn):
        """
        Opens an IOC file, or XML string.  Returns the root element, top level
        indicator element, and parameters element.  If the IOC or string fails
        to parse, an IOCParseError is raised.

        This is a helper function used by __init__.

        :param fn: This is a path to a file to open, or a string containing XML representing an IOC.
        :return: a tuple containing three elementTree Element objects
         The first element, the root, contains the entire IOC itself.
         The second element, the top level OR indicator, allows the user to add
          additional IndicatorItem or Indicator nodes to the IOC easily.
         The third element, the parameters node, allows the user to quickly
          parse the parameters.
        """
        parsed_xml = xmlutils.read_xml_no_ns(fn)
        if not parsed_xml:
            raise IOCParseError('Error occured parsing XML')
        root = parsed_xml.getroot()
        metadata_node = root.find('metadata')
        top_level_indicator = get_top_level_indicator_node(root)
        parameters_node = root.find('parameters')
        if parameters_node is None:
            # parameters node is not required by schema; but we add it if it is not present
            parameters_node = ioc_et.make_parameters_node()
            root.append(parameters_node)
        return root, metadata_node, top_level_indicator, parameters_node


Instruction:
Can you generate the documentation for the following Python 3 function
def make_ioc(name=None,
                 description='Automatically generated IOC',
                 author='IOC_api',
                 links=None,
                 keywords=None,
                 iocid=None):
        """
        This generates all parts of an IOC, but without any definition.

        This is a helper function used by __init__.

        :param name: string, Name of the ioc
        :param description: string, description of the ioc
        :param author: string, author name/email address
        :param links: ist of tuples.  Each tuple should be in the form (rel, href, value).
        :param keywords: string.  This is normally a space delimited string of values that may be used as keywords
        :param iocid: GUID for the IOC.  This should not be specified under normal circumstances.
        :return: a tuple containing three elementTree Element objects
         The first element, the root, contains the entire IOC itself.
         The second element, the top level OR indicator, allows the user to add
          additional IndicatorItem or Indicator nodes to the IOC easily.
         The third element, the parameters node, allows the user to quickly
          parse the parameters.
        """
        root = ioc_et.make_ioc_root(iocid)
        root.append(ioc_et.make_metadata_node(name, description, author, links, keywords))
        metadata_node = root.find('metadata')
        top_level_indicator = make_indicator_node('OR')
        parameters_node = (ioc_et.make_parameters_node())
        root.append(ioc_et.make_criteria_node(top_level_indicator))
        root.append(parameters_node)
        ioc_et.set_root_lastmodified(root)
        return root, metadata_node, top_level_indicator, parameters_node

Response: This function generates all parts of an IOC.


Instruction:
Can you generate the documentation for the following Python 3 function
def set_lastmodified_date(self, date=None):
        if date:
            match = re.match(DATE_REGEX, date)
            if not match:
                raise IOCParseError('last-modified date is not valid.  Must be in the form YYYY-MM-DDTHH:MM:SS')
        ioc_et.set_root_lastmodified(self.root, date)
        return True

Response: Set the last modified date of a IOC to the current date.


Instruction:
Write a Python 3 script for
setting the published date of a IOC to the current date.

Response:
def set_published_date(self, date=None):
        """
        Set the published date of a IOC to the current date.
        User may specify the date they want to set as well.

        :param date: Date value to set the published date to.  This should be in the xsdDate form.
         This defaults to the current date if it is not provided.
         xsdDate Form: YYYY-MM-DDTHH:MM:SS
        :return: True
        :raises: IOCParseError if date format is not valid.
        """
        if date:
            match = re.match(DATE_REGEX, date)
            if not match:
                raise IOCParseError('Published date is not valid.  Must be in the form YYYY-MM-DDTHH:MM:SS')
        ioc_et.set_root_published_date(self.root, date)
        return True


Instruction:
Can you implement a function in Python 3 that
sets the created date of a IOC to the current date.

Response:
def set_created_date(self, date=None):
        """
        Set the created date of a IOC to the current date.
        User may specify the date they want to set as well.

        :param date: Date value to set the created date to.  This should be in the xsdDate form.
         This defaults to the current date if it is not provided.
         xsdDate form: YYYY-MM-DDTHH:MM:SS
        :return: True
        :raises: IOCParseError if date format is not valid.
        """
        if date:
            match = re.match(DATE_REGEX, date)
            if not match:
                raise IOCParseError('Created date is not valid.  Must be in the form YYYY-MM-DDTHH:MM:SS')
        # XXX can this use self.metadata?
        ioc_et.set_root_created_date(self.root, date)
        return True


Instruction:
Can you create a Python 3 function that
adds a parameter to the IOC.

Response:
def add_parameter(self, indicator_id, content, name='comment', ptype='string'):
        """
        Add a a parameter to the IOC.

        :param indicator_id: The unique Indicator/IndicatorItem id the parameter is associated with.
        :param content: The value of the parameter.
        :param name: The name of the parameter.
        :param ptype: The type of the parameter content.
        :return: True
        :raises: IOCParseError if the indicator_id is not associated with a Indicator or IndicatorItem in the IOC.
        """
        parameters_node = self.parameters
        criteria_node = self.top_level_indicator.getparent()
        # first check for duplicate id,name pairs
        elems = parameters_node.xpath('.//param[@ref-id="{}" and @name="{}"]'.format(indicator_id, name))
        if len(elems) > 0:
            # there is no actual restriction on duplicate parameters
            log.info('Duplicate (id,name) parameter pair will be inserted [{}][{}].'.format(indicator_id, name))
        # now check to make sure the id is present in the IOC logic
        elems = criteria_node.xpath(
            './/IndicatorItem[@id="{}"]|.//Indicator[@id="{}"]'.format(indicator_id, indicator_id))
        if len(elems) == 0:
            raise IOCParseError('ID does not exist in the IOC [{}][{}].'.format(str(indicator_id), str(content)))
        parameters_node.append(ioc_et.make_param_node(indicator_id, content, name, ptype))
        return True


Instruction:
How would you explain what the following Python 3 function does
def add_link(self, rel, value, href=None):
        links_node = self.metadata.find('links')
        if links_node is None:
            links_node = ioc_et.make_links_node()
            self.metadata.append(links_node)
        link_node = ioc_et.make_link_node(rel, value, href)
        links_node.append(link_node)
        return True

Response: Add a Link metadata element to the IOC.


Instruction:
Given the following Python 3 function, write the documentation
def update_name(self, name):
        short_desc_node = self.metadata.find('short_description')
        if short_desc_node is None:
            log.debug('Could not find short description node for [{}].'.format(str(self.iocid)))
            log.debug('Creating & inserting the short description node')
            short_desc_node = ioc_et.make_short_description_node(name)
            self.metadata.insert(0, short_desc_node)
        else:
            short_desc_node.text = name
        return True

Response: Update the name of an IOC.


Instruction:
Create a Python 3 function to
update the description node of an IOC.

Response:
def update_description(self, description):
        """
        Update the description) of an IOC

        This creates the description node if it is not present.
        :param description: Value to set the description too
        :return: True
        """
        desc_node = self.metadata.find('description')
        if desc_node is None:
            log.debug('Could not find short description node for [{}].'.format(str(self.iocid)))
            log.debug('Creating & inserting the short description node')
            desc_node = ioc_et.make_description_node(description)
            insert_index = 0
            for child in self.metadata.getchildren():
                if child.tag == 'short_description':
                    index = self.metadata.index(child)
                    insert_index = index + 1
                    break
            self.metadata.insert(insert_index, desc_node)
        else:
            desc_node.text = description
        return True


Instruction:
Can you generate a brief explanation for the following Python 3 code
def update_link_rel_based(self, old_rel, new_rel=None, new_text=None, single_link=False):
        links = self.metadata.xpath('./links/link[@rel="{}"]'.format(old_rel))
        if len(links) < 1:
            log.warning('No links with link/[@rel="{}"]'.format(str(old_rel)))
            return False
        if new_rel and not new_text:
            # update link/@rel value
            for link in links:
                link.attrib['rel'] = new_rel
                if single_link:
                    break
        elif not new_rel and new_text:
            # update link/@text() value
            for link in links:
                link.text = new_text
                if single_link:
                    break
        elif new_rel and new_text:
            log.warning('Cannot update rel and text at the same time')
            return False
        else:
            log.warning('Must specify either new_rel or new_text arguments')
            return False
        return True

Response: Update link nodes based on the existing link / @rel value.


Instruction:
Implement a function in Python 3 to
rewrite the text() value of a link based on the link/@rel and link/text() value. This is similar to update_link_rel_based but users link/@rel AND link/text() values to determine which links have their link/@text() values updated. :param old_rel: The link/@rel value used to select link nodes to update. :param old_text: The link/text() value used to select link nodes to update. :param new_text: The new link/text() value to set on link nodes. :param single_link: Determine if only the first, or multiple, linkes are modified. :return: True, unless there are no links with link/[@rel='old_rel' and text()='old_text']

Response:
def update_link_rewrite(self, old_rel, old_text, new_text, single_link=False):
        """
        Rewrite the text() value of a link based on the link/@rel and link/text() value.

        This is similar to update_link_rel_based but users link/@rel AND link/text() values
        to determine which links have their link/@text() values updated.

        :param old_rel: The link/@rel value used to select link nodes to update.
        :param old_text: The link/text() value used to select link nodes to update.
        :param new_text: The new link/text() value to set on link nodes.
        :param single_link: Determine if only the first, or multiple, linkes are modified.
        :return: True, unless there are no links with link/[@rel='old_rel' and text()='old_text']
        """
        links = self.metadata.xpath('./links/link[@rel="{}" and text()="{}"]'.format(old_rel, old_text))
        if len(links) < 1:
            log.warning('No links with link/[@rel="{}"and text()="{}"]'.format(str(old_rel), str(old_text)))
            return False
        for link in links:
            link.text = new_text
            if single_link:
                break
        return True


Instruction:
Given the following Python 3 function, write the documentation
def update_parameter(self, parameter_id, content=None, name=None, param_type=None):
        if not (content or name or param_type):
            log.warning('Must specify at least the value/text(), param/@name or the value/@type values to update.')
            return False
        parameters_node = self.parameters
        elems = parameters_node.xpath('.//param[@id="{}"]'.format(parameter_id))
        if len(elems) != 1:
            msg = 'Did not find a single parameter with the supplied ID[{}]. Found [{}] parameters'.format(parameter_id,
                                                                                len(elems))
            raise IOCParseError(msg)

        param_node = elems[0]
        value_node = param_node.find('value')

        if name:
            param_node.attrib['name'] = name

        if value_node is None:
            msg = 'No value node is associated with param [{}].  Not updating value node with content or tuple.' \
                .format(parameter_id)
            log.warning(msg)
        else:
            if content:
                value_node.text = content
            if param_type:
                value_node.attrib['type'] = param_type
        return True

Response: Updates the value of a single parameter in an Indicator or IndicatorItem node.


Instruction:
Can you implement a function in Python 3 that
removes a link node based on the function arguments.

Response:
def remove_link(self, rel, value=None, href=None):
        """
        Removes link nodes based on the function arguments.

        This can remove link nodes based on the following combinations of arguments:
            link/@rel
            link/@rel & link/text()
            link/@rel & link/@href
            link/@rel & link/text() & link/@href

        :param rel: link/@rel value to remove.  Required.
        :param value: link/text() value to remove. This is used in conjunction with link/@rel.
        :param href: link/@href value to remove. This is used in conjunction with link/@rel.
        :return: Return the number of link nodes removed, or False if no nodes are removed.
        """
        links_node = self.metadata.find('links')
        if links_node is None:
            log.warning('No links node present')
            return False
        counter = 0
        links = links_node.xpath('.//link[@rel="{}"]'.format(rel))
        for link in links:
            if value and href:
                if link.text == value and link.attrib['href'] == href:
                    links_node.remove(link)
                    counter += 1
            elif value and not href:
                if link.text == value:
                    links_node.remove(link)
                    counter += 1
            elif not value and href:
                if link.attrib['href'] == href:
                    links_node.remove(link)
                    counter += 1
            else:
                links_node.remove(link)
                counter += 1
        return counter


Instruction:
How would you implement a function in Python 3 that
removes a Indicator or IndicatorItem node from the IOC.

Response:
def remove_indicator(self, nid, prune=False):
        """
        Removes a Indicator or IndicatorItem node from the IOC.  By default,
        if nodes are removed, any children nodes are inherited by the removed
        node. It has the  ability to delete all children Indicator and
        IndicatorItem nodes underneath an Indicator node if the 'prune'
        argument is set.

        This will not remove the top level Indicator node from an IOC.
        If the id value has been reused within the IOC, this will remove the
        first node which contains the id value.

        This also removes any parameters associated with any nodes that are
        removed.

        :param nid: The Indicator/@id or IndicatorItem/@id value indicating a specific node to remove.
        :param prune: Remove all children of the deleted node. If a Indicator node is removed and prune is set to
         False, the children nodes will be promoted to be children of the removed nodes' parent.
        :return: True if nodes are removed, False otherwise.
        """
        try:
            node_to_remove = self.top_level_indicator.xpath(
                '//IndicatorItem[@id="{}"]|//Indicator[@id="{}"]'.format(str(nid), str(nid)))[0]
        except IndexError:
            log.exception('Node [{}] not present'.format(nid))
            return False
        if node_to_remove.tag == 'IndicatorItem':
            node_to_remove.getparent().remove(node_to_remove)
            self.remove_parameter(ref_id=nid)
            return True
        elif node_to_remove.tag == 'Indicator':
            if node_to_remove == self.top_level_indicator:
                raise IOCParseError('Cannot remove the top level indicator')
            if prune:
                pruned_ids = node_to_remove.xpath('.//@id')
                node_to_remove.getparent().remove(node_to_remove)
                for pruned_id in pruned_ids:
                    self.remove_parameter(ref_id=pruned_id)
            else:
                for child_node in node_to_remove.getchildren():
                    node_to_remove.getparent().append(child_node)
                node_to_remove.getparent().remove(node_to_remove)
                self.remove_parameter(ref_id=nid)
            return True
        else:
            raise IOCParseError(
                'Bad tag found.  Expected "IndicatorItem" or "Indicator", got [[}]'.format(node_to_remove.tag))


Instruction:
Write a Python 3 function for
removing parameters based on function arguments. This can remove parameters based on the following param values: param/@id param/@name param/@ref_id Each input is mutually exclusive. Calling this function with multiple values set will cause an IOCParseError exception. Calling this function without setting one value will raise an exception. :param param_id: The id of the parameter to remove. :param name: The name of the parameter to remove. :param ref_id: The IndicatorItem/Indicator id of the parameter to remove. :return: Number of parameters removed.

Response:
def remove_parameter(self, param_id=None, name=None, ref_id=None, ):
        """
        Removes parameters based on function arguments.

        This can remove parameters based on the following param values:
            param/@id
            param/@name
            param/@ref_id

        Each input is mutually exclusive.  Calling this function with multiple values set will cause an IOCParseError
         exception. Calling this function without setting one value will raise an exception.

        :param param_id: The id of the parameter to remove.
        :param name: The name of the parameter to remove.
        :param ref_id: The IndicatorItem/Indicator id of the parameter to remove.
        :return: Number of parameters removed.
        """
        l = []
        if param_id:
            l.append('param_id')
        if name:
            l.append('name')
        if ref_id:
            l.append('ref_id')
        if len(l) > 1:
            raise IOCParseError('Must specify only param_id, name or ref_id.  Specified {}'.format(str(l)))
        elif len(l) < 1:
            raise IOCParseError('Must specifiy an param_id, name or ref_id to remove a paramater')

        counter = 0
        parameters_node = self.parameters

        if param_id:
            params = parameters_node.xpath('//param[@id="{}"]'.format(param_id))
            for param in params:
                parameters_node.remove(param)
                counter += 1
        elif name:
            params = parameters_node.xpath('//param[@name="{}"]'.format(name))
            for param in params:
                parameters_node.remove(param)
                counter += 1
        elif ref_id:
            params = parameters_node.xpath('//param[@ref-id="{}"]'.format(ref_id))
            for param in params:
                parameters_node.remove(param)
                counter += 1
        return counter


Instruction:
Create a Python 3 function for
removing the name from the metadata node.

Response:
def remove_name(self):
        """
        Removes the name (short_description node) from the metadata node, if present.

        :return: True if the node is removed.  False is the node is node is not present.
        """
        short_description_node = self.metadata.find('short_description')
        if short_description_node is not None:
            self.metadata.remove(short_description_node)
            return True
        return False


Instruction:
Can you generate a brief explanation for the following Python 3 code
def remove_description(self):
        description_node = self.metadata.find('description')
        if description_node is not None:
            self.metadata.remove(description_node)
            return True
        return False

Response: Removes the description node from the metadata node. Returns True if the description node is removed. Returns False if the description node is not present.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def write_ioc_to_file(self, output_dir=None, force=False):
        return write_ioc(self.root, output_dir, force=force)

Response: Serialize the IOC to a. ioc file.


Instruction:
Write a Python 3 function that can
get a string representation of an IOC.

Response:
def display_ioc(self, width=120, sep='  ', params=False):
        """
        Get a string representation of an IOC.

        :param width: Width to print the description too.
        :param sep: Separator used for displaying the contents of the criteria nodes.
        :param params: Boolean, set to True in order to display node parameters.
        :return:
        """
        s = 'Name: {}\n'.format(self.metadata.findtext('short_description', default='No Name'))
        s += 'ID: {}\n'.format(self.root.attrib.get('id'))
        s += 'Created: {}\n'.format(self.metadata.findtext('authored_date', default='No authored_date'))
        s += 'Updated: {}\n\n'.format(self.root.attrib.get('last-modified', default='No last-modified attrib'))
        s += 'Author: {}\n'.format(self.metadata.findtext('authored_by', default='No authored_by'))
        desc = self.metadata.findtext('description', default='No Description')
        desc = textwrap.wrap(desc, width=width)
        desc = '\n'.join(desc)
        s += 'Description:\n{}\n\n'.format(desc)
        links = self.link_text()
        if links:
            s += '{}'.format(links)
        content_text = self.criteria_text(sep=sep, params=params)
        s += '\nCriteria:\n{}'.format(content_text)
        return s


Instruction:
Can you tell what is the following Python 3 function doing
def link_text(self):
        s = ''
        links_node = self.metadata.find('links')
        if links_node is None:
            return s
        links = links_node.getchildren()
        if links is None:
            return s
        s += 'IOC Links\n'
        for link in links:
            rel = link.attrib.get('rel', 'No Rel')
            href = link.attrib.get('href')
            text = link.text
            lt = '{rel}{href}: {text}\n'.format(rel=rel,
                                                href=' @ {}'.format(href) if href else '',
                                                text=text)
            s += lt
        return s

Response: Get a text represention of the links node.


Instruction:
Can you create a Python 3 function that
gets a text representation of the criteria node.

Response:
def criteria_text(self, sep='  ', params=False):
        """
        Get a text representation of the criteria node.

        :param sep: Separator used to indent the contents of the node.
        :param params: Boolean, set to True in order to display node parameters.
        :return:
        """

        s = ''
        criteria_node = self.root.find('criteria')
        if criteria_node is None:
            return s
        node_texts = []
        for node in criteria_node.getchildren():
            nt = self.get_node_text(node, depth=0, sep=sep, params=params)
            node_texts.append(nt)
        s = '\n'.join(node_texts)
        return s


Instruction:
Here you have a function in Python 3, explain what it does
def get_node_text(self, node, depth, sep, params=False,):
        indent = sep * depth
        s = ''
        tag = node.tag
        if tag == 'Indicator':
            node_text = self.get_i_text(node)
        elif tag == 'IndicatorItem':
            node_text = self.get_ii_text(node)
        else:
            raise IOCParseError('Invalid node encountered: {}'.format(tag))
        s += '{}{}\n'.format(indent, node_text)
        if params:
            param_text = self.get_param_text(node.attrib.get('id'))
            for pt in param_text:
                s += '{}{}\n'.format(indent+sep, pt)
        if node.tag == 'Indicator':
            for child in node.getchildren():
                s += self.get_node_text(node=child, depth=depth+1, sep=sep, params=params)
        return s

Response: Get the text for a given Indicator or IndicatorItem node.


Instruction:
Create a Python 3 function for
getting the text for an Indicator node.

Response:
def get_i_text(node):
        """
        Get the text for an Indicator node.

        :param node: Indicator node.
        :return:
        """
        if node.tag != 'Indicator':
            raise IOCParseError('Invalid tag: {}'.format(node.tag))
        s = node.get('operator').upper()
        return s


Instruction:
Can you write a function in Python 3 where it
gets the text for IndicatorItem node.

Response:
def get_ii_text(node):
        """
        Get the text for IndicatorItem node.

        :param node: IndicatorItem node.
        :return:
        """
        if node.tag != 'IndicatorItem':
            raise IOCParseError('Invalid tag: {}'.format(node.tag))
        condition = node.attrib.get('condition')
        preserve_case = node.attrib.get('preserve-case', '')
        negate = node.attrib.get('negate', '')
        content = node.findtext('Content')
        search = node.find('Context').get('search')
        if preserve_case.lower() == 'true':
            preserve_case = ' (Preserve Case)'
        else:
            preserve_case = ''
        if negate.lower() == 'true':
            negate = 'NOT '
        else:
            negate = ''
        s = '{negate}{search} {condition} "{content}"{preserve_case}'.format(negate=negate,
                                                                             search=search,
                                                                             condition=condition,
                                                                             content=content,
                                                                             preserve_case=preserve_case)
        return s


Instruction:
Can you tell what is the following Python 3 function doing
def get_param_text(self, nid):
        r = []
        params = self.parameters.xpath('.//param[@ref-id="{}"]'.format(nid))
        if not params:
            return r
        for param in params:
            vnode = param.find('value')
            s = 'Parameter: {}, type:{}, value: {}'.format(param.attrib.get('name'),
                                                           vnode.attrib.get('type'),
                                                           param.findtext('value', default='No Value'))
            r.append(s)
        return r

Response: Get a list of parameters as text values for a given node id.


Instruction:
Write a Python 3 script for
reading in an XML file or string into a lxml. _elementTree object.

Response:
def read_xml(filename):
    """
    Use et to read in a xml file, or string, into a Element object.

    :param filename: File to parse.
    :return: lxml._elementTree object or None
    """
    parser = et.XMLParser(remove_blank_text=True)
    isfile=False
    try:
        isfile = os.path.exists(filename)
    except ValueError as e:
        if 'path too long for Windows' in str(e):
            pass
        else:
            raise
    try:
        if isfile:
            return et.parse(filename, parser)
        else:
            r = et.fromstring(filename, parser)
            return r.getroottree()
    except IOError:
        log.exception('unable to open file [[}]'.format(filename))
    except et.XMLSyntaxError:
        log.exception('unable to parse XML [{}]'.format(filename))
        return None
    return None


Instruction:
Can you create a Python 3 function that
removes the specified namespace from all Element nodes within a document.

Response:
def remove_namespace(doc, namespace):
    """
    Takes in a ElementTree object and namespace value.  The length of that
    namespace value is removed from all Element nodes within the document.
    This effectively removes the namespace from that document.

    :param doc: lxml.etree
    :param namespace: Namespace that needs to be removed.
    :return: Returns the source document with namespaces removed.
    """
    # http://homework.nwsnet.de/products/45be_remove-namespace-in-an-xml-document-using-elementtree
    #
    # Permission is hereby granted, free of charge, to any person obtaining
    # a copy of this software and associated documentation files (the
    # "Software"), to deal in the Software without restriction, including
    # without limitation the rights to use, copy, modify, merge, publish,
    # distribute, sublicense, and/or sell copies of the Software, and to
    # permit persons to whom the Software is furnished to do so, subject to
    # the following conditions:
    #
    # The above copyright notice and this permission notice shall be
    # included in all copies or substantial portions of the Software.
    #
    # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    ns = '{{{}}}'.format(namespace)
    nsl = len(ns)
    # print 'DEBUG: removing',ns
    for elem in doc.getiterator():
        if elem.tag.startswith(ns):
            elem.tag = elem.tag[nsl:]
    return doc


Instruction:
Can you generate the documentation for the following Python 3 function
def delete_namespace(parsed_xml):
    if parsed_xml.getroot().tag.startswith('{'):
        root = parsed_xml.getroot().tag
        end_ns = root.find('}')
        remove_namespace(parsed_xml, root[1:end_ns])
    return parsed_xml

Response: Removes the namespace associated with the root node of a XML document and returns the original XML document.


Instruction:
Implement a function in Python 3 to
parse the specified file into memory and inserts them into the class.

Response:
def insert(self, filename):
        """
        Parses files to load them into memory and insert them into the class.

        :param filename: File or directory pointing to .ioc files.
        :return: A list of .ioc files which could not be parsed.
        """
        errors = []
        if os.path.isfile(filename):
            log.info('loading IOC from: {}'.format(filename))
            if not self.parse(filename):
                log.warning('Failed to prase [{}]'.format(filename))
                errors.append(filename)
        elif os.path.isdir(filename):
            log.info('loading IOCs from: {}'.format(filename))
            for fn in glob.glob(filename + os.path.sep + '*.ioc'):
                if not os.path.isfile(fn):
                    continue
                else:
                    if not self.parse(fn):
                        log.warning('Failed to parse [{}]'.format(filename))
                        errors.append(fn)
        else:
            pass
        log.info('Parsed [%s] IOCs' % str(len(self)))
        return errors


Instruction:
Make a summary of the following Python 3 code
def parse(self, fn):
        ioc_xml = xmlutils.read_xml_no_ns(fn)
        if not ioc_xml:
            return False
        root = ioc_xml.getroot()
        iocid = root.get('id', None)
        if not iocid:
            return False
        self.iocs[iocid] = ioc_xml
        return True

Response: Parses a file into a lxml. etree structure with namespaces removed.


Instruction:
Can you generate the documentation for the following Python 3 function
def convert_to_11(self):
        if len(self) < 1:
            log.error('No iocs available to modify.')
            return False
        log.info('Converting IOCs from 1.0 to 1.1')
        errors = []
        for iocid in self.iocs:
            ioc_xml = self.iocs[iocid]
            root = ioc_xml.getroot()
            if root.tag != 'ioc':
                log.error('IOC root is not "ioc" [%s].' % str(iocid))
                errors.append(iocid)
                continue
            name_10 = root.findtext('.//short_description')
            keywords_10 = root.findtext('.//keywords')
            description_10 = root.findtext('.//description')
            author_10 = root.findtext('.//authored_by')
            created_date_10 = root.findtext('.//authored_date')
            last_modified_date_10 = root.get('last-modified', None)
            if last_modified_date_10:
                last_modified_date_10 = last_modified_date_10.rstrip('Z')
            created_date_10 = created_date_10.rstrip('Z')
            links_10 = []
            for link in root.xpath('//link'):
                link_rel = link.get('rel', None)
                link_text = link.text
                links_10.append((link_rel, link_text, None))
            # get ioc_logic
            try:
                ioc_logic = root.xpath('.//definition')[0]
            except IndexError:
                log.exception(
                    'Could not find definition nodes for IOC [%s].  Did you attempt to convert OpenIOC 1.1 iocs?' % str(
                        iocid))
                errors.append(iocid)
                continue
            # create 1.1 ioc obj
            ioc_obj = ioc_api.IOC(name=name_10, description=description_10, author=author_10, links=links_10,
                                  keywords=keywords_10, iocid=iocid)
            ioc_obj.set_lastmodified_date(last_modified_date_10)
            ioc_obj.set_created_date(created_date_10)

            comment_dict = {}
            tlo_10 = ioc_logic.getchildren()[0]
            try:
                self.convert_branch(tlo_10, ioc_obj.top_level_indicator, comment_dict)
            except UpgradeError:
                log.exception('Problem converting IOC [{}]'.format(iocid))
                errors.append(iocid)
                continue
            for node_id in comment_dict:
                ioc_obj.add_parameter(node_id, comment_dict[node_id])
            self.iocs_11[iocid] = ioc_obj
        return errors

Response: Convert the iocs in self. iocs from OpenIOC 1. 0 to OpenIOC 1. 1 format.


Instruction:
Can you generate the documentation for the following Python 3 function
def convert_branch(self, old_node, new_node, comment_dict=None):
        expected_tag = 'Indicator'
        if old_node.tag != expected_tag:
            raise UpgradeError('old_node expected tag is [%s]' % expected_tag)
        if not comment_dict:
            comment_dict = {}
        for node in old_node.getchildren():
            node_id = node.get('id')
            if node.tag == 'IndicatorItem':
                condition = node.get('condition')
                negation = False
                if condition.endswith('not'):
                    negation = True
                    condition = condition[:-3]
                document = node.xpath('Context/@document')[0]
                search = node.xpath('Context/@search')[0]
                content_type = node.xpath('Content/@type')[0]
                content = node.findtext('Content')
                context_type = node.xpath('Context/@type')[0]
                new_ii_node = ioc_api.make_indicatoritem_node(condition=condition,
                                                              document=document,
                                                              search=search,
                                                              content_type=content_type,
                                                              content=content,
                                                              context_type=context_type,
                                                              negate=negation,
                                                              nid=node_id)
                # set comment
                comment = node.find('Comment')
                if comment is not None:
                    comment_dict[node_id] = comment.text
                new_node.append(new_ii_node)
            elif node.tag == 'Indicator':
                operator = node.get('operator')
                if operator.upper() not in ['OR', 'AND']:
                    raise UpgradeError('Indicator@operator is not AND/OR. [%s] has [%s]' % (node_id, operator))
                new_i_node = ioc_api.make_indicator_node(operator, node_id)
                new_node.append(new_i_node)
                self.convert_branch(node, new_i_node, comment_dict)
            else:
                # should never get here
                raise UpgradeError('node is not a Indicator/IndicatorItem')
        return True

Response: This function recursively walks a branch of the Indicator node and converts it to a new node.


Instruction:
Create a Python 3 function for
parsing a file or directory containing IOCs and inserts them into the class.

Response:
def insert(self, filename):
        """
        Parses files to load them into memory and insert them into the class.

        :param filename: File or directory pointing to .ioc files.
        :return: A list of .ioc files which could not be parsed.
        """
        errors = []
        if os.path.isfile(filename):
            log.info('loading IOC from: {}'.format(filename))
            try:
                self.parse(ioc_api.IOC(filename))
            except ioc_api.IOCParseError:
                log.exception('Parse Error')
                errors.append(filename)
        elif os.path.isdir(filename):
            log.info('loading IOCs from: {}'.format(filename))
            for fn in glob.glob(filename + os.path.sep + '*.ioc'):
                if not os.path.isfile(fn):
                    continue
                else:
                    try:
                        self.parse(ioc_api.IOC(fn))
                    except ioc_api.IOCParseError:
                        log.exception('Parse Error')
                        errors.append(fn)
        else:
            pass
        log.info('Parsed [{}] IOCs'.format(len(self)))
        return errors


Instruction:
Implement a function in Python 3 to
parse an ioc to populate self. iocs and self. ioc_name .

Response:
def parse(self, ioc_obj):
        """
        parses an ioc to populate self.iocs and self.ioc_name

        :param ioc_obj:
        :return:
        """
        if ioc_obj is None:
            return
        iocid = ioc_obj.iocid
        try:
            sd = ioc_obj.metadata.xpath('.//short_description/text()')[0]
        except IndexError:
            sd = 'NoName'
        if iocid in self.iocs:
            msg = 'duplicate IOC UUID [{}] [orig_shortName: {}][new_shortName: {}]'.format(iocid,
                                                                                self.ioc_name[iocid],
                                                                                sd)
            log.warning(msg)
        self.iocs[iocid] = ioc_obj
        self.ioc_name[iocid] = sd
        if self.parser_callback:
            self.parser_callback(ioc_obj)
        return True


Instruction:
How would you explain what the following Python 3 function does
def register_parser_callback(self, func):
        if hasattr(func, '__call__'):
            self.parser_callback = func
            log.debug('Set callback to {}'.format(func))
        else:
            raise TypeError('Provided function is not callable: {}'.format(func))

Response: Register a callback function that is called after self. ioc_name is populated.


Instruction:
How would you implement a function in Python 3 that
creates single permission for the given object.

Response:
def create(self, permission):
        """
        Create single permission for the given object.

        :param Permission permission: A single Permission object to be set.
        """
        parent_url = self.client.get_url(self.parent_object._manager._URL_KEY, 'GET', 'single', {'id': self.parent_object.id})
        target_url = parent_url + self.client.get_url_path(self._URL_KEY, 'POST', 'single')
        r = self.client.request('POST', target_url, json=permission._serialize())
        return permission._deserialize(r.json(), self)


Instruction:
Can you tell what is the following Python 3 function doing
def set(self, permissions):
        parent_url = self.client.get_url(self.parent_object._manager._URL_KEY, 'GET', 'single', {'id': self.parent_object.id})
        target_url = parent_url + self.client.get_url_path(self._URL_KEY, 'PUT', 'multi')
        r = self.client.request('PUT', target_url, json=permissions)
        if r.status_code != 201:
            raise exceptions.ServerError("Expected 201 response, got %s: %s" % (r.status_code, target_url))
        return self.list()

Response: Sets the permissions for the object.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def list(self):
        parent_url = self.client.get_url(self.parent_object._manager._URL_KEY, 'GET', 'single', {'id': self.parent_object.id})
        target_url = parent_url + self.client.get_url_path(self._URL_KEY, 'GET', 'multi')
        return base.Query(self, target_url)

Response: List permissions for the given object.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get(self, permission_id, expand=[]):
        parent_url = self.client.get_url(self.parent_object._manager._URL_KEY, 'GET', 'single', {'id': self.parent_object.id})
        target_url = parent_url + self.client.get_url_path(
            self._URL_KEY, 'GET', 'single', {'permission_id': permission_id})
        return self._get(target_url, expand=expand)

Response: Get a specific permisison for the given object.


Instruction:
How would you explain what the following Python 3 function does
def get_config():
    configpath = get_configpath()
    if not configpath.exists():
        raise IOError("Config file {} not found.".format(str(configpath)))
    else:
        config = configparser.ConfigParser()
        config.read(str(configpath))
        return config

Response: Read the configfile and return a dictionary with the content of the configpath file.


Instruction:
Implement a function in Python 3 to
use to write the database path into the config.

Response:
def set_database_path(dbfolder):
    """Use to write the database path into the config.

    Parameters
    ----------
    dbfolder : str or pathlib.Path
        Path to where pyciss will store the ISS images it downloads and receives.
    """
    configpath = get_configpath()
    try:
        d = get_config()
    except IOError:
        d = configparser.ConfigParser()
        d['pyciss_db'] = {}
    d['pyciss_db']['path'] = dbfolder
    with configpath.open('w') as f:
        d.write(f)
    print("Saved database path into {}.".format(configpath))


Instruction:
Can you tell what is the following Python 3 function doing
def get_db_root():
    "Read dbroot folder from config and mkdir if required."
    d = get_config()
    dbroot = Path(d['pyciss_db']['path'])
    dbroot.mkdir(exist_ok=True)
    return dbroot

Response: Read dbroot folder from config and mkdir if required.


Instruction:
Can you create a Python 3 function that
prints database stats. Returns ------- pd. DataFrame pd. DataFrame

Response:
def print_db_stats():
    """Print database stats.

    Returns
    -------
    pd.DataFrame
        Table with the found data items per type.
    """
    dbroot = get_db_root()
    n_ids = len(list(dbroot.glob("[N,W]*")))
    print("Number of WACs and NACs in database: {}".format(n_ids))
    print("These kind of data are in the database: (returning pd.DataFrame)")
    d = {}
    for key, val in PathManager.extensions.items():
        d[key] = [len(list(dbroot.glob("**/*" + val)))]
    return pd.DataFrame(d)


Instruction:
Write a Python 3 function for
checking if the label file is LOSSY.

Response:
def is_lossy(label):
    """Check Label file for the compression type. """
    val = getkey(from_=label, keyword='INST_CMPRS_TYPE').decode().strip()
    if val == 'LOSSY':
        return True
    else:
        return False


Instruction:
Here you have a function in Python 3, explain what it does
def download_and_calibrate_parallel(list_of_ids, n=None):
    setup_cluster(n_cores=n)
    c = Client()
    lbview = c.load_balanced_view()
    lbview.map_async(download_and_calibrate, list_of_ids)
    subprocess.Popen(["ipcluster", "stop", "--quiet"])

Response: Download and calibrate in parallel.


Instruction:
Create a Python 3 function for
downloading and calibrate one or more image ids in parallel.

Response:
def download_and_calibrate(img_id=None, overwrite=False, recalibrate=False, **kwargs):
    """Download and calibrate one or more image ids, in parallel.

    Parameters
    ----------
    img_id : str or io.PathManager, optional
        If more than one item is in img_id, a parallel process is started
    overwrite: bool, optional
        If the pm.cubepath exists, this switch controls if it is being overwritten.
        Default: False
    """
    if isinstance(img_id, io.PathManager):
        pm = img_id
    else:
        # get a PathManager object that knows where your data is or should be
        logger.debug("Creating Pathmanager object")
        pm = io.PathManager(img_id)

    if not pm.raw_image.exists() or overwrite is True:
        logger.debug("Downloading file %s" % pm.img_id)
        download_file_id(pm.img_id)
        pm = io.PathManager(img_id)  # refresh, to get proper PDS version id.
    else:
        logger.info("Found ")

    if not (pm.cubepath.exists() and pm.undestriped.exists()) or overwrite is True:
        calib = pipeline.Calibrator(img_id, **kwargs)
        calib.standard_calib()
    else:
        print("All files exist. Use overwrite=True to redownload and calibrate.")


Instruction:
How would you implement a function in Python 3 that
performs either normal spiceinit or outer spiceinit.

Response:
def spiceinit(self):
        """Perform either normal spiceinit or one for ringdata.

        Note how Python name-spacing can distinguish between the method
        and the function with the same name. `spiceinit` from the outer
        namespace is the one imported from pysis.
        """
        shape = "ringplane" if self.is_ring_data else None
        spiceinit(from_=self.pm.raw_cub, cksmithed="yes", spksmithed="yes", shape=shape)
        logger.info("spiceinit done.")


Instruction:
Explain what the following Python 3 code does
def check_label(self):
        if not self.is_ring_data:
            return
        targetname = getkey(
            from_=self.pm.raw_cub, grp="instrument", keyword="targetname"
        )

        if targetname.lower() != "saturn":
            editlab(
                from_=self.pm.raw_cub,
                options="modkey",
                keyword="TargetName",
                value="Saturn",
                grpname="Instrument",
            )

Response: Check label for target name and fix if necessary.


Instruction:
Create a Python 3 function for
checking and sets the supported content types configuration value.

Response:
def _set_supported_content_type(self, content_types_supported):
        """ Checks and sets the supported content types configuration value.
        """
        if not isinstance(content_types_supported, list):
            raise TypeError(("Settings 'READTIME_CONTENT_SUPPORT' must be"
                             "a list of content types."))

        self.content_type_supported = content_types_supported


Instruction:
How would you implement a function in Python 3 that
checks and sets the per language WPM singular and plural values.

Response:
def _set_lang_settings(self, lang_settings):
        """ Checks and sets the per language WPM, singular and plural values.
        """
        is_int = isinstance(lang_settings, int)
        is_dict = isinstance(lang_settings, dict)
        if not is_int and not is_dict:
            raise TypeError(("Settings 'READTIME_WPM' must be either an int,"
                             "or a dict with settings per language."))

        # For backwards compatability reasons we'll allow the
        # READTIME_WPM setting to be set as an to override just the default
        # set WPM.
        if is_int:
            self.lang_settings['default']['wpm'] = lang_settings
        elif is_dict:
            for lang, conf in lang_settings.items():
                if 'wpm' not in conf:
                    raise KeyError(('Missing wpm value for the'
                                    'language: {}'.format(lang)))

                if not isinstance(conf['wpm'], int):
                    raise TypeError(('WPM is not an integer for'
                                     ' the language: {}'.format(lang)))

                if "min_singular" not in conf:
                    raise KeyError(('Missing singular form for "minute" for'
                                    ' the language: {}'.format(lang)))

                if "min_plural" not in conf:
                    raise KeyError(('Missing plural form for "minutes" for'
                                    ' the language: {}'.format(lang)))

                if "sec_singular" not in conf:
                    raise KeyError(('Missing singular form for "second" for'
                                    ' the language: {}'.format(lang)))

                if "sec_plural" not in conf:
                    raise KeyError(('Missing plural form for "seconds" for'
                                    ' the language: {}'.format(lang)))

            self.lang_settings = lang_settings


Instruction:
Write a Python 3 function for
initializing the ReadTimeParser with the configuration values set by the readtime plugin.

Response:
def initialize_settings(self, sender):
        """ Initializes ReadTimeParser with configuration values set by the
        site author.
        """
        try:
            self.initialized = True

            settings_content_types = sender.settings.get(
                'READTIME_CONTENT_SUPPORT', self.content_type_supported)
            self._set_supported_content_type(settings_content_types)

            lang_settings = sender.settings.get(
                'READTIME_WPM', self.lang_settings)
            self._set_lang_settings(lang_settings)
        except Exception as e:
            raise Exception("ReadTime Plugin: %s" % str(e))


Instruction:
Explain what the following Python 3 code does
def read_time(self, content):
        if get_class_name(content) in self.content_type_supported:
            # Exit if readtime is already set
            if hasattr(content, 'readtime'):
                return None

            default_lang_conf = self.lang_settings['default']
            lang_conf = self.lang_settings.get(content.lang, default_lang_conf)
            avg_reading_wpm = lang_conf['wpm']

            num_words = len(content._content.split())
            # Floor division so we don't have to convert float -> int
            minutes = num_words // avg_reading_wpm
            # Get seconds to read, then subtract our minutes as seconds from
            # the time to get remainder seconds
            seconds = int((num_words / avg_reading_wpm * 60) - (minutes * 60))

            minutes_str = self.pluralize(
                minutes,
                lang_conf['min_singular'],
                lang_conf['min_plural']
            )

            seconds_str = self.pluralize(
                seconds,
                lang_conf['sec_singular'],
                lang_conf['sec_plural']
            )

            content.readtime = minutes
            content.readtime_string = minutes_str
            content.readtime_with_seconds = (minutes, seconds,)
            content.readtime_string_with_seconds = "{}, {}".format(
                minutes_str, seconds_str)

Response: This function generates the read time for the content.


Instruction:
Can you implement a function in Python 3 that
returns a string that contains the measure and its plural form depending on the amount.

Response:
def pluralize(self, measure, singular, plural):
        """ Returns a string that contains the measure (amount) and its plural
        or singular form depending on the amount.

        Parameters:
            :param measure: Amount, value, always a numerical value
            :param singular: The singular form of the chosen word
            :param plural: The plural form of the chosen word

        Returns:
            String
        """
        if measure == 1:
            return "{} {}".format(measure, singular)
        else:
            return "{} {}".format(measure, plural)


Instruction:
Can you tell what is the following Python 3 function doing
def list_datasources(self, source_id):
        target_url = self.client.get_url('DATASOURCE', 'GET', 'multi', {'source_id': source_id})
        return base.Query(self.client.get_manager(Datasource), target_url)

Response: Filterable list of Datasources for a Source.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_datasource(self, source_id, datasource_id):
        target_url = self.client.get_url('DATASOURCE', 'GET', 'single', {'source_id': source_id, 'datasource_id': datasource_id})
        return self.client.get_manager(Datasource)._get(target_url)

Response: Get a Datasource object


Instruction:
Implement a function in Python 3 to
list all Scans for a Source.

Response:
def list_scans(self, source_id=None):
        """
        Filterable list of Scans for a Source.
        Ordered newest to oldest by default
        """
        if source_id:
            target_url = self.client.get_url('SCAN', 'GET', 'multi', {'source_id': source_id})
        else:
            target_url = self.client.get_ulr('SCAN', 'GET', 'all')
        return base.Query(self.client.get_manager(Scan), target_url)


Instruction:
How would you code a function in Python 3 to
get a Scan object from the given source and scan ID.

Response:
def get_scan(self, source_id, scan_id):
        """
        Get a Scan object

        :rtype: Scan
        """
        target_url = self.client.get_url('SCAN', 'GET', 'single', {'source_id': source_id, 'scan_id': scan_id})
        return self.client.get_manager(Scan)._get(target_url)


Instruction:
Explain what the following Python 3 code does
def get_scan_log_lines(self, source_id, scan_id):
        return self.client.get_manager(Scan).get_log_lines(source_id=source_id, scan_id=scan_id)

Response: Get the log lines for a Scan


Instruction:
How would you implement a function in Python 3 that
starts a new scan of a Source.

Response:
def start_scan(self, source_id):
        """
        Start a new scan of a Source.

        :rtype: Scan
        """
        target_url = self.client.get_url('SCAN', 'POST', 'create', {'source_id': source_id})
        r = self.client.request('POST', target_url, json={})
        return self.client.get_manager(Scan).create_from_result(r.json())


Instruction:
Can you generate the documentation for the following Python 3 function
def save(self, with_data=False):
        r = self._client.request('PUT', self.url, json=self._serialize(with_data=with_data))
        return self._deserialize(r.json(), self._manager)

Response: Saves this Source into the server.


Instruction:
Create a Python 3 function for
adding a single file or archive to the upload list.

Response:
def add_file(self, fp, upload_path=None, content_type=None):
        """
        Add a single file or archive to upload.

        To add metadata records with a file, add a .xml file with the same upload path basename
        eg. ``points-with-metadata.geojson`` & ``points-with-metadata.xml``
        Datasource XML must be in one of these three formats:

          - ISO 19115/19139
          - FGDC CSDGM
          - Dublin Core (OAI-PMH)

        :param fp: File to upload into this source, can be a path or a file-like object.
        :type fp: str or file
        :param str upload_path: relative path to store the file as within the source (eg. ``folder/0001.tif``). \
                                By default it will use ``fp``, either the filename from a path or the ``.name`` \
                                attribute of a file-like object.
        :param str content_type: Content-Type of the file. By default it will attempt to auto-detect from the \
                                 file/upload_path.
        """
        if isinstance(fp, six.string_types):
            # path
            if not os.path.isfile(fp):
                raise ClientValidationError("Invalid file: %s", fp)
            if not upload_path:
                upload_path = os.path.split(fp)[1]
        else:
            # file-like object
            if not upload_path:
                upload_path = os.path.split(fp.name)[1]

        content_type = content_type or mimetypes.guess_type(upload_path, strict=False)[0]
        if upload_path in self._files:
            raise ClientValidationError("Duplicate upload path: %s" % upload_path)

        self._files[upload_path] = (fp, content_type)
        logger.debug("UploadSource.add_file: %s -> %s (%s)", repr(fp), upload_path, content_type)


Instruction:
Write a Python 3 function for
getting the log lines for a given source and scan.

Response:
def get_log_lines(self, source_id, scan_id):
        """
        Get the log text for a scan object
        :rtype: Iterator over log lines.
        """
        target_url = self.client.get_url('SCAN', 'GET', 'log', {'source_id': source_id, 'scan_id': scan_id})
        r = self.client.request('GET', target_url, headers={'Accept': 'text/plain'}, stream=True)
        return r.iter_lines(decode_unicode=True)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_log_lines(self):
        rel = self._client.reverse_url('SCAN', self.url)
        return self._manager.get_log_lines(**rel)

Response: Get the log lines for a scan object.


Instruction:
Explain what the following Python 3 code does
def get_creative_commons(self, slug, jurisdiction=None):
        if not slug.startswith('cc-by'):
            raise exceptions.ClientValidationError("slug needs to start with 'cc-by'")

        if jurisdiction is None:
            jurisdiction = ''

        target_url = self.client.get_url(self._URL_KEY, 'GET', 'cc', {'slug': slug, 'jurisdiction': jurisdiction})
        return self._get(target_url)

Response: Returns the Creative Commons license for the given attributes.


Instruction:
How would you explain what the following Python 3 function does
def mad(arr, relative=True):
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        med = np.nanmedian(arr, axis=1)
        mad = np.nanmedian(np.abs(arr - med[:, np.newaxis]), axis=1)
        if relative:
            return mad / med
        else:
            return mad

Response: Median Absolute Deviation of a sample.


Instruction:
How would you explain what the following Python 3 function does
def calc_offset(cube):
    i = 0
    while pd.Series(cube.img[:, i]).count() < 200:
        i += 1
    return max(i, 20)

Response: Calculate an offset from the side of data so that at least 200 image pixels are in the MAD stats.


Instruction:
Create a Python 3 function for
plotting the image of the current object.

Response:
def imshow(
        self,
        data=None,
        save=False,
        ax=None,
        interpolation="none",
        extra_title=None,
        show_resonances="some",
        set_extent=True,
        equalized=False,
        rmin=None,
        rmax=None,
        savepath=".",
        **kwargs,
    ):
        """Powerful default display.

        show_resonances can be True, a list, 'all', or 'some'
        """
        if data is None:
            data = self.img
        if self.resonance_axis is not None:
            logger.debug("removing resonance_axis")
            self.resonance_axis.remove()
        if equalized:
            data = np.nan_to_num(data)
            data[data < 0] = 0
            data = exposure.equalize_hist(data)
        self.plotted_data = data

        extent_val = self.extent if set_extent else None
        min_, max_ = self.plot_limits
        self.min_ = min_
        self.max_ = max_
        if ax is None:
            if not _SEABORN_INSTALLED:
                fig, ax = plt.subplots(figsize=calc_4_3(8))
            else:
                fig, ax = plt.subplots()
        else:
            fig = ax.get_figure()

        with quantity_support():
            im = ax.imshow(
                data,
                extent=extent_val,
                cmap="gray",
                vmin=min_,
                vmax=max_,
                interpolation=interpolation,
                origin="lower",
                aspect="auto",
                **kwargs,
            )
        if any([rmin is not None, rmax is not None]):
            ax.set_ylim(rmin, rmax)
        self.mpl_im = im
        ax.set_xlabel("Longitude [deg]")
        ax.set_ylabel("Radius [Mm]")
        ax.ticklabel_format(useOffset=False)
        # ax.grid('on')
        title = self.plot_title
        if extra_title:
            title += ", " + extra_title
        ax.set_title(title, fontsize=12)
        if show_resonances:
            self.set_resonance_axis(ax, show_resonances, rmin, rmax)
        if save:
            savename = self.plotfname
            if extra_title:
                savename = savename[:-4] + "_" + extra_title + ".png"
            p = Path(savename)
            fullpath = Path(savepath) / p.name
            fig.savefig(fullpath, dpi=150)
            logging.info("Created %s", fullpath)
        self.im = im
        return im


Instruction:
Can you create a Python 3 function that
updates the query count property from the X - Resource - Range response header.

Response:
def _update_range(self, response):
        """ Update the query count property from the `X-Resource-Range` response header """
        header_value = response.headers.get('x-resource-range', '')
        m = re.match(r'\d+-\d+/(\d+)$', header_value)
        if m:
            self._count = int(m.group(1))
        else:
            self._count = None


Instruction:
Write a Python 3 script for
serialising this query into a request - able URL including parameters

Response:
def _to_url(self):
        """ Serialises this query into a request-able URL including parameters """
        url = self._target_url

        params = collections.defaultdict(list, copy.deepcopy(self._filters))
        if self._order_by is not None:
            params['sort'] = self._order_by
        for k, vl in self._extra.items():
            params[k] += vl

        if params:
            url += "?" + urllib.parse.urlencode(params, doseq=True)

        return url


Instruction:
How would you implement a function in Python 3 that
returns a new Query object with extra query parameters added.

Response:
def extra(self, **params):
        """
        Set extra query parameters (eg. filter expressions/attributes that don't validate).
        Appends to any previous extras set.

        :rtype: Query
        """
        q = self._clone()
        for key, value in params.items():
            q._extra[key].append(value)
        return q


Instruction:
Can you generate the documentation for the following Python 3 function
def filter(self, **filters):

        q = self._clone()
        for key, value in filters.items():
            filter_key = re.split('__', key)
            filter_attr = filter_key[0]
            if filter_attr not in self._valid_filter_attrs:
                raise ClientValidationError("Invalid filter attribute: %s" % key)

            # we use __ as a separator in the Python library, the APIs use '.'
            q._filters['.'.join(filter_key)].append(value)
        return q

Response: Add a filter to this query.


Instruction:
How would you explain what the following Python 3 function does
def order_by(self, sort_key=None):
        if sort_key is not None:
            sort_attr = re.match(r'(-)?(.*)$', sort_key).group(2)
            if sort_attr not in self._valid_sort_attrs:
                raise ClientValidationError("Invalid ordering attribute: %s" % sort_key)

        q = self._clone()
        q._order_by = sort_key
        return q

Response: Sets the ordering for this query.


Instruction:
Can you tell what is the following Python 3 function doing
def _serialize(self, skip_empty=True):
        skip = set(getattr(self._meta, 'serialize_skip', []))

        r = {}
        for k, v in self.__dict__.items():
            if k.startswith('_'):
                continue
            elif k in skip:
                continue
            elif v is None and skip_empty:
                continue
            elif isinstance(v, (dict, list, tuple, set)) and len(v) == 0 and skip_empty:
                continue
            else:
                r[k] = self._serialize_value(v)
        return r

Response: Serialise this instance into JSON - style request data.


Instruction:
Explain what the following Python 3 code does
def _serialize_value(self, value):
        if isinstance(value, (list, tuple, set)):
            return [self._serialize_value(v) for v in value]
        elif isinstance(value, dict):
            return dict([(k, self._serialize_value(v)) for k, v in value.items()])
        elif isinstance(value, ModelBase):
            return value._serialize()
        elif isinstance(value, datetime.date):  # includes datetime.datetime
            return value.isoformat()
        else:
            return value

Response: Serialize a single value.


Instruction:
Implement a function in Python 3 to
refresh the current model s attributes with the server - defined values.

Response:
def refresh(self):
        """
        Refresh this model from the server.

        Updates attributes with the server-defined values. This is useful where the Model
        instance came from a partial response (eg. a list query) and additional details
        are required.

        Existing attribute values will be overwritten.
        """
        r = self._client.request('GET', self.url)
        return self._deserialize(r.json(), self._manager)