Instruction:
Can you generate a brief explanation for the following Python 3 code
def field_alpha_histogram(
        self, name, n_bins=10, include_queries=True, **query_dict
    ):
        """Generates a histogram of values from a string field.

        Output is: [[low, high, count, query], ... ]. Bin edges is determined by equal
        division of the fields.

        """
        bin_list = []
        q_bin = []
        try:
            # get total number of values for the field
            # TODO: this is a slow mechanism to retrieve the number of distinct values
            # Need to replace this with something more efficient.
            # Can probably replace with a range of alpha chars - need to check on
            # case sensitivity
            param_dict = query_dict.copy()
            f_vals = self.get_field_values(name, maxvalues=-1, **param_dict)
            n_values = len(f_vals[name]) // 2
            if n_values < n_bins:
                n_bins = n_values
            if n_values == n_bins:
                # Use equivalence instead of range queries to retrieve the
                # values
                for i in range(n_bins):
                    a_bin = [f_vals[name][i * 2], f_vals[name][i * 2], 0]
                    bin_q = '{}:{}'.format(
                        name, self._prepare_query_term(name, a_bin[0])
                    )
                    q_bin.append(bin_q)
                    bin_list.append(a_bin)
            else:
                delta = n_values / n_bins
                if delta == 1:
                    # Use equivalence queries, except the last one which includes the
                    # remainder of terms
                    for i in range(n_bins - 1):
                        a_bin = [f_vals[name][i * 2], f_vals[name][i * 2], 0]
                        bin_q = '{}:{}'.format(
                            name, self._prepare_query_term(name, a_bin[0])
                        )
                        q_bin.append(bin_q)
                        bin_list.append(a_bin)
                    term = f_vals[name][(n_bins - 1) * 2]
                    a_bin = [term, f_vals[name][((n_values - 1) * 2)], 0]
                    bin_q = '{}:[{} TO *]'.format(
                        name, self._prepare_query_term(name, term)
                    )
                    q_bin.append(bin_q)
                    bin_list.append(a_bin)
                else:
                    # Use range for all terms
                    # now need to page through all the values and get those at
                    # the edges
                    c_offset = 0.0
                    delta = float(n_values) / float(n_bins)
                    for i in range(n_bins):
                        idx_l = int(c_offset) * 2
                        idx_u = (int(c_offset + delta) * 2) - 2
                        a_bin = [f_vals[name][idx_l], f_vals[name][idx_u], 0]
                        # logger.info(str(a_bin))
                        try:
                            if i == 0:
                                bin_q = '{}:[* TO {}]'.format(
                                    name, self._prepare_query_term(name, a_bin[1])
                                )
                            elif i == n_bins - 1:
                                bin_q = '{}:[{} TO *]'.format(
                                    name, self._prepare_query_term(name, a_bin[0])
                                )
                            else:
                                bin_q = '{}:[{} TO {}]'.format(
                                    name,
                                    self._prepare_query_term(name, a_bin[0]),
                                    self._prepare_query_term(name, a_bin[1]),
                                )
                        except Exception:
                            self._log.exception('Exception:')
                            raise
                        q_bin.append(bin_q)
                        bin_list.append(a_bin)
                        c_offset = c_offset + delta
            # now execute the facet query request
            param_dict = query_dict.copy()
            param_dict.update(
                {
                    'rows': '0',
                    'facet': 'true',
                    'facet.field': name,
                    'facet.limit': '1',
                    'facet.mincount': 1,
                    'facet.query': [sq.encode('utf-8') for sq in q_bin],
                }
            )
            resp_dict = self._post_query(**param_dict)
            for i in range(len(bin_list)):
                v = resp_dict['facet_counts']['facet_queries'][q_bin[i]]
                bin_list[i][2] = v
                if include_queries:
                    bin_list[i].append(q_bin[i])
        except Exception:
            self._log.exception('Exception')
            raise
        return bin_list

Response: Generates a histogram of values from a string field.


Instruction:
Make a summary of the following Python 3 code
def add_docs(self, docs):
        return self.query(
            'solr',
            '<add>{}</add>'.format(
                ''.join([self._format_add(fields) for fields in docs])
            ),
            do_post=True,
        )

Response: docs is a list of fields that are a dictionary of name : value for a record.


Instruction:
How would you explain what the following Python 3 function does
def _coerce_type(self, field_type, value):
        if value is None:
            return None
        if field_type == 'string':
            return str(value)
        elif field_type == 'text':
            return str(value)
        elif field_type == 'int':
            try:
                v = int(value)
                return str(v)
            except:
                return None
        elif field_type == 'float':
            try:
                v = float(value)
                return str(v)
            except:
                return None
        elif field_type == 'date':
            try:
                v = datetime.datetime(
                    value['year'],
                    value['month'],
                    value['day'],
                    value['hour'],
                    value['minute'],
                    value['second'],
                )
                v = v.strftime('%Y-%m-%dT%H:%M:%S.0Z')
                return v
            except:
                return None
        return str(value)

Response: Coerce the value into the Solr field type.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _get_solr_type(self, field):
        field_type = 'string'
        try:
            field_type = FIELD_TYPE_CONVERSION_MAP[field]
            return field_type
        except:
            pass
        fta = field.split('_')
        if len(fta) > 1:
            ft = fta[len(fta) - 1]
            try:
                field_type = FIELD_TYPE_CONVERSION_MAP[ft]
                # cache the type so it's used next time
                FIELD_TYPE_CONVERSION_MAP[field] = field_type
            except:
                pass
        return field_type

Response: Returns the Solr type of the specified field name.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _get_query(self, **query_dict):
        param_dict = query_dict.copy()
        return self._send_query(do_post=False, **param_dict)

Response: Perform a GET query against Solr and return the response as a Python dict.


Instruction:
Can you implement a function in Python 3 that
performs a POST query against Solr and return the response as a Python dict.

Response:
def _post_query(self, **query_dict):
        """Perform a POST query against Solr and return the response as a Python
        dict."""
        param_dict = query_dict.copy()
        return self._send_query(do_post=True, **param_dict)


Instruction:
Implement a Python 3 function for
performing a query against Solr and return the response as a Python dict.

Response:
def _send_query(self, do_post=False, **query_dict):
        """Perform a query against Solr and return the response as a Python dict."""
        # self._prepare_query_term()
        param_dict = query_dict.copy()
        param_dict.setdefault('wt', 'json')
        param_dict.setdefault('q', '*.*')
        param_dict.setdefault('fl', '*')
        return self.query('solr', '', do_post=do_post, query=param_dict)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _prepare_query_term(self, field, term):
        if term == "*":
            return term
        add_star = False
        if term[len(term) - 1] == '*':
            add_star = True
            term = term[0 : len(term) - 1]
        term = self._escape_query_term(term)
        if add_star:
            term = '{}*'.format(term)
        if self._get_solr_type(field) in ['string', 'text', 'text_ws']:
            return '"{}"'.format(term)
        return term

Response: Prepare a query term for inclusion in a query.


Instruction:
Create a Python 3 function to
escape a query term for inclusion in a query.

Response:
def _escape_query_term(self, term):
        """Escape a query term for inclusion in a query.

        - Also see: prepare_query_term().

        """
        term = term.replace('\\', '\\\\')
        for c in RESERVED_CHAR_LIST:
            term = term.replace(c, r'\{}'.format(c))
        return term


Instruction:
Can you implement a function in Python 3 that
retrieves the next page of results from the service.

Response:
def _next_page(self, offset):
        """Retrieves the next set of results from the service."""
        self._log.debug("Iterator c_record={}".format(self.c_record))
        page_size = self.page_size
        if (offset + page_size) > self.max_records:
            page_size = self.max_records - offset
        param_dict = self.query_dict.copy()
        param_dict.update(
            {
                'start': str(offset),
                'rows': str(page_size),
                'explainOther': '',
                'hl.fl': '',
            }
        )
        self.res = self.client.search(**param_dict)
        self._num_hits = int(self.res['response']['numFound'])


Instruction:
How would you implement a function in Python 3 that
retrieves the next page of results from the service.

Response:
def _next_page(self, offset):
        """Retrieves the next set of results from the service."""
        self._log.debug("Iterator c_record={}".format(self.c_record))
        param_dict = self.query_dict.copy()
        param_dict.update(
            {
                'rows': '0',
                'facet': 'true',
                'facet.limit': str(self.page_size),
                'facet.offset': str(offset),
                'facet.zeros': 'false',
            }
        )
        print(param_dict)
        # resp_dict = self.client._get_query(**param_dict)
        resp_dict = self.client._post_query(**param_dict)
        # resp_dict = self.client.search(**param_dict)
        pprint.pprint(resp_dict)
        try:
            self.res = resp_dict['facet_counts']['facet_fields'][self.field]
            self._log.debug(self.res)
        except Exception:
            self.res = []
        self.index = 0


Instruction:
Create a Python 3 function for
migrating flow_collection field to entity_type.

Response:
def migrate_flow_collection(apps, schema_editor):
    """Migrate 'flow_collection' field to 'entity_type'."""
    Process = apps.get_model('flow', 'Process')
    DescriptorSchema = apps.get_model('flow', 'DescriptorSchema')

    for process in Process.objects.all():
        process.entity_type = process.flow_collection
        process.entity_descriptor_schema = process.flow_collection

        if (process.entity_descriptor_schema is not None and
                not DescriptorSchema.objects.filter(slug=process.entity_descriptor_schema).exists()):
            raise LookupError(
                "Descriptow schema '{}' referenced in 'entity_descriptor_schema' not "
                "found.".format(process.entity_descriptor_schema)
            )

        process.save()


Instruction:
How would you code a function in Python 3 to
map a DataONE API version tag to a PyXB binding.

Response:
def get_pyxb_binding_by_api_version(api_major, api_minor=0):
    """Map DataONE API version tag to PyXB binding.

    Given a DataONE API major version number, return PyXB binding that can
    serialize and deserialize DataONE XML docs of that version.

    Args:
      api_major, api_minor: str or int
        DataONE API major and minor version numbers.

        - If ``api_major`` is an integer, it is combined with ``api_minor`` to form an
          exact version.

        - If ``api_major`` is a string of ``v1`` or ``v2``, ``api_minor`` is ignored
          and the latest PyXB bindingavailable for the ``api_major`` version is
          returned.

    Returns:
      PyXB binding: E.g., ``d1_common.types.dataoneTypes_v1_1``.

    """
    try:
        return VERSION_TO_BINDING_DICT[api_major, api_minor]
    except KeyError:
        raise ValueError(
            'Unknown DataONE API version: {}.{}'.format(api_major, api_minor)
        )


Instruction:
Write a Python 3 script to
extract a DataONE API version tag from a MN or CN service endpoint URL.

Response:
def extract_version_tag_from_url(url):
    """Extract a DataONE API version tag from a MN or CN service endpoint URL.

    Args:
      url : str
        Service endpoint URL. E.g.: ``https://mn.example.org/path/v2/object/pid``.

    Returns:
      str : Valid version tags are currently ``v1`` or ``v2``.

    """
    m = re.match(r'(/|^)(v\d)(/|$)', url)
    if not m:
        return None
    return m.group(2)


