Instruction:
Here you have a function in Python 3, explain what it does
def _decorate_namespace_function(bases: List[type], namespace: MutableMapping[str, Any], key: str) -> None:
    # pylint: disable=too-many-branches
    # pylint: disable=too-many-locals

    value = namespace[key]
    assert inspect.isfunction(value) or isinstance(value, (staticmethod, classmethod))

    # Determine the function to be decorated
    if inspect.isfunction(value):
        func = value
    elif isinstance(value, (staticmethod, classmethod)):
        func = value.__func__
    else:
        raise NotImplementedError("Unexpected value for a function: {}".format(value))

    # Collect preconditions and postconditions of the function
    preconditions = []  # type: List[List[Contract]]
    snapshots = []  # type: List[Snapshot]
    postconditions = []  # type: List[Contract]

    contract_checker = icontract._checkers.find_checker(func=func)
    if contract_checker is not None:
        preconditions = contract_checker.__preconditions__  # type: ignore
        snapshots = contract_checker.__postcondition_snapshots__  # type: ignore
        postconditions = contract_checker.__postconditions__  # type: ignore

    # Collect the preconditions and postconditions from bases.
    #
    # Preconditions and postconditions of __init__ of base classes are deliberately ignored (and not collapsed) since
    # initialization is an operation specific to the concrete class and does not relate to the class hierarchy.
    if key not in ['__init__']:
        base_preconditions = []  # type: List[List[Contract]]
        base_snapshots = []  # type: List[Snapshot]
        base_postconditions = []  # type: List[Contract]

        bases_have_func = False
        for base in bases:
            if hasattr(base, key):
                bases_have_func = True

                # Check if there is a checker function in the base class
                base_func = getattr(base, key)
                base_contract_checker = icontract._checkers.find_checker(func=base_func)

                # Ignore functions which don't have preconditions or postconditions
                if base_contract_checker is not None:
                    base_preconditions.extend(base_contract_checker.__preconditions__)  # type: ignore
                    base_snapshots.extend(base_contract_checker.__postcondition_snapshots__)  # type: ignore
                    base_postconditions.extend(base_contract_checker.__postconditions__)  # type: ignore

        # Collapse preconditions and postconditions from the bases with the the function's own ones
        preconditions = _collapse_preconditions(
            base_preconditions=base_preconditions,
            bases_have_func=bases_have_func,
            preconditions=preconditions,
            func=func)

        snapshots = _collapse_snapshots(base_snapshots=base_snapshots, snapshots=snapshots)

        postconditions = _collapse_postconditions(
            base_postconditions=base_postconditions, postconditions=postconditions)

    if preconditions or postconditions:
        if contract_checker is None:
            contract_checker = icontract._checkers.decorate_with_checker(func=func)

            # Replace the function with the function decorated with contract checks
            if inspect.isfunction(value):
                namespace[key] = contract_checker
            elif isinstance(value, staticmethod):
                namespace[key] = staticmethod(contract_checker)

            elif isinstance(value, classmethod):
                namespace[key] = classmethod(contract_checker)

            else:
                raise NotImplementedError("Unexpected value for a function: {}".format(value))

        # Override the preconditions and postconditions
        contract_checker.__preconditions__ = preconditions  # type: ignore
        contract_checker.__postcondition_snapshots__ = snapshots  # type: ignore
        contract_checker.__postconditions__ = postconditions

Response: Decorate the function at the key in the base class and the base class.


Instruction:
Can you write a function in Python 3 where it
decorates a property in a namespace by iterating over all base classes and checking if they are present.

Response:
def _decorate_namespace_property(bases: List[type], namespace: MutableMapping[str, Any], key: str) -> None:
    """Collect contracts for all getters/setters/deleters corresponding to ``key`` and decorate them."""
    # pylint: disable=too-many-locals
    # pylint: disable=too-many-branches
    # pylint: disable=too-many-statements

    value = namespace[key]
    assert isinstance(value, property)

    fget = value.fget  # type: Optional[Callable[..., Any]]
    fset = value.fset  # type: Optional[Callable[..., Any]]
    fdel = value.fdel  # type: Optional[Callable[..., Any]]

    for func in [value.fget, value.fset, value.fdel]:
        func = cast(Callable[..., Any], func)

        if func is None:
            continue

        # Collect the preconditions and postconditions from bases
        base_preconditions = []  # type: List[List[Contract]]
        base_snapshots = []  # type: List[Snapshot]
        base_postconditions = []  # type: List[Contract]

        bases_have_func = False
        for base in bases:
            if hasattr(base, key):
                base_property = getattr(base, key)
                assert isinstance(base_property, property), \
                    "Expected base {} to have {} as property, but got: {}".format(base, key, base_property)

                if func == value.fget:
                    base_func = getattr(base, key).fget
                elif func == value.fset:
                    base_func = getattr(base, key).fset
                elif func == value.fdel:
                    base_func = getattr(base, key).fdel
                else:
                    raise NotImplementedError("Unhandled case: func neither value.fget, value.fset nor value.fdel")

                if base_func is None:
                    continue

                bases_have_func = True

                # Check if there is a checker function in the base class
                base_contract_checker = icontract._checkers.find_checker(func=base_func)

                # Ignore functions which don't have preconditions or postconditions
                if base_contract_checker is not None:
                    base_preconditions.extend(base_contract_checker.__preconditions__)  # type: ignore
                    base_snapshots.extend(base_contract_checker.__postcondition_snapshots__)  # type: ignore
                    base_postconditions.extend(base_contract_checker.__postconditions__)  # type: ignore

        # Add preconditions and postconditions of the function
        preconditions = []  # type: List[List[Contract]]
        snapshots = []  # type: List[Snapshot]
        postconditions = []  # type: List[Contract]

        contract_checker = icontract._checkers.find_checker(func=func)
        if contract_checker is not None:
            preconditions = contract_checker.__preconditions__  # type: ignore
            snapshots = contract_checker.__postcondition_snapshots__
            postconditions = contract_checker.__postconditions__  # type: ignore

        preconditions = _collapse_preconditions(
            base_preconditions=base_preconditions,
            bases_have_func=bases_have_func,
            preconditions=preconditions,
            func=func)

        snapshots = _collapse_snapshots(base_snapshots=base_snapshots, snapshots=snapshots)

        postconditions = _collapse_postconditions(
            base_postconditions=base_postconditions, postconditions=postconditions)

        if preconditions or postconditions:
            if contract_checker is None:
                contract_checker = icontract._checkers.decorate_with_checker(func=func)

                # Replace the function with the function decorated with contract checks
                if func == value.fget:
                    fget = contract_checker
                elif func == value.fset:
                    fset = contract_checker
                elif func == value.fdel:
                    fdel = contract_checker
                else:
                    raise NotImplementedError("Unhandled case: func neither fget, fset nor fdel")

            # Override the preconditions and postconditions
            contract_checker.__preconditions__ = preconditions  # type: ignore
            contract_checker.__postcondition_snapshots__ = snapshots  # type: ignore
            contract_checker.__postconditions__ = postconditions  # type: ignore

    if fget != value.fget or fset != value.fset or fdel != value.fdel:
        namespace[key] = property(fget=fget, fset=fset, fdel=fdel)


Instruction:
How would you explain what the following Python 3 function does
def _dbc_decorate_namespace(bases: List[type], namespace: MutableMapping[str, Any]) -> None:
    _collapse_invariants(bases=bases, namespace=namespace)

    for key, value in namespace.items():
        if inspect.isfunction(value) or isinstance(value, (staticmethod, classmethod)):
            _decorate_namespace_function(bases=bases, namespace=namespace, key=key)

        elif isinstance(value, property):
            _decorate_namespace_property(bases=bases, namespace=namespace, key=key)

        else:
            # Ignore the value which is neither a function nor a property
            pass

Response: Decorate all methods in the namespace.


Instruction:
Write a Python 3 function that can
check whether the value is representable in the error message on contract breach.

Response:
def _representable(value: Any) -> bool:
    """
    Check whether we want to represent the value in the error message on contract breach.

    We do not want to represent classes, methods, modules and functions.

    :param value: value related to an AST node
    :return: True if we want to represent it in the violation error
    """
    return not inspect.isclass(value) and not inspect.isfunction(value) and not inspect.ismethod(value) and not \
        inspect.ismodule(value) and not inspect.isbuiltin(value)


Instruction:
Here you have a function in Python 3, explain what it does
def inspect_decorator(lines: List[str], lineno: int, filename: str) -> DecoratorInspection:
    if lineno < 0 or lineno >= len(lines):
        raise ValueError(("Given line number {} of one of the decorator lines "
                          "is not within the range [{}, {}) of lines in {}").format(lineno, 0, len(lines), filename))

    # Go up till a line starts with a decorator
    decorator_lineno = None  # type: Optional[int]
    for i in range(lineno, -1, -1):
        if _DECORATOR_RE.match(lines[i]):
            decorator_lineno = i
            break

    if decorator_lineno is None:
        raise SyntaxError("Decorator corresponding to the line {} could not be found in file {}: {!r}".format(
            lineno + 1, filename, lines[lineno]))

    # Find the decorator end -- it's either a function definition, a class definition or another decorator
    decorator_end_lineno = None  # type: Optional[int]
    for i in range(lineno + 1, len(lines)):
        line = lines[i]

        if _DECORATOR_RE.match(line) or _DEF_CLASS_RE.match(line):
            decorator_end_lineno = i
            break

    if decorator_end_lineno is None:
        raise SyntaxError(("The next statement following the decorator corresponding to the line {} "
                           "could not be found in file {}: {!r}").format(lineno + 1, filename, lines[lineno]))

    decorator_lines = lines[decorator_lineno:decorator_end_lineno]

    # We need to dedent the decorator and add a dummy decoratee so that we can parse its text as valid source code.
    decorator_text = textwrap.dedent("".join(decorator_lines)) + "def dummy_{}(): pass".format(uuid.uuid4().hex)

    atok = asttokens.ASTTokens(decorator_text, parse=True)

    assert isinstance(atok.tree, ast.Module), "Expected the parsed decorator text to live in an AST module."

    module_node = atok.tree
    assert len(module_node.body) == 1, "Expected the module AST of the decorator text to have a single statement."
    assert isinstance(module_node.body[0], ast.FunctionDef), \
        "Expected the only statement in the AST module corresponding to the decorator text to be a function definition."

    func_def_node = module_node.body[0]

    assert len(func_def_node.decorator_list) == 1, \
        "Expected the function AST node corresponding to the decorator text to have a single decorator."

    assert isinstance(func_def_node.decorator_list[0], ast.Call), \
        "Expected the only decorator in the function definition AST node corresponding to the decorator text " \
        "to be a call node."

    call_node = func_def_node.decorator_list[0]

    return DecoratorInspection(atok=atok, node=call_node)

