Instruction:
How would you explain what the following Python 3 function does
def update_cache(func):
    @wraps(func)
    def wrapper(self, *args, **kwargs):
        """The wrapper function."""
        lazy = kwargs.get('lazy', False)
        kwargs.pop('lazy', None)

        if not lazy:
            self.get_json()

        ret = func(self, *args, **kwargs)

        # If returning a string assume it is an update message
        if isinstance(ret, str) and not lazy:
            self.set_json(ret)
        else:
            return ret

    return wrapper

Response: Decorate functions that modify the internally stored usernotes JSON.


Instruction:
Can you generate the documentation for the following Python 3 function
def s(cls: Type[C], *args, **kwargs) -> Partial[C]:
        return Partial(cls, *args, **kwargs)

Response: Create an unbound prototype of this class partially applying arguments


Instruction:
Explain what the following Python 3 code does
def _build_mappings(
        self, classes: Sequence[type]
    ) -> Tuple[Mapping[type, Sequence[type]], Mapping[type, Sequence[type]]]:
        """
        Collect all bases and organize into parent/child mappings.
        """
        parents_to_children: MutableMapping[type, Set[type]] = {}
        children_to_parents: MutableMapping[type, Set[type]] = {}
        visited_classes: Set[type] = set()
        class_stack = list(classes)
        while class_stack:
            class_ = class_stack.pop()
            if class_ in visited_classes:
                continue
            visited_classes.add(class_)
            for base in class_.__bases__:
                if base not in visited_classes:
                    class_stack.append(base)
                parents_to_children.setdefault(base, set()).add(class_)
                children_to_parents.setdefault(class_, set()).add(base)
        sorted_parents_to_children: MutableMapping[
            type, List[type]
        ] = collections.OrderedDict()
        for parent, children in sorted(
            parents_to_children.items(), key=lambda x: (x[0].__module__, x[0].__name__)
        ):
            sorted_parents_to_children[parent] = sorted(
                children, key=lambda x: (x.__module__, x.__name__)
            )
        sorted_children_to_parents: MutableMapping[
            type, List[type]
        ] = collections.OrderedDict()
        for child, parents in sorted(
            children_to_parents.items(), key=lambda x: (x[0].__module__, x[0].__name__)
        ):
            sorted_children_to_parents[child] = sorted(
                parents, key=lambda x: (x.__module__, x.__name__)
            )
        return sorted_parents_to_children, sorted_children_to_parents

Response: Build the mappings for the base classes and their children.


Instruction:
Given the following Python 3 function, write the documentation
def _collect_classes(
        self, package_paths: Sequence[str], recurse_subpackages: bool = True
    ) -> Sequence[type]:
        """
        Collect all classes defined in/under ``package_paths``.
        """
        import uqbar.apis

        classes = []
        initial_source_paths: Set[str] = set()
        # Graph source paths and classes
        for path in package_paths:
            try:
                module = importlib.import_module(path)
                if hasattr(module, "__path__"):
                    initial_source_paths.update(getattr(module, "__path__"))
                else:
                    initial_source_paths.add(module.__file__)
            except ModuleNotFoundError:
                path, _, class_name = path.rpartition(".")
                module = importlib.import_module(path)
                classes.append(getattr(module, class_name))
        # Iterate source paths
        for source_path in uqbar.apis.collect_source_paths(
            initial_source_paths, recurse_subpackages=recurse_subpackages
        ):
            package_path = uqbar.apis.source_path_to_package_path(source_path)
            module = importlib.import_module(package_path)
            # Grab any defined classes
            for name in dir(module):
                if name.startswith("_"):
                    continue
                object_ = getattr(module, name)
                if isinstance(object_, type) and object_.__module__ == module.__name__:
                    classes.append(object_)
        return sorted(classes, key=lambda x: (x.__module__, x.__name__))

Response: Collect all classes defined in package_paths.


Instruction:
Can you tell what is the following Python 3 function doing
def get_auth():
    auth = get_auth_from_env()
    if auth[0] and auth[1]:
        return auth

    home = os.path.expanduser("~")
    config = os.path.join(home, '.gitconfig')
    if not os.path.isfile(config):
        raise GithubException('No .gitconfig available')

    parser = configparser.ConfigParser()
    parser.read(config)
    if 'user' in parser:
        user = parser['user']
        if 'username' not in user:
            raise GithubException('Specify username in %s user '
                                  'section' % config)
        if 'token' not in user:
            raise GithubException('Specify token in %s user section'
                                  % config)
        return user['username'], user['token']
    else:
        raise GithubException('No user section in %s' % config)

Response:
Return a tuple for authenticating a user
    If successful raise AgileError.


Instruction:
Write a Python 3 script to
function checkAndCreate Check if an architectures exists and create it if not

Response:
def checkAndCreate(self, key, payload, osIds):
        """ Function checkAndCreate
        Check if an architectures exists and create it if not

        @param key: The targeted architectures
        @param payload: The targeted architectures description
        @param osIds: The list of os ids liked with this architecture
        @return RETURN: The id of the object
        """
        if key not in self:
            self[key] = payload
        oid = self[key]['id']
        if not oid:
            return False
        #~ To be sure the OS list is good, we ensure our os are in the list
        for os in self[key]['operatingsystems']:
            osIds.add(os['id'])
        self[key]["operatingsystem_ids"] = list(osIds)
        if (len(self[key]['operatingsystems']) is not len(osIds)):
            return False
        return oid


Instruction:
Given the following Python 3 function, write the documentation
def pip_command_output(pip_args):
    import sys
    import pip
    from io import StringIO
    # as pip will write to stdout we use some nasty hacks
    # to substitute system stdout with our own
    old_stdout = sys.stdout
    sys.stdout = mystdout = StringIO()
    pip.main(pip_args)
    output = mystdout.getvalue()
    mystdout.truncate(0)
    sys.stdout = old_stdout
    return output

Response: Get output from pip command


Instruction:
Can you implement a function in Python 3 that
generates (temporarily) versioneer.py file in project root directory :return:

Response:
def setup_versioneer():
    """
    Generate (temporarily) versioneer.py file in project root directory
    :return:
    """
    try:
        # assume versioneer.py was generated using "versioneer install" command
        import versioneer
        versioneer.get_version()
    except ImportError:
        # it looks versioneer.py is missing
        # lets assume that versioneer package is installed
        # and versioneer binary is present in $PATH
        import subprocess
        try:
            # call versioneer install to generate versioneer.py
            subprocess.check_output(["versioneer", "install"])
        except OSError:
            # it looks versioneer is missing from $PATH
            # probably versioneer is installed in some user directory

            # query pip for list of files in versioneer package
            # line below is equivalen to putting result of
            #  "pip show -f versioneer" command to string output
            output = pip_command_output(["show", "-f", "versioneer"])

            # now we parse the results
            import os
            # find absolute path where *versioneer package* was installed
            # and store it in main_path
            main_path = [x[len("Location: "):] for x in output.splitlines()
                         if x.startswith("Location")][0]
            # find path relative to main_path where
            # *versioneer binary* was installed
            bin_path = [x[len("  "):] for x in output.splitlines()
                        if x.endswith(os.path.sep + "versioneer")][0]

            # exe_path is absolute path to *versioneer binary*
            exe_path = os.path.join(main_path, bin_path)
            # call versioneer install to generate versioneer.py
            # line below is equivalent to running in terminal
            # "python versioneer install"
            subprocess.check_output(["python", exe_path, "install"])


Instruction:
Here you have a function in Python 3, explain what it does
def clean_cache():
    import importlib
    try:  # Python ver < 3.3
        vermod = importlib.import_module("versioneer")
        globals()["versioneer"] = vermod
    except ImportError:
        importlib.invalidate_caches()

Response: This function is used to clean up the cache of module finders.


Instruction:
Make a summary of the following Python 3 code
def get_version():
    setup_versioneer()
    clean_cache()
    import versioneer
    version = versioneer.get_version()
    parsed_version = parse_version(version)
    if '*@' in str(parsed_version):
        import time
        version += str(int(time.time()))
    return version

Response:
Get project version (using versioneer)
    :return: string containing version


Instruction:
Can you generate a brief explanation for the following Python 3 code
def find_common_prefix(
    paths: Sequence[Union[str, pathlib.Path]]
) -> Optional[pathlib.Path]:
    """
    Find the common prefix of two or more paths.

    ::

        >>> import pathlib
        >>> one = pathlib.Path('foo/bar/baz')
        >>> two = pathlib.Path('foo/quux/biz')
        >>> three = pathlib.Path('foo/quux/wuux')

    ::

        >>> import uqbar.io
        >>> str(uqbar.io.find_common_prefix([one, two, three]))
        'foo'

    :param paths: paths to inspect
    """
    counter: collections.Counter = collections.Counter()
    for path in paths:
        path = pathlib.Path(path)
        counter.update([path])
        counter.update(path.parents)
    valid_paths = sorted(
        [path for path, count in counter.items() if count >= len(paths)],
        key=lambda x: len(x.parts),
    )
    if valid_paths:
        return valid_paths[-1]
    return None

Response: Find the common prefix of two or more paths.


Instruction:
Create a Python 3 function for
generating relative path from source_path to target_path.

Response:
def relative_to(
    source_path: Union[str, pathlib.Path], target_path: Union[str, pathlib.Path]
) -> pathlib.Path:
    """
    Generates relative path from ``source_path`` to ``target_path``.

    Handles the case of paths without a common prefix.

    ::

        >>> import pathlib
        >>> source = pathlib.Path('foo/bar/baz')
        >>> target = pathlib.Path('foo/quux/biz')

    ::

        >>> target.relative_to(source)
        Traceback (most recent call last):
          ...
        ValueError: 'foo/quux/biz' does not start with 'foo/bar/baz'

    ::

        >>> import uqbar.io
        >>> str(uqbar.io.relative_to(source, target))
        '../../quux/biz'

    :param source_path: the source path
    :param target_path: the target path
    """
    source_path = pathlib.Path(source_path).absolute()
    if source_path.is_file():
        source_path = source_path.parent
    target_path = pathlib.Path(target_path).absolute()
    common_prefix = find_common_prefix([source_path, target_path])
    if not common_prefix:
        raise ValueError("No common prefix")
    source_path = source_path.relative_to(common_prefix)
    target_path = target_path.relative_to(common_prefix)
    result = pathlib.Path(*[".."] * len(source_path.parts))
    return result / target_path