Instruction:
Here you have a function in Python 3, explain what it does
def str_to_v1_str(xml_str):
    if str_is_v1(xml_str):
        return xml_str
    etree_obj = str_to_etree(xml_str)
    strip_v2_elements(etree_obj)
    etree_replace_namespace(etree_obj, d1_common.types.dataoneTypes_v1.Namespace)
    return etree_to_str(etree_obj)

Response: Convert a API v2 XML doc to v1 XML doc.


Instruction:
Write a Python 3 script for
converting a API v1 XML doc to v2 XML doc.

Response:
def str_to_v2_str(xml_str):
    """Convert a API v1 XML doc to v2 XML doc.

    All v1 elements are valid for v2, so only changes namespace.

    Args:
      xml_str : str
        API v1 XML doc. E.g.: ``SystemMetadata v1``.

    Returns:
      str : API v2 XML doc. E.g.: ``SystemMetadata v2``.

    """
    if str_is_v2(xml_str):
        return xml_str
    etree_obj = str_to_etree(xml_str)
    etree_replace_namespace(etree_obj, d1_common.types.dataoneTypes_v2_0.Namespace)
    return etree_to_str(etree_obj)


Instruction:
Can you write a function in Python 3 where it
checks if the XML string is well formed.

Response:
def str_is_well_formed(xml_str):
    """
  Args:
    xml_str : str
      DataONE API XML doc.

  Returns:
    bool: **True** if XML doc is well formed.
  """
    try:
        str_to_etree(xml_str)
    except xml.etree.ElementTree.ParseError:
        return False
    else:
        return True


Instruction:
Explain what the following Python 3 code does
def pyxb_is_v1(pyxb_obj):
    # TODO: Will not detect v1.2 as v1.
    return (
        pyxb_obj._element().name().namespace()
        == d1_common.types.dataoneTypes_v1.Namespace
    )

Response: Returns True if pyxb_obj holds an API v1 type.


Instruction:
Given the following Python 3 function, write the documentation
def pyxb_is_v2(pyxb_obj):
    return (
        pyxb_obj._element().name().namespace()
        == d1_common.types.dataoneTypes_v2_0.Namespace
    )

Response: Returns True if pyxb_obj holds an API v2 type.


Instruction:
Can you tell what is the following Python 3 function doing
def replace_namespace_with_prefix(tag_str, ns_reverse_dict=None):
    ns_reverse_dict = ns_reverse_dict or NS_REVERSE_DICT
    for namespace_str, prefix_str in ns_reverse_dict.items():
        tag_str = tag_str.replace(
            '{{{}}}'.format(namespace_str), '{}:'.format(prefix_str)
        )
    return tag_str

Response: Convert XML tag names with namespace on the form ore : ResourceMap tag name with prefix.


Instruction:
Can you generate the documentation for the following Python 3 function
def strip_system_metadata(etree_obj):
    for series_id_el in etree_obj.findall('seriesId'):
        etree_obj.remove(series_id_el)
    for media_type_el in etree_obj.findall('mediaType'):
        etree_obj.remove(media_type_el)
    for file_name_el in etree_obj.findall('fileName'):
        etree_obj.remove(file_name_el)

Response: In - place remove elements and attributes that are only valid in v2 types from v1SystemMetadata.


Instruction:
Create a Python 3 function for
creating a new replica from a sysmeta. pyxb and a sciobj bytestream.

Response:
def _create_replica(self, sysmeta_pyxb, sciobj_bytestream):
        """GMN handles replicas differently from native objects, with the main
        differences being related to handling of restrictions related to revision chains
        and SIDs.

        So this create sequence differs significantly from the regular one that is
        accessed through MNStorage.create().

        """
        pid = d1_common.xml.get_req_val(sysmeta_pyxb.identifier)
        self._assert_is_pid_of_local_unprocessed_replica(pid)
        self._check_and_create_replica_revision(sysmeta_pyxb, "obsoletes")
        self._check_and_create_replica_revision(sysmeta_pyxb, "obsoletedBy")
        sciobj_url = d1_gmn.app.sciobj_store.get_rel_sciobj_file_url_by_pid(pid)
        sciobj_model = d1_gmn.app.sysmeta.create_or_update(sysmeta_pyxb, sciobj_url)
        self._store_science_object_bytes(pid, sciobj_bytestream)
        d1_gmn.app.event_log.create_log_entry(
            sciobj_model, "create", "0.0.0.0", "[replica]", "[replica]"
        )


Instruction:
Implement a Python 3 function for
importing only the Science Objects specified by a text file. The file must be UTF-8 encoded and contain one PIDs or SIDs per line.

Response:
async def restricted_import(self, async_client, node_type):
        """Import only the Science Objects specified by a text file.

        The file must be UTF-8 encoded and contain one PIDs or SIDs per line.

        """
        item_task_name = "Importing objects"
        pid_path = self.options['pid_path']

        if not os.path.exists(pid_path):
            raise ConnectionError('File does not exist: {}'.format(pid_path))

        with open(pid_path, encoding='UTF-8') as pid_file:
            self.progress_logger.start_task_type(
                item_task_name, len(pid_file.readlines())
            )
            pid_file.seek(0)

            for pid in pid_file.readlines():
                pid = pid.strip()
                self.progress_logger.start_task(item_task_name)

                # Ignore any blank lines in the file
                if not pid:
                    continue

                await self.import_aggregated(async_client, pid)

        self.progress_logger.end_task_type(item_task_name)


Instruction:
Make a summary of the following Python 3 code
async def import_aggregated(self, async_client, pid):
        self._logger.info('Importing: {}'.format(pid))

        task_set = set()

        object_info_pyxb = d1_common.types.dataoneTypes.ObjectInfo()
        object_info_pyxb.identifier = pid
        task_set.add(self.import_object(async_client, object_info_pyxb))

        result_set, task_set = await asyncio.wait(task_set)

        assert len(result_set) == 1
        assert not task_set

        sysmeta_pyxb = result_set.pop().result()

        if not sysmeta_pyxb:
            # Import was skipped
            return

        assert d1_common.xml.get_req_val(sysmeta_pyxb.identifier) == pid

        if d1_gmn.app.did.is_resource_map_db(pid):
            for member_pid in d1_gmn.app.resource_map.get_resource_map_members_by_map(
                pid
            ):
                self.progress_logger.event("Importing aggregated SciObj")
                self._logger.info('Importing aggregated SciObj. pid="{}"'.format(pid))
                await self.import_aggregated(async_client, member_pid)

Response: Imports the SciObj at pid.


Instruction:
Create a Python 3 function to
get the proxy location URL for an object.

Response:
async def get_object_proxy_location(self, client, pid):
        """If object is proxied, return the proxy location URL.

        If object is local, return None.

        """
        try:
            return (await client.describe(pid)).get("DataONE-Proxy")
        except d1_common.types.exceptions.DataONEException:
            # Workaround for older GMNs that return 500 instead of 404 for describe()
            pass


Instruction:
Write a Python 3 script for
creating a dict of arguments that will be passed to listObjects.

Response:
def get_list_objects_arg_dict(self, node_type):
        """Create a dict of arguments that will be passed to listObjects().

        If {node_type} is a CN, add filtering to include only objects from this GMN
        instance in the ObjectList returned by CNCore.listObjects().

        """
        arg_dict = {
            # Restrict query for faster debugging
            # "fromDate": datetime.datetime(2017, 1, 1),
            # "toDate": datetime.datetime(2017, 1, 10),
        }
        if node_type == "cn":
            arg_dict["nodeId"] = django.conf.settings.NODE_IDENTIFIER
        return arg_dict


Instruction:
Can you generate the documentation for the following Python 3 function
def get_log_records_arg_dict(self, node_type):
        arg_dict = {
            # Restrict query for faster debugging
            # "fromDate": datetime.datetime(2017, 1, 1),
            # "toDate": datetime.datetime(2017, 1, 3),
        }
        if node_type == "cn":
            arg_dict["nodeId"] = django.conf.settings.NODE_IDENTIFIER
        return arg_dict

Response: Create a dict of arguments that will be passed to getLogRecords.


Instruction:
Can you create a Python 3 function that
returns True if node at base_url is a CN False otherwise Raise a DataONEException

Response:
async def is_cn(self, client):
        """Return True if node at {base_url} is a CN, False if it is an MN.

        Raise a DataONEException if it's not a functional CN or MN.

        """
        node_pyxb = await client.get_capabilities()
        return d1_common.type_conversions.pyxb_get_type_name(node_pyxb) == "NodeList"


Instruction:
Explain what the following Python 3 code does
async def probe_node_type_major(self, client):
        try:
            node_pyxb = await self.get_node_doc(client)
        except d1_common.types.exceptions.DataONEException as e:
            raise django.core.management.base.CommandError(
                "Could not find a functional CN or MN at the provided BaseURL. "
                'base_url="{}" error="{}"'.format(
                    self.options["baseurl"], e.friendly_format()
                )
            )

        is_cn = d1_common.type_conversions.pyxb_get_type_name(node_pyxb) == "NodeList"

        if is_cn:
            self.assert_is_known_node_id(
                node_pyxb, django.conf.settings.NODE_IDENTIFIER
            )
            self._logger.info(
                "Importing from CN: {}. filtered on MN: {}".format(
                    d1_common.xml.get_req_val(
                        self.find_node(node_pyxb, self.options["baseurl"]).identifier
                    ),
                    django.conf.settings.NODE_IDENTIFIER,
                )
            )
            return "cn", "v2"
        else:
            self._logger.info(
                "Importing from MN: {}".format(
                    d1_common.xml.get_req_val(node_pyxb.identifier)
                )
            )
            return "mn", self.find_node_api_version(node_pyxb)

Response:
Determine if import source node is a CN or MN and which major version API to
            use.


Instruction:
Given the following Python 3 function, write the documentation
def find_node(self, node_list_pyxb, base_url):
        for node_pyxb in node_list_pyxb.node:
            if node_pyxb.baseURL == base_url:
                return node_pyxb

Response:
Search NodeList for Node that has base_url.

            Return matching Node or None


Instruction:
Implement a Python 3 function for
finding the highest API major version supported by node.

Response:
def find_node_api_version(self, node_pyxb):
        """Find the highest API major version supported by node."""
        max_major = 0
        for s in node_pyxb.services.service:
            max_major = max(max_major, int(s.version[1:]))
        return max_major


Instruction:
Write a Python 3 script to
search NodeList for Node with id.

Response:
def find_node_by_id(self, node_list_pyxb, node_id):
        """Search NodeList for Node with {node_id}.

        Return matching Node or None

        """
        for node_pyxb in node_list_pyxb.node:
            # if node_pyxb.baseURL == base_url:
            if d1_common.xml.get_req_val(node_pyxb.identifier) == node_id:
                return node_pyxb


Instruction:
Create a Python 3 function for
running the celery process executor.

Response:
def celery_run(data_id, runtime_dir, argv):
    """Run process executor.

    :param data_id: The id of the :class:`~resolwe.flow.models.Data`
        object to be processed.
    :param runtime_dir: The directory from which to run the executor.
    :param argv: The argument vector used to run the executor.
    :param verbosity: The logging verbosity level.
    """
    subprocess.Popen(
        argv,
        cwd=runtime_dir,
        stdin=subprocess.DEVNULL
    ).wait()