Response: Inspects the source code of a decorator call.


Instruction:
Can you implement a function in Python 3 that
inspects the decorator and extract the condition as lambda function.

Response:
def find_lambda_condition(decorator_inspection: DecoratorInspection) -> Optional[ConditionLambdaInspection]:
    """
    Inspect the decorator and extract the condition as lambda.

    If the condition is not given as a lambda function, return None.
    """
    call_node = decorator_inspection.node

    lambda_node = None  # type: Optional[ast.Lambda]

    if len(call_node.args) > 0:
        assert isinstance(call_node.args[0], ast.Lambda), \
            ("Expected the first argument to the decorator to be a condition as lambda AST node, "
             "but got: {}").format(type(call_node.args[0]))

        lambda_node = call_node.args[0]

    elif len(call_node.keywords) > 0:
        for keyword in call_node.keywords:
            if keyword.arg == "condition":
                assert isinstance(keyword.value, ast.Lambda), \
                    "Expected lambda node as value of the 'condition' argument to the decorator."

                lambda_node = keyword.value
                break

        assert lambda_node is not None, "Expected to find a keyword AST node with 'condition' arg, but found none"
    else:
        raise AssertionError(
            "Expected a call AST node of a decorator to have either args or keywords, but got: {}".format(
                ast.dump(call_node)))

    return ConditionLambdaInspection(atok=decorator_inspection.atok, node=lambda_node)


Instruction:
Can you tell what is the following Python 3 function doing
def repr_values(condition: Callable[..., bool], lambda_inspection: Optional[ConditionLambdaInspection],
                condition_kwargs: Mapping[str, Any], a_repr: reprlib.Repr) -> List[str]:
    # pylint: disable=too-many-locals
    """
    Represent function arguments and frame values in the error message on contract breach.

    :param condition: condition function of the contract
    :param lambda_inspection:
        inspected lambda AST node corresponding to the condition function (None if the condition was not given as a
        lambda function)
    :param condition_kwargs: condition arguments
    :param a_repr: representation instance that defines how the values are represented.
    :return: list of value representations
    """
    if _is_lambda(a_function=condition):
        assert lambda_inspection is not None, "Expected a lambda inspection when given a condition as a lambda function"
    else:
        assert lambda_inspection is None, "Expected no lambda inspection in a condition given as a non-lambda function"

    reprs = dict()  # type: MutableMapping[str, Any]

    if lambda_inspection is not None:
        # Collect the variable lookup of the condition function:
        variable_lookup = []  # type: List[Mapping[str, Any]]

        # Add condition arguments to the lookup
        variable_lookup.append(condition_kwargs)

        # Add closure to the lookup
        closure_dict = dict()  # type: Dict[str, Any]

        if condition.__closure__ is not None:  # type: ignore
            closure_cells = condition.__closure__  # type: ignore
            freevars = condition.__code__.co_freevars

            assert len(closure_cells) == len(freevars), \
                "Number of closure cells of a condition function ({}) == number of free vars ({})".format(
                    len(closure_cells), len(freevars))

            for cell, freevar in zip(closure_cells, freevars):
                closure_dict[freevar] = cell.cell_contents

        variable_lookup.append(closure_dict)

        # Add globals to the lookup
        if condition.__globals__ is not None:  # type: ignore
            variable_lookup.append(condition.__globals__)  # type: ignore

        # pylint: disable=protected-access
        recompute_visitor = icontract._recompute.Visitor(variable_lookup=variable_lookup)

        recompute_visitor.visit(node=lambda_inspection.node.body)
        recomputed_values = recompute_visitor.recomputed_values

        repr_visitor = Visitor(
            recomputed_values=recomputed_values, variable_lookup=variable_lookup, atok=lambda_inspection.atok)
        repr_visitor.visit(node=lambda_inspection.node.body)

        reprs = repr_visitor.reprs
    else:
        for key, val in condition_kwargs.items():
            if _representable(value=val):
                reprs[key] = val

    parts = []  # type: List[str]
    for key in sorted(reprs.keys()):
        parts.append('{} was {}'.format(key, a_repr.repr(reprs[key])))

    return parts

Response: Return a list of value representations of the given condition function.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def generate_message(contract: Contract, condition_kwargs: Mapping[str, Any]) -> str:
    # pylint: disable=protected-access
    parts = []  # type: List[str]

    if contract.location is not None:
        parts.append("{}:\n".format(contract.location))

    if contract.description is not None:
        parts.append("{}: ".format(contract.description))

    lambda_inspection = None  # type: Optional[ConditionLambdaInspection]
    if not _is_lambda(a_function=contract.condition):
        condition_text = contract.condition.__name__
    else:
        # We need to extract the source code corresponding to the decorator since inspect.getsource() is broken with
        # lambdas.

        # Find the line corresponding to the condition lambda
        lines, condition_lineno = inspect.findsource(contract.condition)
        filename = inspect.getsourcefile(contract.condition)

        decorator_inspection = inspect_decorator(lines=lines, lineno=condition_lineno, filename=filename)
        lambda_inspection = find_lambda_condition(decorator_inspection=decorator_inspection)

        assert lambda_inspection is not None, \
            "Expected lambda_inspection to be non-None if _is_lambda is True on: {}".format(contract.condition)

        condition_text = lambda_inspection.text

    parts.append(condition_text)

    repr_vals = repr_values(
        condition=contract.condition,
        lambda_inspection=lambda_inspection,
        condition_kwargs=condition_kwargs,
        a_repr=contract._a_repr)

    if len(repr_vals) == 0:
        # Do not append anything since no value could be represented as a string.
        # This could appear in case we have, for example, a generator expression as the return value of a lambda.
        pass

    elif len(repr_vals) == 1:
        parts.append(': ')
        parts.append(repr_vals[0])
    else:
        parts.append(':\n')
        parts.append('\n'.join(repr_vals))

    msg = "".join(parts)
    return msg

Response: Generate the message upon contract violation.


Instruction:
Explain what the following Python 3 code does
def visit_Name(self, node: ast.Name) -> None:
        if node in self._recomputed_values:
            value = self._recomputed_values[node]

            # Check if it is a non-built-in
            is_builtin = True
            for lookup in self._variable_lookup:
                if node.id in lookup:
                    is_builtin = False
                    break

            if not is_builtin and _representable(value=value):
                text = self._atok.get_text(node)
                self.reprs[text] = value

        self.generic_visit(node=node)

Response: Resolve the name from the variable look - up and the built - in lookup.


Instruction:
Here you have a function in Python 3, explain what it does
def visit_Attribute(self, node: ast.Attribute) -> None:
        if node in self._recomputed_values:
            value = self._recomputed_values[node]

            if _representable(value=value):
                text = self._atok.get_text(node)
                self.reprs[text] = value

        self.generic_visit(node=node)

Response: Represent the attribute by dumping its source code.


Instruction:
Explain what the following Python 3 code does
def visit_Call(self, node: ast.Call) -> None:
        if node in self._recomputed_values:
            value = self._recomputed_values[node]
            text = self._atok.get_text(node)

            self.reprs[text] = value

        self.generic_visit(node=node)

Response: Represent the call by dumping its source code.


Instruction:
Write a Python 3 function that can
represent the list comprehension by dumping its source code.

Response:
def visit_ListComp(self, node: ast.ListComp) -> None:
        """Represent the list comprehension by dumping its source code."""
        if node in self._recomputed_values:
            value = self._recomputed_values[node]
            text = self._atok.get_text(node)

            self.reprs[text] = value

        self.generic_visit(node=node)


Instruction:
Given the following Python 3 function, write the documentation
def visit_DictComp(self, node: ast.DictComp) -> None:
        if node in self._recomputed_values:
            value = self._recomputed_values[node]
            text = self._atok.get_text(node)

            self.reprs[text] = value

        self.generic_visit(node=node)

Response: Represent the dictionary comprehension by dumping its source code.


Instruction:
Explain what the following Python 3 code does
def _walk_decorator_stack(func: CallableT) -> Iterable['CallableT']:
    while hasattr(func, "__wrapped__"):
        yield func

        func = getattr(func, "__wrapped__")

    yield func

Response: Iterate through the stack of decorated functions until the original function is decorated.


Instruction:
Given the following Python 3 function, write the documentation
def find_checker(func: CallableT) -> Optional[CallableT]:
    contract_checker = None  # type: Optional[CallableT]
    for a_wrapper in _walk_decorator_stack(func):
        if hasattr(a_wrapper, "__preconditions__") or hasattr(a_wrapper, "__postconditions__"):
            contract_checker = a_wrapper

    return contract_checker

Response: Iterate through the decorator stack till we find the contract checker.


Instruction:
Implement a Python 3 function for
inspecting the input values received at the wrapper for the actual function call.

Response:
def _kwargs_from_call(param_names: List[str], kwdefaults: Dict[str, Any], args: Tuple[Any, ...],
                      kwargs: Dict[str, Any]) -> MutableMapping[str, Any]:
    """
    Inspect the input values received at the wrapper for the actual function call.

    :param param_names: parameter (*i.e.* argument) names of the original (decorated) function
    :param kwdefaults: default argument values of the original function
    :param args: arguments supplied to the call
    :param kwargs: keyword arguments supplied to the call
    :return: resolved arguments as they would be passed to the function
    """
    # pylint: disable=too-many-arguments
    mapping = dict()  # type: MutableMapping[str, Any]

    # Set the default argument values as condition parameters.
    for param_name, param_value in kwdefaults.items():
        mapping[param_name] = param_value

    # Override the defaults with the values actually suplied to the function.
    for i, func_arg in enumerate(args):
        mapping[param_names[i]] = func_arg

    for key, val in kwargs.items():
        mapping[key] = val

    return mapping