Instruction:
Can you generate the documentation for the following Python 3 function
def walk(
    root_path: Union[str, pathlib.Path], top_down: bool = True
) -> Generator[
    Tuple[pathlib.Path, Sequence[pathlib.Path], Sequence[pathlib.Path]], None, None
]:
    """
    Walks a directory tree.

    Like :py:func:`os.walk` but yielding instances of :py:class:`pathlib.Path`
    instead of strings.

    :param root_path: foo
    :param top_down: bar
    """
    root_path = pathlib.Path(root_path)
    directory_paths, file_paths = [], []
    for path in sorted(root_path.iterdir()):
        if path.is_dir():
            directory_paths.append(path)
        else:
            file_paths.append(path)
    if top_down:
        yield root_path, directory_paths, file_paths
    for directory_path in directory_paths:
        yield from walk(directory_path, top_down=top_down)
    if not top_down:
        yield root_path, directory_paths, file_paths

Response: Walks a directory tree and yields a list of tuples.


Instruction:
Implement a Python 3 function for
writing contents to path.

Response:
def write(
    contents: str,
    path: Union[str, pathlib.Path],
    verbose: bool = False,
    logger_func=None,
) -> bool:
    """
    Writes ``contents`` to ``path``.

    Checks if ``path`` already exists and only write out new contents if the
    old contents do not match.

    Creates any intermediate missing directories.

    :param contents: the file contents to write
    :param path: the path to write to
    :param verbose: whether to print output
    """
    print_func = logger_func or print
    path = pathlib.Path(path)
    if path.exists():
        with path.open("r") as file_pointer:
            old_contents = file_pointer.read()
        if old_contents == contents:
            if verbose:
                print_func("preserved {}".format(path))
            return False
        else:
            with path.open("w") as file_pointer:
                file_pointer.write(contents)
            if verbose:
                print_func("rewrote {}".format(path))
            return True
    elif not path.exists():
        if not path.parent.exists():
            path.parent.mkdir(parents=True)
        with path.open("w") as file_pointer:
            file_pointer.write(contents)
        if verbose:
            print_func("wrote {}".format(path))
    return True


Instruction:
Can you generate a brief explanation for the following Python 3 code
def pretty_ref(obj: Any) -> str:
    try:
        return obj.__module__ + ':' + obj.__qualname__
    except AttributeError:
        return pretty_ref(type(obj)) + '(...)'

Response: Pretty object reference using module. path : qual. name format


Instruction:
Implement a function in Python 3 to
display repo github path

Response:
def remote(ctx):
    """Display repo github path
    """
    with command():
        m = RepoManager(ctx.obj['agile'])
        click.echo(m.github_repo().repo_path)


Instruction:
How would you implement a function in Python 3 that
gets the graph - order of the nodes in the tree.

Response:
def graph_order(self):
        """
        Get graph-order tuple for node.

        ::

            >>> from uqbar.containers import UniqueTreeContainer, UniqueTreeNode
            >>> root_container = UniqueTreeContainer(name="root")
            >>> outer_container = UniqueTreeContainer(name="outer")
            >>> inner_container = UniqueTreeContainer(name="inner")
            >>> node_a = UniqueTreeNode(name="a")
            >>> node_b = UniqueTreeNode(name="b")
            >>> node_c = UniqueTreeNode(name="c")
            >>> node_d = UniqueTreeNode(name="d")
            >>> root_container.extend([node_a, outer_container])
            >>> outer_container.extend([inner_container, node_d])
            >>> inner_container.extend([node_b, node_c])

        ::

            >>> for node in root_container.depth_first():
            ...     print(node.name, node.graph_order)
            ...
            a (0,)
            outer (1,)
            inner (1, 0)
            b (1, 0, 0)
            c (1, 0, 1)
            d (1, 1)

        """
        parentage = tuple(reversed(self.parentage))
        graph_order = []
        for i in range(len(parentage) - 1):
            parent, child = parentage[i : i + 2]
            graph_order.append(parent.index(child))
        return tuple(graph_order)


Instruction:
How would you implement a function in Python 3 that
sends a command to the Asterisk Manager and wait for response.

Response:
def sendQuery(self, cmd, multilines=False):
        """ Send command, wait for response (single or multi lines), test for errors and return the returned code.

        :param cmd: command to send
        :param multilines: True - multiline response, False - single line response.
        :return: command return value.
        """
        self.logger.debug("sendQuery(%s)", cmd)
        if not self.is_connected():
            raise socket.error("sendQuery on a disconnected socket")

        if multilines:
            replies = self.__sendQueryReplies(cmd)
            for reply in replies:
                if reply.startswith(XenaSocket.reply_errors):
                    raise XenaCommandException('sendQuery({}) reply({})'.format(cmd, replies))
            self.logger.debug("sendQuery(%s) -- Begin", cmd)
            for l in replies:
                self.logger.debug("%s", l.strip())
            self.logger.debug("sendQuery(%s) -- End", cmd)
            return replies
        else:
            reply = self.__sendQueryReply(cmd)
            if reply.startswith(XenaSocket.reply_errors):
                raise XenaCommandException('sendQuery({}) reply({})'.format(cmd, reply))
            self.logger.debug('sendQuery(%s) reply(%s)', cmd, reply)
            return reply


Instruction:
Explain what the following Python 3 code does
def sendQueryVerify(self, cmd):
        cmd = cmd.strip()
        self.logger.debug("sendQueryVerify(%s)", cmd)
        if not self.is_connected():
            raise socket.error("sendQueryVerify on a disconnected socket")

        resp = self.__sendQueryReply(cmd)
        if resp != self.reply_ok:
            raise XenaCommandException('Command {} Fail Expected {} Actual {}'.format(cmd, self.reply_ok, resp))
        self.logger.debug("SendQueryVerify(%s) Succeed", cmd)

Response: Send command without return value wait for completion verify success.


Instruction:
Can you implement a function in Python 3 that
scans all SHIELDHIT12A config files to find external files used and return them.

Response:
def find_external_files(self, run_input_dir):
        """
        Scan all SHIELDHIT12A config files to find external files used and return them.
        Also change paths in config files to match convention that all resources are
        symlinked in job_xxxx/symlink
        """
        beam_file, geo_file, mat_file, _ = self.input_files

        # check for external files in BEAM input file
        external_beam_files = self._parse_beam_file(beam_file, run_input_dir)
        if external_beam_files:
            logger.info("External files from BEAM file: {0}".format(external_beam_files))
        else:
            logger.debug("No external files from BEAM file")

        # check for external files in MAT input file
        icru_numbers = self._parse_mat_file(mat_file)
        if icru_numbers:
            logger.info("External files from MAT file: {0}".format(icru_numbers))
        else:
            logger.debug("No external files from MAT file")
        # if ICRU+LOADEX pairs were found - get file names for external material files
        icru_files = []
        if icru_numbers:
            icru_files = self._decrypt_icru_files(icru_numbers)

        # check for external files in GEO input file
        geo_files = self._parse_geo_file(geo_file, run_input_dir)
        if geo_files:
            logger.info("External files from GEO file: {0}".format(geo_files))
        else:
            logger.debug("No external files from GEO file")

        external_files = external_beam_files + icru_files + geo_files
        return [os.path.join(self.input_path, e) for e in external_files]


Instruction:
Given the following Python 3 function, write the documentation
def _parse_beam_file(self, file_path, run_input_dir):
        external_files = []
        paths_to_replace = []
        with open(file_path, 'r') as beam_f:
            for line in beam_f.readlines():
                split_line = line.split()
                # line length checking to prevent IndexError
                if len(split_line) > 2 and split_line[0] == "USEBMOD":
                    logger.debug("Found reference to external file in BEAM file: {0} {1}".format(
                                 split_line[0], split_line[2]))
                    external_files.append(split_line[2])
                    paths_to_replace.append(split_line[2])
                elif len(split_line) > 1 and split_line[0] == "USECBEAM":
                    logger.debug("Found reference to external file in BEAM file: {0} {1}".format(
                                 split_line[0], split_line[1]))
                    external_files.append(split_line[1])
                    paths_to_replace.append(split_line[1])
        if paths_to_replace:
            run_dir_config_file = os.path.join(run_input_dir, os.path.split(file_path)[-1])
            logger.debug("Calling rewrite_paths method on file: {0}".format(run_dir_config_file))
            self._rewrite_paths_in_file(run_dir_config_file, paths_to_replace)
        return external_files

Response: Scan SH12A BEAM file for references to external files and return them


Instruction:
Here you have a function in Python 3, explain what it does
def _parse_geo_file(self, file_path, run_input_dir):
        external_files = []
        paths_to_replace = []
        with open(file_path, 'r') as geo_f:
            for line in geo_f.readlines():
                split_line = line.split()
                if len(split_line) > 0 and not line.startswith("*"):
                    base_path = os.path.join(self.input_path, split_line[0])
                    if os.path.isfile(base_path + '.hed'):
                        logger.debug("Found ctx + hed files: {0}".format(base_path))
                        external_files.append(base_path + '.hed')
                        # try to find ctx file
                        if os.path.isfile(base_path + '.ctx'):
                            external_files.append(base_path + '.ctx')
                        elif os.path.isfile(base_path + '.ctx.gz'):
                            external_files.append(base_path + '.ctx.gz')
                        # replace path to match symlink location
                        paths_to_replace.append(split_line[0])
        if paths_to_replace:
            run_dir_config_file = os.path.join(run_input_dir, os.path.split(file_path)[-1])
            logger.debug("Calling rewrite_paths method on file: {0}".format(run_dir_config_file))
            self._rewrite_paths_in_file(run_dir_config_file, paths_to_replace)
        return external_files

Response: Scan SH12A GEO file for references to external files and return them


Instruction:
Write a Python 3 function that can
scan SH12A MAT file for ICRU + LOADEX pairs and return found ICRU numbers

Response:
def _parse_mat_file(self, file_path):
        """Scan SH12A MAT file for ICRU+LOADEX pairs and return found ICRU numbers"""
        mat_file_sections = self._extract_mat_sections(file_path)
        return self._analyse_mat_sections(mat_file_sections)


Instruction:
Write a Python 3 function for
analysing the MAT file sections.