Instruction:
Can you generate the documentation for the following Python 3 function
def archive_sciobj(pid):
    sciobj_model = d1_gmn.app.model_util.get_sci_model(pid)
    sciobj_model.is_archived = True
    sciobj_model.save()
    _update_modified_timestamp(sciobj_model)

Response: Set the status of an object to archived.


Instruction:
Can you create a Python 3 function that
creates or update a System Metadata object and closely related internal state.

Response:
def create_or_update(sysmeta_pyxb, sciobj_url=None):
    """Create or update database representation of a System Metadata object and closely
    related internal state.

    - If ``sciobj_url`` is not passed on create, storage in the internal sciobj store
      is assumed
    - If ``sciobj_url`` is passed on create, it can reference a location in the
      internal sciobj store, or an arbitrary location on disk, or a remote web server.
      See the sciobj_store module for more information
    - if ``sciobj_url`` is not passed on update, the sciobj location remains unchanged
    - If ``sciobj_url`` is passed on update, the sciobj location is updated

    Preconditions:
    - All values in ``sysmeta_pyxb`` must be valid for the operation being performed

    """
    # TODO: Make sure that old sections are removed if not included in update.

    pid = d1_common.xml.get_req_val(sysmeta_pyxb.identifier)

    if sciobj_url is None:
        sciobj_url = d1_gmn.app.sciobj_store.get_rel_sciobj_file_url_by_pid(pid)

    try:
        sci_model = d1_gmn.app.model_util.get_sci_model(pid)
    except d1_gmn.app.models.ScienceObject.DoesNotExist:
        sci_model = d1_gmn.app.models.ScienceObject()
        sci_model.pid = d1_gmn.app.did.get_or_create_did(pid)
        sci_model.url = sciobj_url
        sci_model.serial_version = sysmeta_pyxb.serialVersion
        sci_model.uploaded_timestamp = d1_common.date_time.normalize_datetime_to_utc(
            sysmeta_pyxb.dateUploaded
        )

    _base_pyxb_to_model(sci_model, sysmeta_pyxb)

    sci_model.save()

    if _has_media_type_pyxb(sysmeta_pyxb):
        _media_type_pyxb_to_model(sci_model, sysmeta_pyxb)

    _access_policy_pyxb_to_model(sci_model, sysmeta_pyxb)

    if _has_replication_policy_pyxb(sysmeta_pyxb):
        _replication_policy_pyxb_to_model(sci_model, sysmeta_pyxb)

    replica_pyxb_to_model(sci_model, sysmeta_pyxb)
    revision_pyxb_to_model(sci_model, sysmeta_pyxb, pid)

    sci_model.save()

    return sci_model


Instruction:
Write a Python 3 script for
creating or update the database representation of sysmeta_pyxb access policy.

Response:
def _access_policy_pyxb_to_model(sci_model, sysmeta_pyxb):
    """Create or update the database representation of the sysmeta_pyxb access policy.

    If called without an access policy, any existing permissions on the object
    are removed and the access policy for the rights holder is recreated.

    Preconditions:
      - Each subject has been verified to a valid DataONE account.
      - Subject has changePermission for object.

    Postconditions:
      - The Permission and related tables contain the new access policy.

    Notes:
      - There can be multiple rules in a policy and each rule can contain multiple
        subjects. So there are two ways that the same subject can be specified multiple
        times in a policy. If this happens, multiple, conflicting action levels may be
        provided for the subject. This is handled by checking for an existing row for
        the subject for this object and updating it if it contains a lower action
        level. The end result is that there is one row for each subject, for each
        object and this row contains the highest action level.

    """
    _delete_existing_access_policy(sysmeta_pyxb)
    # Add an implicit allow rule with all permissions for the rights holder.
    allow_rights_holder = d1_common.types.dataoneTypes.AccessRule()
    permission = d1_common.types.dataoneTypes.Permission(
        d1_gmn.app.auth.CHANGEPERMISSION_STR
    )
    allow_rights_holder.permission.append(permission)
    allow_rights_holder.subject.append(
        d1_common.xml.get_req_val(sysmeta_pyxb.rightsHolder)
    )
    top_level = _get_highest_level_action_for_rule(allow_rights_holder)
    _insert_permission_rows(sci_model, allow_rights_holder, top_level)
    # Create db entries for all subjects for which permissions have been granted.
    if _has_access_policy_pyxb(sysmeta_pyxb):
        for allow_rule in sysmeta_pyxb.accessPolicy.allow:
            top_level = _get_highest_level_action_for_rule(allow_rule)
            _insert_permission_rows(sci_model, allow_rule, top_level)


Instruction:
How would you explain what the following Python 3 function does
def add_arguments(self, parser):
        parser.description = __doc__
        parser.formatter_class = argparse.RawDescriptionHelpFormatter
        parser.add_argument("--debug", action="store_true", help="Debug level logging")
        parser.add_argument(
            "--force",
            action="store_true",
            help="Import even if local database is not empty",
        )
        parser.add_argument("--clear", action="store_true", help="Clear local database")
        parser.add_argument(
            "--cert-pub",
            dest="cert_pem_path",
            action="store",
            help="Path to PEM formatted public key of certificate",
        )
        parser.add_argument(
            "--cert-key",
            dest="cert_key_path",
            action="store",
            help="Path to PEM formatted private key of certificate",
        )
        parser.add_argument(
            "--public",
            action="store_true",
            help="Do not use certificate even if available",
        )
        parser.add_argument(
            "--timeout",
            type=float,
            action="store",
            default=DEFAULT_TIMEOUT_SEC,
            help="Timeout for D1 API call to the source MN",
        )
        parser.add_argument(
            "--workers",
            type=int,
            action="store",
            default=DEFAULT_N_WORKERS,
            help="Max number workers making concurrent connections to the source MN",
        )
        parser.add_argument(
            "--object-page-size",
            type=int,
            action="store",
            default=d1_common.const.DEFAULT_SLICE_SIZE,
            help="Number of objects to retrieve in each listObjects() call",
        )
        parser.add_argument(
            "--log-page-size",
            type=int,
            action="store",
            default=d1_common.const.DEFAULT_SLICE_SIZE,
            help="Number of log records to retrieve in each getLogRecords() call",
        )
        parser.add_argument(
            "--major",
            type=int,
            action="store",
            help="Use API major version instead of finding by connecting to CN",
        )
        parser.add_argument(
            "--only-log", action="store_true", help="Only import event logs"
        )
        parser.add_argument(
            "--max-obj",
            type=int,
            action="store",
            help="Limit number of objects to import",
        )
        parser.add_argument("baseurl", help="Source MN BaseURL")

Response: Adds command line arguments to the given parser.


Instruction:
How would you explain what the following Python 3 function does
def _create_log_entry(self, log_record_pyxb):
        event_log_model = d1_gmn.app.event_log.create_log_entry(
            d1_gmn.app.model_util.get_sci_model(
                d1_common.xml.get_req_val(log_record_pyxb.identifier)
            ),
            log_record_pyxb.event,
            log_record_pyxb.ipAddress,
            log_record_pyxb.userAgent,
            log_record_pyxb.subject.value(),
        )
        event_log_model.timestamp = d1_common.date_time.normalize_datetime_to_utc(
            log_record_pyxb.dateLogged
        )
        event_log_model.save()

Response: Create a log entry from a log record.


Instruction:
Write a Python 3 script for
downloading bytes from the source Science Object store to the store.

Response:
def _download_source_sciobj_bytes_to_store(self, client, pid):
        """Args:

        client: pid:

        """
        if d1_gmn.app.sciobj_store.is_existing_sciobj_file(pid):
            self._events.log_and_count(
                "Skipped download of existing sciobj bytes", 'pid="{}"'.format(pid)
            )
        else:
            with d1_gmn.app.sciobj_store.open_sciobj_file_by_pid(
                pid, write=True
            ) as sciobj_file:
                client.get_and_save(pid, sciobj_file)


Instruction:
How would you explain what the following Python 3 function does
def _assert_path_is_dir(self, dir_path):
        if not os.path.isdir(dir_path):
            raise django.core.management.base.CommandError(
                'Invalid dir path. path="{}"'.format(dir_path)
            )

Response: Checks that the path is a directory.


Instruction:
Make a summary of the following Python 3 code
def HEAD(self, rest_path_list, **kwargs):
        kwargs.setdefault("allow_redirects", False)
        return self._request("HEAD", rest_path_list, **kwargs)

Response: Send a HEAD request. See requests. request for optional parameters.


Instruction:
Write a Python 3 script for
sending a POST request to the specified path list.

Response:
def POST(self, rest_path_list, **kwargs):
        """Send a POST request with optional streaming multipart encoding. See
        requests.sessions.request for optional parameters. To post regular data, pass a
        string, iterator or generator as the ``data`` argument. To post a multipart
        stream, pass a dictionary of multipart elements as the ``fields`` argument.
        E.g.:

        fields = {
          'field0': 'value',
          'field1': 'value',
          'field2': ('filename.xml', open('file.xml', 'rb'), 'application/xml')
        }

        :returns: Response object

        """
        fields = kwargs.pop("fields", None)
        if fields is not None:
            return self._send_mmp_stream("POST", rest_path_list, fields, **kwargs)
        else:
            return self._request("POST", rest_path_list, **kwargs)


Instruction:
Can you generate the documentation for the following Python 3 function
def PUT(self, rest_path_list, **kwargs):
        fields = kwargs.pop("fields", None)
        if fields is not None:
            return self._send_mmp_stream("PUT", rest_path_list, fields, **kwargs)
        else:
            return self._request("PUT", rest_path_list, **kwargs)

Response: Send a PUT request with optional streaming multipart encoding. See post for optional parameters.


Instruction:
Can you create a Python 3 function that
gets request as cURL command line for debugging.

Response:
def get_curl_command_line(self, method, url, **kwargs):
        """Get request as cURL command line for debugging."""
        if kwargs.get("query"):
            url = "{}?{}".format(url, d1_common.url.urlencode(kwargs["query"]))
        curl_list = ["curl"]
        if method.lower() == "head":
            curl_list.append("--head")
        else:
            curl_list.append("-X {}".format(method))
        for k, v in sorted(list(kwargs["headers"].items())):
            curl_list.append('-H "{}: {}"'.format(k, v))
        curl_list.append("{}".format(url))
        return " ".join(curl_list)


Instruction:
Write a Python 3 script to
dump the request and response objects for logging and debugging.

Response:
def dump_request_and_response(self, response):
        """Return a string containing a nicely formatted representation of the request
        and response objects for logging and debugging.

        - Note: Does not work if the request or response body is a MultipartEncoder
          object.

        """
        if response.reason is None:
            response.reason = "<unknown>"
        return d1_client.util.normalize_request_response_dump(
            requests_toolbelt.utils.dump.dump_response(response)
        )


Instruction:
Can you generate the documentation for the following Python 3 function
def _timeout_to_float(self, timeout):
        if timeout is not None:
            try:
                timeout_float = float(timeout)
            except ValueError:
                raise ValueError(
                    'timeout_sec must be a valid number or None. timeout="{}"'.format(
                        timeout
                    )
                )
            if timeout_float:
                return timeout_float

Response: Convert timeout to float.


Instruction:
Write a Python 3 script for
printing the RedBaron syntax tree for a Python module.

