Instruction:
Can you generate a brief explanation for the following Python 3 code
def m_c(mcmc, scale, f, alphasMZ=0.1185, loop=3):
    if scale == mcmc:
        return mcmc  # nothing to do
    _sane(scale, f)
    crd = rundec.CRunDec()
    alphas_mc = alpha_s(mcmc, 4, alphasMZ=alphasMZ, loop=loop)
    if f == 4:
        alphas_scale = alpha_s(scale, f, alphasMZ=alphasMZ, loop=loop)
        return crd.mMS2mMS(mcmc, alphas_mc, alphas_scale, f, loop)
    elif f == 3:
        crd.nfMmu.Mth = 1.3
        crd.nfMmu.muth = 1.3
        crd.nfMmu.nf = 4
        return crd.mH2mL(mcmc, alphas_mc, mcmc, crd.nfMmu, scale, loop)
    elif f == 5:
        crd.nfMmu.Mth = 4.8
        crd.nfMmu.muth = 4.8
        crd.nfMmu.nf = 5
        return crd.mL2mH(mcmc, alphas_mc, mcmc, crd.nfMmu, scale, loop)
    else:
        raise ValueError("Invalid input: f={}, scale={}".format(f, scale))

Response: r Returns the running c quark mass in the MSbar scheme at the scale scale.


Instruction:
Here you have a function in Python 3, explain what it does
def m_s(ms2, scale, f, alphasMZ=0.1185, loop=3):
    if scale == 2 and f == 3:
        return ms2  # nothing to do
    _sane(scale, f)
    crd = rundec.CRunDec()
    alphas_2 = alpha_s(2, 3, alphasMZ=alphasMZ, loop=loop)
    if f == 3:
        alphas_scale = alpha_s(scale, f, alphasMZ=alphasMZ, loop=loop)
        return crd.mMS2mMS(ms2, alphas_2, alphas_scale, f, loop)
    elif f == 4:
        crd.nfMmu.Mth = 1.3
        crd.nfMmu.muth = 1.3
        crd.nfMmu.nf = 4
        return crd.mL2mH(ms2, alphas_2, 2, crd.nfMmu, scale, loop)
    elif f == 5:
        mc = 1.3
        crd.nfMmu.Mth = mc
        crd.nfMmu.muth = mc
        crd.nfMmu.nf = 4
        msmc = crd.mL2mH(ms2, alphas_2, 2, crd.nfMmu, mc, loop)
        crd.nfMmu.Mth = 4.8
        crd.nfMmu.muth = 4.8
        crd.nfMmu.nf = 5
        alphas_mc = alpha_s(mc, 4, alphasMZ=alphasMZ, loop=loop)
        return crd.mL2mH(msmc, alphas_mc, mc, crd.nfMmu, scale, loop)
    else:
        raise ValueError("Invalid input: f={}, scale={}".format(f, scale))

Response: r Returns the running s quark mass in the MSbar scheme at the scale scale.


Instruction:
Given the following Python 3 function, write the documentation
def get_confirmation(self):
        if self.clear:
            action = 'This will DELETE ALL FILES in this location!'
        else:
            action = 'This will overwrite existing files!'

        message = (
            "\n"
            "You have requested to collect static files at the destination\n"
            "location as specified in your settings\n"
            "\n"
            "    {destination}\n"
            "\n"
            "{action}\n"
            "Are you sure you want to do this?\n"
            "\n"
            "Type 'yes' to continue, or 'no' to cancel: ".format(
                destination=self.destination_path,
                action=action,
            )
        )

        if input(''.join(message)) != 'yes':
            raise CommandError("Collecting tools cancelled.")

Response: Get user confirmation to proceed.


Instruction:
How would you code a function in Python 3 to
delete contents of the directory on the given path.

Response:
def clear_dir(self):
        """Delete contents of the directory on the given path."""
        self.stdout.write("Deleting contents of '{}'.".format(self.destination_path))

        for filename in os.listdir(self.destination_path):
            if os.path.isfile(filename) or os.path.islink(filename):
                os.remove(filename)
            elif os.path.isdir(filename):
                shutil.rmtree(filename)


Instruction:
Write a Python 3 function that can
change path prefix and include app name.

Response:
def change_path_prefix(self, path, old_prefix, new_prefix, app_name):
        """Change path prefix and include app name."""
        relative_path = os.path.relpath(path, old_prefix)
        return os.path.join(new_prefix, app_name, relative_path)


Instruction:
How would you explain what the following Python 3 function does
def collect(self):
        for app_name, tools_path in get_apps_tools().items():
            self.stdout.write("Copying files from '{}'.".format(tools_path))

            app_name = app_name.replace('.', '_')

            app_destination_path = os.path.join(self.destination_path, app_name)
            if not os.path.isdir(app_destination_path):
                os.mkdir(app_destination_path)

            for root, dirs, files in os.walk(tools_path):
                for dir_name in dirs:
                    dir_source_path = os.path.join(root, dir_name)
                    dir_destination_path = self.change_path_prefix(
                        dir_source_path, tools_path, self.destination_path, app_name
                    )

                    if not os.path.isdir(dir_destination_path):
                        os.mkdir(dir_destination_path)

                for file_name in files:
                    file_source_path = os.path.join(root, file_name)
                    file_destination_path = self.change_path_prefix(
                        file_source_path, tools_path, self.destination_path, app_name
                    )

                    shutil.copy2(file_source_path, file_destination_path)

Response: Get tools locations and copy them to a single location.


Instruction:
Create a Python 3 function to
compute checksum of processor inputs name and version.

Response:
def get_data_checksum(proc_input, proc_slug, proc_version):
    """Compute checksum of processor inputs, name and version."""
    checksum = hashlib.sha256()
    checksum.update(json.dumps(proc_input, sort_keys=True).encode('utf-8'))
    checksum.update(proc_slug.encode('utf-8'))
    checksum.update(str(proc_version).encode('utf-8'))
    return checksum.hexdigest()


Instruction:
Create a Python 3 function to
get or set value using a dot - notation key in a multilevel dict.

Response:
def dict_dot(d, k, val=None, default=None):
    """Get or set value using a dot-notation key in a multilevel dict."""
    if val is None and k == '':
        return d

    def set_default(dict_or_model, key, default_value):
        """Set default field value."""
        if isinstance(dict_or_model, models.Model):
            if not hasattr(dict_or_model, key):
                setattr(dict_or_model, key, default_value)

            return getattr(dict_or_model, key)
        else:
            return dict_or_model.setdefault(key, default_value)

    def get_item(dict_or_model, key):
        """Get field value."""
        if isinstance(dict_or_model, models.Model):
            return getattr(dict_or_model, key)
        else:
            return dict_or_model[key]

    def set_item(dict_or_model, key, value):
        """Set field value."""
        if isinstance(dict_or_model, models.Model):
            setattr(dict_or_model, key, value)
        else:
            dict_or_model[key] = value

    if val is None and callable(default):
        # Get value, default for missing
        return functools.reduce(lambda a, b: set_default(a, b, default()), k.split('.'), d)

    elif val is None:
        # Get value, error on missing
        return functools.reduce(get_item, k.split('.'), d)

    else:
        # Set value
        try:
            k, k_last = k.rsplit('.', 1)
            set_item(dict_dot(d, k, default=dict), k_last, val)
        except ValueError:
            set_item(d, k, val)
        return val


Instruction:
How would you implement a function in Python 3 that
gets applications tools and their paths.

Response:
def get_apps_tools():
    """Get applications' tools and their paths.

    Return a dict with application names as keys and paths to tools'
    directories as values. Applications without tools are omitted.
    """
    tools_paths = {}

    for app_config in apps.get_app_configs():
        proc_path = os.path.join(app_config.path, 'tools')
        if os.path.isdir(proc_path):
            tools_paths[app_config.name] = proc_path

    custom_tools_paths = getattr(settings, 'RESOLWE_CUSTOM_TOOLS_PATHS', [])
    if not isinstance(custom_tools_paths, list):
        raise KeyError("`RESOLWE_CUSTOM_TOOLS_PATHS` setting must be a list.")

    for seq, custom_path in enumerate(custom_tools_paths):
        custom_key = '_custom_{}'.format(seq)
        tools_paths[custom_key] = custom_path

    return tools_paths


Instruction:
Can you generate a brief explanation for the following Python 3 code
def rewire_inputs(data_list):
    if len(data_list) < 2:
        return data_list

    mapped_ids = {bundle['original'].id: bundle['copy'].id for bundle in data_list}

    for bundle in data_list:
        updated = False
        copy = bundle['copy']

        for field_schema, fields in iterate_fields(copy.input, copy.process.input_schema):
            name = field_schema['name']
            value = fields[name]

            if field_schema['type'].startswith('data:') and value in mapped_ids:
                fields[name] = mapped_ids[value]
                updated = True

            elif field_schema['type'].startswith('list:data:') and any([id_ in mapped_ids for id_ in value]):
                fields[name] = [mapped_ids[id_] if id_ in mapped_ids else id_ for id_ in value]
                updated = True

        if updated:
            copy.save()

    return data_list

Response: Rewire inputs of provided data objects.


Instruction:
How would you implement a function in Python 3 that
parses the given XML and use the XML element to create a Python object.

Response:
def CreateFromDocument(xml_text, default_namespace=None, location_base=None):
    """Parse the given XML and use the document element to create a Python instance.

    @param xml_text An XML document.  This should be data (Python 2
    str or Python 3 bytes), or a text (Python 2 unicode or Python 3
    str) in the L{pyxb._InputEncoding} encoding.

    @keyword default_namespace The L{pyxb.Namespace} instance to use as the
    default namespace where there is no default namespace in scope.
    If unspecified or C{None}, the namespace of the module containing
    this function will be used.

    @keyword location_base: An object to be recorded as the base of all
    L{pyxb.utils.utility.Location} instances associated with events and
    objects handled by the parser.  You might pass the URI from which
    the document was obtained.

    """

    if pyxb.XMLStyle_saxer != pyxb._XMLStyle:
        dom = pyxb.utils.domutils.StringToDOM(xml_text)
        return CreateFromDOM(dom.documentElement, default_namespace=default_namespace)
    if default_namespace is None:
        default_namespace = Namespace.fallbackNamespace()
    saxer = pyxb.binding.saxer.make_parser(
        fallback_namespace=default_namespace, location_base=location_base
    )
    handler = saxer.getContentHandler()
    xmld = xml_text
    if isinstance(xmld, pyxb.utils.six.text_type):
        xmld = xmld.encode(pyxb._InputEncoding)
    saxer.parse(io.BytesIO(xmld))
    instance = handler.rootObject()
    return instance


Instruction:
Create a Python 3 function for
creating a Python instance from the given DOM node.

Response:
def CreateFromDOM(node, default_namespace=None):
    """Create a Python instance from the given DOM node. The node tag must correspond to
    an element declaration in this module.

    @deprecated: Forcing use of DOM interface is unnecessary; use L{CreateFromDocument}.

    """
    if default_namespace is None:
        default_namespace = Namespace.fallbackNamespace()
    return pyxb.binding.basis.element.AnyCreateFromDOM(node, default_namespace)