Response:
def _analyse_mat_sections(sections):
        """
        Cases:
        - ICRU flag present, LOADDEDX flag missing -> data loaded from some data hardcoded in SH12A binary,
        no need to load external files
        - ICRU flag present, LOADDEDX flag present -> data loaded from external files. ICRU number read from ICRU flag,
        any number following LOADDEDX flag is ignored.
        - ICRU flag missing, LOADDEDX flag present -> data loaded from external files. ICRU number read from LOADDEDX
        - ICRU flag missing, LOADDEDX flag missing -> nothing happens
        """
        icru_numbers = []
        for section in sections:
            load_present = False
            load_value = False
            icru_value = False
            for e in section:
                split_line = e.split()
                if "LOADDEDX" in e:
                    load_present = True
                    if len(split_line) > 1:
                        load_value = split_line[1] if "!" not in split_line[1] else False  # ignore ! comments
                elif "ICRU" in e and len(split_line) > 1:
                    icru_value = split_line[1] if "!" not in split_line[1] else False  # ignore ! comments
            if load_present:  # LOADDEDX is present, so external file is required
                if icru_value:  # if ICRU value was given
                    icru_numbers.append(icru_value)
                elif load_value:  # if only LOADDEDX with values was present in section
                    icru_numbers.append(load_value)
        return icru_numbers


Instruction:
How would you explain what the following Python 3 function does
def _decrypt_icru_files(numbers):
        import json
        icru_file = resource_string(__name__, os.path.join('data', 'SH12A_ICRU_table.json'))
        ref_dict = json.loads(icru_file.decode('ascii'))
        try:
            return [ref_dict[e] for e in numbers]
        except KeyError as er:
            logger.error("There is no ICRU file for id: {0}".format(er))
            raise

Response: Find matching file names for given ICRU numbers


Instruction:
Can you tell what is the following Python 3 function doing
def _rewrite_paths_in_file(config_file, paths_to_replace):
        lines = []
        # make a copy of config
        import shutil
        shutil.copyfile(config_file, str(config_file + '_original'))
        with open(config_file) as infile:
            for line in infile:
                for old_path in paths_to_replace:
                    if old_path in line:
                        new_path = os.path.split(old_path)[-1]
                        line = line.replace(old_path, new_path)
                        logger.debug("Changed path {0} ---> {1} in file {2}".format(old_path, new_path, config_file))
                lines.append(line)
        with open(config_file, 'w') as outfile:
            for line in lines:
                outfile.write(line)

Response: Rewrite paths in config files to match convention job_xxxx


Instruction:
Can you implement a function in Python 3 that
checks if a given LDAP object exists.

Response:
def _check_exists(database: Database, table: LdapObjectClass, key: str, value: str):
    """ Check if a given LDAP object exists. """
    try:
        get_one(table, Q(**{key: value}), database=database)
        return True
    except ObjectDoesNotExist:
        return False


Instruction:
Given the following Python 3 function, write the documentation
def save_account(changes: Changeset, table: LdapObjectClass, database: Database) -> Changeset:
    d = {}
    settings = database.settings

    uid_number = changes.get_value_as_single('uidNumber')
    if uid_number is None:
        scheme = settings['NUMBER_SCHEME']
        first = settings.get('UID_FIRST', 10000)
        d['uidNumber'] = Counters.get_and_increment(
            scheme, "uidNumber", first,
            lambda n: not _check_exists(database, table, 'uidNumber', n)
        )

    changes = changes.merge(d)
    return changes

Response: Modify a changes to add an automatically generated uidNumber.


Instruction:
Write a Python 3 function that can
transform source text into a list of items.

Response:
def transform_source(text):
    '''Replaces instances of

        switch expression:
    by

        for __case in _Switch(n):

    and replaces

        case expression:

    by

        if __case(expression):

    and

        default:

    by

        if __case():
    '''
    toks = tokenize.generate_tokens(StringIO(text).readline)
    result = []
    replacing_keyword = False
    for toktype, tokvalue, _, _, _ in toks:
        if toktype == tokenize.NAME and tokvalue == 'switch':
            result.extend([
                (tokenize.NAME, 'for'),
                (tokenize.NAME, '__case'),
                (tokenize.NAME, 'in'),
                (tokenize.NAME, '_Switch'),
                (tokenize.OP, '(')
            ])
            replacing_keyword = True
        elif toktype == tokenize.NAME and (tokvalue == 'case' or tokvalue == 'default'):
            result.extend([
                (tokenize.NAME, 'if'),
                (tokenize.NAME, '__case'),
                (tokenize.OP, '(')
            ])
            replacing_keyword = True
        elif replacing_keyword and tokvalue == ':':
            result.extend([
                (tokenize.OP, ')'),
                (tokenize.OP, ':')
            ])
            replacing_keyword = False
        else:
            result.append((toktype, tokvalue))
    return tokenize.untokenize(result)


Instruction:
Here you have a function in Python 3, explain what it does
def search(self, base, scope, filterstr='(objectClass=*)',
               attrlist=None, limit=None) -> Generator[Tuple[str, dict], None, None]:
        """
        Search for entries in LDAP database.
        """

        _debug("search", base, scope, filterstr, attrlist, limit)

        # first results
        if attrlist is None:
            attrlist = ldap3.ALL_ATTRIBUTES
        elif isinstance(attrlist, set):
            attrlist = list(attrlist)

        def first_results(obj):
            _debug("---> searching ldap", limit)
            obj.search(
                base, filterstr, scope, attributes=attrlist, paged_size=limit)
            return obj.response

        # get the 1st result
        result_list = self._do_with_retry(first_results)

        # Loop over list of search results
        for result_item in result_list:
            # skip searchResRef for now
            if result_item['type'] != "searchResEntry":
                continue
            dn = result_item['dn']
            attributes = result_item['raw_attributes']
            # did we already retrieve this from cache?
            _debug("---> got ldap result", dn)
            _debug("---> yielding", result_item)
            yield (dn, attributes)

        # we are finished - return results, eat cake
        _debug("---> done")
        return

Response: Search for entries in LDAP database.


Instruction:
Can you create a Python 3 function that
renames a dn in the ldap database ; see ldap module. doesn t return a result if transactions enabled.

Response:
def rename(self, dn: str, new_rdn: str, new_base_dn: Optional[str] = None) -> None:
        """
        rename a dn in the ldap database; see ldap module. doesn't return a
        result if transactions enabled.
        """
        raise NotImplementedError()


Instruction:
Here you have a function in Python 3, explain what it does
def prepare_env(org):

    # Add services
    key_service = org.service(type='builtin:cobalt_secure_store', name='Keystore')
    wf_service = org.service(type='builtin:workflow_service', name='Workflow', parameters='{}')

    # Add services to environment
    env = org.environment(name='default')
    env.clean()
    env.add_service(key_service)
    env.add_service(wf_service)
    env.add_policy(
        {"action": "provisionVms",
         "parameter": "publicKeyId",
         "value": key_service.regenerate()['id']})

    # Add cloud provider account
    access = {
      "provider": "aws-ec2",
      "usedEnvironments": [],
      "ec2SecurityGroup": "default",
      "providerCopy": "aws-ec2",
      "name": "test-provider",
      "jcloudsIdentity": KEY,
      "jcloudsCredential": SECRET_KEY,
      "jcloudsRegions": "us-east-1"
    }
    prov = org.provider(access)
    env.add_provider(prov)
    return org.organizationId

Response: Prepares the environment to be used by the user


Instruction:
Can you write a function in Python 3 where it
starts the base command line interface

Response:
def start(ctx, debug, version, config):
    """Commands for devops operations"""
    ctx.obj = {}
    ctx.DEBUG = debug
    if os.path.isfile(config):
        with open(config) as fp:
            agile = json.load(fp)
    else:
        agile = {}
    ctx.obj['agile'] = agile
    if version:
        click.echo(__version__)
        ctx.exit(0)
    if not ctx.invoked_subcommand:
        click.echo(ctx.get_help())


Instruction:
How would you explain what the following Python 3 function does
def duplicate(obj, value=None, field=None, duplicate_order=None):

    """
    Duplicate all related objects of obj setting
    field to value. If one of the duplicate
    objects has an FK to another duplicate object
    update that as well. Return the duplicate copy
    of obj.
    duplicate_order is a list of models which specify how
    the duplicate objects are saved. For complex objects
    this can matter. Check to save if objects are being
    saved correctly and if not just pass in related objects
    in the order that they should be saved.
    """
    using = router.db_for_write(obj._meta.model)
    collector = CloneCollector(using=using)
    collector.collect([obj])
    collector.sort()
    related_models = list(collector.data.keys())
    data_snapshot = {}
    for key in collector.data.keys():
        data_snapshot.update({
            key: dict(zip(
                [item.pk for item in collector.data[key]], [item for item in collector.data[key]]))
        })
    root_obj = None

    # Sometimes it's good enough just to save in reverse deletion order.
    if duplicate_order is None:
        duplicate_order = reversed(related_models)

    for model in duplicate_order:
        # Find all FKs on model that point to a related_model.
        fks = []
        for f in model._meta.fields:
            if isinstance(f, ForeignKey) and f.rel.to in related_models:
                fks.append(f)
        # Replace each `sub_obj` with a duplicate.
        if model not in collector.data:
            continue
        sub_objects = collector.data[model]
        for obj in sub_objects:
            for fk in fks:
                fk_value = getattr(obj, "%s_id" % fk.name)
                # If this FK has been duplicated then point to the duplicate.
                fk_rel_to = data_snapshot[fk.rel.to]
                if fk_value in fk_rel_to:
                    dupe_obj = fk_rel_to[fk_value]
                    setattr(obj, fk.name, dupe_obj)
            # Duplicate the object and save it.
            obj.id = None
            if field is not None:
                setattr(obj, field, value)
            obj.save()
            if root_obj is None:
                root_obj = obj
    return root_obj

Response: Duplicate all related objects of obj setting the value field to value.


Instruction:
Explain what the following Python 3 code does
def getPayloadStruct(self, attributes, objType):
        payload = {self.payloadObj: attributes,
                   objType + "_class":
                       {self.payloadObj: attributes}}
        return payload

Response:
Function getPayloadStruct
        Get the payload structure to do a creation or modification of the data object


Instruction:
Write a Python 3 script to
enter transaction management for a running thread.

Response:
def enter_transaction_management(using=None):
    """
    Enters transaction management for a running thread. It must be balanced
    with the appropriate leave_transaction_management call, since the actual
    state is managed as a stack.

    The state and dirty flag are carried over from the surrounding block or
    from the settings, if there is no surrounding block (dirty is always false
    when no current block is running).
    """
    if using is None:
        for using in tldap.backend.connections:
            connection = tldap.backend.connections[using]
            connection.enter_transaction_management()
        return
    connection = tldap.backend.connections[using]
    connection.enter_transaction_management()