Response:
def main():
    """Print the RedBaron syntax tree for a Python module."""
    parser = argparse.ArgumentParser(
        description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
    )
    parser.add_argument("path", help="Python module path")
    args = parser.parse_args()

    r = d1_dev.util.redbaron_module_path_to_tree(args.path)
    print(r.help(True))


Instruction:
How would you implement a function in Python 3 that
joins a base and a relative path and return an absolute path to the resulting location.

Response:
def abs_path_from_base(base_path, rel_path):
    """Join a base and a relative path and return an absolute path to the resulting
    location.

    Args:
      base_path: str
        Relative or absolute path to prepend to ``rel_path``.

      rel_path: str
        Path relative to the location of the module file from which this function is called.

    Returns:
        str : Absolute path to the location specified by ``rel_path``.

    """
    # noinspection PyProtectedMember
    return os.path.abspath(
        os.path.join(
            os.path.dirname(sys._getframe(1).f_code.co_filename), base_path, rel_path
        )
    )


Instruction:
Can you write a function in Python 3 where it
converts a path that is relative to the module file from which this function is called to an absolute path.

Response:
def abs_path(rel_path):
    """Convert a path that is relative to the module from which this function is called,
    to an absolute path.

    Args:
      rel_path: str
        Path relative to the location of the module file from which this function is called.

    Returns:
        str : Absolute path to the location specified by ``rel_path``.

    """
    # noinspection PyProtectedMember
    return os.path.abspath(
        os.path.join(os.path.dirname(sys._getframe(1).f_code.co_filename), rel_path)
    )


Instruction:
Write a Python 3 script to
return the current state of the wrapper as XML

Response:
def get_xml(self, encoding='unicode'):
        """Returns:

        str : Current state of the wrapper as XML

        """
        return xml.etree.ElementTree.tostring(self._root_el, encoding)


Instruction:
Explain what the following Python 3 code does
def get_pretty_xml(self, encoding='unicode'):
        return d1_common.xml.reformat_to_pretty_xml(
            xml.etree.ElementTree.tostring(self._root_el, encoding)
        )

Response: Returns the current state of the wrapper as a pretty printed XML string.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_xml_below_element(self, el_name, el_idx=0, encoding='unicode'):
        return xml.etree.ElementTree.tostring(
            self.get_element_by_name(el_name, el_idx), encoding
        )

Response: Returns the XML fragment rooted at el_name below el_idx.


Instruction:
Explain what the following Python 3 code does
def get_element_by_xpath(self, xpath_str, namespaces=None):
        try:
            return self._root_el.findall('.' + xpath_str, namespaces)
        except (ValueError, xml.etree.ElementTree.ParseError) as e:
            raise SimpleXMLWrapperException(
                'XPath select raised exception. xpath_str="{}" error="{}"'.format(
                    xpath_str, str(e)
                )
            )

Response: Returns the list of elements matching xpath_str.


Instruction:
Given the following Python 3 function, write the documentation
def get_element_by_name(self, el_name, el_idx=0):
        el_list = self.get_element_list_by_name(el_name)
        try:
            return el_list[el_idx]
        except IndexError:
            raise SimpleXMLWrapperException(
                'Element not found. element_name="{}" requested_idx={} '
                'available_elements={}'.format(el_name, el_idx, len(el_list))
            )

Response: Returns the element with the specified name.


Instruction:
Can you tell what is the following Python 3 function doing
def get_element_by_attr_key(self, attr_key, el_idx=0):
        el_list = self.get_element_list_by_attr_key(attr_key)
        try:
            return el_list[el_idx]
        except IndexError:
            raise SimpleXMLWrapperException(
                'Element with tag not found. tag_name="{}" requested_idx={} '
                'available_elements={}'.format(attr_key, el_idx, len(el_list))
            )

Response: Returns the element with the specified attribute key.


Instruction:
Implement a Python 3 function for
updating the text of the element with the given name.

Response:
def set_element_text(self, el_name, el_text, el_idx=0):
        """
    Args:
      el_name : str
        Name of element to update.

      el_text : str
        Text to set for element.

      el_idx : int
        Index of element to use in the event that there are multiple sibling
        elements with the same name.
    """
        self.get_element_by_name(el_name, el_idx).text = el_text


Instruction:
Create a Python 3 function for
setting the text of the element with the given attribute key.

Response:
def set_element_text_by_attr_key(self, attr_key, el_text, el_idx=0):
        """
    Args:
      attr_key : str
        Name of attribute for which to search

      el_text : str
        Text to set for element.

      el_idx : int
        Index of element to use in the event that there are multiple sibling
        elements with the same name.
    """
        self.get_element_by_attr_key(attr_key, el_idx).text = el_text


Instruction:
Can you tell what is the following Python 3 function doing
def get_attr_value(self, attr_key, el_idx=0):
        return self.get_element_by_attr_key(attr_key, el_idx).attrib[attr_key]

Response: Return the value of the selected attribute in the selected element.


Instruction:
Can you generate the documentation for the following Python 3 function
def set_attr_text(self, attr_key, attr_val, el_idx=0):
        self.get_element_by_attr_key(attr_key, el_idx).attrib[attr_key] = attr_val

Response: Set the value of the selected attribute of the selected element.


Instruction:
Implement a function in Python 3 to
return the text of the selected element as a datetime. datetime object.

Response:
def get_element_dt(self, el_name, tz=None, el_idx=0):
        """Return the text of the selected element as a ``datetime.datetime`` object.

        The element text must be a ISO8601 formatted datetime

        Args:
          el_name : str
            Name of element to use.

          tz : datetime.tzinfo
            Timezone in which to return the datetime.

            - Without a timezone, other contextual information is required in order to
              determine the exact represented time.
            - If dt has timezone: The ``tz`` parameter is ignored.
            - If dt is naive (without timezone): The timezone is set to ``tz``.
            - ``tz=None``: Prevent naive dt from being set to a timezone. Without a
              timezone, other contextual information is required in order to determine
              the exact represented time.
            - ``tz=d1_common.date_time.UTC()``: Set naive dt to UTC.

          el_idx : int
            Index of element to use in the event that there are multiple sibling
            elements with the same name.

        Returns:
          datetime.datetime

        """
        return iso8601.parse_date(self.get_element_by_name(el_name, el_idx).text, tz)


Instruction:
Can you implement a function in Python 3 that
sets the text of the selected element to an ISO8601 formatted datetime.

Response:
def set_element_dt(self, el_name, dt, tz=None, el_idx=0):
        """Set the text of the selected element to an ISO8601 formatted datetime.

        Args:
          el_name : str
            Name of element to update.

          dt : datetime.datetime
            Date and time to set

          tz : datetime.tzinfo
            Timezone to set

            - Without a timezone, other contextual information is required in order to
              determine the exact represented time.
            - If dt has timezone: The ``tz`` parameter is ignored.
            - If dt is naive (without timezone): The timezone is set to ``tz``.
            - ``tz=None``: Prevent naive dt from being set to a timezone. Without a
              timezone, other contextual information is required in order to determine
              the exact represented time.
            - ``tz=d1_common.date_time.UTC()``: Set naive dt to UTC.

          el_idx : int
            Index of element to use in the event that there are multiple sibling
            elements with the same name.

        """
        dt = d1_common.date_time.cast_naive_datetime_to_tz(dt, tz)
        self.get_element_by_name(el_name, el_idx).text = dt.isoformat()


Instruction:
Create a Python 3 function to
replace element. with a new element with the root_el.

Response:
def replace_by_etree(self, root_el, el_idx=0):
        """Replace element.

        Select element that has the same name as ``root_el``, then replace the selected
        element with ``root_el``

        ``root_el`` can be a single element or the root of an element tree.

        Args:
          root_el : element
            New element that will replace the existing element.

        """
        el = self.get_element_by_name(root_el.tag, el_idx)
        el[:] = list(root_el)
        el.attrib = root_el.attrib


Instruction:
Here you have a function in Python 3, explain what it does
def replace_by_xml(self, xml_str, el_idx=0):
        root_el = self.parse_xml(xml_str)
        self.replace_by_etree(root_el, el_idx)

Response:
Replace element.
 Arc with xml_str


Instruction:
Implement a function in Python 3 to
compile SQL for this function.

Response:
def as_sql(self, compiler, connection):  # pylint: disable=arguments-differ
        """Compile SQL for this function."""
        sql, params = super().as_sql(compiler, connection)
        params.append(self.path)
        return sql, params


Instruction:
Make a summary of the following Python 3 code
def with_json_path(self, path, field=None):
        if field is None:
            field = '_'.join(['json'] + json_path_components(path))

        kwargs = {field: JsonGetPath('json', path)}
        return self.defer('json').annotate(**kwargs)

Response: Annotate Storage objects with a specific JSON path.


Instruction:
Can you tell what is the following Python 3 function doing
def get_json_path(self, path):
        return self.with_json_path(path, field='result').values_list('result', flat=True)

Response: Return only a specific JSON path of the object.


Instruction:
Can you implement a function in Python 3 that
loads json field from Storage object.

Response:
def _get_storage(self):
        """Load `json` field from `Storage` object."""
        if self._json is None:
            self._json = Storage.objects.get(**self._kwargs).json


Instruction:
Write a Python 3 function for
migrating process schema. :param process: Process instance :param schema: Process schema to migrate :param from_state: Database model state :return: True if the process was migrated, False otherwise

Response:
def migrate_process_schema(self, process, schema, from_state):
        """Migrate process schema.

        :param process: Process instance
        :param schema: Process schema to migrate
        :param from_state: Database model state
        :return: True if the process was migrated, False otherwise
        """
        container = dict_dot(schema, '.'.join(self.field[:-1]), default=list)

        # Ignore processes, which already contain the target field with the
        # target schema.
        for field in container:
            if field['name'] == self.field[-1]:
                if field == self.schema:
                    return False
                else:
                    raise ValueError(
                        "Failed to migrate schema for process '{process}' as the field '{field}' "
                        "already exists and has an incompatible schema".format(
                            process=process.slug,
                            field=self.field[-1]
                        )
                    )

        # Add field to container.
        container.append(self.schema)
        return True


Instruction:
How would you explain what the following Python 3 function does
def migrate_data(self, data, from_state):
        if not self.default:
            return

        self.default.prepare(data, from_state)
        for instance in data:
            value = self.default.get_default_for(instance, from_state)
            if not value and not self.schema.get('required', True):
                continue

            # Set default value.
            container = getattr(instance, self.schema_type, {})
            dict_dot(container, '.'.join(self.field), value)
            setattr(instance, self.schema_type, container)
            instance.save()

Response:
Migrate data objects.

        :param data: Queryset containing all data objects that need
            to be migrated
        :param from_state: Database model state


Instruction:
Given the following Python 3 function, write the documentation
def migrate_process_schema(self, process, schema, from_state):
        container = dict_dot(schema, '.'.join(self.field[:-1]), default=list)

        # Ignore processes, which already contain the target field.
        migrate = False
        for field in container:
            if field['name'] == self.field[-1]:
                field['name'] = self.new_field
                migrate = True
                break
            elif field['name'] == self.new_field:
                # Already has target field.
                migrate = False
                break
        else:
            if not self.skip_no_field:
                raise ValueError(
                    "Unable to rename: there is no field with name '{field}' or '{new_field}'.".format(
                        field=self.field[-1],
                        new_field=self.new_field,
                    )
                )

        return migrate