Instruction:
Given the following Python 3 function, write the documentation
def _assert_precondition(contract: Contract, resolved_kwargs: Mapping[str, Any]) -> None:
    # Check that all arguments to the condition function have been set.
    missing_args = [arg_name for arg_name in contract.condition_args if arg_name not in resolved_kwargs]
    if missing_args:
        raise TypeError(
            ("The argument(s) of the precondition have not been set: {}. "
             "Does the original function define them? Did you supply them in the call?").format(missing_args))

    condition_kwargs = {
        arg_name: value
        for arg_name, value in resolved_kwargs.items() if arg_name in contract.condition_arg_set
    }

    check = contract.condition(**condition_kwargs)

    if not check:
        if contract.error is not None and (inspect.ismethod(contract.error) or inspect.isfunction(contract.error)):
            assert contract.error_arg_set is not None, "Expected error_arg_set non-None if contract.error a function."
            assert contract.error_args is not None, "Expected error_args non-None if contract.error a function."

            error_kwargs = {
                arg_name: value
                for arg_name, value in resolved_kwargs.items() if arg_name in contract.error_arg_set
            }

            missing_args = [arg_name for arg_name in contract.error_args if arg_name not in resolved_kwargs]
            if missing_args:
                msg_parts = []  # type: List[str]
                if contract.location is not None:
                    msg_parts.append("{}:\n".format(contract.location))

                msg_parts.append(
                    ("The argument(s) of the precondition error have not been set: {}. "
                     "Does the original function define them? Did you supply them in the call?").format(missing_args))

                raise TypeError(''.join(msg_parts))

            raise contract.error(**error_kwargs)

        else:
            msg = icontract._represent.generate_message(contract=contract, condition_kwargs=condition_kwargs)
            if contract.error is None:
                raise ViolationError(msg)
            elif isinstance(contract.error, type):
                raise contract.error(msg)

Response: Assert that the contract holds as a precondition.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _assert_invariant(contract: Contract, instance: Any) -> None:
    if 'self' in contract.condition_arg_set:
        check = contract.condition(self=instance)
    else:
        check = contract.condition()

    if not check:
        if contract.error is not None and (inspect.ismethod(contract.error) or inspect.isfunction(contract.error)):
            assert contract.error_arg_set is not None, "Expected error_arg_set non-None if contract.error a function."
            assert contract.error_args is not None, "Expected error_args non-None if contract.error a function."

            if 'self' in contract.error_arg_set:
                raise contract.error(self=instance)
            else:
                raise contract.error()
        else:
            if 'self' in contract.condition_arg_set:
                msg = icontract._represent.generate_message(contract=contract, condition_kwargs={"self": instance})
            else:
                msg = icontract._represent.generate_message(contract=contract, condition_kwargs=dict())

            if contract.error is None:
                raise ViolationError(msg)
            elif isinstance(contract.error, type):
                raise contract.error(msg)
            else:
                raise NotImplementedError("Unhandled contract.error: {}".format(contract.error))

Response: Assert that the contract holds as a class invariant given the instance of the class.


Instruction:
Can you write a function in Python 3 where it
captures the snapshot from the keyword arguments resolved before the function call.

Response:
def _capture_snapshot(a_snapshot: Snapshot, resolved_kwargs: Mapping[str, Any]) -> Any:
    """
    Capture the snapshot from the keyword arguments resolved before the function call (including the default values).

    :param a_snapshot: snapshot to be captured
    :param resolved_kwargs: resolved keyword arguments (including the default values)
    :return: captured value
    """
    if a_snapshot.arg is not None:
        if a_snapshot.arg not in resolved_kwargs:
            raise TypeError(("The argument of the snapshot has not been set: {}. "
                             "Does the original function define it? Did you supply it in the call?").format(
                                 a_snapshot.arg))

        value = a_snapshot.capture(**{a_snapshot.arg: resolved_kwargs[a_snapshot.arg]})
    else:
        value = a_snapshot.capture()

    return value


Instruction:
Can you implement a function in Python 3 that
decorates the function with a checker that verifies the preconditions and postconditions.

Response:
def decorate_with_checker(func: CallableT) -> CallableT:
    """Decorate the function with a checker that verifies the preconditions and postconditions."""
    assert not hasattr(func, "__preconditions__"), \
        "Expected func to have no list of preconditions (there should be only a single contract checker per function)."

    assert not hasattr(func, "__postconditions__"), \
        "Expected func to have no list of postconditions (there should be only a single contract checker per function)."

    assert not hasattr(func, "__postcondition_snapshots__"), \
        "Expected func to have no list of postcondition snapshots (there should be only a single contract checker " \
        "per function)."

    sign = inspect.signature(func)
    param_names = list(sign.parameters.keys())

    # Determine the default argument values.
    kwdefaults = dict()  # type: Dict[str, Any]

    # Add to the defaults all the values that are needed by the contracts.
    for param in sign.parameters.values():
        if param.default != inspect.Parameter.empty:
            kwdefaults[param.name] = param.default

    def wrapper(*args, **kwargs):
        """Wrap func by checking the preconditions and postconditions."""
        preconditions = getattr(wrapper, "__preconditions__")  # type: List[List[Contract]]
        snapshots = getattr(wrapper, "__postcondition_snapshots__")  # type: List[Snapshot]
        postconditions = getattr(wrapper, "__postconditions__")  # type: List[Contract]

        resolved_kwargs = _kwargs_from_call(param_names=param_names, kwdefaults=kwdefaults, args=args, kwargs=kwargs)

        if postconditions:
            if 'result' in resolved_kwargs:
                raise TypeError("Unexpected argument 'result' in a function decorated with postconditions.")

            if 'OLD' in resolved_kwargs:
                raise TypeError("Unexpected argument 'OLD' in a function decorated with postconditions.")

        # Assert the preconditions in groups. This is necessary to implement "require else" logic when a class
        # weakens the preconditions of its base class.
        violation_err = None  # type: Optional[ViolationError]
        for group in preconditions:
            violation_err = None
            try:
                for contract in group:
                    _assert_precondition(contract=contract, resolved_kwargs=resolved_kwargs)
                break
            except ViolationError as err:
                violation_err = err

        if violation_err is not None:
            raise violation_err  # pylint: disable=raising-bad-type

        # Capture the snapshots
        if postconditions:
            old_as_mapping = dict()  # type: MutableMapping[str, Any]
            for snap in snapshots:
                # This assert is just a last defense.
                # Conflicting snapshot names should have been caught before, either during the decoration or
                # in the meta-class.
                assert snap.name not in old_as_mapping, "Snapshots with the conflicting name: {}"
                old_as_mapping[snap.name] = _capture_snapshot(a_snapshot=snap, resolved_kwargs=resolved_kwargs)

            resolved_kwargs['OLD'] = _Old(mapping=old_as_mapping)

        # Execute the wrapped function
        result = func(*args, **kwargs)

        if postconditions:
            resolved_kwargs['result'] = result

            # Assert the postconditions as a conjunction
            for contract in postconditions:
                _assert_postcondition(contract=contract, resolved_kwargs=resolved_kwargs)

        return result  # type: ignore

    # Copy __doc__ and other properties so that doctests can run
    functools.update_wrapper(wrapper=wrapper, wrapped=func)

    assert not hasattr(wrapper, "__preconditions__"), "Expected no preconditions set on a pristine contract checker."
    assert not hasattr(wrapper, "__postcondition_snapshots__"), \
        "Expected no postcondition snapshots set on a pristine contract checker."
    assert not hasattr(wrapper, "__postconditions__"), "Expected no postconditions set on a pristine contract checker."

    # Precondition is a list of condition groups (i.e. disjunctive normal form):
    # each group consists of AND'ed preconditions, while the groups are OR'ed.
    #
    # This is necessary in order to implement "require else" logic when a class weakens the preconditions of
    # its base class.
    setattr(wrapper, "__preconditions__", [])
    setattr(wrapper, "__postcondition_snapshots__", [])
    setattr(wrapper, "__postconditions__", [])

    return wrapper


Instruction:
Explain what the following Python 3 code does
def _find_self(param_names: List[str], args: Tuple[Any, ...], kwargs: Dict[str, Any]) -> Any:
    instance_i = param_names.index("self")
    if instance_i < len(args):
        instance = args[instance_i]
    else:
        instance = kwargs["self"]

    return instance

Response: Find the instance of self in the arguments.


Instruction:
Write a Python 3 function that can
decorate the function func with invariant checks.

Response:
def _decorate_with_invariants(func: CallableT, is_init: bool) -> CallableT:
    """
    Decorate the function ``func`` of the class ``cls`` with invariant checks.

    If the function has been already decorated with invariant checks, the function returns immediately.

    :param func: function to be wrapped
    :param is_init: True if the ``func`` is __init__
    :return: function wrapped with invariant checks
    """
    if _already_decorated_with_invariants(func=func):
        return func

    sign = inspect.signature(func)
    param_names = list(sign.parameters.keys())

    if is_init:

        def wrapper(*args, **kwargs):
            """Wrap __init__ method of a class by checking the invariants *after* the invocation."""
            result = func(*args, **kwargs)
            instance = _find_self(param_names=param_names, args=args, kwargs=kwargs)

            for contract in instance.__class__.__invariants__:
                _assert_invariant(contract=contract, instance=instance)

            return result
    else:

        def wrapper(*args, **kwargs):
            """Wrap a function of a class by checking the invariants *before* and *after* the invocation."""
            instance = _find_self(param_names=param_names, args=args, kwargs=kwargs)

            for contract in instance.__class__.__invariants__:
                _assert_invariant(contract=contract, instance=instance)

            result = func(*args, **kwargs)

            for contract in instance.__class__.__invariants__:
                _assert_invariant(contract=contract, instance=instance)

            return result

    functools.update_wrapper(wrapper=wrapper, wrapped=func)

    setattr(wrapper, "__is_invariant_check__", True)

    return wrapper


Instruction:
Here you have a function in Python 3, explain what it does
def _already_decorated_with_invariants(func: CallableT) -> bool:
    already_decorated = False
    for a_decorator in _walk_decorator_stack(func=func):
        if getattr(a_decorator, "__is_invariant_check__", False):
            already_decorated = True
            break

    return already_decorated

Response: Check if the function has already been decorated with an invariant check by going through its decorator stack.


Instruction:
Write a Python 3 function that can
decorate each of the class methods with invariant checks if not already decorated.