Instruction:
Write a Python 3 function that can
leave transaction management for a running thread.

Response:
def leave_transaction_management(using=None):
    """
    Leaves transaction management for a running thread. A dirty flag is carried
    over to the surrounding block, as a commit will commit all changes, even
    those from outside. (Commits are on connection level.)
    """
    if using is None:
        for using in tldap.backend.connections:
            connection = tldap.backend.connections[using]
            connection.leave_transaction_management()
        return
    connection = tldap.backend.connections[using]
    connection.leave_transaction_management()


Instruction:
Write a Python 3 script for
returning True if the current transaction requires a commit for changes to happen.

Response:
def is_dirty(using=None):
    """
    Returns True if the current transaction requires a commit for changes to
    happen.
    """
    if using is None:
        dirty = False
        for using in tldap.backend.connections:
            connection = tldap.backend.connections[using]
            if connection.is_dirty():
                dirty = True
        return dirty
    connection = tldap.backend.connections[using]
    return connection.is_dirty()


Instruction:
Can you tell what is the following Python 3 function doing
def is_managed(using=None):
    if using is None:
        managed = False
        for using in tldap.backend.connections:
            connection = tldap.backend.connections[using]
            if connection.is_managed():
                managed = True
        return managed
    connection = tldap.backend.connections[using]
    return connection.is_managed()

Response: Checks whether the transaction manager is in manual or auto state.


Instruction:
How would you explain what the following Python 3 function does
def commit(using=None):
    if using is None:
        for using in tldap.backend.connections:
            connection = tldap.backend.connections[using]
            connection.commit()
        return
    connection = tldap.backend.connections[using]
    connection.commit()

Response: Commits the current state of the current language.


Instruction:
Explain what the following Python 3 code does
def rollback(using=None):
    if using is None:
        for using in tldap.backend.connections:
            connection = tldap.backend.connections[using]
            connection.rollback()
        return
    connection = tldap.backend.connections[using]
    connection.rollback()

Response: This function does the rollback itself resets the dirty flag.


Instruction:
Can you create a Python 3 function that
returns a function that can be used to create a Transaction object.

Response:
def _transaction_func(entering, exiting, using):
    """
    Takes 3 things, an entering function (what to do to start this block of
    transaction management), an exiting function (what to do to end it, on both
    success and failure, and using which can be: None, indiciating transaction
    should occur on all defined servers, or a callable, indicating that using
    is None and to return the function already wrapped.

    Returns either a Transaction objects, which is both a decorator and a
    context manager, or a wrapped function, if using is a callable.
    """
    # Note that although the first argument is *called* `using`, it
    # may actually be a function; @autocommit and @autocommit('foo')
    # are both allowed forms.
    if callable(using):
        return Transaction(entering, exiting, None)(using)
    return Transaction(entering, exiting, using)


Instruction:
Explain what the following Python 3 code does
def commit_on_success(using=None):
    def entering(using):
        enter_transaction_management(using=using)

    def exiting(exc_value, using):
        try:
            if exc_value is not None:
                if is_dirty(using=using):
                    rollback(using=using)
            else:
                commit(using=using)
        finally:
            leave_transaction_management(using=using)

    return _transaction_func(entering, exiting, using)

Response: A decorator that activates commit on response.


Instruction:
Create a Python 3 function for
yielding all checks for all functions in the database.

Response:
def run(self) -> Generator[Tuple[int, int, str, type], None, None]:
        """
        Yields:
            tuple (line_number: int, offset: int, text: str, check: type)
        """
        if is_test_file(self.filename):
            self.load()
            for func in self.all_funcs():
                try:
                    for error in func.check_all():
                        yield (error.line_number, error.offset, error.text, Checker)
                except ValidationError as error:
                    yield error.to_flake8(Checker)


Instruction:
Create a Python 3 function to
reload glitter URL patterns if page URLs change.

Response:
def process_request(self, request):
        """
        Reloads glitter URL patterns if page URLs change.

        Avoids having to restart the server to recreate the glitter URLs being used by Django.
        """
        global _urlconf_pages

        page_list = list(
            Page.objects.exclude(glitter_app_name='').values_list('id', 'url').order_by('id')
        )

        with _urlconf_lock:
            if page_list != _urlconf_pages:
                glitter_urls = 'glitter.urls'
                if glitter_urls in sys.modules:
                    importlib.reload(sys.modules[glitter_urls])
                _urlconf_pages = page_list


Instruction:
Can you generate the documentation for the following Python 3 function
def run(self):
        self._logger.info('runner started: %s', self)
        try:
            with self._lock:
                assert not self.running.is_set() and self._stopped.is_set(), 'cannot re-run: %s' % self
                self.running.set()
                self._stopped.clear()
            self._run()
        except Exception:
            self._logger.exception('runner aborted: %s', self)
            raise
        else:
            self._logger.info('runner stopped: %s', self)
        finally:
            with self._lock:
                self.running.clear()
                self._stopped.set()

Response: Execute all current and future payloads and execute any orphaned payloads until stop is called.


Instruction:
How would you explain what the following Python 3 function does
def stop(self):
        if not self.running.wait(0.2):
            return
        self._logger.debug('runner disabled: %s', self)
        with self._lock:
            self.running.clear()
        self._stopped.wait()

Response: Stop execution of all current and future payloads


Instruction:
Can you tell what is the following Python 3 function doing
def delimit_words(string: str) -> Generator[str, None, None]:
    # TODO: Reimplement this
    wordlike_characters = ("<", ">", "!")
    current_word = ""
    for i, character in enumerate(string):
        if (
            not character.isalpha()
            and not character.isdigit()
            and character not in wordlike_characters
        ):
            if current_word:
                yield current_word
                current_word = ""
        elif not current_word:
            current_word += character
        elif character.isupper():
            if current_word[-1].isupper():
                current_word += character
            else:
                yield current_word
                current_word = character
        elif character.islower():
            if current_word[-1].isalpha():
                current_word += character
            else:
                yield current_word
                current_word = character
        elif character.isdigit():
            if current_word[-1].isdigit() or current_word[-1].isupper():
                current_word += character
            else:
                yield current_word
                current_word = character
        elif character in wordlike_characters:
            if current_word[-1] in wordlike_characters:
                current_word += character
            else:
                yield current_word
                current_word = character
    if current_word:
        yield current_word

Response: Yields a list of strings that are separated by the words in the given string.


Instruction:
Can you implement a function in Python 3 that
normalizes whitespace and dedents.

Response:
def normalize(string: str) -> str:
    """
    Normalizes whitespace.

    Strips leading and trailing blank lines, dedents, and removes trailing
    whitespace from the result.
    """
    string = string.replace("\t", "    ")
    lines = string.split("\n")
    while lines and (not lines[0] or lines[0].isspace()):
        lines.pop(0)
    while lines and (not lines[-1] or lines[-1].isspace()):
        lines.pop()
    for i, line in enumerate(lines):
        lines[i] = line.rstrip()
    string = "\n".join(lines)
    string = textwrap.dedent(string)
    return string


Instruction:
Can you generate the documentation for the following Python 3 function
def to_dash_case(string: str) -> str:
    string = unidecode.unidecode(string)
    words = (_.lower() for _ in delimit_words(string))
    string = "-".join(words)
    return string

Response: Convert a string to dash - delimited words.


Instruction:
Write a Python 3 script for
returning a list of all fixers found in the lib2to3 library

Response:
def get_lib2to3_fixers():
    '''returns a list of all fixers found in the lib2to3 library'''
    fixers = []
    fixer_dirname = fixer_dir.__path__[0]
    for name in sorted(os.listdir(fixer_dirname)):
        if name.startswith("fix_") and name.endswith(".py"):
            fixers.append("lib2to3.fixes." + name[:-3])
    return fixers


Instruction:
How would you explain what the following Python 3 function does
def get_single_fixer(fixname):
    '''return a single fixer found in the lib2to3 library'''
    fixer_dirname = fixer_dir.__path__[0]
    for name in sorted(os.listdir(fixer_dirname)):
        if (name.startswith("fix_") and name.endswith(".py")
            and fixname == name[4:-3]):
            return "lib2to3.fixes." + name[:-3]

Response: return a single fixer found in the lib2to3 library


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

        # ensure value is valid
        self.validate(value)

        assert isinstance(value, list)
        value = list(value)
        for i, v in enumerate(value):
            value[i] = self.value_to_db(v)

        # return result
        assert isinstance(value, list)
        return value

Response: Returns field s single value prepared for saving into a database.


Instruction:
Explain what the following Python 3 code does
def to_python(self, value):
        assert isinstance(value, list)

        # convert every value in list
        value = list(value)
        for i, v in enumerate(value):
            value[i] = self.value_to_python(v)

        # return result
        return value

Response: Converts the input value into the expected Python data type raising a ValidationException if the data can t be converted.


Instruction:
Can you generate the documentation for the following Python 3 function
def validate(self, value):
        # check object type
        if not isinstance(value, list):
            raise tldap.exceptions.ValidationError(
                "is not a list and max_instances is %s" %
                self._max_instances)
        # check maximum instances
        if (self._max_instances is not None and
                len(value) > self._max_instances):
            raise tldap.exceptions.ValidationError(
                "exceeds max_instances of %d" %
                self._max_instances)
        # check this required value is given
        if self._required:
            if len(value) == 0:
                raise tldap.exceptions.ValidationError(
                    "is required")
        # validate the value
        for i, v in enumerate(value):
            self.value_validate(v)

Response: Validates value and throws ValidationError. Subclasses should override this to provide validation logic.


Instruction:
Write a Python 3 script to
convert the value s type and run validation.

Response:
def clean(self, value):
        """
        Convert the value's type and run validation. Validation errors from
        to_python and validate are propagated. The correct value is returned if
        no error is raised.
        """
        value = self.to_python(value)
        self.validate(value)
        return value


Instruction:
Can you generate the documentation for the following Python 3 function
def value_to_db(self, value):
        if isinstance(value, six.string_types):
            value = value.encode("utf_8")
        return value

Response: Returns field s single value prepared for saving into a database.


Instruction:
Implement a function in Python 3 to
convert the input single value into the expected Python data type raising django. core. exceptions. ValidationError if the data can t be converted.