Response: Migrate the process schema.


Instruction:
Here you have a function in Python 3, explain what it does
def migrate_data(self, data, from_state):
        for instance in data:
            if instance.status == 'ER':
                continue

            container = getattr(instance, self.schema_type, {})
            schema = container.pop(self.field[-1])
            container[self.new_field] = schema

            setattr(instance, self.schema_type, container)
            instance.save()

Response:
Migrate data objects.

        :param data: Queryset containing all data objects that need
            to be migrated
        :param from_state: Database model state


Instruction:
Can you tell what is the following Python 3 function doing
def migrate_process_schema(self, process, schema, from_state):
        if process.type == self.new_type:
            return False

        process.type = self.new_type
        return True

Response: Migrate process instance to new schema.


Instruction:
Can you write a function in Python 3 where it
homes page. Root of web server should redirect to here.

Response:
def home(request):
    """Home page.

    Root of web server should redirect to here.

    """
    if request.path.endswith('/'):
        return django.http.HttpResponseRedirect(request.path[:-1])

    return django.http.HttpResponse(
        generate_status_xml(), d1_common.const.CONTENT_TYPE_XML
    )


Instruction:
Given the following Python 3 function, write the documentation
def error_404(request, exception):
    return django.http.HttpResponseNotFound(
        d1_common.types.exceptions.NotFound(
            0,
            'Invalid API endpoint',
            # Include the regexes the URL was tested against
            # traceInformation=str(exception),
            nodeId=django.conf.settings.NODE_IDENTIFIER,
        ).serialize_to_transport(xslt_url=django.urls.base.reverse('home_xslt')),
        d1_common.const.CONTENT_TYPE_XML,
    )

Response: Handle 404s outside of the valid API URL endpoints.