Response:
def add_invariant_checks(cls: type) -> None:
    """Decorate each of the class functions with invariant checks if not already decorated."""
    # Candidates for the decoration as list of (name, dir() value)
    init_name_func = None  # type: Optional[Tuple[str, Callable[..., None]]]
    names_funcs = []  # type: List[Tuple[str, Callable[..., None]]]
    names_properties = []  # type: List[Tuple[str, property]]

    # Filter out entries in the directory which are certainly not candidates for decoration.
    for name, value in [(name, getattr(cls, name)) for name in dir(cls)]:
        # We need to ignore __repr__ to prevent endless loops when generating error messages.
        # __getattribute__, __setattr__ and __delattr__ are too invasive and alter the state of the instance.
        # Hence we don't consider them "public".
        if name in ["__repr__", "__getattribute__", "__setattr__", "__delattr__"]:
            continue

        if name == "__init__":
            assert inspect.isfunction(value) or isinstance(value, _SLOT_WRAPPER_TYPE), \
                "Expected __init__ to be either a function or a slot wrapper, but got: {}".format(type(value))

            init_name_func = (name, value)
            continue

        if not inspect.isfunction(value) and not isinstance(value, _SLOT_WRAPPER_TYPE) and \
                not isinstance(value, property):
            continue

        # Ignore class methods
        if getattr(value, "__self__", None) is cls:
            continue

        # Ignore "protected"/"private" methods
        if name.startswith("_") and not (name.startswith("__") and name.endswith("__")):
            continue

        if inspect.isfunction(value) or isinstance(value, _SLOT_WRAPPER_TYPE):
            names_funcs.append((name, value))

        elif isinstance(value, property):
            names_properties.append((name, value))

        else:
            raise NotImplementedError("Unhandled directory entry of class {} for {}: {}".format(cls, name, value))

    if init_name_func:
        name, func = init_name_func
        wrapper = _decorate_with_invariants(func=func, is_init=True)
        setattr(cls, name, wrapper)

    for name, func in names_funcs:
        wrapper = _decorate_with_invariants(func=func, is_init=False)
        setattr(cls, name, wrapper)

    for name, prop in names_properties:
        new_prop = property(  # type: ignore
            fget=_decorate_with_invariants(func=prop.fget, is_init=False) if prop.fget else None,
            fset=_decorate_with_invariants(func=prop.fset, is_init=False) if prop.fset else None,
            fdel=_decorate_with_invariants(func=prop.fdel, is_init=False) if prop.fdel else None,
            doc=prop.__doc__)
        setattr(cls, name, new_prop)


Instruction:
How would you code a function in Python 3 to
execute the main routine.

Response:
def main() -> None:
    """"Execute the main routine."""
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--module", help="name of the module to import",
                        choices=[
                            "functions_100_with_no_contract",

                            "functions_100_with_1_contract",
                            "functions_100_with_5_contracts",
                            "functions_100_with_10_contracts",

                            "functions_100_with_1_disabled_contract",
                            "functions_100_with_5_disabled_contracts",
                            "functions_100_with_10_disabled_contracts",

                            "classes_100_with_no_invariant",

                            "classes_100_with_1_invariant",
                            "classes_100_with_5_invariants",
                            "classes_100_with_10_invariants",

                            "classes_100_with_1_disabled_invariant",
                            "classes_100_with_5_disabled_invariants",
                            "classes_100_with_10_disabled_invariants",
                        ],
                        required=True)
    args = parser.parse_args()

    a_module = str(args.module)
    if a_module == "functions_100_with_no_contract":
        start = time.time()
        import functions_100_with_no_contract
        print(time.time() - start)

    elif a_module == "functions_100_with_1_contract":
        start = time.time()
        import functions_100_with_1_contract
        print(time.time() - start)

    elif a_module == "functions_100_with_5_contracts":
        start = time.time()
        import functions_100_with_5_contracts
        print(time.time() - start)

    elif a_module == "functions_100_with_10_contracts":
        start = time.time()
        import functions_100_with_10_contracts
        print(time.time() - start)

    elif a_module == "functions_100_with_1_disabled_contract":
        start = time.time()
        import functions_100_with_1_disabled_contract
        print(time.time() - start)

    elif a_module == "functions_100_with_5_disabled_contracts":
        start = time.time()
        import functions_100_with_5_disabled_contracts
        print(time.time() - start)

    elif a_module == "functions_100_with_10_disabled_contracts":
        start = time.time()
        import functions_100_with_10_disabled_contracts
        print(time.time() - start)

    elif a_module == "classes_100_with_no_invariant":
        start = time.time()
        import classes_100_with_no_invariant
        print(time.time() - start)

    elif a_module == "classes_100_with_1_invariant":
        start = time.time()
        import classes_100_with_1_invariant
        print(time.time() - start)

    elif a_module == "classes_100_with_5_invariants":
        start = time.time()
        import classes_100_with_5_invariants
        print(time.time() - start)

    elif a_module == "classes_100_with_10_invariants":
        start = time.time()
        import classes_100_with_10_invariants
        print(time.time() - start)

    elif a_module == "classes_100_with_1_disabled_invariant":
        start = time.time()
        import classes_100_with_1_disabled_invariant
        print(time.time() - start)

    elif a_module == "classes_100_with_5_disabled_invariants":
        start = time.time()
        import classes_100_with_5_disabled_invariants
        print(time.time() - start)

    elif a_module == "classes_100_with_10_disabled_invariants":
        start = time.time()
        import classes_100_with_10_disabled_invariants
        print(time.time() - start)

    else:
        raise NotImplementedError("Unhandled module: {}".format(a_module))


Instruction:
Given the following Python 3 function, write the documentation
def main() -> None:
    modules = [
        "functions_100_with_no_contract",

        "functions_100_with_1_contract",
        "functions_100_with_5_contracts",
        "functions_100_with_10_contracts",

        "functions_100_with_1_disabled_contract",
        "functions_100_with_5_disabled_contracts",
        "functions_100_with_10_disabled_contracts",

        "classes_100_with_no_invariant",

        "classes_100_with_1_invariant",
        "classes_100_with_5_invariants",
        "classes_100_with_10_invariants",

        "classes_100_with_1_disabled_invariant",
        "classes_100_with_5_disabled_invariants",
        "classes_100_with_10_disabled_invariants",
    ]

    for a_module in modules:
        durations = []  # type: List[float]
        for i in range(0, 10):
            duration = float(
                subprocess.check_output(["./measure.py", "--module", a_module], cwd=os.path.dirname(__file__)).strip())
            durations.append(duration)

        print("Duration to import the module {} (in milliseconds): {:.2f} ± {:.2f}".format(
            a_module, statistics.mean(durations) * 10e3, statistics.stdev(durations) * 10e3))

Response: Execute the main routine.


Instruction:
Write a Python 3 script to
execute the main routine.

Response:
def main() -> None:
    """"Execute the main routine."""
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--outdir", help="output directory", default=os.path.dirname(__file__))
    args = parser.parse_args()

    outdir = pathlib.Path(args.outdir)
    if not outdir.exists():
        raise FileNotFoundError("Output directory is missing: {}".format(outdir))

    for contracts in [0, 1, 5, 10]:
        if contracts == 0:
            pth = outdir / "functions_100_with_no_contract.py"
        elif contracts == 1:
            pth = outdir / "functions_100_with_1_contract.py"
        else:
            pth = outdir / "functions_100_with_{}_contracts.py".format(contracts)

        text = generate_functions(functions=100, contracts=contracts, disabled=False)
        pth.write_text(text)

    for contracts in [1, 5, 10]:
        if contracts == 1:
            pth = outdir / "functions_100_with_1_disabled_contract.py"
        else:
            pth = outdir / "functions_100_with_{}_disabled_contracts.py".format(contracts)

        text = generate_functions(functions=100, contracts=contracts, disabled=True)
        pth.write_text(text)

    for invariants in [0, 1, 5, 10]:
        if invariants == 0:
            pth = outdir / "classes_100_with_no_invariant.py"
        elif invariants == 1:
            pth = outdir / "classes_100_with_1_invariant.py"
        else:
            pth = outdir / "classes_100_with_{}_invariants.py".format(invariants)

        text = generate_classes(classes=100, invariants=invariants, disabled=False)
        pth.write_text(text)

    for invariants in [1, 5, 10]:
        if invariants == 1:
            pth = outdir / "classes_100_with_1_disabled_invariant.py"
        else:
            pth = outdir / "classes_100_with_{}_disabled_invariants.py".format(invariants)

        text = generate_classes(classes=100, invariants=invariants, disabled=True)
        pth.write_text(text)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def visit_Bytes(self, node: ast.Bytes) -> bytes:
        result = node.s

        self.recomputed_values[node] = result
        return node.s

Response: Recompute the value as the bytes at the node.


Instruction:
Write a Python 3 function that can
visit the elements and assemble the results into a list.

Response:
def visit_List(self, node: ast.List) -> List[Any]:
        """Visit the elements and assemble the results into a list."""
        if isinstance(node.ctx, ast.Store):
            raise NotImplementedError("Can not compute the value of a Store on a list")

        result = [self.visit(node=elt) for elt in node.elts]

        self.recomputed_values[node] = result
        return result


Instruction:
Here you have a function in Python 3, explain what it does
def visit_Tuple(self, node: ast.Tuple) -> Tuple[Any, ...]:
        if isinstance(node.ctx, ast.Store):
            raise NotImplementedError("Can not compute the value of a Store on a tuple")

        result = tuple(self.visit(node=elt) for elt in node.elts)

        self.recomputed_values[node] = result
        return result

Response: Visit the elements and assemble the results into a tuple.


Instruction:
Given the following Python 3 function, write the documentation
def visit_Set(self, node: ast.Set) -> Set[Any]:
        result = set(self.visit(node=elt) for elt in node.elts)

        self.recomputed_values[node] = result
        return result

Response: Visit the elements and assemble the results into a set.


Instruction:
How would you code a function in Python 3 to
visit keys and values and assemble a dictionary with the results.

Response:
def visit_Dict(self, node: ast.Dict) -> Dict[Any, Any]:
        """Visit keys and values and assemble a dictionary with the results."""
        recomputed_dict = dict()  # type: Dict[Any, Any]
        for key, val in zip(node.keys, node.values):
            recomputed_dict[self.visit(node=key)] = self.visit(node=val)

        self.recomputed_values[node] = recomputed_dict
        return recomputed_dict


Instruction:
How would you explain what the following Python 3 function does
def visit_NameConstant(self, node: ast.NameConstant) -> Any:
        self.recomputed_values[node] = node.value
        return node.value

Response: Forward the node value as a result.


Instruction:
Explain what the following Python 3 code does
def visit_Name(self, node: ast.Name) -> Any:
        if not isinstance(node.ctx, ast.Load):
            raise NotImplementedError("Can only compute a value of Load on a name {}, but got context: {}".format(
                node.id, node.ctx))

        result = None  # type: Optional[Any]

        if node.id in self._name_to_value:
            result = self._name_to_value[node.id]

        if result is None and hasattr(builtins, node.id):
            result = getattr(builtins, node.id)

        if result is None and node.id != "None":
            # The variable refers to a name local of the lambda (e.g., a target in the generator expression).
            # Since we evaluate generator expressions with runtime compilation, None is returned here as a placeholder.
            return PLACEHOLDER

        self.recomputed_values[node] = result
        return result

Response: Load the variable by looking it up in the variable look - up and in the built - ins.


Instruction:
Make a summary of the following Python 3 code
def visit_Expr(self, node: ast.Expr) -> Any:
        result = self.visit(node=node.value)

        self.recomputed_values[node] = result
        return result

Response: Visit the node s value and return the result.