Response:
def value_to_python(self, value):
        """
        Converts the input single value into the expected Python data type,
        raising django.core.exceptions.ValidationError if the data can't be
        converted.  Returns the converted value. Subclasses should override
        this.
        """
        if not isinstance(value, bytes):
            raise tldap.exceptions.ValidationError("should be a bytes")
        value = value.decode("utf_8")
        return value


Instruction:
How would you explain what the following Python 3 function does
def value_validate(self, value):
        if not isinstance(value, six.string_types):
            raise tldap.exceptions.ValidationError("should be a string")

Response: Validates value and throws ValidationError.


Instruction:
Can you tell what is the following Python 3 function doing
def value_to_python(self, value):
        if not isinstance(value, bytes):
            raise tldap.exceptions.ValidationError("should be bytes")
        if value is None:
            return value
        try:
            return int(value)
        except (TypeError, ValueError):
            raise tldap.exceptions.ValidationError("is invalid integer")

Response: Converts the input single value into the expected Python data type raising django. core. exceptions. ValidationError if the data can t be converted.


Instruction:
Can you generate the documentation for the following Python 3 function
def value_to_db(self, value):
        assert isinstance(value, six.integer_types)
        return str(value).encode("utf_8")

Response: Returns field s single value prepared for saving into a database.


Instruction:
Given the following Python 3 function, write the documentation
def value_validate(self, value):
        if not isinstance(value, six.integer_types):
            raise tldap.exceptions.ValidationError("should be a integer")

        try:
            return str(value)
        except (TypeError, ValueError):
            raise tldap.exceptions.ValidationError("is invalid integer")

Response: Validate that the input value is a valid Python data type.


Instruction:
Create a Python 3 function for
converting the input single value into the expected Python data type raising django. core. exceptions. ValidationError if the data can t be converted.

Response:
def value_to_python(self, value):
        """
        Converts the input single value into the expected Python data type,
        raising django.core.exceptions.ValidationError if the data can't be
        converted.  Returns the converted value. Subclasses should override
        this.
        """
        if not isinstance(value, bytes):
            raise tldap.exceptions.ValidationError("should be a bytes")

        try:
            value = int(value)
        except (TypeError, ValueError):
            raise tldap.exceptions.ValidationError("is invalid integer")

        try:
            value = datetime.date.fromtimestamp(value * 24 * 60 * 60)
        except OverflowError:
            raise tldap.exceptions.ValidationError("is too big a date")

        return value


Instruction:
Make a summary of the following Python 3 code
def value_to_db(self, value):
        assert isinstance(value, datetime.date)
        assert not isinstance(value, datetime.datetime)

        try:
            value = value - datetime.date(year=1970, month=1, day=1)
        except OverflowError:
            raise tldap.exceptions.ValidationError("is too big a date")

        return str(value.days).encode("utf_8")

Response: Returns field s single value prepared for saving into a database.


Instruction:
Create a Python 3 function for
validating that the input value is of the expected Python data type.

Response:
def value_validate(self, value):
        """
        Converts the input single value into the expected Python data type,
        raising django.core.exceptions.ValidationError if the data can't be
        converted.  Returns the converted value. Subclasses should override
        this.
        """
        if not isinstance(value, datetime.date):
            raise tldap.exceptions.ValidationError("is invalid date")
        # a datetime is also a date but they are not compatable
        if isinstance(value, datetime.datetime):
            raise tldap.exceptions.ValidationError("should be a date, not a datetime")


Instruction:
Here you have a function in Python 3, explain what it does
def value_to_db(self, value):
        assert isinstance(value, datetime.datetime)

        try:
            value = value - datetime.datetime(1970, 1, 1)
        except OverflowError:
            raise tldap.exceptions.ValidationError("is too big a date")

        value = value.seconds + value.days * 24 * 3600
        value = str(value).encode("utf_8")

        return value

Response: Returns field s single value prepared for saving into a database.


Instruction:
Can you write a function in Python 3 where it
validates that the input value is of the expected Python data type.

Response:
def value_validate(self, value):
        """
        Converts the input single value into the expected Python data type,
        raising django.core.exceptions.ValidationError if the data can't be
        converted.  Returns the converted value. Subclasses should override
        this.
        """
        if not isinstance(value, datetime.datetime):
            raise tldap.exceptions.ValidationError("is invalid date time")


Instruction:
Explain what the following Python 3 code does
def value_to_python(self, value):
        if not isinstance(value, bytes):
            raise tldap.exceptions.ValidationError("should be a bytes")

        length = len(value) - 8
        if length % 4 != 0:
            raise tldap.exceptions.ValidationError("Invalid sid")

        length = length // 4

        array = struct.unpack('<bbbbbbbb' + 'I' * length, value)

        if array[1] != length:
            raise tldap.exceptions.ValidationError("Invalid sid")

        if array[2:7] != (0, 0, 0, 0, 0):
            raise tldap.exceptions.ValidationError("Invalid sid")

        array = ("S", ) + array[0:1] + array[7:]
        return "-".join([str(i) for i in array])

Response: Converts the input single value into the expected Python data type raising django. core. exceptions. ValidationError


Instruction:
How would you explain what the following Python 3 function does
def value_to_db(self, value):

        assert isinstance(value, str)

        array = value.split("-")
        length = len(array) - 3

        assert length >= 0
        assert array[0] == 'S'

        array = array[1:2] + [length, 0, 0, 0, 0, 0] + array[2:]
        array = [int(i) for i in array]

        return struct.pack('<bbbbbbbb' + 'I' * length, *array)

Response: Returns field s single value prepared for saving into a database.


Instruction:
Make a summary of the following Python 3 code
def value_validate(self, value):
        if not isinstance(value, str):
            raise tldap.exceptions.ValidationError("Invalid sid")

        array = value.split("-")
        length = len(array) - 3

        if length < 1:
            raise tldap.exceptions.ValidationError("Invalid sid")

        if array.pop(0) != "S":
            raise tldap.exceptions.ValidationError("Invalid sid")

        try:
            [int(i) for i in array]
        except TypeError:
            raise tldap.exceptions.ValidationError("Invalid sid")

Response: Validates the input single value into the expected Python data type raising django. core. exceptions. ValidationError if the data can t be converted.


Instruction:
Here you have a function in Python 3, explain what it does
def get(self, id):
        id = self.as_id(id)
        url = '%s/%s' % (self, id)
        response = self.http.get(url, auth=self.auth)
        response.raise_for_status()
        return response.json()

Response: Get data for this component


Instruction:
Implement a function in Python 3 to
create a new component

Response:
def create(self, data):
        """Create a new component
        """
        response = self.http.post(str(self), json=data, auth=self.auth)
        response.raise_for_status()
        return response.json()


Instruction:
Can you implement a function in Python 3 that
updates a component s related resource attributes.

Response:
def update(self, id, data):
        """Update a component
        """
        id = self.as_id(id)
        response = self.http.patch(
            '%s/%s' % (self, id), json=data, auth=self.auth
        )
        response.raise_for_status()
        return response.json()


Instruction:
How would you implement a function in Python 3 that
deletes a component by id

Response:
def delete(self, id):
        """Delete a component by id
        """
        id = self.as_id(id)
        response = self.http.delete(
            '%s/%s' % (self.api_url, id),
            auth=self.auth)
        response.raise_for_status()


Instruction:
Can you write a function in Python 3 where it
gets a list of this github component.

Response:
def get_list(self, url=None, callback=None, limit=100, **data):
        """Get a list of this github component
        :param url: full url
        :param Comp: a :class:`.Component` class
        :param callback: Optional callback
        :param limit: Optional number of items to retrieve
        :param data: additional query data
        :return: a list of ``Comp`` objects with data
        """
        url = url or str(self)
        data = dict(((k, v) for k, v in data.items() if v))
        all_data = []
        if limit:
            data['per_page'] = min(limit, 100)
        while url:
            response = self.http.get(url, params=data, auth=self.auth)
            response.raise_for_status()
            result = response.json()
            n = m = len(result)
            if callback:
                result = callback(result)
                m = len(result)
            all_data.extend(result)
            if limit and len(all_data) > limit:
                all_data = all_data[:limit]
                break
            elif m == n:
                data = None
                next = response.links.get('next', {})
                url = next.get('url')
            else:
                break
        return all_data


Instruction:
Here you have a function in Python 3, explain what it does
def comments(self, issue):
        commit = self.as_id(issue)
        return self.get_list(url='%s/%s/comments' % (self, commit))

Response: Return all comments for this issue


Instruction:
How would you implement a function in Python 3 that
returns a boolean if the user in the request has edit permission for the object.

Response:
def has_edit_permission(self, request, obj=None, version=None):
        """
        Returns a boolean if the user in the request has edit permission for the object.

        Can also be passed a version object to check if the user has permission to edit a version
        of the object (if they own it).
        """
        # Has the edit permission for this object type
        permission_name = '{}.edit_{}'.format(self.opts.app_label, self.opts.model_name)
        has_permission = request.user.has_perm(permission_name)

        if obj is not None and has_permission is False:
            has_permission = request.user.has_perm(permission_name, obj=obj)

        if has_permission and version is not None:
            # Version must not be saved, and must belong to this user
            if version.version_number or version.owner != request.user:
                has_permission = False

        return has_permission


Instruction:
Given the following Python 3 function, write the documentation
def has_publish_permission(self, request, obj=None):
        permission_name = '{}.publish_{}'.format(self.opts.app_label, self.opts.model_name)
        has_permission = request.user.has_perm(permission_name)

        if obj is not None and has_permission is False:
            has_permission = request.user.has_perm(permission_name, obj=obj)

        return has_permission

Response: Returns a boolean if the user in the request has publish permission for the object.


Instruction:
Explain what the following Python 3 code does
def semantic_version(tag):
    try:
        version = list(map(int, tag.split('.')))
        assert len(version) == 3
        return tuple(version)
    except Exception as exc:
        raise CommandError(
            'Could not parse "%s", please use '
            'MAJOR.MINOR.PATCH' % tag
        ) from exc

Response: Get a valid semantic version for a tag.


Instruction:
Create a Python 3 function for
functioning load Load the object data into the internal cache

Response:
def load(self, data):
        """ Function load
        Store the object data
        """
        self.clear()
        self.update(data)
        self.enhance()


Instruction:
Create a Python 3 function for
functioning enhance Enhance the object with new item or enhanced items