Instruction:
Can you create a Python 3 function that
returns the total free space on the object storage on which the object storage resides ( in bytes

Response:
def get_obj_store_free_space_bytes():
    """Return total free space available on the disk on which the object storage resides
    (in bytes)"""
    obj_store_path = django.conf.settings.OBJECT_STORE_PATH
    if platform.system() == 'Windows':
        free_bytes = ctypes.c_ulonglong(0)
        ctypes.windll.kernel32.GetDiskFreeSpaceExW(
            ctypes.c_wchar_p(obj_store_path), None, None, ctypes.pointer(free_bytes)
        )
        return free_bytes.value
    else:
        return os.statvfs(obj_store_path).f_bfree * os.statvfs(obj_store_path).f_frsize


Instruction:
Can you tell what is the following Python 3 function doing
def m2Lambda_to_vMh2(m2, Lambda, C):
    try:
        v = (sqrt(2 * m2 / Lambda) + 3 * m2**(3 / 2) /
             (sqrt(2) * Lambda**(5 / 2)) * C['phi'])
    except ValueError:
        v = 0
    Mh2 = 2 * m2 * (1 - m2 / Lambda * (3 * C['phi'] - 4 * Lambda * C['phiBox'] +
                                        Lambda * C['phiD']))
    return {'v': v, 'Mh2': Mh2}

Response:
Function to numerically determine the physical Higgs VEV and mass
    given the parameters of the Higgs potential.


Instruction:
Write a Python 3 script for
functioning to numerically determine the parameters of the Higgs potential given the physical Higgs VEV and mass.

Response:
def vMh2_to_m2Lambda(v, Mh2, C):
    """Function to numerically determine the parameters of the Higgs potential
    given the physical Higgs VEV and mass."""
    if C['phi'] == 0 and C['phiBox'] == 0 and C['phiD'] == 0:
        return _vMh2_to_m2Lambda_SM(v, Mh2)
    else:
        def f0(x):  # we want the root of this function
            m2, Lambda = x
            d = m2Lambda_to_vMh2(m2=m2.real, Lambda=Lambda.real,
                                 C=C)
            return np.array([d['v'] - v, d['Mh2'] - Mh2])
        dSM = _vMh2_to_m2Lambda_SM(v, Mh2)
        x0 = np.array([dSM['m2'], dSM['Lambda']])
        try:
            xres = scipy.optimize.newton_krylov(f0, x0)
        except (scipy.optimize.nonlin.NoConvergence, ValueError) as e:
            warnings.warn('Standard optimization method did not converge. The GMRES method is used instead.', Warning)
            try:
                xres = scipy.optimize.newton_krylov(f0, x0, method='gmres',
                                                    f_tol=1e-7)
            except (scipy.optimize.nonlin.NoConvergence, ValueError) as e:
                raise ValueError("No solution for m^2 and Lambda found. This problem can be caused by very large values for one or several Wilson coefficients.")
        return {'m2': xres[0], 'Lambda': xres[1]}


Instruction:
How would you explain what the following Python 3 function does
def smeftpar(scale, C, basis):
    # start with a zero dict and update it with the input values
    MW = p['m_W']
    # MZ = p['m_Z']
    GF = p['GF']
    Mh = p['m_h']
    vb = sqrt(1 / sqrt(2) / GF)
    v = vb  # TODO
    _d = vMh2_to_m2Lambda(v=v, Mh2=Mh**2, C=C)
    m2 = _d['m2'].real
    Lambda = _d['Lambda'].real
    gsbar = sqrt(4 * pi * p['alpha_s'])
    gs = (1 - C['phiG'] * (v**2)) * gsbar
    gbar = 2 * MW / v
    g = gbar * (1 - C['phiW'] * (v**2))
    ebar = sqrt(4 * pi * p['alpha_e'])
    gp = get_gpbar(ebar, gbar, v, C)
    c = {}
    c['m2'] = m2
    c['Lambda'] = Lambda
    c['g'] = g
    c['gp'] = gp
    c['gs'] = gs
    K = ckmutil.ckm.ckm_tree(p['Vus'], p['Vub'], p['Vcb'], p['delta'])
    if basis == 'Warsaw':
        Mu = K.conj().T @ np.diag([p['m_u'], p['m_c'], p['m_t']])
        Md = np.diag([p['m_d'], p['m_s'], p['m_b']])
    elif basis == 'Warsaw up':
        Mu = np.diag([p['m_u'], p['m_c'], p['m_t']])
        Md = K @ np.diag([p['m_d'], p['m_s'], p['m_b']])
    else:
        raise ValueError("Basis '{}' not supported".format(basis))
    Me = np.diag([p['m_e'], p['m_mu'], p['m_tau']])
    c['Gd'] = Md / (v / sqrt(2)) + C['dphi'] * (v**2) / 2
    c['Gu'] = Mu / (v / sqrt(2)) + C['uphi'] * (v**2) / 2
    c['Ge'] = Me / (v / sqrt(2)) + C['ephi'] * (v**2) / 2
    return c

Response: Get the running parameters in SMEFT.


Instruction:
Write a Python 3 script for
returning the running effective SM parameters.

Response:
def smpar(C):
    """Get the running effective SM parameters."""
    m2 = C['m2'].real
    Lambda = C['Lambda'].real
    v = (sqrt(2 * m2 / Lambda) + 3 * m2**(3 / 2) /
         (sqrt(2) * Lambda**(5 / 2)) * C['phi'])
    GF = 1 / (sqrt(2) * v**2)  # TODO
    Mh2 = 2 * m2 * (1 - m2 / Lambda * (3 * C['phi'] - 4 * Lambda * C['phiBox'] +
                                       Lambda * C['phiD']))
    eps = C['phiWB'] * (v**2)
    gb = (C['g'] / (1 - C['phiW'] * (v**2))).real
    gpb = (C['gp'] / (1 - C['phiB'] * (v**2))).real
    gsb = (C['gs'] / (1 - C['phiG'] * (v**2))).real
    MW = gb * v / 2
    ZG0 = 1 + C['phiD'] * (v**2) / 4
    MZ = (sqrt(gb**2 + gpb**2) / 2 * v
          * (1 + eps * gb * gpb / (gb**2 + gpb**2)) * ZG0)
    Mnup = -(v**2) * C['llphiphi']
    Mep = v / sqrt(2) * (C['Ge'] - C['ephi'] * (v**2) / 2)
    Mup = v / sqrt(2) * (C['Gu'] - C['uphi'] * (v**2) / 2)
    Mdp = v / sqrt(2) * (C['Gd'] - C['dphi'] * (v**2) / 2)
    UeL, Me, UeR = ckmutil.diag.msvd(Mep)
    UuL, Mu, UuR = ckmutil.diag.msvd(Mup)
    UdL, Md, UdR = ckmutil.diag.msvd(Mdp)
    UnuL, Mnu = ckmutil.diag.mtakfac(Mnup)
    eb = (gb * gpb / sqrt(gb**2 + gpb**2) *
          (1 - eps * gb * gpb / (gb**2 + gpb**2)))
    K = UuL.conj().T @ UdL
    # U = UeL.conj().T @ UnuL
    sm = {}
    sm['GF'] = GF
    sm['alpha_e'] = eb**2 / (4 * pi)
    sm['alpha_s'] = gsb**2 / (4 * pi)
    sm['Vub'] = abs(K[0, 2])
    sm['Vcb'] = abs(K[1, 2])
    sm['Vus'] = abs(K[0, 1])
    sm['delta'] = phase(-K[0, 0] * K[0, 2].conj()
                        / (K[1, 0] * K[1, 2].conj()))
    # sm['U'] = Uu
    sm['m_W'] = MW
    sm['m_Z'] = MZ
    sm['m_h'] = sqrt(abs(Mh2))
    sm['m_u'] = Mu[0]
    sm['m_c'] = Mu[1]
    sm['m_t'] = Mu[2]
    sm['m_d'] = Md[0]
    sm['m_s'] = Md[1]
    sm['m_b'] = Md[2]
    sm['m_e'] = Me[0]
    sm['m_mu'] = Me[1]
    sm['m_tau'] = Me[2]
    return  {k: v.real for k, v in sm.items()}


Instruction:
Can you generate a brief explanation for the following Python 3 code
def scale_8(b):
    a = np.array(b, copy=True, dtype=complex)
    for i in range(3):
        a[0, 0, 1, i] = 1/2 * b[0, 0, 1, i]
        a[0, 0, 2, i] = 1/2 * b[0, 0, 2, i]
        a[0, 1, 1, i] = 1/2 * b[0, 1, 1, i]
        a[0, 1, 2, i] = 2/3 * b[0, 1, 2, i] - 1/6 * b[0, 2, 1, i] - 1/6 * b[1, 0, 2, i] + 1/6 * b[1, 2, 0, i]
        a[0, 2, 1, i] = - (1/6) * b[0, 1, 2, i] + 2/3 * b[0, 2, 1, i] + 1/6 * b[1, 0, 2, i] + 1/3 * b[1, 2, 0, i]
        a[0, 2, 2, i] = 1/2 * b[0, 2, 2, i]
        a[1, 0, 2, i] = - (1/6) * b[0, 1, 2, i] + 1/6 * b[0, 2, 1, i] + 2/3 * b[1, 0, 2, i] - 1/6 * b[1, 2, 0, i]
        a[1, 1, 2, i] = 1/2 * b[1, 1, 2, i]
        a[1, 2, 0, i] = 1/6 * b[0, 1, 2, i] + 1/3 * b[0, 2, 1, i] - 1/6 * b[1, 0, 2, i] + 2/3 * b[1, 2, 0, i]
        a[1, 2, 2, i] = 1/2 * b[1, 2, 2, i]
    return a

Response:
Translations necessary for class - 8 coefficients
    to go from a basis with only non - redundant WCxf
    operators to a basis where the Wilson coefficients are symmetrized like
    the operators are not symmetric


Instruction:
Write a Python 3 script for
converting a dictionary with Wilson coefficient names as keys and numbers or numpy arrays as values to a dictionary with a Wilson coefficient name followed by underscore and numeric indices as keys and numbers as values. This is needed for the output in WCxf format.

Response:
def arrays2wcxf(C):
    """Convert a dictionary with Wilson coefficient names as keys and
    numbers or numpy arrays as values to a dictionary with a Wilson coefficient
    name followed by underscore and numeric indices as keys and numbers as
    values. This is needed for the output in WCxf format."""
    d = {}
    for k, v in C.items():
        if np.shape(v) == () or np.shape(v) == (1,):
            d[k] = v
        else:
            ind = np.indices(v.shape).reshape(v.ndim, v.size).T
            for i in ind:
                name = k + '_' + ''.join([str(int(j) + 1) for j in i])
                d[name] = v[tuple(i)]
    return d


Instruction:
Can you generate the documentation for the following Python 3 function
def wcxf2arrays(d):
    C = {}
    for k, v in d.items():
        name = k.split('_')[0]
        s = C_keys_shape[name]
        if s == 1:
            C[k] = v
        else:
            ind = k.split('_')[-1]
            if name not in C:
                C[name] = np.zeros(s, dtype=complex)
            C[name][tuple([int(i) - 1 for i in ind])] = v
    return C

Response:
Convert a dictionary with a Wilson coefficient
    name followed by underscore and numeric indices as keys and numbers as arrays as values. This is needed for parsing a Wilson coefficient tree in WCxf format.


Instruction:
How would you code a function in Python 3 to
add arrays with zeros for missing Wilson coefficient keys

Response:
def add_missing(C):
    """Add arrays with zeros for missing Wilson coefficient keys"""
    C_out = C.copy()
    for k in (set(WC_keys) - set(C.keys())):
        C_out[k] = np.zeros(C_keys_shape[k])
    return C_out


Instruction:
Write a Python 3 script to
convert a 1D array containing C values to a dictionary.

Response:
def C_array2dict(C):
    """Convert a 1D array containing C values to a dictionary."""
    d = OrderedDict()
    i=0
    for k in C_keys:
        s = C_keys_shape[k]
        if s == 1:
            j = i+1
            d[k] = C[i]
        else:
            j = i \
      + reduce(operator.mul, s, 1)
            d[k] = C[i:j].reshape(s)
        i = j
    return d


Instruction:
Given the following Python 3 function, write the documentation
def C_dict2array(C):
    return np.hstack([np.asarray(C[k]).ravel() for k in C_keys])

Response: Convert an OrderedDict containing C values to a 1D array.


Instruction:
Make a summary of the following Python 3 code
def symmetrize_nonred(C):
    C_symm = {}
    for i, v in C.items():
        if i in C_symm_keys[0]:
            C_symm[i] = v.real
        elif i in C_symm_keys[1] + C_symm_keys[3]:
            C_symm[i] = v # nothing to do
        elif i in C_symm_keys[2]:
            C_symm[i] = symmetrize_2(C[i])
        elif i in C_symm_keys[4]:
            C_symm[i] = symmetrize_4(C[i])
            C_symm[i] = C_symm[i] / _d_4
        elif i in C_symm_keys[5]:
            C_symm[i] = symmetrize_5(C[i])
        elif i in C_symm_keys[6]:
            C_symm[i] = symmetrize_6(C[i])
            C_symm[i] = C_symm[i] / _d_6
        elif i in C_symm_keys[7]:
            C_symm[i] = symmetrize_7(C[i])
            C_symm[i] = C_symm[i] / _d_7
        elif i in C_symm_keys[8]:
            C_symm[i] = scale_8(C[i])
            C_symm[i] = symmetrize_8(C_symm[i])
        elif i in C_symm_keys[9]:
            C_symm[i] = symmetrize_9(C[i])
    return C_symm

Response:
This function takes into account the symmetry factors
    that occur when transitioning from a basis with only non - redundant operators and the Wilson coefficients are symmetrized
   .


Instruction:
Given the following Python 3 function, write the documentation
def unscale_dict(C):
    C_out = {k: _scale_dict[k] * v for k, v in C.items()}
    for k in C_symm_keys[8]:
        C_out['qqql'] = unscale_8(C_out['qqql'])
    return C_out

Response: Undo the scaling applied in scale_dict.


Instruction:
Explain what the following Python 3 code does
def wcxf2arrays_symmetrized(d):
    C = wcxf2arrays(d)
    C = symmetrize_nonred(C)
    C = add_missing(C)
    return C

Response: Convert a dictionary with a Wilson coefficient name followed by underscore numeric indices as keys and numbers as values and numpy arrays as values.


Instruction:
Implement a Python 3 function for
nudging manager at the end of every Data object save event.

Response:
def commit_signal(data_id):
    """Nudge manager at the end of every Data object save event."""
    if not getattr(settings, 'FLOW_MANAGER_DISABLE_AUTO_CALLS', False):
        immediate = getattr(settings, 'FLOW_MANAGER_SYNC_AUTO_CALLS', False)
        async_to_sync(manager.communicate)(data_id=data_id, save_settings=False, run_sync=immediate)


Instruction:
How would you implement a function in Python 3 that
calls when a new process is created.

Response:
def manager_post_save_handler(sender, instance, created, **kwargs):
    """Run newly created (spawned) processes."""
    if instance.status == Data.STATUS_DONE or instance.status == Data.STATUS_ERROR or created:
        # Run manager at the end of the potential transaction. Otherwise
        # tasks are send to workers before transaction ends and therefore
        # workers cannot access objects created inside transaction.
        transaction.on_commit(lambda: commit_signal(instance.id))


Instruction:
Create a Python 3 function to
delete Entity when last Data object is deleted.

Response:
def delete_entity(sender, instance, **kwargs):
    """Delete Entity when last Data object is deleted."""
    # 1 means that the last Data object is going to be deleted.
    Entity.objects.annotate(num_data=Count('data')).filter(data=instance, num_data=1).delete()


Instruction:
Here you have a function in Python 3, explain what it does
def delete_relation(sender, instance, **kwargs):
    def process_signal(relation_id):
        """Get the relation and delete it if it has no entities left."""
        try:
            relation = Relation.objects.get(pk=relation_id)
        except Relation.DoesNotExist:
            return

        if relation.entities.count() == 0:
            relation.delete()

    # Wait for partitions to be recreated.
    transaction.on_commit(lambda: process_signal(instance.relation_id))

Response: Delete the relation when the last Entity is removed.


Instruction:
Create a Python 3 function to
start the actual execution ; instantiate the executor and run.

Response:
async def run_executor():
    """Start the actual execution; instantiate the executor and run."""
    parser = argparse.ArgumentParser(description="Run the specified executor.")
    parser.add_argument('module', help="The module from which to instantiate the concrete executor.")
    args = parser.parse_args()

    module_name = '{}.run'.format(args.module)
    class_name = 'FlowExecutor'

    module = import_module(module_name, __package__)
    executor = getattr(module, class_name)()
    with open(ExecutorFiles.PROCESS_SCRIPT, 'rt') as script_file:
        await executor.run(DATA['id'], script_file.read())


Instruction:
How would you code a function in Python 3 to
validate file at sysmeta_pyxb against schema selected via formatId and raise InvalidRequest if invalid.

Response:
def assert_valid(sysmeta_pyxb, pid):
    """Validate file at {sciobj_path} against schema selected via formatId and raise
    InvalidRequest if invalid.

    Validation is only performed when:

    - SciMeta validation is enabled
    - and Object size is below size limit for validation
    - and formatId designates object as a Science Metadata object which is recognized
      and parsed by DataONE CNs
    - and XML Schema (XSD) files for formatId are present on local system

    """
    if not (_is_validation_enabled() and _is_installed_scimeta_format_id(sysmeta_pyxb)):
        return

    if _is_above_size_limit(sysmeta_pyxb):
        if _is_action_accept():
            return
        else:
            raise d1_common.types.exceptions.InvalidRequest(
                0,
                'Science Metadata file is above size limit for validation and this '
                'node has been configured to reject unvalidated Science Metadata '
                'files. For more information, see the SCIMETA_VALIDATE* settings. '
                'size={} size_limit={}'.format(
                    sysmeta_pyxb.size, django.conf.settings.SCIMETA_VALIDATION_MAX_SIZE
                ),
            )

    with d1_gmn.app.sciobj_store.open_sciobj_file_by_pid_ctx(pid) as sciobj_file:
        try:
            d1_scimeta.xml_schema.validate(sysmeta_pyxb.formatId, sciobj_file.read())
        except d1_scimeta.xml_schema.SciMetaValidationError as e:
            raise d1_common.types.exceptions.InvalidRequest(0, str(e))


Instruction:
Can you write a function in Python 3 where it
converts to internal value.

Response:
def to_internal_value(self, data):
        """Convert to internal value."""
        user = getattr(self.context.get('request'), 'user')
        queryset = self.get_queryset()
        permission = get_full_perm('view', queryset.model)
        try:
            return get_objects_for_user(
                user,
                permission,
                queryset.filter(**{self.slug_field: data}),
            ).latest()
        except ObjectDoesNotExist:
            self.fail(
                'does_not_exist',
                slug_name=self.slug_field,
                value=smart_text(data),
                model_name=queryset.model._meta.model_name,  # pylint: disable=protected-access
            )
        except (TypeError, ValueError):
            self.fail('invalid')


Instruction:
Here you have a function in Python 3, explain what it does
def dumps(dct, as_dict=False, **kwargs):

    result_ = TypeSerializer().serialize(json.loads(json.dumps(dct, default=json_serial),
                                                    use_decimal=True))
    if as_dict:
        return next(six.iteritems(result_))[1]
    else:
        return json.dumps(next(six.iteritems(result_))[1], **kwargs)

Response: Dump the dict to DynamoDB format.


Instruction:
Can you implement a function in Python 3 that
loads dynamodb json format to a python dict

Response:
def loads(s, as_dict=False, *args, **kwargs):
    """ Loads dynamodb json format to a python dict.
        :param s - the json string or dict (with the as_dict variable set to True) to convert
        :returns python dict object
    """
    if as_dict or (not isinstance(s, six.string_types)):
        s = json.dumps(s)
    kwargs['object_hook'] = object_hook
    return json.loads(s, *args, **kwargs)


Instruction:
Write a Python 3 function for
returning the size ( in meters ) of a tile

Response:
def tileSize(self, zoom):
        "Returns the size (in meters) of a tile"
        assert zoom in range(0, len(self.RESOLUTIONS))
        return self.tileSizePx * self.RESOLUTIONS[int(zoom)]


Instruction:
Write a Python 3 script for
returning the bounds of a tile in LV03 ( EPSG : 21781 )

Response:
def tileBounds(self, zoom, tileCol, tileRow):
        "Returns the bounds of a tile in LV03 (EPSG:21781)"
        assert zoom in range(0, len(self.RESOLUTIONS))

        # 0,0 at top left: y axis down and x axis right
        tileSize = self.tileSize(zoom)
        minX = self.MINX + tileCol * tileSize
        maxX = self.MINX + (tileCol + 1) * tileSize
        if self.originCorner == 'bottom-left':
            minY = self.MINY + tileRow * tileSize
            maxY = self.MINY + (tileRow + 1) * tileSize
        elif self.originCorner == 'top-left':
            minY = self.MAXY - (tileRow + 1) * tileSize
            maxY = self.MAXY - tileRow * tileSize
        return [minX, minY, maxX, maxY]


Instruction:
Can you create a Python 3 function that
returns a tile address based on a zoom level and \ a point in the tile

Response:
def tileAddress(self, zoom, point):
        "Returns a tile address based on a zoom level and \
        a point in the tile"
        [x, y] = point
        assert x <= self.MAXX and x >= self.MINX
        assert y <= self.MAXY and y >= self.MINY
        assert zoom in range(0, len(self.RESOLUTIONS))

        tileS = self.tileSize(zoom)
        offsetX = abs(x - self.MINX)
        if self.originCorner == 'bottom-left':
            offsetY = abs(y - self.MINY)
        elif self.originCorner == 'top-left':
            offsetY = abs(self.MAXY - y)
        col = offsetX / tileS
        row = offsetY / tileS
        # We are exactly on the edge of a tile and the extent
        if x in (self.MINX, self.MAXX) and col.is_integer():
            col = max(0, col - 1)
        if y in (self.MINY, self.MAXY) and row.is_integer():
            row = max(0, row - 1)
        return [
            int(math.floor(col)),
            int(math.floor(row))
        ]


Instruction:
Create a Python 3 function for
determining if an extent intersects this instance extent

Response:
def intersectsExtent(self, extent):
        "Determine if an extent intersects this instance extent"
        return \
            self.extent[0] <= extent[2] and self.extent[2] >= extent[0] and \
            self.extent[1] <= extent[3] and self.extent[3] >= extent[1]


Instruction:
Create a Python 3 function to
yield the tileBounds zoom tileCol and tileRow

Response:
def iterGrid(self, minZoom, maxZoom):
        "Yields the tileBounds, zoom, tileCol and tileRow"
        assert minZoom in range(0, len(self.RESOLUTIONS))
        assert maxZoom in range(0, len(self.RESOLUTIONS))
        assert minZoom <= maxZoom

        for zoom in xrange(minZoom, maxZoom + 1):
            [minRow, minCol, maxRow, maxCol] = self.getExtentAddress(zoom)
            for row in xrange(minRow, maxRow + 1):
                for col in xrange(minCol, maxCol + 1):
                    tileBounds = self.tileBounds(zoom, col, row)
                    yield (tileBounds, zoom, col, row)


Instruction:
Can you implement a function in Python 3 that
returns the number of tiles over x at a given zoom level

Response:
def numberOfXTilesAtZoom(self, zoom):
        "Returns the number of tiles over x at a given zoom level"
        [minRow, minCol, maxRow, maxCol] = self.getExtentAddress(zoom)
        return maxCol - minCol + 1


Instruction:
Here you have a function in Python 3, explain what it does
def numberOfTilesAtZoom(self, zoom):
        "Returns the total number of tile at a given zoom level"
        [minRow, minCol, maxRow, maxCol] = self.getExtentAddress(zoom)
        return (maxCol - minCol + 1) * (maxRow - minRow + 1)

Response: Returns the total number of tile at a given zoom level


Instruction:
Can you write a function in Python 3 where it
returns the total number of tiles for this instance extent

Response:
def totalNumberOfTiles(self, minZoom=None, maxZoom=None):
        "Return the total number of tiles for this instance extent"
        nbTiles = 0
        minZoom = minZoom or 0
        if maxZoom:
            maxZoom = maxZoom + 1
        else:
            maxZoom = len(self.RESOLUTIONS)
        for zoom in xrange(minZoom, maxZoom):
            nbTiles += self.numberOfTilesAtZoom(zoom)
        return nbTiles


Instruction:
Can you implement a function in Python 3 that
returns the zoom level for a given resolution

Response:
def getZoom(self, resolution):
        "Return the zoom level for a given resolution"
        assert resolution in self.RESOLUTIONS
        return self.RESOLUTIONS.index(resolution)


Instruction:
Given the following Python 3 function, write the documentation
def _getZoomLevelRange(self, resolution, unit='meters'):
        "Return lower and higher zoom level given a resolution"
        assert unit in ('meters', 'degrees')
        if unit == 'meters' and self.unit == 'degrees':
            resolution = resolution / self.metersPerUnit
        elif unit == 'degrees' and self.unit == 'meters':
            resolution = resolution * EPSG4326_METERS_PER_UNIT
        lo = 0
        hi = len(self.RESOLUTIONS)
        while lo < hi:
            mid = (lo + hi) // 2
            if resolution > self.RESOLUTIONS[mid]:
                hi = mid
            else:
                lo = mid + 1
        return lo, hi

Response: Return lower and higher zoom level given a resolution


Instruction:
Create a Python 3 function for
returning the closest zoom level for a given resolution

Response:
def getClosestZoom(self, resolution, unit='meters'):
        """
        Return the closest zoom level for a given resolution
        Parameters:
            resolution -- max. resolution
            unit -- unit for output (default='meters')
        """
        lo, hi = self._getZoomLevelRange(resolution, unit)
        if lo == 0:
            return lo
        if hi == len(self.RESOLUTIONS):
            return hi - 1
        before = self.RESOLUTIONS[lo - 1]
        if abs(self.RESOLUTIONS[lo] - resolution) < abs(before - resolution):
            return lo
        return lo - 1


Instruction:
How would you explain what the following Python 3 function does
def getCeilingZoom(self, resolution, unit='meters'):
        if resolution in self.RESOLUTIONS:
            return self.getZoom(resolution)
        lo, hi = self._getZoomLevelRange(resolution, unit)
        if lo == 0 or lo == hi:
            return lo
        if hi == len(self.RESOLUTIONS):
            return hi - 1
        return lo + 1

Response: Returns the highest zoom level for a given resolution


Instruction:
Can you implement a function in Python 3 that
returns the scale at a given zoom level

Response:
def getScale(self, zoom):
        """Returns the scale at a given zoom level"""
        if self.unit == 'degrees':
            resolution = self.getResolution(zoom) * EPSG4326_METERS_PER_UNIT
        else:
            resolution = self.getResolution(zoom)
        return resolution / STANDARD_PIXEL_SIZE


Instruction:
Explain what the following Python 3 code does
def getExtentAddress(self, zoom, extent=None, contained=False):
        if extent:
            bbox = extent
        else:
            bbox = self.extent
        minX = bbox[0]
        maxX = bbox[2]
        if self.originCorner == 'bottom-left':
            minY = bbox[3]
            maxY = bbox[1]
        elif self.originCorner == 'top-left':
            minY = bbox[1]
            maxY = bbox[3]
        [minCol, minRow] = self.tileAddress(zoom, [minX, maxY])
        [maxCol, maxRow] = self.tileAddress(zoom, [maxX, minY])

        if contained and minCol != maxCol or minRow != maxRow:
            parentBoundsMin = self.tileBounds(zoom, minCol, minRow)
            if self.originCorner == 'bottom-left':
                if parentBoundsMin[2] == maxX:
                    maxCol -= 1
                if parentBoundsMin[3] == minY:
                    maxRow -= 1
            elif self.originCorner == 'top-left':
                if parentBoundsMin[2] == maxX:
                    maxCol -= 1
                if parentBoundsMin[1] == minY:
                    maxRow -= 1
        return [minRow, minCol, maxRow, maxCol]

Response: Returns the bounding addresses of the current instance based on the extent.


Instruction:
Given the following Python 3 function, write the documentation
def getParentTiles(self, zoom, col, row, zoomParent):
        assert zoomParent <= zoom
        if zoomParent == zoom:
            return [[zoom, col, row]]
        extent = self.tileBounds(zoom, col, row)
        minRow, minCol, maxRow, maxCol = self.getExtentAddress(
            zoomParent, extent=extent, contained=True)
        addresses = []
        for c in range(minCol, maxCol + 1):
            for r in range(minRow, maxRow + 1):
                addresses.append([zoomParent, c, r])
        return addresses

Response: Returns the parent tile addresses for an irregular tiling scheme.


Instruction:
Implement a Python 3 function for
enabling loggly if it is configured and not debug or testing.

Response:
def enable_loggly(graph):
    """
    Enable loggly if it is configured and not debug/testing.

    """
    if graph.metadata.debug or graph.metadata.testing:
        return False

    try:
        if not graph.config.logging.loggly.token:
            return False

        if not graph.config.logging.loggly.environment:
            return False
    except AttributeError:
        return False

    return True


Instruction:
Can you generate a brief explanation for the following Python 3 code
def make_dict_config(graph):
    formatters = {}
    handlers = {}
    loggers = {}

    # create the console handler
    formatters["ExtraFormatter"] = make_extra_console_formatter(graph)
    handlers["console"] = make_stream_handler(graph, formatter="ExtraFormatter")

    # maybe create the loggly handler
    if enable_loggly(graph):
        formatters["JSONFormatter"] = make_json_formatter(graph)
        handlers["LogglyHTTPSHandler"] = make_loggly_handler(graph, formatter="JSONFormatter")

    # configure the root logger to output to all handlers
    loggers[""] = {
        "handlers": handlers.keys(),
        "level": graph.config.logging.level,
    }

    # set log levels for libraries
    loggers.update(make_library_levels(graph))

    return dict(
        version=1,
        disable_existing_loggers=False,
        formatters=formatters,
        handlers=handlers,
        loggers=loggers,
    )

Response: Build a dictionary configuration from conventions and configuration.


Instruction:
Explain what the following Python 3 code does
def make_json_formatter(graph):

    return {
        "()": graph.config.logging.json_formatter.formatter,
        "fmt": graph.config.logging.json_required_keys,
    }

Response: Create the default json formatter.


Instruction:
Make a summary of the following Python 3 code
def make_stream_handler(graph, formatter):
    return {
        "class": graph.config.logging.stream_handler.class_,
        "formatter": formatter,
        "level": graph.config.logging.level,
        "stream": graph.config.logging.stream_handler.stream,
    }

Response: Create the stream handler. Used for console output.


Instruction:
Here you have a function in Python 3, explain what it does
def make_loggly_handler(graph, formatter):
    base_url = graph.config.logging.loggly.base_url
    loggly_url = "{}/inputs/{}/tag/{}".format(
        base_url,
        graph.config.logging.loggly.token,
        ",".join([
            graph.metadata.name,
            graph.config.logging.loggly.environment,
        ]),
    )
    return {
        "class": graph.config.logging.https_handler.class_,
        "formatter": formatter,
        "level": graph.config.logging.level,
        "url": loggly_url,
    }

Response: Create the loggly handler.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def make_library_levels(graph):
    # inject the default components; these can, but probably shouldn't, be overridden
    levels = {}
    for level in ["DEBUG", "INFO", "WARN", "ERROR"]:
        levels.update({
            component: {
                "level": level,
            } for component in graph.config.logging.levels.default[level.lower()]
        })
    # override components; these can be set per application
    for level in ["DEBUG", "INFO", "WARN", "ERROR"]:
        levels.update({
            component: {
                "level": level,
            } for component in graph.config.logging.levels.override[level.lower()]
        })
    return levels

Response: Create third party library logging level configurations.


Instruction:
How would you explain what the following Python 3 function does
def process_request(self, request):
        site = request.site
        cache_key = '{prefix}-{site}'.format(prefix=settings.REDIRECT_CACHE_KEY_PREFIX, site=site.domain)
        redirects = cache.get(cache_key)
        if redirects is None:
            redirects = {redirect.old_path: redirect.new_path for redirect in Redirect.objects.filter(site=site)}
            cache.set(cache_key, redirects, settings.REDIRECT_CACHE_TIMEOUT)
        redirect_to = redirects.get(request.path)
        if redirect_to:
            return redirect(redirect_to, permanent=True)

Response:
Process the request and return a new URL if there is a matching Redirect model
        with the current request URL as the new_path field.


Instruction:
Can you generate the documentation for the following Python 3 function
def patched_get_current(self, request=None):
    # Imported here to avoid circular import
    from django.conf import settings
    if request:
        try:
            return self._get_site_by_request(request)  # pylint: disable=protected-access
        except Site.DoesNotExist:
            pass

    if getattr(settings, 'SITE_ID', ''):
        return self._get_site_by_id(settings.SITE_ID)  # pylint: disable=protected-access

    raise ImproperlyConfigured(
        "You're using the Django \"sites framework\" without having "
        "set the SITE_ID setting. Create a site in your database and "
        "set the SITE_ID setting or pass a request to "
        "Site.objects.get_current() to fix this error."
    )

Response: Monkey patched version of Django s SiteManager. get_current function.


Instruction:
Can you tell what is the following Python 3 function doing
def patched_get_site_by_id(self, site_id):
    now = datetime.datetime.utcnow()
    site = models.SITE_CACHE.get(site_id)
    cache_timeout = SITE_CACHE_TIMEOUTS.get(site_id, now)
    if not site or cache_timeout <= now:
        site = self.get(pk=site_id)
        models.SITE_CACHE[site_id] = site
        SITE_CACHE_TIMEOUTS[site_id] = now + get_site_cache_ttl()
    return models.SITE_CACHE[site_id]

Response: Monkey patched version of Django s SiteManager. _get_site_by_id method.


Instruction:
How would you implement a function in Python 3 that
monkeys patched version of Django s SiteManager. _get_site_by_request function.

Response:
def patched_get_site_by_request(self, request):
    """
    Monkey patched version of Django's SiteManager._get_site_by_request() function.

    Adds a configurable timeout to the in-memory SITE_CACHE for each cached Site.
    This allows for the use of an in-memory cache for Site models, avoiding one
    or more DB hits on every request made to the Django application, but also allows
    for changes made to models associated with the Site model and accessed via the
    Site model's relationship accessors to take effect without having to manual
    recycle all Django worker processes active in an application environment.
    """
    host = request.get_host()
    now = datetime.datetime.utcnow()
    site = models.SITE_CACHE.get(host)
    cache_timeout = SITE_CACHE_TIMEOUTS.get(host, now)
    if not site or cache_timeout <= now:
        site = self.get(domain__iexact=host)
        models.SITE_CACHE[host] = site
        SITE_CACHE_TIMEOUTS[host] = now + get_site_cache_ttl()
    return models.SITE_CACHE[host]


Instruction:
Can you implement a function in Python 3 that
truncates a string starting from lim chars.

Response:
def str_brief(obj, lim=20, dots='...', use_repr=True):
    """Truncates a string, starting from 'lim' chars. The given object
    can be a string, or something that can be casted to a string.

    >>> import string
    >>> str_brief(string.uppercase)
    'ABCDEFGHIJKLMNOPQRST...'
    >>> str_brief(2 ** 50, lim=10, dots='0')
    '11258999060'
    """
    if isinstance(obj, basestring) or not use_repr:
        full = str(obj)
    else:
        full = repr(obj)
    postfix = []
    CLOSERS = {'(': ')', '{': '}', '[': ']', '"': '"', "'": "'", '<': '>'}
    for i, c in enumerate(full):
        if i >= lim + len(postfix):
            return full[:i] + dots + ''.join(reversed(postfix))
        if postfix and postfix[-1] == c:
            postfix.pop(-1)
            continue
        closer = CLOSERS.get(c, None)
        if closer is not None:
            postfix.append(closer)
    return full


Instruction:
How would you explain what the following Python 3 function does
def get_mirror_resources_by_name_map(self, scope=None):
        volumes_mirrors_by_name = dict()
        cgs_mirrors_by_name = dict()
        if ((scope is None) or (scope.lower() == 'volume')):
            mirror_list = self.xcli_client.cmd.mirror_list(scope='Volume')
            for xcli_mirror in mirror_list:
                name = MirroredEntities.get_mirrored_object_name(xcli_mirror)
                volumes_mirrors_by_name[name] = xcli_mirror
        if ((scope is None) or (scope.lower() == CG)):
            for xcli_mirror in self.xcli_client.cmd.mirror_list(scope='CG'):
                name = MirroredEntities.get_mirrored_object_name(xcli_mirror)
                cgs_mirrors_by_name[name] = xcli_mirror
        res = Bunch(volumes=volumes_mirrors_by_name, cgs=cgs_mirrors_by_name)
        return res

Response: returns a map volume_name -> volume cg_name -> cg


Instruction:
Implement a Python 3 function for
returning a list of the port names of a XIV host

Response:
def get_host_port_names(self, host_name):
        """ return a list of the port names of XIV host """
        port_names = list()
        host = self.get_hosts_by_name(host_name)
        fc_ports = host.fc_ports
        iscsi_ports = host.iscsi_ports
        port_names.extend(fc_ports.split(',') if fc_ports != '' else [])
        port_names.extend(iscsi_ports.split(',') if iscsi_ports != '' else [])
        return port_names


Instruction:
Implement a function in Python 3 to
return a list of the port names under a given cluster

Response:
def get_cluster_port_names(self, cluster_name):
        """ return a list of the port names under XIV CLuster """
        port_names = list()
        for host_name in self.get_hosts_by_clusters()[cluster_name]:
            port_names.extend(self.get_hosts_by_name(host_name))
        return port_names


Instruction:
Create a Python 3 function to
remove all stale clients from pool

Response:
def flush(self):
        """remove all stale clients from pool"""
        now = time.time()
        to_remove = []
        for k, entry in self.pool.items():
            if entry.timestamp < now:
                entry.client.close()
                to_remove.append(k)
        for k in to_remove:
            del self.pool[k]


Instruction:
Can you write a function in Python 3 where it
gets an existing connection or opens a new one

Response:
def get(self, user, password, endpoints):
        """Gets an existing connection or opens a new one
        """
        now = time.time()
        # endpoints can either be str or list
        if isinstance(endpoints, str):
            endpoints = [endpoints]
        for ep in endpoints:
            if ep not in self.pool:
                continue
            entry = self.pool[ep]
            if (not entry.client.is_connected() or
               entry.timestamp + self.time_to_live < now):
                    xlog.debug("XCLIClientPool: clearing stale client %s",
                               ep)
                    del self.pool[ep]
                    entry.client.close()
                    continue
            user_client = entry.user_clients.get(user, None)
            if not user_client or not user_client.is_connected():
                user_client = entry.client.get_user_client(user, password)
                entry.user_clients[user] = user_client
            return user_client

        xlog.debug("XCLIClientPool: connecting to %s", endpoints)
        client = self.connector(None, None, endpoints)
        user_client = {user: client.get_user_client(user, password)}
        for ep in endpoints:
            self.pool[ep] = PoolEntry(client, now, user_client)
        return user_client[user]


Instruction:
Explain what the following Python 3 code does
def size(value: int, suffixes: list = None) -> str:
    suffixes = suffixes or [
        'bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
    order = int(log2(value) / 10) if value else 0
    return '{:.4g} {}'.format(value / (1 << (order * 10)), suffixes[order])

Response: Returns a size string for the given integer.


Instruction:
Implement a function in Python 3 to
parse the size of a single object.

Response:
def parse_size(value):
    """
    >>> parse_size('1M')
    1048576
    >>> parse_size('512K512K')
    1048576
    >>> parse_size('512K 512K')
    1048576
    >>> parse_size('512K 512K 4')
    1048580
    """
    if isinstance(value, (int, float)):
        return value
    elif not pattern_valid.fullmatch(value):
        raise ValueError(value)

    result = 0
    for m in pattern.finditer(value):
        v = m.groupdict()
        n = int(v['n'])
        unit = v['unit']
        if not unit:
            result += n
        elif unit in size_levels:
            result += n << size_levels[unit]
        else:
            raise ValueError(value)
    return result


Instruction:
Write a Python 3 function for
parsing duration from a string

Response:
def parse_duration(value):
    """
    >>> parse_duration('1h')
    3600
    >>> parse_duration('1m')
    60
    >>> parse_duration('1m 2s')
    62
    >>> parse_duration('1')
    1

    """
    if isinstance(value, (int, float)):
        return value
    elif not pattern_valid.fullmatch(value):
        raise ValueError(value)

    result = 0
    for m in pattern.finditer(value):
        v = m.groupdict()
        n = int(v['n'])
        unit = v['unit']
        if not unit:
            result += n
        elif unit in durations:
            result += n * durations[unit]
        else:
            raise ValueError(value)
    return result


Instruction:
Can you generate the documentation for the following Python 3 function
def send_event(self, action, properties, event_severity=EVENT_SEVERITY):
        # verify properties
        event_properties = dict() if (properties is None) else properties
        if type(event_properties) is not dict:
            raise TypeError('properties is not dict')

        # prepare event
        event_bunch = Bunch(
            Product=self.product_name,
            Version=self.product_version,
            Server=self.server_name,
            Platform=self.platform,
            Action=action,
            Properties=event_properties)
        event_description = self._get_description_prefix() + \
            json.dumps(event_bunch)

        use_custom_event = True
        if CSS_PRODUCT_EVENT in dir(self.xcli.cmd):
            try:
                # send css product event
                log.debug("sending css_product_event "
                          "description=%s severity=%s",
                          event_description, event_severity)
                self.xcli.cmd.css_product_event(severity=event_severity,
                                                product=self.product_name,
                                                version=self.product_version,
                                                server=self.server_name,
                                                platform=self.platform,
                                                action=action,
                                                properties=event_properties)
                use_custom_event = False
            except (UnrecognizedCommandError,
                    OperationForbiddenForUserCategoryError):
                log.warning("failed css_product_event "
                            "description=%s severity=%s",
                            event_description, event_severity)
        if use_custom_event:
            # send custom event
            log.debug("sending custom_event description=%s severity=%s",
                      event_description, event_severity)
            self.xcli.cmd.custom_event(
                description=event_description, severity=event_severity)

Response: send an event to the available media modules