Instruction:
Make a summary of the following Python 3 code
def visit_UnaryOp(self, node: ast.UnaryOp) -> Any:
        if isinstance(node.op, ast.UAdd):
            result = +self.visit(node=node.operand)
        elif isinstance(node.op, ast.USub):
            result = -self.visit(node=node.operand)
        elif isinstance(node.op, ast.Not):
            result = not self.visit(node=node.operand)
        elif isinstance(node.op, ast.Invert):
            result = ~self.visit(node=node.operand)
        else:
            raise NotImplementedError("Unhandled op of {}: {}".format(node, node.op))

        self.recomputed_values[node] = result
        return result

Response: Visit the node operand and apply the operation on the result.


Instruction:
Can you tell what is the following Python 3 function doing
def visit_Compare(self, node: ast.Compare) -> Any:
        # pylint: disable=too-many-branches
        left = self.visit(node=node.left)

        comparators = [self.visit(node=comparator) for comparator in node.comparators]

        result = None  # type: Optional[Any]
        for comparator, op in zip(comparators, node.ops):
            if isinstance(op, ast.Eq):
                comparison = left == comparator
            elif isinstance(op, ast.NotEq):
                comparison = left != comparator
            elif isinstance(op, ast.Lt):
                comparison = left < comparator
            elif isinstance(op, ast.LtE):
                comparison = left <= comparator
            elif isinstance(op, ast.Gt):
                comparison = left > comparator
            elif isinstance(op, ast.GtE):
                comparison = left >= comparator
            elif isinstance(op, ast.Is):
                comparison = left is comparator
            elif isinstance(op, ast.IsNot):
                comparison = left is not comparator
            elif isinstance(op, ast.In):
                comparison = left in comparator
            elif isinstance(op, ast.NotIn):
                comparison = left not in comparator
            else:
                raise NotImplementedError("Unhandled op of {}: {}".format(node, op))

            if result is None:
                result = comparison
            else:
                result = result and comparison

            left = comparator

        self.recomputed_values[node] = result
        return result

Response: Recursively visit the comparators and apply the operations on them.


Instruction:
Implement a function in Python 3 to
visit the function and the arguments and then make the function call with them.

Response:
def visit_Call(self, node: ast.Call) -> Any:
        """Visit the function and the arguments and finally make the function call with them."""
        func = self.visit(node=node.func)

        args = []  # type: List[Any]
        for arg_node in node.args:
            if isinstance(arg_node, ast.Starred):
                args.extend(self.visit(node=arg_node))
            else:
                args.append(self.visit(node=arg_node))

        kwargs = dict()  # type: Dict[str, Any]
        for keyword in node.keywords:
            if keyword.arg is None:
                kw = self.visit(node=keyword.value)
                for key, val in kw.items():
                    kwargs[key] = val

            else:
                kwargs[keyword.arg] = self.visit(node=keyword.value)

        result = func(*args, **kwargs)

        self.recomputed_values[node] = result
        return result


Instruction:
Can you create a Python 3 function that
visits the test and depending on its outcome the body or orelse.

Response:
def visit_IfExp(self, node: ast.IfExp) -> Any:
        """Visit the ``test``, and depending on its outcome, the ``body`` or ``orelse``."""
        test = self.visit(node=node.test)

        if test:
            result = self.visit(node=node.body)
        else:
            result = self.visit(node=node.orelse)

        self.recomputed_values[node] = result
        return result


Instruction:
Can you tell what is the following Python 3 function doing
def visit_Attribute(self, node: ast.Attribute) -> Any:
        value = self.visit(node=node.value)
        if not isinstance(node.ctx, ast.Load):
            raise NotImplementedError(
                "Can only compute a value of Load on the attribute {}, but got context: {}".format(node.attr, node.ctx))

        result = getattr(value, node.attr)

        self.recomputed_values[node] = result
        return result

Response: Visit the node s value and get the attribute from the result.


Instruction:
Can you tell what is the following Python 3 function doing
def visit_Index(self, node: ast.Index) -> Any:
        result = self.visit(node=node.value)

        self.recomputed_values[node] = result
        return result

Response: Visit the node s value and store the result in the recomputed_values dict.


Instruction:
Create a Python 3 function for
visiting lower upper and step and recompute the node as a slice.

Response:
def visit_Slice(self, node: ast.Slice) -> slice:
        """Visit ``lower``, ``upper`` and ``step`` and recompute the node as a ``slice``."""
        lower = None  # type: Optional[int]
        if node.lower is not None:
            lower = self.visit(node=node.lower)

        upper = None  # type: Optional[int]
        if node.upper is not None:
            upper = self.visit(node=node.upper)

        step = None  # type: Optional[int]
        if node.step is not None:
            step = self.visit(node=node.step)

        result = slice(lower, upper, step)

        self.recomputed_values[node] = result
        return result


Instruction:
Create a Python 3 function for
visiting each dimension of the advanced slicing and assemble the dimensions in a tuple.

Response:
def visit_ExtSlice(self, node: ast.ExtSlice) -> Tuple[Any, ...]:
        """Visit each dimension of the advanced slicing and assemble the dimensions in a tuple."""
        result = tuple(self.visit(node=dim) for dim in node.dims)

        self.recomputed_values[node] = result
        return result


Instruction:
Here you have a function in Python 3, explain what it does
def visit_Subscript(self, node: ast.Subscript) -> Any:
        value = self.visit(node=node.value)
        a_slice = self.visit(node=node.slice)

        result = value[a_slice]

        self.recomputed_values[node] = result
        return result

Response: Visit the slice and a value and get the element.


Instruction:
Create a Python 3 function to
compile the generator or comprehension from the node and execute the compiled code.

Response:
def _execute_comprehension(self, node: Union[ast.ListComp, ast.SetComp, ast.GeneratorExp, ast.DictComp]) -> Any:
        """Compile the generator or comprehension from the node and execute the compiled code."""
        args = [ast.arg(arg=name) for name in sorted(self._name_to_value.keys())]

        func_def_node = ast.FunctionDef(
            name="generator_expr",
            args=ast.arguments(args=args, kwonlyargs=[], kw_defaults=[], defaults=[]),
            decorator_list=[],
            body=[ast.Return(node)])

        module_node = ast.Module(body=[func_def_node])

        ast.fix_missing_locations(module_node)

        code = compile(source=module_node, filename='<ast>', mode='exec')

        module_locals = {}  # type: Dict[str, Any]
        module_globals = {}  # type: Dict[str, Any]
        exec(code, module_globals, module_locals)  # pylint: disable=exec-used

        generator_expr_func = module_locals["generator_expr"]

        return generator_expr_func(**self._name_to_value)


Instruction:
Can you implement a function in Python 3 that
compiles the generator expression as a function and call it.

Response:
def visit_GeneratorExp(self, node: ast.GeneratorExp) -> Any:
        """Compile the generator expression as a function and call it."""
        result = self._execute_comprehension(node=node)

        for generator in node.generators:
            self.visit(generator.iter)

        # Do not set the computed value of the node since its representation would be non-informative.
        return result


Instruction:
Can you generate a brief explanation for the following Python 3 code
def visit_ListComp(self, node: ast.ListComp) -> Any:
        result = self._execute_comprehension(node=node)

        for generator in node.generators:
            self.visit(generator.iter)

        self.recomputed_values[node] = result
        return result

Response: Compile the list comprehension as a function and call it.


Instruction:
Here you have a function in Python 3, explain what it does
def visit_SetComp(self, node: ast.SetComp) -> Any:
        result = self._execute_comprehension(node=node)

        for generator in node.generators:
            self.visit(generator.iter)

        self.recomputed_values[node] = result
        return result

Response: Compile the set comprehension as a function and call it.


Instruction:
Here you have a function in Python 3, explain what it does
def visit_DictComp(self, node: ast.DictComp) -> Any:
        result = self._execute_comprehension(node=node)

        for generator in node.generators:
            self.visit(generator.iter)

        self.recomputed_values[node] = result
        return result

Response: Compile the dictionary comprehension as a function and call it.


Instruction:
Make a summary of the following Python 3 code
def visit_Return(self, node: ast.Return) -> Any:  # pylint: disable=no-self-use
        raise AssertionError("Unexpected return node during the re-computation: {}".format(ast.dump(node)))

Response: Raise an exception that this node is unexpected.


Instruction:
Implement a Python 3 function for
raising an exception that this node has not been handled.

Response:
def generic_visit(self, node: ast.AST) -> None:
        """Raise an exception that this node has not been handled."""
        raise NotImplementedError("Unhandled recomputation of the node: {} {}".format(type(node), node))


Instruction:
Explain what the following Python 3 code does
def tokenize_words(string):
    string = six.text_type(string)
    return re.findall(WORD_TOKENIZATION_RULES, string)

Response: Tokenize input text to words.


Instruction:
Can you tell what is the following Python 3 function doing
def align(self):
        '''
        Every step we have 3 choices:
        1) Move pointer witness a --> omission
        2) Move pointer witness b --> addition
        3) Move pointer of both witness a/b  --> match
        Note: a replacement is omission followed by an addition or the other way around

        Choice 1 and 2 are only possible if token a and b are not a match OR when tokens are repeated.
        For now I ignore token repetition..
        '''
        # extract tokens from witness (note that this can be done in a streaming manner if desired)
        tokens_a = self.witness_a.tokens()
        tokens_b = self.witness_b.tokens()
        # create virtual decision tree (nodes are created on demand)
        # see above
        # create start node
        start = DecisionTreeNode(self)

        # search the decision tree
        result = self.tree.search(start)
        print(result)

        pass

Response: Aligns the decision tree with the witnesses


Instruction:
Implement a function in Python 3 to
connect two sets of nodes near to the target set.

Response:
def connect_near(self, source, target, weight):
        # Near edges are added to self.near_graph, not self.graph, to avoid cycles
        """
        :type source: integer
        :type target: integer
        """
        self.near_graph.add_edge(source, target, weight = weight, type='near')


Instruction:
Write a Python 3 script for
merging the given graph with the given witness_sigil and returns a dictionary of all the vertices that are connected to the witness_sigil.

Response:
def merge(self, graph, witness_sigil, witness_tokens, alignments={}):
        """
        :type graph: VariantGraph
        """
        # NOTE: token_to_vertex only contains newly generated vertices
        token_to_vertex = {}
        last = graph.start
        for token in witness_tokens:
            vertex = alignments.get(token, None)
            if not vertex:
                vertex = graph.add_vertex(token, witness_sigil)
                token_to_vertex[token] = vertex
            else:
                vertex.add_token(witness_sigil, token)
                # graph.add_token_to_vertex(vertex, token, witness_sigil)
            graph.connect(last, vertex, witness_sigil)
            last = vertex
        graph.connect(last, graph.end, witness_sigil)
        return token_to_vertex