Response:
def enhance(self):
        """ Function enhance
        Enhance the object with new item or enhanced items
        """
        if self.objName in ['hosts', 'hostgroups',
                            'puppet_classes']:
            from foreman.itemSmartClassParameter\
                import ItemSmartClassParameter
            self.update({'smart_class_parameters':
                        SubDict(self.api, self.objName,
                                self.payloadObj, self.key,
                                ItemSmartClassParameter)})


Instruction:
How would you explain what the following Python 3 function does
def reload(self):
        self.load(self.api.get(self.objName, self.key))

Response:
Function reload
        Sync the full object with the current object s metadata


Instruction:
Implement a Python 3 function for
functioning getParamGet Return a dict of parameters or a parameter value

Response:
def getParam(self, name=None):
        """ Function getParam
        Return a dict of parameters or a parameter value

        @param key: The parameter name
        @return RETURN: dict of parameters or a parameter value
        """
        if 'parameters' in self.keys():
            l = {x['name']: x['value'] for x in self['parameters'].values()}
            if name:
                if name in l.keys():
                    return l[name]
                else:
                    return False
            else:
                return l


Instruction:
How would you implement a function in Python 3 that
functions checkAndCreateClasses Check and add puppet class to the list of classes and return True if the classes are already present in the list.

Response:
def checkAndCreateClasses(self, classes):
        """ Function checkAndCreateClasses
        Check and add puppet class

        @param classes: The classes ids list
        @return RETURN: boolean
        """
        actual_classes = self['puppetclasses'].keys()
        for i in classes:
            if i not in actual_classes:
                self['puppetclasses'].append(i)
        self.reload()
        return set(classes).issubset(set((self['puppetclasses'].keys())))


Instruction:
Write a Python 3 function that can
function checkAndCreateParams Check and add global parameters to the parameters dict

Response:
def checkAndCreateParams(self, params):
        """ Function checkAndCreateParams
        Check and add global parameters

        @param key: The parameter name
        @param params: The params dict
        @return RETURN: boolean
        """
        actual_params = self['parameters'].keys()
        for k, v in params.items():
            if k not in actual_params:
                self['parameters'].append({"name": k, "value": v})
        self.reload()
        return self['parameters'].keys() == params.keys()


Instruction:
Make a summary of the following Python 3 code
def version_dict(version):
    match = version_re.match(str(version) or '')
    letters = 'alpha pre'.split()
    numbers = 'major minor1 minor2 minor3 alpha_ver pre_ver'.split()
    if match:
        d = match.groupdict()
        for letter in letters:
            d[letter] = d[letter] if d[letter] else None
        for num in numbers:
            if d[num] == '*':
                d[num] = 99
            else:
                d[num] = int(d[num]) if d[num] else None
    else:
        d = dict((k, None) for k in numbers)
        d.update((k, None) for k in letters)
    return d

Response: Turn a version string into a dict with major / minor info.


Instruction:
Write a Python 3 function that can
get the diff between two records lists in this order.

Response:
def get_diff(source, dest):
    """Get the diff between two records list in this order:
        - to_create
        - to_delete
    """
    # First build a dict from the lists, with the ID as the key.
    source_dict = {record['id']: record for record in source}
    dest_dict = {record['id']: record for record in dest}

    source_keys = set(source_dict.keys())
    dest_keys = set(dest_dict.keys())
    to_create = source_keys - dest_keys
    to_delete = dest_keys - source_keys
    to_update = set()

    to_check = source_keys - to_create - to_delete

    for record_id in to_check:
        # Make sure to remove properties that are part of kinto
        # records and not amo records.
        # Here we will compare the record properties ignoring:
        # ID, last_modified and enabled.
        new = canonical_json(source_dict[record_id])
        old = canonical_json(dest_dict[record_id])
        if new != old:
            to_update.add(record_id)

    return ([source_dict[k] for k in to_create],
            [source_dict[k] for k in to_update],
            [dest_dict[k] for k in to_delete])


Instruction:
Here you have a function in Python 3, explain what it does
def object_version_choices(obj):
    choices = BLANK_CHOICE_DASH + [(PublishAction.UNPUBLISH_CHOICE, 'Unpublish current version')]

    # When creating a new object in the Django admin - obj will be None
    if obj is not None:
        saved_versions = Version.objects.filter(
            content_type=ContentType.objects.get_for_model(obj),
            object_id=obj.pk,
        ).exclude(
            version_number=None,
        )

        for version in saved_versions:
            choices.append((version.version_number, version))

    return choices

Response: Return a list of form choices for versions of this object which can be published.


Instruction:
How would you code a function in Python 3 to
load a manifest file and apply template values

Response:
def manifest(self, values, *paths, filename: str = None) -> Dict:
        """Load a manifest file and apply template values
        """
        filename = filename or self.filename(*paths)
        with open(filename, 'r') as fp:
            template = Template(fp.read())
        return yaml.load(template.render(values))


Instruction:
Implement a function in Python 3 to
set the header of the current packet.

Response:
def set_packet_headers(self, headers):
        """ Set packet header.

        The method will try to set ps_headerprotocol to inform the Xena GUI and tester how to interpret the packet
        header byte sequence specified with PS_PACKETHEADER.
        This is mainly for information purposes, and the stream will transmit the packet header bytes even if no
        protocol segments are specified.
        If the method fails to set some segment it will log a warning and skip setup.

        :param headers: current packet headers
        :type headers: pypacker.layer12.ethernet.Ethernet
        """

        bin_headers = '0x' + binascii.hexlify(headers.bin()).decode('utf-8')
        self.set_attributes(ps_packetheader=bin_headers)

        body_handler = headers
        ps_headerprotocol = []
        while body_handler:
            segment = pypacker_2_xena.get(str(body_handler).split('(')[0].lower(), None)
            if not segment:
                self.logger.warning('pypacker header {} not in conversion list'.format(segment))
                return
            ps_headerprotocol.append(segment)
            if type(body_handler) is Ethernet and body_handler.vlan:
                ps_headerprotocol.append('vlan')
            body_handler = body_handler.body_handler
        self.set_attributes(ps_headerprotocol=' '.join(ps_headerprotocol))


Instruction:
How would you explain what the following Python 3 function does
def add_modifier(self, m_type=XenaModifierType.standard, **kwargs):

        if m_type == XenaModifierType.standard:
            modifier = XenaModifier(self, index='{}/{}'.format(self.index, len(self.modifiers)))
        else:
            modifier = XenaXModifier(self, index='{}/{}'.format(self.index, len(self.xmodifiers)))
        modifier._create()
        modifier.get()
        modifier.set(**kwargs)
        return modifier

Response: Add a new modifier to the list.


Instruction:
How would you implement a function in Python 3 that
removes a modifier from the current object.

Response:
def remove_modifier(self, index, m_type=XenaModifierType.standard):
        """ Remove modifier.

        :param m_type: modifier type - standard or extended.
        :param index: index of modifier to remove.
        """

        if m_type == XenaModifierType.standard:
            current_modifiers = OrderedDict(self.modifiers)
            del current_modifiers[index]

            self.set_attributes(ps_modifiercount=0)
            self.del_objects_by_type('modifier')

        else:
            current_modifiers = OrderedDict(self.xmodifiers)
            del current_modifiers[index]

            self.set_attributes(ps_modifierextcount=0)
            self.del_objects_by_type('xmodifier')

        for modifier in current_modifiers.values():
            self.add_modifier(m_type,
                              mask=modifier.mask, action=modifier.action, repeat=modifier.repeat,
                              min_val=modifier.min_val, step=modifier.step, max_val=modifier.max_val)


Instruction:
Write a Python 3 function that can
return a dictionary of standard modifiers.

Response:
def modifiers(self):
        """
        :return: dictionary {index: object} of standard modifiers.
        """
        if not self.get_objects_by_type('modifier'):
            for index in range(int(self.get_attribute('ps_modifiercount'))):
                XenaModifier(self, index='{}/{}'.format(self.index, index)).get()
        return {s.id: s for s in self.get_objects_by_type('modifier')}


Instruction:
Write a Python 3 function that can
return dictionary of extended modifiers.

Response:
def xmodifiers(self):
        """
        :return: dictionary {index: object} of extended modifiers.
        """
        if not self.get_objects_by_type('xmodifier'):
            try:
                for index in range(int(self.get_attribute('ps_modifierextcount'))):
                    XenaXModifier(self, index='{}/{}'.format(self.index, index)).get()
            except Exception as _:
                pass
        return {s.id: s for s in self.get_objects_by_type('xmodifier')}


Instruction:
Create a Python 3 function for
returning a DAGONS cosmology for the current WMAP7 + BAO + H_0 mean from a base - set of WMAP7 + BAO + H_0 mean from a base - set of WMAP7 + BAO + H_0 mean from a base - set of WMAP7 + H_0 mean from a base - set of WMAP7 + H_0 mean from a base - set of WMAP7 + H_0.

Response:
def DRAGONS(flat=False, extras=True):
    """DRAGONS cosmology assumes WMAP7 + BAO + H_0 mean from
    Komatsu et al. (2011) ApJS 192 18K (arxiv:1001.4538v1)

    Parameters
    ----------

    flat: boolean

      If True, sets omega_lambda_0 = 1 - omega_M_0 to ensure omega_k_0
      = 0 exactly. Also sets omega_k_0 = 0 explicitly.

    extras: boolean

      If True, sets neutrino number N_nu = 0, neutrino density
      omega_n_0 = 0.0, Helium mass fraction Y_He = 0.24.

      """
    omega_c_0 = 0.2292
    omega_b_0 = 0.0458
    cosmo = {'omega_b_0': omega_b_0,
             'omega_M_0': omega_b_0 + omega_c_0,
             'omega_lambda_0': 0.725,
             'h': 0.702,
             'n': 0.963,
             'sigma_8': 0.816,
             'tau': 0.088,
             'z_reion': 10.6,
             't_0': 13.76,
             }
    if flat:
        cosmo['omega_lambda_0'] = 1 - cosmo['omega_M_0']
        cosmo['omega_k_0'] = 0.0
    if extras:
        add_extras(cosmo)
    return cosmo


Instruction:
Create a Python 3 function to
return a base - level Planck - 2015 cosmological parameters table for Ade et al.