Instruction:
Can you tell what is the following Python 3 function doing
def postloop(self):
        cmd.Cmd.postloop(self)  # Clean up command completion
        d1_cli.impl.util.print_info("Exiting...")

Response: Take care of any unfinished business.


Instruction:
Make a summary of the following Python 3 code
def default(self, line):
        args = self._split_args(line, 0, 99)
        d1_cli.impl.util.print_error("Unknown command: {}".format(args[0]))

Response: Called on an input line when the command prefix is not recognized.


Instruction:
Make a summary of the following Python 3 code
def do_help(self, line):
        command, = self._split_args(line, 0, 1)
        if command is None:
            return self._print_help()
        cmd.Cmd.do_help(self, line)

Response:
Get help on commands help or "?" with no arguments displays a list_objects
        of commands which help is available


Instruction:
How would you explain what the following Python 3 function does
def do_history(self, line):
        self._split_args(line, 0, 0)
        for idx, item in enumerate(self._history):
            d1_cli.impl.util.print_info("{0: 3d} {1}".format(idx, item))

Response: history Display a list of commands that have been entered.


Instruction:
Implement a Python 3 function for
exiting Exit from the CLI.

Response:
def do_exit(self, line):
        """exit Exit from the CLI."""
        n_remaining_operations = len(self._command_processor.get_operation_queue())
        if n_remaining_operations:
            d1_cli.impl.util.print_warn(
                """There are {} unperformed operations in the write operation queue. These will
be lost if you exit.""".format(
                    n_remaining_operations
                )
            )
            if not d1_cli.impl.util.confirm("Exit?", default="yes"):
                return
        sys.exit()


Instruction:
Write a Python 3 function for
exiting on system EOF character.

Response:
def do_eof(self, line):
        """Exit on system EOF character."""
        d1_cli.impl.util.print_info("")
        self.do_exit(line)


Instruction:
Can you tell what is the following Python 3 function doing
def do_set(self, line):
        session_parameter, value = self._split_args(line, 0, 2)
        if value is None:
            self._command_processor.get_session().print_variable(session_parameter)
        else:
            self._command_processor.get_session().set_with_conversion(
                session_parameter, value
            )
            self._print_info_if_verbose(
                'Set session variable {} to "{}"'.format(session_parameter, value)
            )