Instruction:
How would you code a function in Python 3 to
collate the given VariantGraph into a single variant graph for each witness in the sequence

Response:
def collate(self, graph):
        """
        :type graph: VariantGraph
        """
        # prepare the token index
        self.token_index.prepare()
        self.vertex_array = [None] * len(self.token_index.token_array)

        # Build the variant graph for the first witness
        # this is easy: generate a vertex for every token
        first_witness = self.collation.witnesses[0]
        tokens = first_witness.tokens()
        token_to_vertex = self.merge(graph, first_witness.sigil, tokens)
        # print("> token_to_vertex=", token_to_vertex)
        self.update_token_position_to_vertex(token_to_vertex)
        self.update_token_to_vertex_array(tokens, first_witness, self.token_position_to_vertex)

        # align witness 2 - n
        for x in range(1, len(self.collation.witnesses)):
            witness = self.collation.witnesses[x]
            tokens = witness.tokens()
            # print("\nwitness", witness.sigil)

            variant_graph_ranking = VariantGraphRanking.of(graph)
            # print("> x =", x, ", variant_graph_ranking =", variant_graph_ranking.byRank)
            variant_graph_ranks = list(set(map(lambda v: variant_graph_ranking.byVertex.get(v), graph.vertices())))
            # we leave in the rank of the start vertex, but remove the rank of the end vertex
            variant_graph_ranks.pop()

            # now the vertical stuff
            tokens_as_index_list = self.as_index_list(tokens)

            match_cube = MatchCube(self.token_index, witness, self.vertex_array, variant_graph_ranking,
                                   self.properties_filter)
            # print("> match_cube.matches=", match_cube.matches)
            self.fill_needleman_wunsch_table(variant_graph_ranks, tokens_as_index_list, match_cube)

            aligned = self.align_matching_tokens(match_cube)
            # print("> aligned=", aligned)
            # print("self.token_index.token_array=", self.token_index.token_array)
            # alignment = self.align_function(superbase, next_witness, token_to_vertex, match_cube)

            # merge
            witness_token_to_generated_vertex = self.merge(graph, witness.sigil, witness.tokens(), aligned)
            # print("> witness_token_to_generated_vertex =", witness_token_to_generated_vertex)
            token_to_vertex.update(witness_token_to_generated_vertex)
            # print("> token_to_vertex =", token_to_vertex)
            self.update_token_position_to_vertex(token_to_vertex, aligned)
            witness_token_position_to_vertex = {}
            for p in self.token_index.get_range_for_witness(witness.sigil):
                # print("> p= ", p)
                witness_token_position_to_vertex[p] = self.token_position_to_vertex[p]
            self.update_token_to_vertex_array(tokens, witness, witness_token_position_to_vertex)
            # print("> vertex_array =", self.vertex_array)

            #             print("actual")
            #             self._debug_edit_graph_table(self.table)
            #             print("expected")
            #             self._debug_edit_graph_table(self.table2)

            # change superbase
            # superbase = self.new_superbase

            if self.detect_transpositions:
                detector = TranspositionDetection(self)
                detector.detect()


Instruction:
Write a Python 3 function that can
parse a string of comma - separated x - y - step - like ranges and return a tuple of the node range info.

Response:
def _parse(self, pattern):
        """Parse string of comma-separated x-y/step -like ranges"""
        # Comma separated ranges
        if pattern.find(',') < 0:
            subranges = [pattern]
        else:
            subranges = pattern.split(',')

        for subrange in subranges:
            if subrange.find('/') < 0:
                step = 1
                baserange = subrange
            else:
                baserange, step = subrange.split('/', 1)

            try:
                step = int(step)
            except ValueError:
                raise RangeSetParseError(subrange,
                        "cannot convert string to integer")

            if baserange.find('-') < 0:
                if step != 1:
                    raise RangeSetParseError(subrange,
                            "invalid step usage")
                begin = end = baserange
            else:
                begin, end = baserange.split('-', 1)

            # compute padding and return node range info tuple
            try:
                pad = 0
                if int(begin) != 0:
                    begins = begin.lstrip("0")
                    if len(begin) - len(begins) > 0:
                        pad = len(begin)
                    start = int(begins)
                else:
                    if len(begin) > 1:
                        pad = len(begin)
                    start = 0
                if int(end) != 0:
                    ends = end.lstrip("0")
                else:
                    ends = end
                stop = int(ends)
            except ValueError:
                raise RangeSetParseError(subrange,
                        "cannot convert string to integer")

            # check preconditions
            if stop > 1e100 or start > stop or step < 1:
                raise RangeSetParseError(subrange,
                                         "invalid values in range")

            self.add_range(start, stop + 1, step, pad)


Instruction:
Implement a function in Python 3 to
class method that returns a new RangeSet with ranges from provided list.

Response:
def fromlist(cls, rnglist, autostep=None):
        """Class method that returns a new RangeSet with ranges from provided
        list."""
        inst = RangeSet(autostep=autostep)
        inst.updaten(rnglist)
        return inst


Instruction:
Can you generate the documentation for the following Python 3 function
def fromone(cls, index, pad=0, autostep=None):
        inst = RangeSet(autostep=autostep)
        # support slice object with duck-typing
        try:
            inst.add(index, pad)
        except TypeError:
            if not index.stop:
                raise ValueError("Invalid range upper limit (%s)" % index.stop)
            inst.add_range(index.start or 0, index.stop, index.step or 1, pad)
        return inst