Response:
def Planck_2015(flat=False, extras=True):
    """Planck 2015 XII: Cosmological parameters Table 4
    column Planck TT, TE, EE + lowP + lensing + ext
    from Ade et al. (2015) A&A in press (arxiv:1502.01589v1)

    Parameters
    ----------

    flat: boolean

      If True, sets omega_lambda_0 = 1 - omega_M_0 to ensure omega_k_0
      = 0 exactly. Also sets omega_k_0 = 0 explicitly.

    extras: boolean

      If True, sets neutrino number N_nu = 0, neutrino density
      omega_n_0 = 0.0, Helium mass fraction Y_He = 0.24.

      """
    omega_b_0 = 0.02230/(0.6774**2)
    cosmo = {'omega_b_0': omega_b_0,
             'omega_M_0': 0.3089,
             'omega_lambda_0': 0.6911,
             'h': 0.6774,
             'n': 0.9667,
             'sigma_8': 0.8159,
             'tau': 0.066,
             'z_reion': 8.8,
             't_0': 13.799,
             }
    if flat:
        cosmo['omega_lambda_0'] = 1 - cosmo['omega_M_0']
        cosmo['omega_k_0'] = 0.0
    if extras:
        add_extras(cosmo)
    return cosmo


Instruction:
Make a summary of the following Python 3 code
def add(self, dn: str, mod_list: dict) -> None:

        return self._do_with_retry(lambda obj: obj.add_s(dn, mod_list))

Response: Add a DN to the LDAP database ; See ldap module. Doesn t return a result


Instruction:
Can you generate the documentation for the following Python 3 function
def modify(self, dn: str, mod_list: dict) -> None:

        return self._do_with_retry(lambda obj: obj.modify_s(dn, mod_list))

Response: Modify a DN in the LDAP database.


Instruction:
Given the following Python 3 function, write the documentation
def rename(self, dn: str, new_rdn: str, new_base_dn: Optional[str] = None) -> None:

        return self._do_with_retry(
            lambda obj: obj.rename_s(dn, new_rdn, new_base_dn))

Response:
Rename a dn in the ldap database ; see ldap module. doesn t return a
            result if transactions enabled.


Instruction:
How would you explain what the following Python 3 function does
def get_column_name(self, column_name):
        name = pretty_name(column_name)
        if column_name in self._meta.columns:
            column_cls = self._meta.columns[column_name]
            if column_cls.verbose_name:
                name = column_cls.verbose_name
        return name

Response: Get a column name from META api.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def version(self):
        branches = self.branches()
        if self.info['branch'] == branches.sandbox:
            try:
                return self.software_version()
            except Exception as exc:
                raise utils.CommandError(
                    'Could not obtain repo version, do you have a makefile '
                    'with version entry?\n%s' % exc
                )
        else:
            branch = self.info['branch'].lower()
            branch = re.sub('[^a-z0-9_-]+', '-', branch)
            return f"{branch}-{self.info['head']['id'][:8]}"

Response: Returns the software version of the current repository


Instruction:
Can you write a function in Python 3 where it
validates version by checking if it is a valid semantic version and its value is higher than latest github tag

Response:
def validate_version(self, prefix='v'):
        """Validate version by checking if it is a valid semantic version
        and its value is higher than latest github tag
        """
        version = self.software_version()
        repo = self.github_repo()
        repo.releases.validate_tag(version, prefix)
        return version


Instruction:
Implement a Python 3 function for
checking if build should be skipped

Response:
def skip_build(self):
        """Check if build should be skipped
        """
        skip_msg = self.config.get('skip', '[ci skip]')
        return (
            os.environ.get('CODEBUILD_BUILD_SUCCEEDING') == '0' or
            self.info['current_tag'] or
            skip_msg in self.info['head']['message']
        )


Instruction:
How would you code a function in Python 3 to
send a message to third party applications

Response:
def message(self, msg):
        """Send a message to third party applications
        """
        for broker in self.message_brokers:
            try:
                broker(msg)
            except Exception as exc:
                utils.error(exc)


Instruction:
Implement a function in Python 3 to
return all the kinto records for this bucket and collection.

Response:
def get_kinto_records(kinto_client, bucket, collection, permissions,
                      config=None):
    """Return all the kinto records for this bucket/collection."""
    # Create bucket if needed
    try:
        kinto_client.create_bucket(id=bucket, if_not_exists=True)
    except KintoException as e:
        if hasattr(e, 'response') and e.response.status_code == 403:
            # The user cannot create buckets on this server, ignore the creation.
            pass

    try:
        kinto_client.create_collection(id=collection, bucket=bucket,
                                       permissions=permissions, if_not_exists=True)
    except KintoException as e:
        if hasattr(e, 'response') and e.response.status_code == 403:
            # The user cannot create collection on this bucket, ignore the creation.
            pass

    return kinto_client.get_records(bucket=bucket, collection=collection)


Instruction:
Implement a Python 3 function for
adding a new chassis to the set of chassis list

Response:
def add_chassis(self, chassis):
        """
        :param ip: chassis object
        """

        self.chassis_list[chassis] = XenaSocket(self.logger, chassis.ip, chassis.port)
        self.chassis_list[chassis].connect()
        KeepAliveThread(self.chassis_list[chassis]).start()
        self.send_command(chassis, 'c_logon', '"{}"'.format(chassis.password))
        self.send_command(chassis, 'c_owner', '"{}"'.format(chassis.owner))


Instruction:
Can you tell what is the following Python 3 function doing
def send_command(self, obj, command, *arguments):
        index_command = obj._build_index_command(command, *arguments)
        self.chassis_list[obj.chassis].sendQueryVerify(index_command)

Response: Send command and do not parse output.


Instruction:
Can you generate the documentation for the following Python 3 function
def send_command_return(self, obj, command, *arguments):
        index_command = obj._build_index_command(command, *arguments)
        return obj._extract_return(command, self.chassis_list[obj.chassis].sendQuery(index_command))

Response: Send command and wait for single line output.


Instruction:
Write a Python 3 function for
sending command and wait for multiple lines output.

Response:
def send_command_return_multilines(self, obj, command, *arguments):
        """ Send command and wait for multiple lines output. """
        index_command = obj._build_index_command(command, *arguments)
        return self.chassis_list[obj.chassis].sendQuery(index_command, True)


Instruction:
Can you generate the documentation for the following Python 3 function
def get_attribute(self, obj, attribute):
        raw_return = self.send_command_return(obj, attribute, '?')
        if len(raw_return) > 2 and raw_return[0] == '"' and raw_return[-1] == '"':
            return raw_return[1:-1]
        return raw_return

Response: Returns single object attribute.


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

        attributes = {}
        for info_config_command in obj.info_config_commands:
            index_commands_values = self.send_command_return_multilines(obj, info_config_command, '?')
            # poor implementation...
            li = obj._get_index_len()
            ci = obj._get_command_len()
            for index_command_value in index_commands_values:
                command = index_command_value.split()[ci].lower()
                if len(index_command_value.split()) > li + 1:
                    value = ' '.join(index_command_value.split()[li+1:]).replace('"', '')
                else:
                    value = ''
                attributes[command] = value
        return attributes

Response: Get all object s attributes.


Instruction:
How would you code a function in Python 3 to
set attributes of the object.

Response:
def set_attributes(self, obj, **attributes):
        """ Set attributes.

        :param obj: requested object.
        :param attributes: dictionary of {attribute: value} to set
        """
        for attribute, value in attributes.items():
            self.send_command(obj, attribute, value)


Instruction:
Can you tell what is the following Python 3 function doing
def get_stats(self, obj, stat_name):
        return [int(v) for v in self.get_attribute(obj, stat_name).split()]

Response: Send CLI command that returns list of integer counters.


Instruction:
Can you generate the documentation for the following Python 3 function
def depth_first(self, top_down=True):
        for child in tuple(self):
            if top_down:
                yield child
            if isinstance(child, UniqueTreeContainer):
                yield from child.depth_first(top_down=top_down)
            if not top_down:
                yield child

Response: Iterate depth - first.


Instruction:
Create a Python 3 function to
reserve port. XenaManager-2G -> Reserve/Relinquish Port. :param force: True - take forcefully, False - fail if port is reserved by other user

Response:
def reserve(self, force=False):
        """ Reserve port.

        XenaManager-2G -> Reserve/Relinquish Port.

        :param force: True - take forcefully, False - fail if port is reserved by other user
        """

        p_reservation = self.get_attribute('p_reservation')
        if p_reservation == 'RESERVED_BY_YOU':
            return
        elif p_reservation == 'RESERVED_BY_OTHER' and not force:
            raise TgnError('Port {} reserved by {}'.format(self, self.get_attribute('p_reservedby')))
        self.relinquish()
        self.send_command('p_reservation', 'reserve')


Instruction:
How would you code a function in Python 3 to
load the configuration file from xpc file.

Response:
def load_config(self, config_file_name):
        """ Load configuration file from xpc file.

        :param config_file_name: full path to the configuration file.
        """

        with open(config_file_name) as f:
            commands = f.read().splitlines()

        for command in commands:
            if not command.startswith(';'):
                try:
                    self.send_command(command)
                except XenaCommandException as e:
                    self.logger.warning(str(e))


Instruction:
Write a Python 3 function for
saving the current configuration file to xpc file.

Response:
def save_config(self, config_file_name):
        """ Save configuration file to xpc file.

        :param config_file_name: full path to the configuration file.
        """

        with open(config_file_name, 'w+') as f:
            f.write('P_RESET\n')
            for line in self.send_command_return_multilines('p_fullconfig', '?'):
                f.write(line.split(' ', 1)[1].lstrip())


Instruction:
Can you tell what is the following Python 3 function doing
def add_stream(self, name=None, tpld_id=None, state=XenaStreamState.enabled):

        stream = XenaStream(parent=self, index='{}/{}'.format(self.index, len(self.streams)), name=name)
        stream._create()
        tpld_id = tpld_id if tpld_id else XenaStream.next_tpld_id
        stream.set_attributes(ps_comment='"{}"'.format(stream.name), ps_tpldid=tpld_id)
        XenaStream.next_tpld_id = max(XenaStream.next_tpld_id + 1, tpld_id + 1)
        stream.set_state(state)
        return stream

Response: Add a new stream to the current entry.


Instruction:
Write a Python 3 function that can
read the stats for all the elements in the stats_captions attribute of the Sea XenaPort object.

Response:
def read_port_stats(self):
        """
        :return: dictionary {group name {stat name: value}}.
            Sea XenaPort.stats_captions.
        """

        stats_with_captions = OrderedDict()
        for stat_name in self.stats_captions.keys():
            stats_with_captions[stat_name] = self.read_stat(self.stats_captions[stat_name], stat_name)
        return stats_with_captions


Instruction:
Can you write a function in Python 3 where it
reads the stream stats for all the streams in this instance