Response: \ x1b [ 1mNAME \ x1b [ 0m set - Set the value of the allCOOKIE_NAME \ x1b [ 0m session_parameter - Name of the parameter.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def do_load(self, line):
        config_file = self._split_args(line, 0, 1)[0]
        self._command_processor.get_session().load(config_file)
        if config_file is None:
            config_file = (
                self._command_processor.get_session().get_default_pickle_file_path()
            )
        self._print_info_if_verbose("Loaded session from file: {}".format(config_file))

Response: Load a session from file


Instruction:
Write a Python 3 script for
saving session variables to file save.

Response:
def do_save(self, line):
        """save [config_file] Save session variables to file save (without parameters):
        Save session to default file ~/.dataone_cli.conf save.

        <file>: Save session to specified file.

        """
        config_file = self._split_args(line, 0, 1)[0]
        self._command_processor.get_session().save(config_file)
        if config_file is None:
            config_file = (
                self._command_processor.get_session().get_default_pickle_file_path()
            )
        self._print_info_if_verbose("Saved session to file: {}".format(config_file))


Instruction:
Can you create a Python 3 function that
resets Reset all session variables to their default values.

Response:
def do_reset(self, line):
        """reset Set all session variables to their default values."""
        self._split_args(line, 0, 0)
        self._command_processor.get_session().reset()
        self._print_info_if_verbose("Successfully reset session variables")


Instruction:
Can you generate the documentation for the following Python 3 function
def do_allowaccess(self, line):
        subject, permission = self._split_args(line, 1, 1)
        self._command_processor.get_session().get_access_control().add_allowed_subject(
            subject, permission
        )
        self._print_info_if_verbose(
            'Set {} access for subject "{}"'.format(permission, subject)
        )

Response:
allowaccess - Set the access level for subject Access
        level is read write or changePermission.


Instruction:
Given the following Python 3 function, write the documentation
def do_allowrep(self, line):
        self._split_args(line, 0, 0)
        self._command_processor.get_session().get_replication_policy().set_replication_allowed(
            True
        )
        self._print_info_if_verbose("Set replication policy to allow replication")

Response: allowrep Allow new objects to be replicated


Instruction:
How would you explain what the following Python 3 function does
def do_denyrep(self, line):
        self._split_args(line, 0, 0)
        self._command_processor.get_session().get_replication_policy().set_replication_allowed(
            False
        )
        self._print_info_if_verbose("Set replication policy to deny replication")

Response: denyrep Prevent new objects from being replicated


Instruction:
Make a summary of the following Python 3 code
def do_blockrep(self, line):
        mns = self._split_args(line, 1, -1)
        self._command_processor.get_session().get_replication_policy().add_blocked(mns)
        self._print_info_if_verbose(
            "Set {} as blocked replication target(s)".format(", ".join(mns))
        )

Response: blockrep | blockrep | node name | |


Instruction:
Can you tell what is the following Python 3 function doing
def do_removerep(self, line):
        mns = self._split_args(line, 1, -1)
        self._command_processor.get_session().get_replication_policy().repremove(mns)
        self._print_info_if_verbose(
            "Removed {} from replication policy".format(", ".join(mns))
        )

Response:
removerep remove one or more Member Nodes
        from replication policy


Instruction:
Can you tell what is the following Python 3 function doing
def do_numberrep(self, line):
        n_replicas = self._split_args(line, 1, 0)[0]
        self._command_processor.get_session().get_replication_policy().set_number_of_replicas(
            n_replicas
        )
        self._print_info_if_verbose("Set number of replicas to {}".format(n_replicas))

Response:
numberrep <number of replicas > Set preferred number of replicas for new
        objects.


Instruction:
Can you generate the documentation for the following Python 3 function
def do_get(self, line):
        pid, output_file = self._split_args(line, 2, 0)
        self._command_processor.science_object_get(pid, output_file)
        self._print_info_if_verbose(
            'Downloaded "{}" to file: {}'.format(pid, output_file)
        )

Response: get - Get an object from a Member Node.


Instruction:
Can you generate the documentation for the following Python 3 function
def do_meta(self, line):
        pid, output_file = self._split_args(line, 1, 1)
        self._command_processor.system_metadata_get(pid, output_file)
        if output_file is not None:
            self._print_info_if_verbose(
                'Downloaded system metadata for "{}" to file: {}'.format(
                    pid, output_file
                )
            )

Response: \ x1b [ 1mNAME \ x1b [ 0m meta - Gets the System Metadata that is associated with the Science Object.


Instruction:
Write a Python 3 script for
listing a list of Science Data Objects from Member Node

Response:
def do_list(self, line):
        """list [path] Retrieve a list of available Science Data Objects from Member
        Node The response is filtered by the from-date, to-date, search, start and count
        session variables.

        See also: search

        """
        path = self._split_args(line, 0, 1, pad=False)
        if len(path):
            path = path[0]
        self._command_processor.list_objects(path)


Instruction:
Explain what the following Python 3 code does
def do_log(self, line):
        path = self._split_args(line, 0, 1, pad=False)
        if len(path):
            path = path[0]
        self._command_processor.log(path)

Response: log [ path ] Retrieve event log from Member Node The response is filtered by from - date to - date start and count session parameters


Instruction:
Can you generate a brief explanation for the following Python 3 code
def do_resolve(self, line):
        pid, = self._split_args(line, 1, 0)
        self._command_processor.resolve(pid)

Response:
resolve <identifier > Find all locations from which the given Science Object
        can be downloaded.


Instruction:
Explain what the following Python 3 code does
def do_create(self, line):
        pid, sciobj_path = self._split_args(line, 2, 0)
        self._command_processor.science_object_create(pid, sciobj_path)
        self._print_info_if_verbose(
            'Added create operation for identifier "{}" to write queue'.format(pid)
        )

Response: create - Create a new Science Object on a Member Node.


Instruction:
Explain what the following Python 3 code does
def do_update(self, line):
        curr_pid, pid_new, input_file = self._split_args(line, 3, 0)
        self._command_processor.science_object_update(curr_pid, input_file, pid_new)
        self._print_info_if_verbose(
            'Added update operation for identifier "{}" to write queue'.format(curr_pid)
        )

Response:
update - update a.
. pid


Instruction:
Make a summary of the following Python 3 code
def do_package(self, line):
        pids = self._split_args(line, 3, -1, pad=False)
        self._command_processor.create_package(pids)
        self._print_info_if_verbose(
            'Added package create operation for identifier "{}" to write queue'.format(
                pids[0]
            )
        )

Response: \ x1b [ 1mNAME \ x1b [ 0m package - Create a simple OAI -ORE Resource Map on a Member Node.


Instruction:
How would you code a function in Python 3 to
archive - Add one or more existing Science Objects as archived.

Response:
def do_archive(self, line):
        """archive <identifier> [identifier ...] Mark one or more existing Science
        Objects as archived."""
        pids = self._split_args(line, 1, -1)
        self._command_processor.science_object_archive(pids)
        self._print_info_if_verbose(
            "Added archive operation for identifier(s) {} to write queue".format(
                ", ".join(pids)
            )
        )


Instruction:
Can you generate a brief explanation for the following Python 3 code
def do_updateaccess(self, line):
        pids = self._split_args(line, 1, -1)
        self._command_processor.update_access_policy(pids)
        self._print_info_if_verbose(
            "Added access policy update operation for identifiers {} to write queue".format(
                ", ".join(pids)
            )
        )

Response:
updateaccess <identifier> [identifier ...] Update the Access Policy on one or
        more existing Science Data Objects.


Instruction:
Write a Python 3 script to
search [ query ] Comprehensive search for Science Data Objects across all available MNs.

Response:
def do_search(self, line):
        """search [query] Comprehensive search for Science Data Objects across all
        available MNs.

        See https://releases.dataone.org/online/api-
        documentation-v2.0.1/design/SearchMetadata.html for the available search terms.

        """
        args = self._split_args(line, 0, -1)
        query = " ".join([_f for _f in args if _f])
        self._command_processor.search(query)


Instruction:
Make a summary of the following Python 3 code
def do_ping(self, line):
        hosts = self._split_args(line, 0, 99, pad=False)
        self._command_processor.ping(hosts)

Response: ping - Ping the CN and MN of the current session.


Instruction:
Can you create a Python 3 function that
queues Print the queue of write operations.

Response:
def do_queue(self, line):
        """queue Print the queue of write operations."""
        self._split_args(line, 0, 0)
        self._command_processor.get_operation_queue().display()


Instruction:
Can you tell what is the following Python 3 function doing
def do_run(self, line):
        self._split_args(line, 0, 0)
        self._command_processor.get_operation_queue().execute()
        self._print_info_if_verbose(
            "All operations in the write queue were successfully executed"
        )

Response: run Perform each operation in the queue of write operations.


Instruction:
Make a summary of the following Python 3 code
def do_edit(self, line):
        self._split_args(line, 0, 0)
        self._command_processor.get_operation_queue().edit()
        self._print_info_if_verbose("The write operation queue was successfully edited")

Response: edit Edit the queue of write operations.


Instruction:
Can you tell what is the following Python 3 function doing
def _print_help(self):
        msg = """Commands (type help <command> for details)

CLI:                     help history exit quit
Session, General:        set load save reset
Session, Access Control: allowaccess denyaccess clearaccess
Session, Replication:    allowrep denyrep preferrep blockrep
                         removerep numberrep clearrep
Read Operations:         get meta list log resolve
Write Operations:        update create package archive
                         updateaccess updatereplication
Utilities:               listformats listnodes search ping
Write Operation Queue:   queue run edit clearqueue

Command History:         Arrow Up, Arrow Down
Command Editing:         Arrow Left, Arrow Right, Delete
        """
        if platform.system() != "Windows":
            msg += """Command Completion:      Single Tab: Complete unique command
                         Double Tab: Display possible commands
            """
        d1_cli.impl.util.print_info(msg)

Response: Print help message to group commands by functionality.


Instruction:
Explain what the following Python 3 code does
def get_groups_with_perms(obj, attach_perms=False):
    ctype = get_content_type(obj)
    group_model = get_group_obj_perms_model(obj)

    if not attach_perms:
        # It's much easier without attached perms so we do it first if that is the case
        group_rel_name = group_model.group.field.related_query_name()
        if group_model.objects.is_generic():
            group_filters = {
                '%s__content_type' % group_rel_name: ctype,
                '%s__object_pk' % group_rel_name: obj.pk,
            }
        else:
            group_filters = {'%s__content_object' % group_rel_name: obj}
        return Group.objects.filter(**group_filters).distinct()
    else:
        group_perms_mapping = defaultdict(list)
        groups_with_perms = get_groups_with_perms(obj)
        queryset = group_model.objects.filter(group__in=groups_with_perms).prefetch_related('group', 'permission')
        if group_model is GroupObjectPermission:
            queryset = queryset.filter(object_pk=obj.pk, content_type=ctype)
        else:
            queryset = queryset.filter(content_object_id=obj.pk)

        for group_perm in queryset:
            group_perms_mapping[group_perm.group].append(group_perm.permission.codename)
        return dict(group_perms_mapping)

Response: Return queryset of all Groups with any object permissions for the given object.


Instruction:
Can you implement a function in Python 3 that
groups permissions by group.

Response:
def _group_groups(perm_list):
    """Group permissions by group.

    Input is list of tuples of length 3, where each tuple is in
    following format::

        (<group_id>, <group_name>, <single_permission>)

    Permissions are regrouped and returned in such way that there is
    only one tuple for each group::

        (<group_id>, <group_name>, [<first_permission>, <second_permission>,...])

    :param list perm_list: list of touples of length 3
    :return: list tuples with grouped permissions
    :rtype: list

    """
    perm_list = sorted(perm_list, key=lambda tup: tup[0])

    grouped_perms = []
    for key, group in groupby(perm_list, lambda tup: (tup[0], tup[1])):
        grouped_perms.append((key[0], key[1], [g[2] for g in group]))

    return grouped_perms


Instruction:
Create a Python 3 function to
get permissins for user groups.

Response:
def get_user_group_perms(user_or_group, obj):
    """Get permissins for user groups.

    Based on guardian.core.ObjectPermissionChecker.

    """
    user, group = get_identity(user_or_group)

    if user and not user.is_active:
        return [], []
    user_model = get_user_model()
    ctype = ContentType.objects.get_for_model(obj)

    group_model = get_group_obj_perms_model(obj)
    group_rel_name = group_model.permission.field.related_query_name()
    if user:
        user_rel_name = user_model.groups.field.related_query_name()
        group_filters = {user_rel_name: user}
    else:
        group_filters = {'pk': group.pk}
    if group_model.objects.is_generic():
        group_filters.update({
            '{}__content_type'.format(group_rel_name): ctype,
            '{}__object_pk'.format(group_rel_name): obj.pk,
        })
    else:
        group_filters['{}__content_object'.format(group_rel_name)] = obj

    user_perms, group_perms = [], []

    if user:
        perms_qs = Permission.objects.filter(content_type=ctype)
        if user.is_superuser:
            user_perms = list(chain(perms_qs.values_list("codename", flat=True)))
        else:
            model = get_user_obj_perms_model(obj)
            related_name = model.permission.field.related_query_name()
            user_filters = {'{}__user'.format(related_name): user}
            if model.objects.is_generic():
                user_filters.update({
                    '{}__content_type'.format(related_name): ctype,
                    '{}__object_pk'.format(related_name): obj.pk,
                })
            else:
                user_filters['{}__content_object'.format(related_name)] = obj

            user_perms_qs = perms_qs.filter(**user_filters)
            user_perms = list(chain(user_perms_qs.values_list("codename", flat=True)))

    group_perms_qs = Group.objects.filter(**group_filters)
    group_perms = list(chain(group_perms_qs.order_by("pk").values_list(
        "pk", "name", "{}__permission__codename".format(group_rel_name))))

    group_perms = _group_groups(group_perms)

    return user_perms, group_perms


Instruction:
Can you tell what is the following Python 3 function doing
def get_object_perms(obj, user=None):
    def format_permissions(perms):
        """Remove model name from permission."""
        ctype = ContentType.objects.get_for_model(obj)
        return [perm.replace('_{}'.format(ctype.name), '') for perm in perms]

    perms_list = []

    if user:
        if user.is_authenticated:
            user_perms, group_perms = get_user_group_perms(user, obj)
        else:
            user_perms, group_perms = [], []

        if user_perms != []:
            perms_list.append({
                'type': 'user',
                'id': user.pk,
                'name': user.get_full_name() or user.username,
                'permissions': format_permissions(user_perms),
            })

        if group_perms != []:
            for group_id, group_name, perms in group_perms:
                perms_list.append({
                    'type': 'group',
                    'id': group_id,
                    'name': group_name,
                    'permissions': format_permissions(perms),
                })
    else:
        user_options = {
            'attach_perms': True,
            'with_group_users': False
        }
        for user, perms in get_users_with_perms(obj, **user_options).items():
            if user.username == settings.ANONYMOUS_USER_NAME:
                # public user is treated separately
                continue
            perms_list.append({
                'type': 'user',
                'id': user.pk,
                'name': user.get_full_name() or user.username,
                'permissions': format_permissions(perms),
            })

        group_options = {
            'attach_perms': True,
        }
        for group, perms in get_groups_with_perms(obj, **group_options).items():
            perms_list.append({
                'type': 'group',
                'id': group.pk,
                'name': group.name,
                'permissions': format_permissions(perms),
            })

    public_perms = get_perms(AnonymousUser(), obj)
    if public_perms != []:
        perms_list.append({
            'type': 'public',
            'permissions': format_permissions(public_perms),
        })

    return perms_list

Response: Returns list of permissions for given object in Resolwe specific format.


Instruction:
Write a Python 3 script to
return a queryset with required permissions.

Response:
def get_objects_for_user(user, perms, klass=None, use_groups=True, any_perm=False,
                         with_superuser=True, accept_global_perms=True, perms_filter='pk__in'):
    """Return queryset with required permissions."""
    if isinstance(perms, str):
        perms = [perms]

    ctype = None
    app_label = None
    codenames = set()

    # Compute codenames set and ctype if possible
    for perm in perms:
        if '.' in perm:
            new_app_label, codename = perm.split('.', 1)
            if app_label is not None and app_label != new_app_label:
                raise MixedContentTypeError(
                    "Given perms must have same app label "
                    "({} != {})".format(app_label, new_app_label))
            else:
                app_label = new_app_label
        else:
            codename = perm
        codenames.add(codename)

        if app_label is not None:
            new_ctype = ContentType.objects.get(app_label=app_label,
                                                permission__codename=codename)
            if ctype is not None and ctype != new_ctype:
                raise MixedContentTypeError(
                    "ContentType was once computed to be {} and another "
                    "one {}".format(ctype, new_ctype))
            else:
                ctype = new_ctype

    # Compute queryset and ctype if still missing
    if ctype is None and klass is not None:
        queryset = _get_queryset(klass)
        ctype = ContentType.objects.get_for_model(queryset.model)
    elif ctype is not None and klass is None:
        queryset = _get_queryset(ctype.model_class())
    elif klass is None:
        raise WrongAppError("Cannot determine content type")
    else:
        queryset = _get_queryset(klass)
        if ctype.model_class() != queryset.model and perms_filter == 'pk__in':
            raise MixedContentTypeError("Content type for given perms and "
                                        "klass differs")

    # At this point, we should have both ctype and queryset and they should
    # match which means: ctype.model_class() == queryset.model
    # we should also have `codenames` list

    # First check if user is superuser and if so, return queryset immediately
    if with_superuser and user.is_superuser:
        return queryset

    # Check if the user is anonymous. The
    # django.contrib.auth.models.AnonymousUser object doesn't work for queries
    # and it's nice to be able to pass in request.user blindly.
    if user.is_anonymous:
        user = get_anonymous_user()

    global_perms = set()
    has_global_perms = False
    # a superuser has by default assigned global perms for any
    if accept_global_perms and with_superuser:
        for code in codenames:
            if user.has_perm(ctype.app_label + '.' + code):
                global_perms.add(code)
        for code in global_perms:
            codenames.remove(code)
        # prerequisite: there must be elements in global_perms otherwise just
        # follow the procedure for object based permissions only AND
        # 1. codenames is empty, which means that permissions are ONLY set
        # globally, therefore return the full queryset.
        # OR
        # 2. any_perm is True, then the global permission beats the object
        # based permission anyway, therefore return full queryset
        if global_perms and (not codenames or any_perm):
            return queryset
        # if we have global perms and still some object based perms differing
        # from global perms and any_perm is set to false, then we have to flag
        # that global perms exist in order to merge object based permissions by
        # user and by group correctly. Scenario: global perm change_xx and
        # object based perm delete_xx on object A for user, and object based
        # permission delete_xx  on object B for group, to which user is
        # assigned.
        # get_objects_for_user(user, [change_xx, delete_xx], use_groups=True,
        # any_perm=False, accept_global_perms=True) must retrieve object A and
        # B.
        elif global_perms and codenames:
            has_global_perms = True

    # Now we should extract list of pk values for which we would filter
    # queryset
    user_model = get_user_obj_perms_model(queryset.model)
    user_obj_perms_queryset = (user_model.objects
                               .filter(Q(user=user) | Q(user=get_anonymous_user()))
                               .filter(permission__content_type=ctype))

    if codenames:
        user_obj_perms_queryset = user_obj_perms_queryset.filter(
            permission__codename__in=codenames)
    direct_fields = ['content_object__pk', 'permission__codename']
    generic_fields = ['object_pk', 'permission__codename']
    if user_model.objects.is_generic():
        user_fields = generic_fields
    else:
        user_fields = direct_fields

    if use_groups:
        group_model = get_group_obj_perms_model(queryset.model)
        group_filters = {
            'permission__content_type': ctype,
            'group__{}'.format(get_user_model().groups.field.related_query_name()): user,
        }
        if codenames:
            group_filters.update({
                'permission__codename__in': codenames,
            })
        groups_obj_perms_queryset = group_model.objects.filter(**group_filters)
        if group_model.objects.is_generic():
            group_fields = generic_fields
        else:
            group_fields = direct_fields
        if not any_perm and codenames and not has_global_perms:
            user_obj_perms = user_obj_perms_queryset.values_list(*user_fields)
            groups_obj_perms = groups_obj_perms_queryset.values_list(*group_fields)
            data = list(user_obj_perms) + list(groups_obj_perms)
            # sorting/grouping by pk (first in result tuple)
            data = sorted(data, key=lambda t: t[0])
            pk_list = []
            for pk, group in groupby(data, lambda t: t[0]):
                obj_codenames = set((e[1] for e in group))
                if codenames.issubset(obj_codenames):
                    pk_list.append(pk)
            objects = queryset.filter(**{perms_filter: pk_list})
            return objects

    if not any_perm and len(codenames) > 1:
        counts = user_obj_perms_queryset.values(
            user_fields[0]).annotate(object_pk_count=Count(user_fields[0]))
        user_obj_perms_queryset = counts.filter(
            object_pk_count__gte=len(codenames))

    values = user_obj_perms_queryset.values_list(user_fields[0], flat=True)
    if user_model.objects.is_generic():
        values = list(values)
    query = Q(**{perms_filter: values})
    if use_groups:
        values = groups_obj_perms_queryset.values_list(group_fields[0], flat=True)
        if group_model.objects.is_generic():
            values = list(values)
        query |= Q(**{perms_filter: values})

    return queryset.filter(query)


Instruction:
Can you tell what is the following Python 3 function doing
def get_users_with_permission(obj, permission):
    user_model = get_user_model()
    return user_model.objects.filter(
        userobjectpermission__object_pk=obj.pk,
        userobjectpermission__permission__codename=permission,
    ).distinct()

Response: Return users with specific permission on object.


Instruction:
Write a Python 3 script to
run purge for the object with location_id specified in event argument.

Response:
def purge_run(self, event):
        """Run purge for the object with ``location_id`` specified in ``event`` argument."""
        location_id = event['location_id']
        verbosity = event['verbosity']

        try:
            logger.info(__("Running purge for location id {}.", location_id))
            location_purge(location_id=location_id, delete=True, verbosity=verbosity)
        except Exception:  # pylint: disable=broad-except
            logger.exception("Error while purging location.", extra={'location_id': location_id})


Instruction:
Write a Python 3 function for
getting the core count and memory usage limits for this process.

Response:
def get_resource_limits(self):
        """Get the core count and memory usage limits for this process.

        :return: A dictionary with the resource limits, containing the
            following keys:

            - ``memory``: Memory usage limit, in MB. Defaults to 4096 if
              not otherwise specified in the resource requirements.
            - ``cores``: Core count limit. Defaults to 1.

        :rtype: dict
        """
        # Get limit defaults and overrides.
        limit_defaults = getattr(settings, 'FLOW_PROCESS_RESOURCE_DEFAULTS', {})
        limit_overrides = getattr(settings, 'FLOW_PROCESS_RESOURCE_OVERRIDES', {})

        limits = {}

        resources = self.requirements.get('resources', {})  # pylint: disable=no-member

        limits['cores'] = int(resources.get('cores', 1))

        max_cores = getattr(settings, 'FLOW_PROCESS_MAX_CORES', None)
        if max_cores:
            limits['cores'] = min(limits['cores'], max_cores)

        memory = limit_overrides.get('memory', {}).get(self.slug, None)
        if memory is None:
            memory = int(resources.get(
                'memory',
                # If no memory resource is configured, check settings.
                limit_defaults.get('memory', 4096)
            ))
        limits['memory'] = memory

        return limits


Instruction:
Explain what the following Python 3 code does
def validate_query_params(self):
        allowed_params = set(self.get_filters().keys())
        allowed_params.update(self.get_always_allowed_arguments())

        unallowed = set(self.request.query_params.keys()) - allowed_params

        if unallowed:
            msg = 'Unsupported parameter(s): {}. Please use a combination of: {}.'.format(
                ', '.join(unallowed),
                ', '.join(allowed_params),
            )
            self.form.add_error(field=None, error=ParseError(msg))

Response: Ensure no unsupported query params were used.


Instruction:
How would you implement a function in Python 3 that
classes method. Set the default value of the option key to value for all future instances of the class.

Response:
def set_default_option(cls, key, value):
        """Class method. Set the default value of the option `key` (string)
        to `value` for all future instances of the class.

        Note that this does not affect existing instances or the instance
        called from."""
        cls._default_options.update(cls._option_schema({key: value}))


Instruction:
Here you have a function in Python 3, explain what it does
def set_option(self, key, value):
        self._options.update(self._option_schema({key: value}))
        self.clear_cache()

Response: Set the option key to value.


Instruction:
Create a Python 3 function for
returning the current value of the option key ( string. Instance method only refers to current instance.

Response:
def get_option(self, key):
        """Return the current value of the option `key` (string).

        Instance method, only refers to current instance."""
        return self._options.get(key, self._default_options[key])


Instruction:
Can you implement a function in Python 3 that
returns a Wilson instance initialized by a wcxf. WC instance

Response:
def from_wc(cls, wc):
        """Return a `Wilson` instance initialized by a `wcxf.WC` instance"""
        return cls(wcdict=wc.dict, scale=wc.scale, eft=wc.eft, basis=wc.basis)


Instruction:
Create a Python 3 function for
returning a Wilson instance initialized by a WCxf file - like object

Response:
def load_wc(cls, stream):
        """Return a `Wilson` instance initialized by a WCxf file-like object"""
        wc = wcxf.WC.load(stream)
        return cls.from_wc(wc)


Instruction:
Can you tell what is the following Python 3 function doing
def match_run(self, scale, eft, basis, sectors='all'):
        cached = self._get_from_cache(sector=sectors, scale=scale, eft=eft, basis=basis)
        if cached is not None:
            return cached
        if sectors  == 'all':
            # the default value for sectors is "None" for translators
            translate_sectors = None
        else:
            translate_sectors = sectors
        scale_ew = self.get_option('smeft_matchingscale')
        mb = self.get_option('mb_matchingscale')
        mc = self.get_option('mc_matchingscale')
        if self.wc.basis == basis and self.wc.eft == eft and scale == self.wc.scale:
            return self.wc  # nothing to do
        if self.wc.eft == 'SMEFT':
            smeft_accuracy = self.get_option('smeft_accuracy')
            if eft == 'SMEFT':
                smeft = SMEFT(self.wc.translate('Warsaw', sectors=translate_sectors, parameters=self.parameters))
                # if input and output EFT ist SMEFT, just run.
                wc_out = smeft.run(scale, accuracy=smeft_accuracy)
                self._set_cache('all', scale, 'SMEFT', wc_out.basis, wc_out)
                return wc_out
            else:
                # if SMEFT -> WET-x: match to WET at the EW scale
                wc_ew = self._get_from_cache(sector='all', scale=scale_ew, eft='WET', basis='JMS')
                if wc_ew is None:
                    if self.wc.scale == scale_ew:
                        wc_ew = self.wc.match('WET', 'JMS', parameters=self.parameters)  # no need to run
                    else:
                        smeft = SMEFT(self.wc.translate('Warsaw', parameters=self.parameters))
                        wc_ew = smeft.run(scale_ew, accuracy=smeft_accuracy).match('WET', 'JMS', parameters=self.parameters)
                self._set_cache('all', scale_ew, wc_ew.eft, wc_ew.basis, wc_ew)
                wet = WETrunner(wc_ew, **self._wetrun_opt())
        elif self.wc.eft in ['WET', 'WET-4', 'WET-3']:
            wet = WETrunner(self.wc.translate('JMS', parameters=self.parameters, sectors=translate_sectors), **self._wetrun_opt())
        else:
            raise ValueError("Input EFT {} unknown or not supported".format(self.wc.eft))
        if eft == wet.eft:  # just run
            wc_out = wet.run(scale, sectors=sectors).translate(basis, sectors=translate_sectors, parameters=self.parameters)
            self._set_cache(sectors, scale, eft, basis, wc_out)
            return wc_out
        elif eft == 'WET-4' and wet.eft == 'WET':  # match at mb
            wc_mb = wet.run(mb, sectors=sectors).match('WET-4', 'JMS', parameters=self.parameters)
            wet4 = WETrunner(wc_mb, **self._wetrun_opt())
            wc_out = wet4.run(scale, sectors=sectors).translate(basis, sectors=translate_sectors, parameters=self.parameters)
            self._set_cache(sectors, scale, 'WET-4', basis, wc_out)
            return wc_out
        elif eft == 'WET-3' and wet.eft == 'WET-4':  # match at mc
            wc_mc = wet.run(mc, sectors=sectors).match('WET-3', 'JMS', parameters=self.parameters)
            wet3 = WETrunner(wc_mc, **self._wetrun_opt())
            wc_out = wet3.run(scale, sectors=sectors).translate(basis, sectors=translate_sectors, parameters=self.parameters)
            return wc_out
            self._set_cache(sectors, scale, 'WET-3', basis, wc_out)
        elif eft == 'WET-3' and wet.eft == 'WET':  # match at mb and mc
            wc_mb = wet.run(mb, sectors=sectors).match('WET-4', 'JMS', parameters=self.parameters)
            wet4 = WETrunner(wc_mb, **self._wetrun_opt())
            wc_mc = wet4.run(mc, sectors=sectors).match('WET-3', 'JMS', parameters=self.parameters)
            wet3 = WETrunner(wc_mc, **self._wetrun_opt())
            wc_out = wet3.run(scale, sectors=sectors).translate(basis, sectors=translate_sectors, parameters=self.parameters)
            self._set_cache(sectors, scale, 'WET-3', basis, wc_out)
            return wc_out
        else:
            raise ValueError("Running from {} to {} not implemented".format(wet.eft, eft))

Response: Returns the Wilson coefficients to a different scale and EFT and basis.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _get_from_cache(self, sector, scale, eft, basis):
        try:
            return self._cache[eft][scale][basis][sector]
        except KeyError:
            return None

Response: Try to load a set of Wilson coefficients from the cache else return None.


Instruction:
Given the following Python 3 function, write the documentation
def plotdata(self, key, part='re', scale='log', steps=50):
        if scale == 'log':
            x = np.logspace(log(self.scale_min),
                            log(self.scale_max),
                            steps,
                            base=e)
        elif scale == 'linear':
            x = np.linspace(self.scale_min,
                            self.scale_max,
                            steps)
        y = self.fun(x)
        y = np.array([d[key] for d in y])
        if part == 're':
            return x, y.real
        elif part == 'im':
            return x, y.imag

Response: Return a tuple of arrays x y that can be fed to plt. plot


Instruction:
Given the following Python 3 function, write the documentation
def plot(self, key, part='re', scale='log', steps=50, legend=True, plotargs={}):
        try:
            import matplotlib.pyplot as plt
        except ImportError:
            raise ImportError("Please install matplotlib if you want to use the plot method")
        pdat = self.plotdata(key, part=part, scale=scale, steps=steps)
        plt.plot(*pdat, label=key, **plotargs)
        if scale == 'log':
            plt.xscale('log')
        if legend:
            plt.legend()

Response: Plot the RG evolution of parameter key.


Instruction:
Here you have a function in Python 3, explain what it does
async def run_consumer(timeout=None, dry_run=False):
    channel = state.MANAGER_CONTROL_CHANNEL
    scope = {
        'type': 'control_event',
        'channel': channel,
    }

    app = ApplicationCommunicator(ManagerConsumer, scope)

    channel_layer = get_channel_layer()

    async def _consume_loop():
        """Run a loop to consume messages off the channels layer."""
        while True:
            message = await channel_layer.receive(channel)
            if dry_run:
                continue
            if message.get('type', {}) == '_resolwe_manager_quit':
                break
            message.update(scope)
            await app.send_input(message)

    if timeout is None:
        await _consume_loop()
    try:
        # A further grace period to catch late messages.
        async with async_timeout.timeout(timeout or 1):
            await _consume_loop()
    except asyncio.TimeoutError:
        pass

    await app.wait()

Response: Run the consumer until it finishes processing messages.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _get_class_path(self, klass_or_instance):
        if inspect.isclass(klass_or_instance):
            klass = '{}.{}'.format(klass_or_instance.__module__, klass_or_instance.__name__)
        elif not isinstance(klass_or_instance, str):
            klass = klass_or_instance.__class__
            klass = '{}.{}'.format(klass.__module__, klass.__name__)
        else:
            klass = klass_or_instance

        return klass

Response: Return the class path for a given class.


Instruction:
Write a Python 3 script for
registering an extension for a class.

Response:
def add_extension(self, klass, extension):
        """Register an extension for a class.

        :param klass: Class to register an extension for
        :param extension: Extension (arbitrary type)
        """
        klass = self._get_class_path(klass)

        # TODO: Take order into account.
        self._extensions.setdefault(klass, []).append(extension)


Instruction:
Can you generate the documentation for the following Python 3 function
def get_extensions(self, klass):
        self.discover_extensions()

        return self._extensions.get(self._get_class_path(klass), [])

Response: Return all registered extensions of a given class.


Instruction:
Write a Python 3 function for
getting the running parameters for the given scale.

Response:
def _get_running_parameters(self, scale, f, loop=3):
        """Get the running parameters (e.g. quark masses and the strong
        coupling at a given scale."""
        p = {}
        p['alpha_s'] = qcd.alpha_s(scale, self.f, self.parameters['alpha_s'], loop=loop)
        p['m_b'] = qcd.m_b(self.parameters['m_b'], scale, self.f, self.parameters['alpha_s'], loop=loop)
        p['m_c'] = qcd.m_c(self.parameters['m_c'], scale, self.f, self.parameters['alpha_s'], loop=loop)
        p['m_s'] = qcd.m_s(self.parameters['m_s'], scale, self.f, self.parameters['alpha_s'], loop=loop)
        p['m_u'] = qcd.m_s(self.parameters['m_u'], scale, self.f, self.parameters['alpha_s'], loop=loop)
        p['m_d'] = qcd.m_s(self.parameters['m_d'], scale, self.f, self.parameters['alpha_s'], loop=loop)
        # running ignored for alpha_e and lepton mass
        p['alpha_e'] = self.parameters['alpha_e']
        p['m_e'] = self.parameters['m_e']
        p['m_mu'] = self.parameters['m_mu']
        p['m_tau'] = self.parameters['m_tau']
        return p


Instruction:
Can you create a Python 3 function that
evolves the Wilson coefficients to the scale scale_out.

Response:
def run(self, scale_out, sectors='all'):
        """Evolve the Wilson coefficients to the scale `scale_out`.

        Parameters:

        - scale_out: output scale
        - sectors: optional. If provided, must be a tuple of strings
          corresponding to WCxf sector names. Only Wilson coefficients
          belonging to these sectors will be present in the output.

        Returns an instance of `wcxf.WC`.
        """
        C_out = self._run_dict(scale_out, sectors=sectors)
        all_wcs = set(wcxf.Basis[self.eft, 'JMS'].all_wcs)  # to speed up lookup
        C_out = {k: v for k, v in C_out.items()
                 if v != 0 and k in all_wcs}
        return wcxf.WC(eft=self.eft, basis='JMS',
                       scale=scale_out,
                       values=wcxf.WC.dict2values(C_out))


Instruction:
How would you code a function in Python 3 to
return the total number of bytes that will be returned by the iterator.

Response:
def size(self):
        """Returns:
        int : The total number of bytes that will be returned by the iterator.
        """
        if hasattr(self._stream, 'len'):
            return len(self._stream)
        elif hasattr(self._stream, 'fileno'):
            return os.fstat(self._stream.fileno()).st_size
        else:
            cur_pos = self._stream.tell()
            size = self._stream.seek(0, os.SEEK_END)
            self._stream.seek(cur_pos)
            return size


Instruction:
Make a summary of the following Python 3 code
def _get_cn_cert():
    try:
        cert_obj = django.core.cache.cache.cn_cert_obj
        d1_common.cert.x509.log_cert_info(
            logging.debug, 'Using cached CN cert for JWT validation', cert_obj
        )
        return cert_obj
    except AttributeError:
        cn_cert_obj = _download_and_decode_cn_cert()
        django.core.cache.cache.cn_cert_obj = cn_cert_obj
        return cn_cert_obj

Response:
Get the public TLS X.509 certificate from the root CN of the DataONE
    environment.


Instruction:
Can you tell what is the following Python 3 function doing
def key(self, key=""):
    params = join_params(self.parameters, {"key": key})

    return self.__class__(**params)

Response: Takes a user s API key string which applies content settings.


Instruction:
Make a summary of the following Python 3 code
def query(self, *q):
    params = join_params(self.parameters, {"q": q})

    return self.__class__(**params)

Response: Returns a new object with the given q


Instruction:
Implement a Python 3 function for
returning a new object with the sort parameters set to sf.

Response:
def sort_by(self, sf):
    """
    Determines how to sort search results. Available sorting methods are
    sort.SCORE, sort.COMMENTS, sort.HEIGHT, sort.RELEVANCE, sort.CREATED_AT,
    and sort.RANDOM; default is sort.CREATED_AT.
    """
    params = join_params(self.parameters, {"sf": sf})

    return self.__class__(**params)


Instruction:
Explain what the following Python 3 code does
def limit(self, limit):
    params = join_params(self.parameters, {"limit": limit})

    return self.__class__(**params)

Response: Returns a new object with the specified limit.


Instruction:
Here you have a function in Python 3, explain what it does
def filter(self, filter_id=""):
    params = join_params(self.parameters, {"filter_id": filter_id})

    return self.__class__(**params)

Response: Returns a new object with the current filter ID.


Instruction:
Make a summary of the following Python 3 code
def faves(self, option):
    params = join_params(self.parameters, {"faves": option})

    return self.__class__(**params)

Response: Returns a new object with the specified option applied to all users.


Instruction:
How would you explain what the following Python 3 function does
def uploads(self, option):
    params = join_params(self.parameters, {"uploads": option})

    return self.__class__(**params)

Response: Sets whether to filter by a user s uploads list. Options available are user. ONLY user. NOT user. ONLY user. NOT user. ONLY user. NOT user. ONLY user. NOT user. ONLY user. NOT user. ONLY user. NOT user. ONLY user. NOT user. ONLY user. NOT user. ONLY user. NOT user. ONLY user. NOT user. None is used.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def define_contributor(self, request):
        request.data['contributor'] = self.resolve_user(request.user).pk

Response: Define contributor by adding it to request. data.


Instruction:
Implement a Python 3 function for
checking if given url slug exists. Return True if slug exists and False otherwise.

Response:
def slug_exists(self, request):
        """Check if given url slug exists.

        Check if slug given in query parameter ``name`` exists. Return
        ``True`` if slug already exists and ``False`` otherwise.

        """
        if not request.user.is_authenticated:
            return Response(status=status.HTTP_401_UNAUTHORIZED)

        if 'name' not in request.query_params:
            return Response({'error': 'Query parameter `name` must be given.'},
                            status=status.HTTP_400_BAD_REQUEST)

        queryset = self.get_queryset()
        slug_name = request.query_params['name']
        return Response(queryset.filter(slug__iexact=slug_name).exists())


Instruction:
Can you tell what is the following Python 3 function doing
def get_ids(self, request_data, parameter_name='ids'):
        if parameter_name not in request_data:
            raise ParseError("`{}` parameter is required".format(parameter_name))

        ids = request_data.get(parameter_name)
        if not isinstance(ids, list):
            raise ParseError("`{}` parameter not a list".format(parameter_name))

        if not ids:
            raise ParseError("`{}` parameter is empty".format(parameter_name))

        if any(map(lambda id: not isinstance(id, int), ids)):
            raise ParseError("`{}` parameter contains non-integers".format(parameter_name))

        return ids

Response: Extract a list of integers from request data.


Instruction:
Explain what the following Python 3 code does
def get_id(self, request_data, parameter_name='id'):
        if parameter_name not in request_data:
            raise ParseError("`{}` parameter is required".format(parameter_name))

        id_parameter = request_data.get(parameter_name, None)
        if not isinstance(id_parameter, int):
            raise ParseError("`{}` parameter not an integer".format(parameter_name))

        return id_parameter

Response: Extract an integer from request data.


Instruction:
Can you generate the documentation for the following Python 3 function
def deserialize_from_headers(headers):
    return create_exception_by_name(
        _get_header(headers, 'DataONE-Exception-Name'),
        _get_header(headers, 'DataONE-Exception-DetailCode'),
        _get_header(headers, 'DataONE-Exception-Description'),
        _get_header(headers, 'DataONE-Exception-TraceInformation'),
        _get_header(headers, 'DataONE-Exception-Identifier'),
        _get_header(headers, 'DataONE-Exception-NodeId'),
    )

Response: Deserialize a DataONE Exception from a dictionary of HTTP headers.


Instruction:
How would you explain what the following Python 3 function does
def create_exception_by_name(
    name,
    detailCode='0',
    description='',
    traceInformation=None,
    identifier=None,
    nodeId=None,
):
    """Create a DataONEException based object by name.

    Args:
      name: str
        The type name of a DataONE Exception. E.g. NotFound.

        If an unknown type name is used, it is automatically set to ServiceFailure. As
        the XML Schema for DataONE Exceptions does not restrict the type names, this
        may occur when deserializing an exception not defined by DataONE.

      detailCode: int
        Optional index into a table of predefined error conditions.

    See Also:
      For remaining args, see: ``DataONEException()``

    """
    try:
        dataone_exception = globals()[name]
    except LookupError:
        dataone_exception = ServiceFailure
    return dataone_exception(
        detailCode, description, traceInformation, identifier, nodeId
    )

Response: Create a DataONE Exception based object by name.


Instruction:
Create a Python 3 function for
creating a DataONE Exception object by errorCode.

Response:
def create_exception_by_error_code(
    errorCode,
    detailCode='0',
    description='',
    traceInformation=None,
    identifier=None,
    nodeId=None,
):
    """Create a DataONE Exception object by errorCode.

    See Also:   For args, see: ``DataONEException()``

    """
    try:
        dataone_exception = ERROR_CODE_TO_EXCEPTION_DICT[errorCode]
    except LookupError:
        dataone_exception = ServiceFailure
    return dataone_exception(
        detailCode, description, traceInformation, identifier, nodeId
    )


Instruction:
Create a Python 3 function to
format a string for inclusion in the exception s string representation.

Response:
def _fmt(self, tag, msg):
        """Format a string for inclusion in the exception's string representation.

        If msg is None, format to empty string. If msg has a single line, format to:
        tag: msg If msg has multiple lines, format to: tag:   line 1   line 2 Msg is
        truncated to 1024 chars.

        """
        msg = msg or '<unset>'
        msg = str(msg)
        msg = msg.strip()
        if not msg:
            return
        if len(msg) > 2048:
            msg = msg[:1024] + '...'
        if msg.count('\n') <= 1:
            return '{}: {}\n'.format(tag, msg.strip())
        else:
            return '{}:\n  {}\n'.format(tag, msg.replace('\n', '\n  ').strip())


Instruction:
Write a Python 3 script to
serialize to a format more suitable for displaying to end users.

Response:
def friendly_format(self):
        """Serialize to a format more suitable for displaying to end users."""
        if self.description is not None:
            msg = self.description
        else:
            msg = 'errorCode: {} / detailCode: {}'.format(
                self.errorCode, self.detailCode
            )
        return self._fmt(self.name, msg)


Instruction:
How would you explain what the following Python 3 function does
def serialize_to_transport(self, encoding='utf-8', xslt_url=None):
        assert encoding in ('utf-8', 'UTF-8')
        dataone_exception_pyxb = self.get_pyxb()
        return d1_common.xml.serialize_for_transport(
            dataone_exception_pyxb, xslt_url=xslt_url
        )

Response: Serialize to XML bytes with prolog.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def serialize_to_display(self, xslt_url=None):
        return d1_common.xml.serialize_to_xml_str(
            self.get_pyxb(), pretty=True, xslt_url=xslt_url
        )

Response: Serialize to a pretty printed Unicode str suitable for display.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def serialize_to_headers(self):
        return {
            'DataONE-Exception-Name': self.__class__.__name__,
            'DataONE-Exception-ErrorCode': self._format_header(self.errorCode),
            'DataONE-Exception-DetailCode': self._format_header(self.detailCode),
            'DataONE-Exception-Description': self._format_header(self.description),
            'DataONE-Exception-TraceInformation': self._format_header(
                self.traceInformation
            ),
            'DataONE-Exception-Identifier': self._format_header(self.identifier),
            'DataONE-Exception-NodeID': self._format_header(self.nodeId),
        }

Response: Serialize to a dict of HTTP headers.


Instruction:
Can you implement a function in Python 3 that
generates a DataONE Exception PyXB object.

Response:
def get_pyxb(self):
        """Generate a DataONE Exception PyXB object.

        The PyXB object supports directly reading and writing the individual values that
        may be included in a DataONE Exception.

        """
        dataone_exception_pyxb = dataoneErrors.error()
        dataone_exception_pyxb.name = self.__class__.__name__
        dataone_exception_pyxb.errorCode = self.errorCode
        dataone_exception_pyxb.detailCode = self.detailCode
        if self.description is not None:
            dataone_exception_pyxb.description = self.description
        dataone_exception_pyxb.traceInformation = self.traceInformation
        if self.identifier is not None:
            dataone_exception_pyxb.identifier = self.identifier
        if self.nodeId is not None:
            dataone_exception_pyxb.nodeId = self.nodeId
        return dataone_exception_pyxb


Instruction:
Can you generate the documentation for the following Python 3 function
def recreate_parent_dependencies(apps, schema_editor):
    Data = apps.get_model('flow', 'Data')
    DataDependency = apps.get_model('flow', 'DataDependency')

    def process_dependency(data, parent):
        if not Data.objects.filter(pk=parent).exists():
            DataDependency.objects.create(
                child=data, parent=None, kind='io'
            )

    for data in Data.objects.all():
        for field_schema, fields in iterate_fields(data.input, data.process.input_schema):
            name = field_schema['name']
            value = fields[name]

            if field_schema.get('type', '').startswith('data:'):
                process_dependency(data, value)

            elif field_schema.get('type', '').startswith('list:data:'):
                for parent in value:
                    process_dependency(data, parent)

Response: Recreate empty dependency relation if parent has been deleted.


Instruction:
How would you implement a function in Python 3 that
adds a filter to the query that only has read or better access for one or more active subjects.

Response:
def add_access_policy_filter(request, query, column_name):
    """Filter records that do not have ``read`` or better access for one or more of the
    active subjects.

    Since ``read`` is the lowest access level that a subject can have, this method only
    has to filter on the presence of the subject.

    """
    q = d1_gmn.app.models.Subject.objects.filter(
        subject__in=request.all_subjects_set
    ).values('permission__sciobj')
    filter_arg = '{}__in'.format(column_name)
    return query.filter(**{filter_arg: q})


Instruction:
Explain what the following Python 3 code does
def add_redact_annotation(request, query):
    return query.annotate(
        redact=django.db.models.Exists(
            d1_gmn.app.models.Permission.objects.filter(
                sciobj=django.db.models.OuterRef('sciobj'),
                subject__subject__in=request.all_subjects_set,
                level__gte=d1_gmn.app.auth.WRITE_LEVEL,
            ),
            negated=True,
        )
    )

Response: Add redact annotation to LogEntry records that require unredacted IP and subject fields to be returned to the client.


Instruction:
Can you write a function in Python 3 where it
adds total_size field to all file - type outputs.

Response:
def calculate_total_size(apps, schema_editor):
    """Add ``total_size`` field to all file/dir-type outputs."""
    Data = apps.get_model('flow', 'Data')
    for data in Data.objects.all():
        hydrate_size(data, force=True)
        data.save()


Instruction:
Can you write a function in Python 3 where it
removes total_size field from all file - type outputs.

Response:
def remove_total_size(apps, schema_editor):
    """Remove ``total_size`` field from all file/dir-type outputs."""
    Data = apps.get_model('flow', 'Data')
    for data in Data.objects.all():
        for field_schema, fields in iterate_fields(data.output, data.process.output_schema):
            name = field_schema['name']
            value = fields[name]
            if 'type' in field_schema:
                if field_schema['type'].startswith('basic:file:'):
                    del value['total_size']
                elif field_schema['type'].startswith('list:basic:file:'):
                    for obj in value:
                        del obj['total_size']
                elif field_schema['type'].startswith('basic:dir:'):
                    del value['total_size']
                elif field_schema['type'].startswith('list:basic:dir:'):
                    for obj in value:
                        del obj['total_size']
        data.save()


Instruction:
How would you code a function in Python 3 to
assert that the given DID can be used as a PID for creating a new object with ivgs.

Response:
def is_valid_pid_for_create(did):
    """Assert that ``did`` can be used as a PID for creating a new object with
    MNStorage.create() or MNStorage.update()."""
    if not d1_gmn.app.did.is_valid_pid_for_create(did):
        raise d1_common.types.exceptions.IdentifierNotUnique(
            0,
            'Identifier is already in use as {}. did="{}"'.format(
                d1_gmn.app.did.classify_identifier(did), did
            ),
            identifier=did,
        )


Instruction:
Can you generate a brief explanation for the following Python 3 code
def is_valid_pid_to_be_updated(did):
    if not d1_gmn.app.did.is_valid_pid_to_be_updated(did):
        raise d1_common.types.exceptions.InvalidRequest(
            0,
            'Object cannot be updated because the identifier for the object to be '
            'updated is {}. did="{}"'.format(
                d1_gmn.app.did.classify_identifier(did), did
            ),
            identifier=did,
        )

Response: Assert that the PID of an object that can be updated.


Instruction:
Implement a Python 3 function for
validating that a MMP POST contains all required parts.

Response:
def post_has_mime_parts(request, parts):
    """Validate that a MMP POST contains all required sections.

    :param request: Django Request
    :param parts: [(part_type, part_name), ...]
    :return: None or raises exception.

    Where information is stored in the request:
    part_type header: request.META['HTTP_<UPPER CASE NAME>']
    part_type file: request.FILES['<name>']
    part_type field: request.POST['<name>']

    """
    missing = []

    for part_type, part_name in parts:
        if part_type == 'header':
            if 'HTTP_' + part_name.upper() not in request.META:
                missing.append('{}: {}'.format(part_type, part_name))
        elif part_type == 'file':
            if part_name not in list(request.FILES.keys()):
                missing.append('{}: {}'.format(part_type, part_name))
        elif part_type == 'field':
            if part_name not in list(request.POST.keys()):
                missing.append('{}: {}'.format(part_type, part_name))
        else:
            raise d1_common.types.exceptions.ServiceFailure(
                0, 'Invalid part_type. part_type="{}"'.format(part_type)
            )

    if len(missing) > 0:
        raise d1_common.types.exceptions.InvalidRequest(
            0,
            'Missing part(s) in MIME Multipart document. missing="{}"'.format(
                ', '.join(missing)
            ),
        )


Instruction:
Can you generate the documentation for the following Python 3 function
def _decade_ranges_in_date_range(self, begin_date, end_date):
        begin_dated = begin_date.year / 10
        end_dated = end_date.year / 10
        decades = []
        for d in range(begin_dated, end_dated + 1):
            decades.append('{}-{}'.format(d * 10, d * 10 + 9))
        return decades

Response: Return a list of decades which is covered by date range.


Instruction:
Can you implement a function in Python 3 that
returns a list of years in one decade which is covered by date range.

Response:
def _years_in_date_range_within_decade(self, decade, begin_date, end_date):
        """Return a list of years in one decade which is covered by date range."""
        begin_year = begin_date.year
        end_year = end_date.year
        if begin_year < decade:
            begin_year = decade
        if end_year > decade + 9:
            end_year = decade + 9
        return list(range(begin_year, end_year + 1))


Instruction:
Can you implement a function in Python 3 that
solves the SMEFT RGEs in the leading log approximation. Input C_in and output C_out are dictionaries of arrays.

Response:
def smeft_evolve_leadinglog(C_in, scale_in, scale_out, newphys=True):
    """Solve the SMEFT RGEs in the leading log approximation.

    Input C_in and output C_out are dictionaries of arrays."""
    C_out = deepcopy(C_in)
    b = beta.beta(C_out, newphys=newphys)
    for k, C in C_out.items():
        C_out[k] = C + b[k] / (16 * pi**2) * log(scale_out / scale_in)
    return C_out


Instruction:
Can you tell what is the following Python 3 function doing
def _smeft_evolve(C_in, scale_in, scale_out, newphys=True, **kwargs):
    def fun(t0, y):
        return beta.beta_array(C=C_array2dict(y.view(complex)),
                               newphys=newphys).view(float) / (16 * pi**2)
    y0 = C_dict2array(C_in).view(float)
    sol = solve_ivp(fun=fun,
                    t_span=(log(scale_in), log(scale_out)),
                    y0=y0, **kwargs)
    return sol

Response: Axuliary function used in meft_evolve and meft_evolve_continuous


Instruction:
Can you implement a function in Python 3 that
solves the SMEFT RGEs by numeric integration. Input C_in and output C_out are dictionaries of arrays.

Response:
def smeft_evolve(C_in, scale_in, scale_out, newphys=True, **kwargs):
    """Solve the SMEFT RGEs by numeric integration.

    Input C_in and output C_out are dictionaries of arrays."""
    sol = _smeft_evolve(C_in, scale_in, scale_out, newphys=newphys, **kwargs)
    return C_array2dict(sol.y[:, -1].view(complex))


Instruction:
How would you implement a function in Python 3 that
solves the SMEFT RGEs by numeric integration returning a function that allows to compute an interpolated solution at arbitrary intermediate scales.

Response:
def smeft_evolve_continuous(C_in, scale_in, scale_out, newphys=True, **kwargs):
    """Solve the SMEFT RGEs by numeric integration, returning a function that
    allows to compute an interpolated solution at arbitrary intermediate
    scales."""
    sol = _smeft_evolve(C_in, scale_in, scale_out, newphys=newphys,
                        dense_output=True, **kwargs)
    @np.vectorize
    def _rge_solution(scale):
        t = log(scale)
        y = sol.sol(t).view(complex)
        yd = C_array2dict(y)
        yw = arrays2wcxf_nonred(yd)
        return yw
    def rge_solution(scale):
        # this is to return a scalar if the input is scalar
        return _rge_solution(scale)[()]
    return rge_solution


Instruction:
Write a Python 3 function that can
show an invalid index error message.

Response:
def invalid_index(self, name):
        """Show an invalid index error message."""
        self.stderr.write("Unknown index: {}".format(name))
        self.stderr.write("Supported indices are:")
        for index in index_builder.indexes:
            self.stderr.write("  * {}".format(index.__class__.__name__))


Instruction:
Make a summary of the following Python 3 code
def filter_indices(self, options, verbosity, *args, **kwargs):
        index_name_map = {
            index.__class__.__name__: index
            for index in index_builder.indexes
        }

        # Process includes.
        if options['index']:
            indices = set(options['index'])
        else:
            indices = set(index_name_map.keys())

        # Process excludes.
        for index_name in options['exclude']:
            if index_name not in index_name_map:
                self.invalid_index(index_name)
                return

            indices.discard(index_name)

        # Execute action for each remaining index.
        for index_name in indices:
            try:
                index = index_name_map[index_name]
            except KeyError:
                self.invalid_index(index_name)
                return

            if verbosity > 0:
                self.stdout.write("Processing index '{}'...".format(index_name))
            self.handle_index(index, *args, **kwargs)

Response: Filter indices and execute an action for each index.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_contributor_sort_value(self, obj):
        user = obj.contributor

        if user.first_name or user.last_name:
            contributor = user.get_full_name()
        else:
            contributor = user.username

        return contributor.strip().lower()

Response: Generate display name for contributor.


Instruction:
Implement a Python 3 function for
generating user filtering tokens.

Response:
def _get_user(self, user):
        """Generate user filtering tokens."""
        return ' '.join([user.username, user.first_name, user.last_name])


Instruction:
Write a Python 3 function for
returning param_str converted to an int. Raise InvalidRequest.

Response:
def _get_and_assert_slice_param(url_dict, param_name, default_int):
    """Return ``param_str`` converted to an int.

    If str cannot be converted to int or int is not zero or positive, raise
    InvalidRequest.

    """
    param_str = url_dict['query'].get(param_name, default_int)
    try:
        n = int(param_str)
    except ValueError:
        raise d1_common.types.exceptions.InvalidRequest(
            0,
            'Slice parameter is not a valid integer. {}="{}"'.format(
                param_name, param_str
            ),
        )
    if n < 0:
        raise d1_common.types.exceptions.InvalidRequest(
            0,
            'Slice parameter cannot be a negative number. {}="{}"'.format(
                param_name, param_str
            ),
        )
    return n


Instruction:
Can you generate the documentation for the following Python 3 function
def _assert_valid_start(start_int, count_int, total_int):
    if total_int and start_int >= total_int:
        raise d1_common.types.exceptions.InvalidRequest(
            0,
            'Requested a non-existing slice. start={} count={} total={}'.format(
                start_int, count_int, total_int
            ),
        )

Response: Assert that the requested start position is lower than the requested count.


Instruction:
Can you generate the documentation for the following Python 3 function
def _adjust_count_if_required(start_int, count_int, total_int):
    if start_int + count_int > total_int:
        count_int = total_int - start_int
    count_int = min(count_int, django.conf.settings.MAX_SLICE_ITEMS)
    return count_int

Response: Adjust requested object count down if needed.


Instruction:
Can you tell what is the following Python 3 function doing
def _add_fallback_slice_filter(query, start_int, count_int, total_int):
    logging.debug(
        'Adding fallback slice filter. start={} count={} total={} '.format(
            start_int, count_int, total_int
        )
    )
    if not count_int:
        return query.none()
    else:
        return query[start_int : start_int + count_int]

Response: Add a slice filter to the query that will not return any results.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _cache_get_last_in_slice(url_dict, start_int, total_int, authn_subj_list):
    key_str = _gen_cache_key_for_slice(url_dict, start_int, total_int, authn_subj_list)
    # TODO: Django docs state that cache.get() should return None on unknown key.
    try:
        last_ts_tup = django.core.cache.cache.get(key_str)
    except KeyError:
        last_ts_tup = None
    logging.debug('Cache get. key="{}" -> last_ts_tup={}'.format(key_str, last_ts_tup))
    return last_ts_tup

Response: Return the last cached version of a given slice.


Instruction:
Implement a function in Python 3 to
generate a key for the REST URL for a given slice.

Response:
def _gen_cache_key_for_slice(url_dict, start_int, total_int, authn_subj_list):
    """Generate cache key for the REST URL the client is currently accessing or is
    expected to access in order to get the slice starting at the given ``start_int`` of
    a multi-slice result set.

    When used for finding the key to check in the current call, ``start_int`` is
    0, or the start that was passed in the current call.

    When used for finding the key to set for the anticipated call, ``start_int`` is
    current ``start_int`` + ``count_int``, the number of objects the current call will
    return.

    The URL for the slice is the same as for the current slice, except that the
    `start` query parameter has been increased by the number of items returned in
    the current slice.

    Except for advancing the start value and potentially adjusting the desired
    slice size, it doesn't make sense for the client to change the REST URL during
    slicing, but such queries are supported. They will, however, trigger
    potentially expensive database queries to find the current slice position.

    To support adjustments in desired slice size during slicing, the count is not
    used when generating the key.

    The active subjects are used in the key in order to prevent potential security
    issues if authenticated subjects change during slicing.

    The url_dict is normalized by encoding it to a JSON string with sorted keys. A
    hash of the JSON is used for better distribution in a hash map and to avoid
    the 256 bytes limit on keys in some caches.

    """
    # logging.debug('Gen key. result_record_count={}'.format(result_record_count))
    key_url_dict = copy.deepcopy(url_dict)
    key_url_dict['query'].pop('start', None)
    key_url_dict['query'].pop('count', None)
    key_json = d1_common.util.serialize_to_normalized_compact_json(
        {
            'url_dict': key_url_dict,
            'start': start_int,
            'total': total_int,
            'subject': authn_subj_list,
        }
    )
    logging.debug('key_json={}'.format(key_json))
    return hashlib.sha256(key_json.encode('utf-8')).hexdigest()


Instruction:
Write a Python 3 function that can
construct a numpy array with Wilson coefficient values from a dictionary of label - value pairs corresponding to the non - redundant elements.

Response:
def smeft_toarray(wc_name, wc_dict):
    """Construct a numpy array with Wilson coefficient values from a
    dictionary of label-value pairs corresponding to the non-redundant
    elements."""
    shape = smeftutil.C_keys_shape[wc_name]
    C = np.zeros(shape, dtype=complex)
    for k, v in wc_dict.items():
        if k.split('_')[0] != wc_name:
            continue
        indices = k.split('_')[-1]  # e.g. '1213'
        indices = tuple(int(s) - 1 for s in indices)  # e.g. (1, 2, 1, 3)
        C[indices] = v
    C = smeftutil.symmetrize({wc_name: C})[wc_name]
    return C


Instruction:
Create a Python 3 function to
translate from the Warsaw basis to the Warsaw mass basis.

Response:
def warsaw_to_warsawmass(C, parameters=None, sectors=None):
    """Translate from the Warsaw basis to the 'Warsaw mass' basis.

    Parameters used:
    - `Vus`, `Vub`, `Vcb`, `gamma`: elements of the unitary CKM matrix (defined
      as the mismatch between left-handed quark mass matrix diagonalization
      matrices).
    """
    p = default_parameters.copy()
    if parameters is not None:
        # if parameters are passed in, overwrite the default values
        p.update(parameters)
    # start out with a 1:1 copy
    C_out = C.copy()
    # rotate left-handed up-type quark fields in uL-uR operator WCs
    C_rotate_u = ['uphi', 'uG', 'uW', 'uB']
    for name in C_rotate_u:
        _array = smeft_toarray(name, C)
        V = ckmutil.ckm.ckm_tree(p["Vus"], p["Vub"], p["Vcb"], p["delta"])
        UuL = V.conj().T
        _array = UuL.conj().T @ _array
        _dict = smeft_fromarray(name, _array)
        C_out.update(_dict)
    # diagonalize dimension-5 Weinberg operator
    _array = smeft_toarray('llphiphi', C)
    _array = np.diag(ckmutil.diag.msvd(_array)[1])
    _dict = smeft_fromarray('llphiphi', _array)
    C_out.update(_dict)
    return C_out


Instruction:
Implement a Python 3 function for
translating from the Warsaw up basis to the Warsaw basis.

Response:
def warsaw_up_to_warsaw(C, parameters=None, sectors=None):
    """Translate from the 'Warsaw up' basis to the Warsaw basis.

    Parameters used:
    - `Vus`, `Vub`, `Vcb`, `gamma`: elements of the unitary CKM matrix (defined
      as the mismatch between left-handed quark mass matrix diagonalization
      matrices).
    """
    C_in = smeftutil.wcxf2arrays_symmetrized(C)
    p = default_parameters.copy()
    if parameters is not None:
        # if parameters are passed in, overwrite the default values
        p.update(parameters)
    Uu = Ud = Ul = Ue = np.eye(3)
    V = ckmutil.ckm.ckm_tree(p["Vus"], p["Vub"], p["Vcb"], p["delta"])
    Uq = V
    C_out = smeftutil.flavor_rotation(C_in, Uq, Uu, Ud, Ul, Ue)
    C_out = smeftutil.arrays2wcxf_nonred(C_out)
    warsaw = wcxf.Basis['SMEFT', 'Warsaw']
    all_wcs = set(warsaw.all_wcs)  # to speed up lookup
    return {k: v for k, v in C_out.items() if k in all_wcs}


Instruction:
Explain what the following Python 3 code does
def sysmeta_add_preferred(sysmeta_pyxb, node_urn):
    if not has_replication_policy(sysmeta_pyxb):
        sysmeta_set_default_rp(sysmeta_pyxb)
    rp_pyxb = sysmeta_pyxb.replicationPolicy
    _add_node(rp_pyxb, 'pref', node_urn)
    _remove_node(rp_pyxb, 'block', node_urn)

Response: Add a preferred replication target to the list of blocked Member Nodes.


Instruction:
Implement a function in Python 3 to
normalize a ReplicationPolicy PyXB type in place.

Response:
def normalize(rp_pyxb):
    """Normalize a ReplicationPolicy PyXB type in place.

    The preferred and blocked lists are sorted alphabetically. As blocked nodes
    override preferred nodes, and any node present in both lists is removed from the
    preferred list.

    Args:
      rp_pyxb : ReplicationPolicy PyXB object
        The object will be normalized in place.

    """

    # noinspection PyMissingOrEmptyDocstring
    def sort(r, a):
        d1_common.xml.sort_value_list_pyxb(_get_attr_or_list(r, a))

    rp_pyxb.preferredMemberNode = set(_get_attr_or_list(rp_pyxb, 'pref')) - set(
        _get_attr_or_list(rp_pyxb, 'block')
    )
    sort(rp_pyxb, 'block')
    sort(rp_pyxb, 'pref')


Instruction:
Make a summary of the following Python 3 code
def are_equivalent_xml(a_xml, b_xml):
    return are_equivalent_pyxb(
        d1_common.xml.deserialize(a_xml), d1_common.xml.deserialize(b_xml)
    )

Response: Checks if two ReplicationPolicy XML docs are semantically equivalent.


Instruction:
How would you implement a function in Python 3 that
converts ReplicationPolicy PyXB object to a normalized dict.

Response:
def pyxb_to_dict(rp_pyxb):
    """Convert ReplicationPolicy PyXB object to a normalized dict.

    Args:
      rp_pyxb: ReplicationPolicy to convert.

    Returns:
        dict : Replication Policy as normalized dict.

    Example::

      {
        'allowed': True,
        'num': 3,
        'blockedMemberNode': {'urn:node:NODE1', 'urn:node:NODE2', 'urn:node:NODE3'},
        'preferredMemberNode': {'urn:node:NODE4', 'urn:node:NODE5'},
      }

    """
    return {
        'allowed': bool(_get_attr_or_list(rp_pyxb, 'allowed')),
        'num': _get_as_int(rp_pyxb),
        'block': _get_as_set(rp_pyxb, 'block'),
        'pref': _get_as_set(rp_pyxb, 'pref'),
    }


Instruction:
How would you code a function in Python 3 to
convert dict to ReplicationPolicy PyXB object.

Response:
def dict_to_pyxb(rp_dict):
    """Convert dict to ReplicationPolicy PyXB object.

    Args:
      rp_dict: Native Python structure representing a Replication Policy.

    Example::

      {
        'allowed': True,
        'num': 3,
        'blockedMemberNode': {'urn:node:NODE1', 'urn:node:NODE2', 'urn:node:NODE3'},
        'preferredMemberNode': {'urn:node:NODE4', 'urn:node:NODE5'},
      }

    Returns:
      ReplicationPolicy PyXB object.

    """
    rp_pyxb = d1_common.types.dataoneTypes.replicationPolicy()
    rp_pyxb.replicationAllowed = rp_dict['allowed']
    rp_pyxb.numberReplicas = rp_dict['num']
    rp_pyxb.blockedMemberNode = rp_dict['block']
    rp_pyxb.preferredMemberNode = rp_dict['pref']
    normalize(rp_pyxb)
    return rp_pyxb


Instruction:
Can you implement a function in Python 3 that
ensures that RP allows replication.

Response:
def _ensure_allow_rp(rp_pyxb):
    """Ensure that RP allows replication."""
    if not rp_pyxb.replicationAllowed:
        rp_pyxb.replicationAllowed = True
    if not rp_pyxb.numberReplicas:
        rp_pyxb.numberReplicas = 3


Instruction:
Here you have a function in Python 3, explain what it does
def _display_interval(i):
    sigils = ["d", "h", "m", "s"]
    factors = [24 * 60 * 60, 60 * 60, 60, 1]
    remain = int(i)
    result = ""
    for fac, sig in zip(factors, sigils):
        if remain < fac:
            continue
        result += "{}{}".format(remain // fac, sig)
        remain = remain % fac
    return result

Response: Convert a time interval into a human - readable string.


Instruction:
Can you generate the documentation for the following Python 3 function
def update(self, num):
        num = float(num)
        self.count += 1
        self.low = min(self.low, num)
        self.high = max(self.high, num)

        # Welford's online mean and variance algorithm.
        delta = num - self.mean
        self.mean = self.mean + delta / self.count
        delta2 = num - self.mean
        self._rolling_variance = self._rolling_variance + delta * delta2

        if self.count > 1:
            self.deviation = math.sqrt(self._rolling_variance / (self.count - 1))
        else:
            self.deviation = 0.0

Response: Update the internal statistics with the new number.


Instruction:
Can you tell what is the following Python 3 function doing
def to_dict(self):
        return {
            'high': self.high,
            'low': self.low,
            'mean': self.mean,
            'count': self.count,
            'deviation': self.deviation,
        }

Response: Pack the stats computed into a dictionary.


Instruction:
How would you implement a function in Python 3 that
adds a value at the specified time to the series.

Response:
def add(self, count, timestamp=None):
        """Add a value at the specified time to the series.

        :param count: The number of work items ready at the specified
            time.
        :param timestamp: The timestamp to add. Defaults to None,
            meaning current time. It should be strictly greater (newer)
            than the last added timestamp.
        """
        if timestamp is None:
            timestamp = time.time()
        if self.last_data >= timestamp:
            raise ValueError("Time {} >= {} in load average calculation".format(self.last_data, timestamp))
        self.last_data = timestamp

        for meta in self.intervals.values():
            meta.push(count, timestamp)


Instruction:
Create a Python 3 function to
pack the load averages into a nicely - keyed dictionary.

Response:
def to_dict(self):
        """Pack the load averages into a nicely-keyed dictionary."""
        result = {}
        for meta in self.intervals.values():
            result[meta.display] = meta.value
        return result


Instruction:
Can you tell what is the following Python 3 function doing
def listFormats(self, vendorSpecific=None):
        response = self.listFormatsResponse(vendorSpecific)
        return self._read_dataone_type_response(response, 'ObjectFormatList')

Response: See Also : listFormatsResponse


Instruction:
Can you write a function in Python 3 where it
sees Also : getFormatResponse

Response:
def getFormat(self, formatId, vendorSpecific=None):
        """See Also: getFormatResponse()

        Args:
          formatId:
          vendorSpecific:

        Returns:

        """
        response = self.getFormatResponse(formatId, vendorSpecific)
        return self._read_dataone_type_response(response, 'ObjectFormat')


Instruction:
How would you implement a function in Python 3 that
reserves the identifier of a specific object.

Response:
def reserveIdentifierResponse(self, pid, vendorSpecific=None):
        """CNCore.getLogRecords(session[, fromDate][, toDate][, event][, start][,
        count]) → Log https://releases.dataone.org/online/api-
        documentation-v2.0.1/apis/CN_APIs.html#CNCore.getLogRecords Implemented in
        d1_client.baseclient.py.

        CNCore.reserveIdentifier(session, pid) → Identifier
        https://releases.dataone.org/online/api-documentation-v2.0.1/apis/CN_APIs.html#CNCore.reserveIdentifier

        Args:
          pid:
          vendorSpecific:

        Returns:

        """
        mmp_dict = {'pid': pid}
        return self.POST(['reserve', pid], fields=mmp_dict, headers=vendorSpecific)


Instruction:
How would you implement a function in Python 3 that
sees Also : reserveIdentifierResponse

Response:
def reserveIdentifier(self, pid, vendorSpecific=None):
        """See Also: reserveIdentifierResponse()

        Args:
          pid:
          vendorSpecific:

        Returns:

        """
        response = self.reserveIdentifierResponse(pid, vendorSpecific)
        return self._read_dataone_type_response(response, 'Identifier', vendorSpecific)


Instruction:
Here you have a function in Python 3, explain what it does
def listChecksumAlgorithms(self, vendorSpecific=None):
        response = self.listChecksumAlgorithmsResponse(vendorSpecific)
        return self._read_dataone_type_response(response, 'ChecksumAlgorithmList')

Response: See Also : listChecksumAlgorithmsResponse


Instruction:
Can you generate a brief explanation for the following Python 3 code
def setObsoletedByResponse(
        self, pid, obsoletedByPid, serialVersion, vendorSpecific=None
    ):
        """CNCore.setObsoletedBy(session, pid, obsoletedByPid, serialVersion) → boolean
        https://releases.dataone.org/online/api-
        documentation-v2.0.1/apis/CN_APIs.html#CNCore.setObsoletedBy.

        Args:
          pid:
          obsoletedByPid:
          serialVersion:
          vendorSpecific:

        Returns:

        """
        mmp_dict = {
            'obsoletedByPid': obsoletedByPid,
            'serialVersion': str(serialVersion),
        }
        return self.PUT(['obsoletedBy', pid], fields=mmp_dict, headers=vendorSpecific)

Response: This method is used to set the state of an object in the NX - API to indicate that a specific object is obsoleted by another object.


Instruction:
Can you write a function in Python 3 where it
sets the flag that the user is obsoleted by the object.

Response:
def setObsoletedBy(self, pid, obsoletedByPid, serialVersion, vendorSpecific=None):
        """See Also: setObsoletedByResponse()

        Args:
          pid:
          obsoletedByPid:
          serialVersion:
          vendorSpecific:

        Returns:

        """
        response = self.setObsoletedByResponse(
            pid, obsoletedByPid, serialVersion, vendorSpecific
        )
        return self._read_boolean_response(response)


Instruction:
Can you generate the documentation for the following Python 3 function
def listNodes(self, vendorSpecific=None):
        response = self.listNodesResponse(vendorSpecific)
        return self._read_dataone_type_response(response, 'NodeList')

Response: See Also : listNodesResponse


Instruction:
Write a Python 3 script to
get a boolean version of the CN has reservation response.

Response:
def hasReservationResponse(self, pid, subject, vendorSpecific=None):
        """CNCore.registerSystemMetadata(session, pid, sysmeta) → Identifier CN
        INTERNAL.

        CNCore.hasReservation(session, pid) → boolean
        https://releases.dataone.org/online/api-documentation-v2.0.1/apis/CN_APIs.html#CNCore.hasReservation

        Args:
          pid:
          subject:
          vendorSpecific:

        Returns:

        """
        return self.GET(['reserve', pid, subject], headers=vendorSpecific)


Instruction:
Can you create a Python 3 function that
sees Also : hasReservationResponse

Response:
def hasReservation(self, pid, subject, vendorSpecific=None):
        """See Also: hasReservationResponse()

        Args:
          pid:
          subject:
          vendorSpecific:

        Returns:

        """
        response = self.hasReservationResponse(pid, subject, vendorSpecific)
        return self._read_boolean_404_response(response)


Instruction:
Create a Python 3 function to
see Also : resolveResponse

Response:
def resolve(self, pid, vendorSpecific=None):
        """See Also: resolveResponse()

        Args:
          pid:
          vendorSpecific:

        Returns:

        """
        response = self.resolveResponse(pid, vendorSpecific)
        return self._read_dataone_type_response(
            response, 'ObjectLocationList', response_is_303_redirect=True
        )