Response:
Class method that returns a new RangeSet of one single item or
            a single range ( from integer or slice object.


Instruction:
Implement a function in Python 3 to
set autostep value ( property

Response:
def set_autostep(self, val):
        """Set autostep value (property)"""
        if val is None:
            # disabled by default for pdsh compat (+inf is 1E400, but a bug in
            # python 2.4 makes it impossible to be pickled, so we use less)
            # NOTE: Later, we could consider sys.maxint here
            self._autostep = 1E100
        else:
            # - 1 because user means node count, but we means real steps
            self._autostep = int(val) - 1


Instruction:
Write a Python 3 function for
iterating over each string element in RangeSet.

Response:
def striter(self):
        """Iterate over each (optionally padded) string element in RangeSet."""
        pad = self.padding or 0
        for i in self._sorted():
            yield "%0*d" % (pad, i)


Instruction:
Write a Python 3 script to
object - based iterator over contiguous range sets.

Response:
def contiguous(self):
        """Object-based iterator over contiguous range sets."""
        pad = self.padding or 0
        for sli in self._contiguous_slices():
            yield RangeSet.fromone(slice(sli.start, sli.stop, sli.step), pad)


Instruction:
Make a summary of the following Python 3 code
def _strslices(self):
        pad = self.padding or 0
        for sli in self.slices():
            if sli.start + 1 == sli.stop:
                yield "%0*d" % (pad, sli.start)
            else:
                assert sli.step >= 0, "Internal error: sli.step < 0"
                if sli.step == 1:
                    yield "%0*d-%0*d" % (pad, sli.start, pad, sli.stop - 1)
                else:
                    yield "%0*d-%0*d/%d" % (pad, sli.start, pad, sli.stop - 1, \
                                            sli.step)

Response: Stringify the slices list.


Instruction:
Here you have a function in Python 3, explain what it does
def _contiguous_slices(self):
        k = j = None
        for i in self._sorted():
            if k is None:
                k = j = i
            if i - j > 1:
                yield slice(k, j + 1, 1)
                k = i
            j = i
        if k is not None:
            yield slice(k, j + 1, 1)

Response: Internal iterator over contiguous slices in RangeSet.


Instruction:
Given the following Python 3 function, write the documentation
def _folded_slices(self):
        if len(self) == 0:
            return

        prng = None         # pending range
        istart = None       # processing starting indice
        m = 0               # processing step
        for sli in self._contiguous_slices():
            start = sli.start
            stop = sli.stop
            unitary = (start + 1 == stop)   # one indice?
            if istart is None:  # first loop
                if unitary:
                    istart = start
                else:
                    prng = [start, stop, 1]
                    istart = stop - 1
                i = k = istart
            elif m == 0:        # istart is set but step is unknown
                if not unitary:
                    if prng is not None:
                        # yield and replace pending range
                        yield slice(*prng)
                    else:
                        yield slice(istart, istart + 1, 1)
                    prng = [start, stop, 1]
                    istart = k = stop - 1
                    continue
                i = start
            else:               # step m > 0
                assert m > 0
                i = start
                # does current range lead to broken step?
                if m != i - k or not unitary:
                    #j = i if m == i - k else k
                    if m == i - k: j = i
                    else: j = k
                    # stepped is True when autostep setting does apply
                    stepped = (j - istart >= self._autostep * m)
                    if prng:    # yield pending range?
                        if stepped:
                            prng[1] -= 1
                        else:
                            istart += m
                        yield slice(*prng)
                        prng = None
                if m != i - k:
                    # case: step value has changed
                    if stepped:
                        yield slice(istart, k + 1, m)
                    else:
                        for j in range(istart, k - m + 1, m):
                            yield slice(j, j + 1, 1)
                        if not unitary:
                            yield slice(k, k + 1, 1)
                    if unitary:
                        if stepped:
                            istart = i = k = start
                        else:
                            istart = k
                    else:
                        prng = [start, stop, 1]
                        istart = i = k = stop - 1
                elif not unitary:
                    # case: broken step by contiguous range
                    if stepped:
                        # yield 'range/m' by taking first indice of new range
                        yield slice(istart, i + 1, m)
                        i += 1
                    else:
                        # autostep setting does not apply in that case
                        for j in range(istart, i - m + 1, m):
                            yield slice(j, j + 1, 1)
                    if stop > i + 1:    # current->pending only if not unitary
                        prng = [i, stop, 1]
                    istart = i = k = stop - 1
            m = i - k   # compute step
            k = i
        # exited loop, process pending range or indice...
        if m == 0:
            if prng:
                yield slice(*prng)
            else:
                yield slice(istart, istart + 1, 1)
        else:
            assert m > 0
            stepped = (k - istart >= self._autostep * m)
            if prng:
                if stepped:
                    prng[1] -= 1
                else:
                    istart += m
                yield slice(*prng)
                prng = None
            if stepped:
                yield slice(istart, i + 1, m)
            else:
                for j in range(istart, i + 1, m):
                    yield slice(j, j + 1, 1)

Response: Internal generator that is able to retrieve ranges organized by step.


Instruction:
Write a Python 3 function for
splitting the rangeset into nbr sub - nodesets.

Response:
def split(self, nbr):
        """
        Split the rangeset into nbr sub-rangesets (at most). Each
        sub-rangeset will have the same number of elements more or
        less 1. Current rangeset remains unmodified. Returns an
        iterator.

        >>> RangeSet("1-5").split(3)
        RangeSet("1-2")
        RangeSet("3-4")
        RangeSet("foo5")
        """
        assert(nbr > 0)

        # We put the same number of element in each sub-nodeset.
        slice_size = len(self) / nbr
        left = len(self) % nbr

        begin = 0
        for i in range(0, min(nbr, len(self))):
            length = slice_size + int(i < left)
            yield self[begin:begin + length]
            begin += length


Instruction:
Write a Python 3 function that can
add a range to the RangeSet.

Response:
def add_range(self, start, stop, step=1, pad=0):
        """
        Add a range (start, stop, step and padding length) to RangeSet.
        Like the Python built-in function range(), the last element is
        the largest start + i * step less than stop.
        """
        assert start < stop, "please provide ordered node index ranges"
        assert step > 0
        assert pad >= 0
        assert stop - start < 1e9, "range too large"

        if pad > 0 and self.padding is None:
            self.padding = pad
        set.update(self, range(start, stop, step))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def copy(self):
        cpy = self.__class__()
        cpy._autostep = self._autostep
        cpy.padding = self.padding
        cpy.update(self)
        return cpy

Response: Return a shallow copy of a RangeSet.


Instruction:
Can you create a Python 3 function that
wraps built - in set operations for RangeSet to workaround built - in set operations.

Response:
def _wrap_set_op(self, fun, arg):
        """Wrap built-in set operations for RangeSet to workaround built-in set
        base class issues (RangeSet.__new/init__ not called)"""
        result = fun(self, arg)
        result._autostep = self._autostep
        result.padding = self.padding
        return result


Instruction:
Can you implement a function in Python 3 that
returns the intersection of two sets as a new set instance.

Response:
def intersection(self, other):
        """Return the intersection of two RangeSets as a new RangeSet.

        (I.e. all elements that are in both sets.)
        """
        #NOTE: This is a work around
        # Python 3 return as the result of set.intersection a new set instance.
        # Python 2 however returns as a the result a ClusterShell.RangeSet.RangeSet instance.
        # ORIGINAL CODE: return self._wrap_set_op(set.intersection, other)
        copy = self.copy()
        copy.intersection_update(other)
        return copy


Instruction:
Given the following Python 3 function, write the documentation
def difference(self, other):
        #NOTE: This is a work around
        # Python 3 return as the result of set.intersection a new set instance.
        # Python 2 however returns as a the result a ClusterShell.RangeSet.RangeSet instance.
        # ORIGINAL CODE: return self._wrap_set_op(set.difference, other)
        copy = self.copy()
        copy.difference_update(other)
        return copy

Response: Return the difference of two sets as a new set.


Instruction:
Given the following Python 3 function, write the documentation
def issubset(self, other):
        self._binary_sanity_check(other)
        return set.issubset(self, other)

Response: Report whether this set contains another set.


Instruction:
How would you code a function in Python 3 to
report whether this RangeSet contains another set.

Response:
def issuperset(self, other):
        """Report whether this RangeSet contains another set."""
        self._binary_sanity_check(other)
        return set.issuperset(self, other)


Instruction:
Write a Python 3 script to
update this RangeSet with the difference of the elements of another set.

Response:
def difference_update(self, other, strict=False):
        """Remove all elements of another set from this RangeSet.

        If strict is True, raise KeyError if an element cannot be removed.
        (strict is a RangeSet addition)"""
        if strict and other not in self:
            raise KeyError(other.difference(self)[0])
        set.difference_update(self, other)


Instruction:
How would you explain what the following Python 3 function does
def update(self, iterable):
        if isinstance(iterable, RangeSet):
            # keep padding unless is has not been defined yet
            if self.padding is None and iterable.padding is not None:
                self.padding = iterable.padding
        assert type(iterable) is not str
        set.update(self, iterable)

Response: Add all integers from an iterable.


Instruction:
Write a Python 3 script for
updating a set of all the items in the set with the union of itself and several others.

Response:
def updaten(self, rangesets):
        """
        Update a rangeset with the union of itself and several others.
        """
        for rng in rangesets:
            if isinstance(rng, set):
                self.update(rng)
            else:
                self.update(RangeSet(rng))


Instruction:
Create a Python 3 function to
add an element to a RangeSet.

Response:
def add(self, element, pad=0):
        """Add an element to a RangeSet.
        This has no effect if the element is already present.
        """
        set.add(self, int(element))
        if pad > 0 and self.padding is None:
            self.padding = pad


Instruction:
How would you code a function in Python 3 to
remove element from the RangeSet.

Response:
def discard(self, element):
        """Remove element from the RangeSet if it is a member.

        If the element is not a member, do nothing.
        """
        try:
            i = int(element)
            set.discard(self, i)
        except ValueError:
            pass


Instruction:
Make a summary of the following Python 3 code
def _open(filename, mode="r"):
    if filename.endswith(".gz"):
        return GzipFile(filename, mode, COMPRESSION_LEVEL)
    elif filename == "-":
        if mode == "r":
            return _stdin
        elif mode == "w":
            return _stdout
    else:
        # TODO: set encoding to UTF-8?
        return open(filename, mode=mode)

Response: Open a file with the specified mode.


Instruction:
How would you code a function in Python 3 to
stable sort of the sequence a according to the keys given in r. >>> a=range(5) >>> b=[0]*5 >>> r=[2,1,3,0,4] >>> _radixPass(a, b, r, 5, 5) >>> b [3, 1, 0, 2, 4] When n is less than the length of a, the end of b must be left unaltered. >>> b=[5]*5 >>> _radixPass(a, b, r, 2, 2) >>> b [1, 0, 5, 5, 5] >>> _a=a=[1, 0] >>> b= [0]*2 >>> r=[0, 1] >>> _radixPass(a, b, r, 2, 2) >>> a=_a >>> b [0, 1] >>> a=[1, 1] >>> _radixPass(a, b, r, 2, 2) >>> b [1, 1] >>> a=[0, 1, 1, 0] >>> b= [0]*4 >>> r=[0, 1] >>> _radixPass(a, b, r, 4, 2) >>> a=_a >>> b [0, 0, 1, 1]

Response:
def _radixPass(a, b, r, n, K):
    """
    Stable sort of the sequence a according to the keys given in r.

    >>> a=range(5)
    >>> b=[0]*5
    >>> r=[2,1,3,0,4]

    >>> _radixPass(a, b, r, 5, 5)
    >>> b
    [3, 1, 0, 2, 4]

    When n is less than the length of a, the end of b must be left unaltered.
    >>> b=[5]*5
    >>> _radixPass(a, b, r, 2, 2)
    >>> b
    [1, 0, 5, 5, 5]

    >>> _a=a=[1, 0]
    >>> b= [0]*2
    >>> r=[0, 1]
    >>> _radixPass(a, b, r, 2, 2)
    >>> a=_a
    >>> b
    [0, 1]

    >>> a=[1, 1]
    >>> _radixPass(a, b, r, 2, 2)
    >>> b
    [1, 1]

    >>> a=[0, 1, 1, 0]
    >>> b= [0]*4
    >>> r=[0, 1]
    >>> _radixPass(a, b, r, 4, 2)
    >>> a=_a
    >>> b
    [0, 0, 1, 1]
    """
    c = _array("i", [0] * (K + 1))  # counter array

    for i in range(n):  # count occurrences
        c[r[a[i]]] += 1

    sum = 0

    for i in range(K + 1):  # exclusive prefix sums
        t = c[i]
        c[i] = sum
        sum += t

    for a_i in a[:n]:  # sort
        b[c[r[a_i]]] = a_i
        c[r[a_i]] += 1


Instruction:
Explain what the following Python 3 code does
def _nbOperations(n):
    if n < 2:
        return 0
    else:
        n0 = (n + 2) // 3
        n02 = n0 + n // 3
        return 3 * (n02) + n0 + _nbOperations(n02)

Response: Returns the number of atomic operations in _radixPass.


Instruction:
Write a Python 3 script to
return the length of the longest common prefix of seq1 and seq2 starting at offset start1 and start2.

Response:
def _longestCommonPrefix(seq1, seq2, start1=0, start2=0):
    """
    Returns the length of the longest common prefix of seq1
    starting at offset start1 and seq2 starting at offset start2.

    >>> _longestCommonPrefix("abcdef", "abcghj")
    3

    >>> _longestCommonPrefix("abcghj", "abcdef")
    3

    >>> _longestCommonPrefix("miss", "")
    0

    >>> _longestCommonPrefix("", "mr")
    0

    >>> _longestCommonPrefix(range(128), range(128))
    128

    >>> _longestCommonPrefix("abcabcabc", "abcdefabcdef", 0, 6)
    3

    >>> _longestCommonPrefix("abcdefabcdef", "abcabcabc", 6, 0)
    3

    >>> _longestCommonPrefix("abc", "abcabc", 1, 4)
    2

    >>> _longestCommonPrefix("abcabc", "abc", 4, 1)
    2
    """

    len1 = len(seq1) - start1
    len2 = len(seq2) - start2

    # We set seq2 as the shortest sequence
    if len1 < len2:
        seq1, seq2 = seq2, seq1
        start1, start2 = start2, start1
        len1, len2 = len2, len1

    # if seq2 is empty returns 0
    if len2 == 0:
        return 0

    i = 0
    pos2 = start2
    for i in range(min(len1, len2)):
        # print seq1, seq2, start1, start2
        if seq1[start1 + i] != seq2[start2 + i]:
            return i

    # we have reached the end of seq2 (need to increment i)
    return i + 1


Instruction:
How would you code a function in Python 3 to
compute the longest common prefix for every adjacent suffixes.

Response:
def LCP(SA):
    """
    Compute the longest common prefix for every adjacent suffixes.
    The result is a list of same size as SA.
    Given two suffixes at positions i and i+1,
    their LCP is stored at position i+1.
    A zero is stored at position 0 of the output.

    >>> SA=SuffixArray("abba", unit=UNIT_BYTE)
    >>> SA._LCP_values
    array('i', [0, 1, 0, 1])

    >>> SA=SuffixArray("", unit=UNIT_BYTE)
    >>> SA._LCP_values
    array('i')

    >>> SA=SuffixArray("", unit=UNIT_CHARACTER)
    >>> SA._LCP_values
    array('i')

    >>> SA=SuffixArray("", unit=UNIT_WORD)
    >>> SA._LCP_values
    array('i')

    >>> SA=SuffixArray("abab", unit=UNIT_BYTE)
    >>> SA._LCP_values
    array('i', [0, 2, 0, 1])
    """
    string = SA.string
    length = SA.length
    lcps = _array("i", [0] * length)
    SA = SA.SA

    if _trace:
        delta = max(length // 100, 1)
        for i, pos in enumerate(SA):
            if i % delta == 0:
                percent = float((i + 1) * 100) / length
                print >> _stderr, "Compute_LCP %.2f%% (%i/%i)\r" % (percent, i + 1, length),
            lcps[i] = _longestCommonPrefix(string, string, SA[i - 1], pos)
    else:
        for i, pos in enumerate(SA):
            lcps[i] = _longestCommonPrefix(string, string, SA[i - 1], pos)

    if _trace:
        print >> _stderr, "Compute_LCP %.2f%% (%i/%i)\r" % (100.0, length, length)

    if lcps:  # Correct the case where string[0] == string[-1]
        lcps[0] = 0
    return lcps


Instruction:
Write a Python 3 script for
parsing the command line arguments.

Response:
def parseArgv():
    """
    Command line option parser.
    """
    parser = OptionParser()
    parser.usage = r""" cat <TEXT> | %prog [--unit <UNIT>] [--output <SA_FILE>]

Create the suffix array of TEXT with the processing UNIT and optionally store it in SA_FILE for subsequent use.
UNIT may be set to 'byte', 'character' (given an encoding with the --encoding option) or 'word', which is the default.
"""

    parser.add_option("-i", "--input",
                      action="store", type="string", dest="input",
                      default=False,
                      help="Path of the file containing the input text. When '-' is given, read the standard input (default). If the path ends with '.gz', reads the decompressed file.")

    parser.add_option("-o", "--output",
                      action="store", type="string", dest="output",
                      default=False,
                      help="Store the suffix array of the input to the file OUTPUT. When '-' is given, writes to the standard output. If the filename ends with '.gz', the suffix array will be stored  compressed.")

    parser.add_option("", "--load",
                      action="store", type="string", dest="SAFile",
                      default=False,
                      help="Load a suffix array from SAFILE, this option and --input are mutually exclusive.")

    parser.add_option("-u", "--unit",
                      action="store", type="string", dest="unit",
                      default=DEFAULT_UNIT_STR,
                      help="Processing unit used for the creation of the suffix array." + \
                           'Possible values are "byte", "character" and "word". Default is "%s".' % DEFAULT_UNIT_STR + \
                           "This option is ignored when the suffix array is loaded from SAFILE." + \
                           'For characters, the input is decoded according to the encoding set via the option --encoding.')

    parser.add_option("-e", "--encoding",
                      action="store", type="string", dest="encoding",
                      default=DEFAULT_ENCODING,
                      help="Encoding of the input. This information is required only when processing characters. Default is '%s'." % DEFAULT_ENCODING)

    parser.add_option("-p", "--print",
                      action="store_true", dest="printSA",
                      default=False,
                      help="Prints the suffix array in a human readable format to the standard error output.")

    parser.add_option("", "--verbose",
                      action="store_true", dest="verbose",
                      default=False,
                      help="Prints more information.")

    parser.add_option("", "--no-lcps",
                      action="store_true", dest="noLCPs",
                      default=False,
                      help="Switch off the computation of LCPs. By doing so, the find functions are unusable.")

    (options, args) = parser.parse_args(_argv)
    strings = args[1:]
    return (options, strings)


Instruction:
Can you write a function in Python 3 where it
adds a feature to the suffix array.

Response:
def addFeatureSA(self, callback, default=None, name=None):
        """
        Add a feature to the suffix array.
        The callback must return a sequence such that
        the feature at position i is attached to the suffix referenced by
        self.SA[i].

        It is called with one argument: the instance of SuffixArray self.
        The callback may traverse self.SA in any fashion.

        The default behavior is to name the new feature after the callback name.
        To give another name, set the argument name accordingly.

        When the feature of an unknown substring of the text is requested,
        the value of the default argument is used.

        If the feature attached to a suffix is independent of the other suffix
        features, then the method addFeature gives a better alternative.

        You may use addFeatureSA as a decorator as in the following example.

        Example: feature named bigram which attach the frequencies of the
        leading bigram to each suffix.

        >>> SA=SuffixArray("mississippi", unit=UNIT_BYTE)

        >>> def bigram(SA):
        ...     res=[0]*SA.length
        ...     end=0
        ...     while end <= SA.length:
        ...
        ...         begin=end-1
        ...         while end < SA.length and  SA._LCP_values[end]>=2:
        ...             if SA.SA[end]+2<=SA.length: #end of string
        ...                 end+=1
        ...
        ...         nbBigram=end-begin
        ...         for i in xrange(begin, end):
        ...             if SA.SA[i]+2<=SA.length:
        ...                 res[i]=nbBigram
        ...
        ...         end+=1
        ...     return res

        >>> SA.addFeatureSA(bigram, 0)

        >>> SA._bigram_values
        [0, 1, 2, 2, 1, 1, 1, 2, 2, 2, 2]

        >>> print str(SA).expandtabs(14) #doctest: +SKIP
        ...     10        'i'           LCP=0 ,       bigram=0
        ...      7        'ippi'        LCP=1 ,       bigram=1
        ...      4        'issippi'     LCP=1 ,       bigram=2
        ...      1        'ississippi'  LCP=4 ,       bigram=2
        ...      0        'mississipp'  LCP=0 ,       bigram=1
        ...      9        'pi'          LCP=0 ,       bigram=1
        ...      8        'ppi'         LCP=1 ,       bigram=1
        ...      6        'sippi'       LCP=0 ,       bigram=2
        ...      3        'sissippi'    LCP=2 ,       bigram=2
        ...      5        'ssippi'      LCP=1 ,       bigram=2
        ...      2        'ssissippi'   LCP=3 ,       bigram=2

        >>> SA.bigram('ip')
        1

        >>> SA.bigram('si')
        2

        >>> SA.bigram('zw')
        0

        """
        if name is None:
            featureName = callback.__name__
        else:
            featureName = name

        featureValues = callback(self)
        setattr(self, "_%s_values" % featureName, featureValues)
        setattr(self, "%s_default" % featureName, default)
        self.features.append(featureName)

        def findFeature(substring):
            res = self._findOne(substring, )
            if res is not False:
                return featureValues[res]
            else:
                return default

        setattr(self, featureName, findFeature)


Instruction:
How would you implement a function in Python 3 that
adds a feature to the suffix array.

Response:
def addFeature(self, callback, default=None, name=None, arguments=None):
        """
        Add a feature to the suffix array.
        The callback must return the feature corresponding to the suffix at
        position self.SA[i].

        The callback must be callable (a function or lambda).
        The argument names of the callback are used to determine the data
        needed. If an argument is the name of feature already defined, then
        this argument will be the value of that feature for the current suffix.
        In addition the argument pos is the position of the current suffix
        and iSA is the index of pos in SA.
        Other attributes of the SuffixArray instance may be use as argument
        names.

        If the feature attached to a suffix depends on other suffix features,
        then the method addFeatureSA is the only choice.

        """
        if name is None:
            featureName = callback.__name__
        else:
            featureName = name

        if arguments is None:
            signature = getargspec(callback)[0]
        else:
            signature = arguments

        featureValues = [default] * (self.length)
        args = [getattr(self, "_%s_values" % featName) for featName in signature]
        # print args
        for i, pos in enumerate(self.SA):
            arg = [j[i] for j in args]
            # print arg
            featureValues[i] = callback(*arg)
        # end alternative

        setattr(self, "_%s_values" % featureName, featureValues)
        setattr(self, "%s_default" % featureName, default)
        self.features.append(featureName)

        def findFeature(substring):
            res = self._findOne(substring)
            if res:
                return featureValues[res]
            else:
                return default

        setattr(self, featureName, findFeature)


Instruction:
Explain what the following Python 3 code does
def reprString(self, string, length):
        if isinstance(string, int):
            length = min(length, self.length - string)
            string = self.string[string:string + length]

        voc = self.voc
        res = self.tokSep.join((voc[id] for id in string[:length]))
        if self.unit == UNIT_WORD:
            res = res.replace(" \n", "\n")
            res = res.replace("\n ", "\n")

        if self.unit == UNIT_CHARACTER:
            res = res.encode(self.encoding)

        return res

Response: Return a string of length tokens in the original form.


Instruction:
Write a Python 3 script to
save the suffix array instance to a file.

Response:
def toFile(self, filename):
        """
        Save the suffix array instance including all features attached in
        filename. Accept any filename following the _open conventions,
        for example if it ends with .gz the file created will be a compressed
        GZip file.
        """
        start = _time()
        fd = _open(filename, "w")

        savedData = [self.string, self.unit, self.voc, self.vocSize, self.SA, self.features]

        for featureName in self.features:
            featureValues = getattr(self, "_%s_values" % featureName)
            featureDefault = getattr(self, "%s_default" % featureName)

            savedData.append((featureValues, featureDefault))

        fd.write(_dumps(savedData, _HIGHEST_PROTOCOL))
        fd.flush()
        try:
            self.sizeOfSavedFile = getsize(fd.name)
        except OSError:  # if stdout is used
            self.sizeOfSavedFile = "-1"
        self.toFileTime = _time() - start
        if _trace: print >> _stderr, "toFileTime %.2fs" % self.toFileTime

        if _trace: print >> _stderr, "sizeOfSavedFile %sb" % self.sizeOfSavedFile
        fd.close()


Instruction:
Can you tell what is the following Python 3 function doing
def fromFile(cls, filename):
        self = cls.__new__(cls)  # new instance which does not call __init__

        start = _time()

        savedData = _loads(_open(filename, "r").read())

        # load common attributes
        self.string, self.unit, self.voc, self.vocSize, self.SA, features = savedData[:6]
        self.length = len(self.SA)

        # determine token delimiter
        if self.unit == UNIT_WORD:
            self.tokSep = " "
        elif self.unit in (UNIT_CHARACTER, UNIT_BYTE):
            self.tokSep = ""
        else:
            raise Exception("Unknown unit type identifier:", self.unit)

        # recompute tokId based on voc
        self.tokId = dict((char, iChar) for iChar, char in enumerate(self.voc))
        self.nbSentences = self.string.count(self.tokId.get("\n", 0))

        # Load features
        self.features = []
        for featureName, (featureValues, featureDefault) in zip(features, savedData[6:]):
            self.addFeatureSA((lambda _: featureValues), name=featureName, default=featureDefault)

        self.fromFileTime = _time() - start
        if _trace: print >> _stderr, "fromFileTime %.2fs" % self.fromFileTime
        return self

Response: Load a suffix array instance from a file created by toFile.