Response:
def read_stream_stats(self):
        """
        :return: dictionary {stream index {stat name: value}}.
            Sea XenaStream.stats_captions.
        """
        stream_stats = OrderedDict()
        for stream in self.streams.values():
            stream_stats[stream] = stream.read_stats()
        return stream_stats


Instruction:
How would you code a function in Python 3 to
read the tpld stats for all the items in the tplds.

Response:
def read_tpld_stats(self):
        """
        :return: dictionary {tpld index {group name {stat name: value}}}.
            Sea XenaTpld.stats_captions.
        """
        payloads_stats = OrderedDict()
        for tpld in self.tplds.values():
            payloads_stats[tpld] = tpld.read_stats()
        return payloads_stats


Instruction:
Implement a Python 3 function for
returning a dictionary of all streams.

Response:
def streams(self):
        """
        :return: dictionary {id: object} of all streams.
        :rtype: dict of (int, xenamanager.xena_stream.XenaStream)
        """

        if not self.get_objects_by_type('stream'):
            tpld_ids = []
            for index in self.get_attribute('ps_indices').split():
                stream = XenaStream(parent=self, index='{}/{}'.format(self.index, index))
                tpld_ids.append(stream.get_attribute('ps_tpldid'))
            if tpld_ids:
                XenaStream.next_tpld_id = max([XenaStream.next_tpld_id] + [int(t) for t in tpld_ids]) + 1
        return {s.id: s for s in self.get_objects_by_type('stream')}


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

        # As TPLDs are dynamic we must re-read them each time from the port.
        self.parent.del_objects_by_type('tpld')
        for tpld in self.get_attribute('pr_tplds').split():
            XenaTpld(parent=self, index='{}/{}'.format(self.index, tpld))
        return {t.id: t for t in self.get_objects_by_type('tpld')}

Response: Return a dictionary of all current tplds.


Instruction:
Make a summary of the following Python 3 code
def read_stats(self):

        stats_with_captions = OrderedDict()
        for stat_name in self.stats_captions.keys():
            stats_with_captions[stat_name] = self.read_stat(self.stats_captions[stat_name], stat_name)
        return stats_with_captions

Response: Reads the stats from the Sea XenaTpld and returns a dictionary of the stats.


Instruction:
Write a Python 3 function for
getting captured packets from chassis.

Response:
def get_packets(self, from_index=0, to_index=None, cap_type=XenaCaptureBufferType.text,
                    file_name=None, tshark=None):
        """ Get captured packets from chassis.

        :param from_index: index of first packet to read.
        :param to_index: index of last packet to read. If None - read all packets.
        :param cap_type: returned capture format. If pcap then file name and tshark must be provided.
        :param file_name: if specified, capture will be saved in file.
        :param tshark: tshark object for pcap type only.
        :type: xenamanager.xena_tshark.Tshark
        :return: list of requested packets, None for pcap type.
        """

        to_index = to_index if to_index else len(self.packets)

        raw_packets = []
        for index in range(from_index, to_index):
            raw_packets.append(self.packets[index].get_attribute('pc_packet').split('0x')[1])

        if cap_type == XenaCaptureBufferType.raw:
            self._save_captue(file_name, raw_packets)
            return raw_packets

        text_packets = []
        for raw_packet in raw_packets:
            text_packet = ''
            for c, b in zip(range(len(raw_packet)), raw_packet):
                if c % 32 == 0:
                    text_packet += '\n{:06x} '.format(int(c / 2))
                elif c % 2 == 0:
                    text_packet += ' '
                text_packet += b
            text_packets.append(text_packet)

        if cap_type == XenaCaptureBufferType.text:
            self._save_captue(file_name, text_packets)
            return text_packets

        temp_file_name = file_name + '_'
        self._save_captue(temp_file_name, text_packets)
        tshark.text_to_pcap(temp_file_name, file_name)
        os.remove(temp_file_name)


Instruction:
How would you code a function in Python 3 to
return a dictionary of all packets.

Response:
def packets(self):
        """
        :return: dictionary {id: object} of all packets.
        :rtype: dict of (int, xenamanager.xena_port.XenaCapturePacket)
        """

        if not self.get_object_by_type('cappacket'):
            for index in range(0, self.read_stats()['packets']):
                XenaCapturePacket(parent=self, index='{}/{}'.format(self.index, index))
        return {p.id: p for p in self.get_objects_by_type('cappacket')}


Instruction:
Make a summary of the following Python 3 code
def add(self, rule: ControlRule = None, *, supply: float):
        if supply in self._thresholds:
            raise ValueError('rule for threshold %s re-defined' % supply)
        if rule is not None:
            self.rules.append((supply, rule))
            self._thresholds.add(supply)
            return rule
        else:
            return partial(self.add, supply=supply)

Response: Register a new rule above a given supply threshold.


Instruction:
How would you explain what the following Python 3 function does
def s(self, *args, **kwargs) -> Partial[Stepwise]:
        return Partial(Stepwise, self.base, *self.rules, *args, **kwargs)

Response: Create an unbound prototype of this class partially applying arguments


Instruction:
Make a summary of the following Python 3 code
def revisionId(self):
        log.warning("'RevisionId' requested, ensure that you are don't need 'id'")
        revision_id = self.json()['revisionId']
        assert revision_id == self.id, "RevisionId differs id-{}!=revisionId-{}".format(self.id, revision_id)
        return revision_id

Response: Get the revisionId of this object.


Instruction:
Given the following Python 3 function, write the documentation
def changeset(python_data: LdapObject, d: dict) -> Changeset:
    table: LdapObjectClass = type(python_data)
    fields = table.get_fields()
    changes = Changeset(fields, src=python_data, d=d)
    return changes

Response: Generate changes object for ldap object.


Instruction:
How would you explain what the following Python 3 function does
def _db_to_python(db_data: dict, table: LdapObjectClass, dn: str) -> LdapObject:
    fields = table.get_fields()

    python_data = table({
        name: field.to_python(db_data[name])
        for name, field in fields.items()
        if field.db_field
    })
    python_data = python_data.merge({
        'dn': dn,
    })
    return python_data

Response: Convert a DbDate object to a LdapObject.


Instruction:
Here you have a function in Python 3, explain what it does
def _python_to_mod_new(changes: Changeset) -> Dict[str, List[List[bytes]]]:
    table: LdapObjectClass = type(changes.src)
    fields = table.get_fields()

    result: Dict[str, List[List[bytes]]] = {}

    for name, field in fields.items():
        if field.db_field:
            try:
                value = field.to_db(changes.get_value_as_list(name))
                if len(value) > 0:
                    result[name] = value
            except ValidationError as e:
                raise ValidationError(f"{name}: {e}.")

    return result

Response: Convert a LdapChanges object to a modlist for add operation.


Instruction:
How would you code a function in Python 3 to
convert a LdapChanges object to a modlist for a modify operation.

Response:
def _python_to_mod_modify(changes: Changeset) -> Dict[str, List[Tuple[Operation, List[bytes]]]]:
    """ Convert a LdapChanges object to a modlist for a modify operation. """
    table: LdapObjectClass = type(changes.src)
    changes = changes.changes

    result: Dict[str, List[Tuple[Operation, List[bytes]]]] = {}
    for key, l in changes.items():
        field = _get_field_by_name(table, key)

        if field.db_field:
            try:
                new_list = [
                    (operation, field.to_db(value))
                    for operation, value in l
                ]
                result[key] = new_list
            except ValidationError as e:
                raise ValidationError(f"{key}: {e}.")

    return result


Instruction:
Given the following Python 3 function, write the documentation
def search(table: LdapObjectClass, query: Optional[Q] = None,
           database: Optional[Database] = None, base_dn: Optional[str] = None) -> Iterator[LdapObject]:
    """ Search for a object of given type in the database. """
    fields = table.get_fields()
    db_fields = {
        name: field
        for name, field in fields.items()
        if field.db_field
    }

    database = get_database(database)
    connection = database.connection

    search_options = table.get_search_options(database)

    iterator = tldap.query.search(
        connection=connection,
        query=query,
        fields=db_fields,
        base_dn=base_dn or search_options.base_dn,
        object_classes=search_options.object_class,
        pk=search_options.pk_field,
    )

    for dn, data in iterator:
        python_data = _db_to_python(data, table, dn)
        python_data = table.on_load(python_data, database)
        yield python_data

Response: Search for an object of given type in the database.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_one(table: LdapObjectClass, query: Optional[Q] = None,
            database: Optional[Database] = None, base_dn: Optional[str] = None) -> LdapObject:
    """ Get exactly one result from the database or fail. """
    results = search(table, query, database, base_dn)

    try:
        result = next(results)
    except StopIteration:
        raise ObjectDoesNotExist(f"Cannot find result for {query}.")

    try:
        next(results)
        raise MultipleObjectsReturned(f"Found multiple results for {query}.")
    except StopIteration:
        pass

    return result

Response: Get exactly one result from the database or fail.


Instruction:
Here you have a function in Python 3, explain what it does
def preload(python_data: LdapObject, database: Optional[Database] = None) -> LdapObject:

    changes = {}

    # Load objects within lists.
    def preload_item(value: Any) -> Any:
        if isinstance(value, NotLoaded):
            return value.load(database)
        else:
            return value

    for name in python_data.keys():
        value_list = python_data.get_as_list(name)

        # Check for errors.
        if isinstance(value_list, NotLoadedObject):
            raise RuntimeError(f"{name}: Unexpected NotLoadedObject outside list.")

        elif isinstance(value_list, NotLoadedList):
            value_list = value_list.load(database)

        else:
            if any(isinstance(v, NotLoadedList) for v in value_list):
                raise RuntimeError(f"{name}: Unexpected NotLoadedList in list.")
            elif any(isinstance(v, NotLoadedObject) for v in value_list):
                value_list = [preload_item(value) for value in value_list]
            else:
                value_list = None

        if value_list is not None:
            changes[name] = value_list

    return python_data.merge(changes)

Response: Preload all NotLoaded fields in LdapObject.


Instruction:
Make a summary of the following Python 3 code
def insert(python_data: LdapObject, database: Optional[Database] = None) -> LdapObject:
    assert isinstance(python_data, LdapObject)

    table: LdapObjectClass = type(python_data)

    # ADD NEW ENTRY
    empty_data = table()
    changes = changeset(empty_data, python_data.to_dict())

    return save(changes, database)

Response: Insert a new python_data object in the database.