Instruction:
Make a summary of the following Python 3 code
def to_automaton_(f, labels:Set[Symbol]=None):

    nnf = f.to_nnf()

    if labels is None:
        # if the labels of the formula are not specified in input,
        # retrieve them from the formula
        labels = nnf.find_labels()

    # the alphabet is the powerset of the set of fluents
    alphabet = powerset(labels)
    initial_state = MacroState({nnf})
    final_states = {MacroState()}
    delta = set()

    d = f.delta(PLFalseInterpretation(), epsilon=True)
    if d.truth(d):
        final_states.add(initial_state)

    states = {MacroState(), initial_state}

    states_changed, delta_changed = True, True

    while states_changed or delta_changed:

        states_changed, delta_changed = False, False
        for actions_set in alphabet:
            states_list = list(states)
            for q in states_list:

                # delta function applied to every formula in the macro state Q
                delta_formulas = [f.delta(actions_set) for f in q]

                # find the list of atoms, which are "true" atoms (i.e. propositional atoms) or LDLf formulas
                atomics = [s for subf in delta_formulas for s in find_atomics(subf)]

                # "freeze" the found atoms as symbols and build a mapping from symbols to formulas
                symbol2formula = {Symbol(str(f)): f for f in atomics if f != PLTrue() and f != PLFalse()}

                # build a map from formula to a "freezed" propositional Atomic Formula
                formula2atomic_formulas = {
                    f: PLAtomic(Symbol(str(f)))
                    if f != PLTrue() and f != PLFalse()# and not isinstance(f, PLAtomic)
                    else f for f in atomics
                }

                # the final list of Propositional Atomic Formulas, one for each formula in the original macro state Q
                transformed_delta_formulas = [_transform_delta(f, formula2atomic_formulas) for f in delta_formulas]

                # the empty conjunction stands for true
                if len(transformed_delta_formulas) == 0:
                    conjunctions = PLTrue()
                elif len(transformed_delta_formulas) == 1:
                    conjunctions = transformed_delta_formulas[0]
                else:
                    conjunctions = PLAnd(transformed_delta_formulas)

                # the model in this case is the smallest set of symbols s.t. the conjunction of "freezed" atomic formula
                # is true.
                models = frozenset(conjunctions.minimal_models(Alphabet(symbol2formula)))

                if len(models) == 0:
                    continue
                for min_model in models:
                    q_prime = MacroState(
                        {symbol2formula[s] for s in min_model.true_propositions})

                    len_before = len(states)
                    states.add(q_prime)
                    if len(states) == len_before + 1:
                        states_list.append(q_prime)
                        states_changed = True

                    len_before = len(delta)
                    delta.add((q, actions_set, q_prime))
                    if len(delta) == len_before + 1:
                        delta_changed = True

                    # check if q_prime should be added as final state
                    if len(q_prime) == 0:
                        final_states.add(q_prime)
                    else:
                        subf_deltas = [subf.delta(PLFalseInterpretation(), epsilon=True) for subf in q_prime]
                        if len(subf_deltas)==1:
                            q_prime_delta_conjunction = subf_deltas[0]
                        else:
                            q_prime_delta_conjunction = PLAnd(subf_deltas)

                        if q_prime_delta_conjunction.truth(PLFalseInterpretation()):
                            final_states.add(q_prime)

    alphabet = PythomataAlphabet({PLInterpretation(set(sym)) for sym in alphabet})
    delta = frozenset((i, PLInterpretation(set(a)), o) for i, a, o in delta)

    nfa = NFA.fromTransitions(
        alphabet=alphabet,
        states=frozenset(states),
        initial_state=initial_state,
        accepting_states=frozenset(final_states),
        transitions=delta
    )

    return nfa

Response: DEPRECATED - Builds a NFA from a LDLfFormula.


Instruction:
Implement a Python 3 function for
getting a setting from the settings file.

Response:
def get_setting(key, **kwargs):
    """Gets a settings key or raises an improperly configured error.

    :param key: the settings key to get.
    :param default: the default value to return if no value is found
    """
    has_default = 'default' in kwargs
    default_val = kwargs.get('default')

    try:
        if has_default:
            return getattr(settings, key, default_val)
        else:
            return getattr(settings, key)
    except Exception as e:
        raise ImproperlyConfigured(
            _('"{0}" setting has not been properly set. {1}').format(key, e)
        )


Instruction:
How would you explain what the following Python 3 function does
def get_class_from_settings(settings_key):
    cls_path = getattr(settings, settings_key, None)
    if not cls_path:
        raise NotImplementedError()

    try:
        # First check to see if it's an installed model
        return get_model_from_settings(settings_key=settings_key)
    except:
        try:
            # Next, check from installed apps
            return get_class_from_settings_from_apps(settings_key=settings_key)
        except:
            # Last, try to load from the full path
            return get_class_from_settings_full_path(settings_key)

Response: Gets a class from a setting key.


Instruction:
Can you tell what is the following Python 3 function doing
def get_model_from_settings(settings_key):
    cls_path = getattr(settings, settings_key, None)
    if not cls_path:
        raise NotImplementedError()

    try:
        app_label, model_name = cls_path.split('.')
    except ValueError:
        raise ImproperlyConfigured("{0} must be of the form "
                                   "'app_label.model_name'".format(settings_key))

    model = apps.get_model(app_label, model_name)

    if model is None:
        raise ImproperlyConfigured("{0} refers to model '%s' that has not "
                                   "been installed".format(settings_key))

    return model

Response: Return the django model from a settings key.


Instruction:
Implement a Python 3 function for
trying and get a class from a settings key in the installed apps.

Response:
def get_class_from_settings_from_apps(settings_key):
    """Try and get a class from a settings path by lookin in installed apps.
    """
    cls_path = getattr(settings, settings_key, None)

    if not cls_path:
        raise NotImplementedError()

    try:
        app_label = cls_path.split('.')[-2]
        model_name = cls_path.split('.')[-1]
    except ValueError:
        raise ImproperlyConfigured("{0} must be of the form "
                                   "'app_label.model_name'".format(
                                                                settings_key))

    app = apps.get_app_config(app_label).models_module

    if not app:
        raise ImproperlyConfigured("{0} setting refers to an app that has not "
                                   "been installed".format(settings_key))

    return getattr(app, model_name)


Instruction:
Implement a function in Python 3 to
get a class from it s full path.

Response:
def get_class_from_settings_full_path(settings_key):
    """Get a class from it's full path.

    Example:

    some.path.module.MyClass
    """
    cls_path = getattr(settings, settings_key, None)

    if not cls_path:
        raise NotImplementedError()

    try:
        module_name, class_name = cls_path.rsplit('.', 1)
    except ValueError:
        raise ImproperlyConfigured("{0} must be of the form "
                                   "'some.path.module.MyClass'".format(
                                                                settings_key))

    manager_module = importlib.import_module(module_name)

    if not manager_module:
        raise ImproperlyConfigured("{0} refers to a module that has not been "
                                   "installed".format(settings_key))

    return getattr(manager_module, class_name)


Instruction:
Create a Python 3 function to
get a function from the string path defined in a settings file.

Response:
def get_function_from_settings(settings_key):
    """Gets a function from the string path defined in a settings file.

    Example:

    # my_app/my_file.py
    def some_function():
        # do something
        pass

    # settings.py
    SOME_FUNCTION = 'my_app.my_file.some_function'

    > get_function_from_settings('SOME_FUNCTION')
    <function my_app.my_file.some_function>
    """

    renderer_func_str = getattr(settings, settings_key, None)
    if not renderer_func_str:
        return None

    module_str, renderer_func_name = renderer_func_str.rsplit('.', 1)

    try:
        mod = importlib.import_module(module_str)
        return getattr(mod, renderer_func_name)
    except Exception:
        return None


Instruction:
Can you generate a brief explanation for the following Python 3 code
def caller_locals():
    # type: () -> Dict
    """Return the frame object for the caller's stack frame."""
    try:
        raise ValueError
    except ValueError:
        _, _, tb = sys.exc_info()
        assert tb, "Can't get traceback, this shouldn't happen"
        caller_frame = tb.tb_frame.f_back.f_back
        return caller_frame.f_locals

Response: Return the frame object for the caller s stack frame.


Instruction:
Can you tell what is the following Python 3 function doing
def make_repr(inst, attrs):
    # type: (object, Sequence[str]) -> str
    """Create a repr from an instance of a class

    Args:
        inst: The class instance we are generating a repr of
        attrs: The attributes that should appear in the repr
    """
    arg_str = ", ".join(
        "%s=%r" % (a, getattr(inst, a)) for a in attrs if hasattr(inst, a))
    repr_str = "%s(%s)" % (inst.__class__.__name__, arg_str)
    return repr_str

Response: Create a repr from an instance of a class


Instruction:
Explain what the following Python 3 code does
def default_rdo_tr(mod):
    pkg = mod.rsplit('-python')[0]
    pkg = pkg.replace('_', '-').replace('.', '-').lower()
    if not pkg.startswith('python-'):
        pkg = 'python-' + pkg
    py2pkg = pkg
    py3pkg = re.sub('python', 'python3', pkg)
    return (pkg, py2pkg, py3pkg)

Response: Default translation function for Fedora - RDO based systems


Instruction:
Explain what the following Python 3 code does
def default_ubuntu_tr(mod):
    pkg = 'python-%s' % mod.lower()
    py2pkg = pkg
    py3pkg = 'python3-%s' % mod.lower()
    return (pkg, py2pkg, py3pkg)

Response: Default translation function for Ubuntu based systems


Instruction:
Implement a function in Python 3 to
default translation function for openSUSE SLES and otherSUSE based systems

Response:
def default_suse_tr(mod):
    """
    Default translation function for openSUSE, SLES, and other
    SUSE based systems

    Returns a tuple of 3 elements - the unversioned name, the python2 versioned
    name and the python3 versioned name.
    """
    pkg = 'python-%s' % mod
    py2pkg = 'python2-%s' % mod
    py3pkg = 'python3-%s' % mod
    return (pkg, py2pkg, py3pkg)


Instruction:
Make a summary of the following Python 3 code
def module2package(mod, dist, pkg_map=None, py_vers=('py',)):
    if not pkg_map:
        pkg_map = get_pkg_map(dist)
    for rule in pkg_map:
        pkglist = rule(mod, dist)
        if pkglist:
            break
    else:
        tr_func = get_default_tr_func(dist)
        pkglist = tr_func(mod)

    output = []
    for v in py_vers:
        if v == 'py':
            output.append(pkglist[0])
        elif v == 'py2':
            output.append(pkglist[1])
        elif v == 'py3':
            output.append(pkglist[2])
        else:
            raise Exception('Invalid version "%s"' % (v))

    if len(output) == 1:
        # just return a single value (backwards compatible)
        return output[0]
    else:
        return output

Response: Return a corresponding package name for a python module.


Instruction:
Create a Python 3 function to
return a corresponding OpenStack upstream name for a python module.

Response:
def module2upstream(mod):
    """Return a corresponding OpenStack upstream name for a python module.

    mod  -- python module name
    """
    for rule in OPENSTACK_UPSTREAM_PKG_MAP:
        pkglist = rule(mod, dist=None)
        if pkglist:
            return pkglist[0]
    return mod


Instruction:
How would you explain what the following Python 3 function does
def main():
    parser = argparse.ArgumentParser(description='Python module name to'
                                     'package name')
    group = parser.add_mutually_exclusive_group()
    group.add_argument('--dist', help='distribution style '
                       '(default: %(default)s)',
                       default=platform.linux_distribution()[0])
    group.add_argument('--upstream', help='map to OpenStack project name',
                       action='store_true')
    parser.add_argument('--pyver', help='Python versions to return. "py" is '
                        'the unversioned name',
                        action='append', choices=['py', 'py2', 'py3'],
                        default=[])
    parser.add_argument('modulename', help='python module name')
    args = vars(parser.parse_args())

    pyversions = args['pyver'] if args['pyver'] else ['py']

    if args['upstream']:
        print(module2upstream(args['modulename']))
    else:
        pylist = module2package(args['modulename'], args['dist'],
                                py_vers=pyversions)
        # When only 1 version is requested, it will be returned as a string,
        # for backwards compatibility. Else, it will be a list.
        if type(pylist) is list:
            print(' '.join(pylist))
        else:
            print(pylist)

Response: for resolving names from command line


Instruction:
Can you generate the documentation for the following Python 3 function
def get_paging(self):
        orig_page_num = self.page_num
        orig_page_size = self.page_size

        try:

            page_num = int(self.request.GET.get(self.page_kwarg or 'p'))

            if page_num < 1:
                page_num = orig_page_num
        except:
            page_num = orig_page_num

        try:
            orig_page_size = self.page_size
            page_size = int(self.request.GET.get(self.page_size_kwarg or 'ps'))

            if page_size < 1:
                page_size = orig_page_size
        except:
            page_size = orig_page_size

        return page_num, page_size

Response: Gets the paging values passed through the query string params.


Instruction:
Implement a Python 3 function for
making a call_types dictionary that describes what arguments to pass to f Taxonomy

Response:
def make_call_types(f, globals_d):
    # type: (Callable, Dict) -> Tuple[Dict[str, Anno], Anno]
    """Make a call_types dictionary that describes what arguments to pass to f

    Args:
        f: The function to inspect for argument names (without self)
        globals_d: A dictionary of globals to lookup annotation definitions in
    """
    arg_spec = getargspec(f)
    args = [k for k in arg_spec.args if k != "self"]

    defaults = {}  # type: Dict[str, Any]
    if arg_spec.defaults:
        default_args = args[-len(arg_spec.defaults):]
        for a, default in zip(default_args, arg_spec.defaults):
            defaults[a] = default

    if not getattr(f, "__annotations__", None):
        # Make string annotations from the type comment if there is one
        annotations = make_annotations(f, globals_d)
    else:
        annotations = f.__annotations__

    call_types = OrderedDict()  # type: Dict[str, Anno]
    for a in args:
        anno = anno_with_default(annotations[a], defaults.get(a, NO_DEFAULT))
        assert isinstance(anno, Anno), \
            "Argument %r has type %r which is not an Anno" % (a, anno)
        call_types[a] = anno

    return_type = anno_with_default(annotations.get("return", None))
    if return_type is Any:
        return_type = Anno("Any return value", Any, "return")
    assert return_type is None or isinstance(return_type, Anno), \
        "Return has type %r which is not an Anno" % (return_type,)

    return call_types, return_type


Instruction:
Write a Python 3 function that can
create an annotations dictionary from Python 2 type comments and type idents.

Response:
def make_annotations(f, globals_d=None):
    # type: (Callable, Dict) -> Dict[str, Any]
    """Create an annotations dictionary from Python2 type comments

    http://mypy.readthedocs.io/en/latest/python2.html

    Args:
        f: The function to examine for type comments
        globals_d: The globals dictionary to get type idents from. If not
            specified then make the annotations dict contain strings rather
            than the looked up objects
    """
    locals_d = {}  # type: Dict[str, Any]
    if globals_d is None:
        # If not given a globals_d then we should just populate annotations with
        # the strings in the type comment.
        globals_d = {}
        # The current approach is to use eval, which means manufacturing a
        # dict like object that will just echo the string back to you. This
        # has a number of complexities for somthing like numpy.number or
        # Callable[..., int], which are handled in EchoStr above, so it might be
        # better off as an ast.parse in the future...
        locals_d = EchoDict()
    lines, _ = inspect.getsourcelines(f)
    arg_spec = getargspec(f)
    args = list(arg_spec.args)
    if arg_spec.varargs is not None:
        args.append(arg_spec.varargs)
    if arg_spec.keywords is not None:
        args.append(arg_spec.keywords)
    it = iter(lines)
    types = []  # type: List
    found = None
    for token in tokenize.generate_tokens(lambda: next(it)):
        typ, string, start, end, line = token
        if typ == tokenize.COMMENT:
            found = type_re.match(string)
            if found:
                parts = found.groups()
                # (...) is used to represent all the args so far
                if parts[0] != "(...)":
                    expr = parts[0].replace("*", "")
                    try:
                        ob = eval(expr, globals_d, locals_d)
                    except Exception as e:
                        raise ValueError(
                            "Error evaluating %r: %s" % (expr, e))
                    if isinstance(ob, tuple):
                        # We got more than one argument
                        types += list(ob)
                    else:
                        # We got a single argument
                        types.append(ob)
                if parts[1]:
                    # Got a return, done
                    try:
                        ob = eval(parts[2], globals_d, locals_d)
                    except Exception as e:
                        raise ValueError(
                            "Error evaluating %r: %s" % (parts[2], e))
                    if args and args[0] in ["self", "cls"]:
                        # Allow the first argument to be inferred
                        if len(args) == len(types) + 1:
                            args = args[1:]
                    assert len(args) == len(types), \
                        "Args %r Types %r length mismatch" % (args, types)
                    ret = dict(zip(args, types))
                    ret["return"] = ob
                    return ret
    if found:
        # If we have ever found a type comment, but not the return value, error
        raise ValueError("Got to the end of the function without seeing ->")
    return {}


Instruction:
Can you write a function in Python 3 where it
overrides get_object to set the object in the object attribute.

Response:
def get_object(self, **kwargs):
        """Sometimes preprocessing of a view need to happen before the object
        attribute has been set for a view.  In this case, just return the
        object if it has already been set when it's called down the road since
        there's no need to make another query.
        """
        if hasattr(self, 'object') and self.object:
            return self.object

        obj = super(CommonSingleObjectViewMixin, self).get_object(**kwargs)
        self.object = obj
        return obj


Instruction:
Write a Python 3 script to
convert a hex timestamp to a datetime object.

Response:
def hex_timestamp_to_datetime(hex_timestamp):
    """Converts hex timestamp to a datetime object.

    >>> hex_timestamp_to_datetime('558BBCF9')
    datetime.datetime(2015, 6, 25, 8, 34, 1)
    >>> hex_timestamp_to_datetime('0x558BBCF9')
    datetime.datetime(2015, 6, 25, 8, 34, 1)
    >>> datetime.fromtimestamp(0x558BBCF9)
    datetime.datetime(2015, 6, 25, 8, 34, 1)
    """
    if not hex_timestamp.startswith('0x'):
        hex_timestamp = '0x{0}'.format(hex_timestamp)

    return datetime.fromtimestamp(int(hex_timestamp, 16))


Instruction:
Explain what the following Python 3 code does
def now_by_tz(tz='US/Central', ignoretz=True):
    if isinstance(tz, string_types):
        tz = pytz.timezone(tz)

    if ignoretz:
        return datetime.now(tz).replace(tzinfo=None)
    return datetime.now(tz)

Response: Gets the current datetime object by timezone.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def tz_to_utc(dt, tz, ignoretz=True):
    if isinstance(tz, string_types):
        tz = pytz.timezone(tz)

    dt = tz.localize(dt)
    dt = datetime.astimezone(dt, pytz.timezone('UTC'))

    if ignoretz:
        return dt.replace(tzinfo=None)
    return dt

Response: Converts a datetime object from the specified timezone to a UTC datetime.


Instruction:
Make a summary of the following Python 3 code
def utc_to_tz(dt, tz, ignoretz=True):
    if isinstance(tz, string_types):
        tz = pytz.timezone(tz)

    dt = pytz.utc.localize(dt)
    dt = dt.astimezone(tz)

    if ignoretz:
        return dt.replace(tzinfo=None)
    return dt

Response: Converts the given UTC datetime object to the specific timezone.


Instruction:
How would you explain what the following Python 3 function does
def parse_date(dt, ignoretz=True, as_tz=None):
    dttm = parse_datetime(dt, ignoretz=ignoretz)
    return None if dttm is None else dttm.date()

Response: Parse a date string into a date object.


Instruction:
Can you create a Python 3 function that
parses a string datetime into a datetime object.

Response:
def parse_datetime(dt, ignoretz=True, **kwargs):
    """
    :param dt: string datetime to convert into datetime object.
    :return: datetime object if the string can be parsed into a datetime.
        Otherwise, return None.

    :see: http://labix.org/python-dateutil

    Examples:

    >>> parse_datetime('2011-12-30 13:45:12 CDT')
    2011-12-30 13:45:12
    >>> parse_datetime('12/30/2011 13:45:12 CDT')
    2011-12-30 13:45:12
    >>> parse_datetime('2011-12-30 13:45:12 CDT', ignoretz=False)
    2011-12-30 13:45:12-06:00
    >>> parse_datetime('12/30/2011 13:45:12 CDT', ignoretz=False)
    2011-12-30 13:45:12-06:00

    """
    try:
        return parse(dt, ignoretz=ignoretz, **kwargs)
    except:
        return None


Instruction:
How would you explain what the following Python 3 function does
def turn_emails_off(view_func):

    # Dummy email backend so no emails are sent.
    EMAIL_BACKEND_DUMMY = 'django.core.mail.backends.dummy.EmailBackend'

    def decorated(request, *args, **kwargs):
        orig_email_backend = settings.EMAIL_BACKEND
        settings.EMAIL_BACKEND = EMAIL_BACKEND_DUMMY

        response = view_func(request, *args, **kwargs)
        settings.EMAIL_BACKEND = orig_email_backend
        return response

    return decorated

Response: Turns emails off so no emails will be sent.


Instruction:
Implement a Python 3 function for
getting the md5 hash for a file.

Response:
def get_md5_for_file(file):
    """Get the md5 hash for a file.

    :param file: the file to get the md5 hash for
    """
    md5 = hashlib.md5()

    while True:
        data = file.read(md5.block_size)

        if not data:
            break

        md5.update(data)

    return md5.hexdigest()


Instruction:
Make a summary of the following Python 3 code
def get_dict_from_json_file(path, encoding='utf-8'):
    with open(path, encoding=encoding) as data_file:
        return json.loads(data_file.read())

Response: Gets a dict of data form a json file.


Instruction:
Given the following Python 3 function, write the documentation
def linebreaks_safe(value, autoescape=True):
    if isinstance(value, string_types) and '\n' in value:
        return linebreaks_filter(value, autoescape=autoescape)

    return value

Response: Adds linebreaks only for text that has a newline character.


Instruction:
Can you tell what is the following Python 3 function doing
def add_sub_resource(self, relative_id, sub_resource):
        existing_sub_resources = self.resources.get(sub_resource.RELATIVE_PATH_TEMPLATE, defaultdict(list))
        existing_sub_resources[relative_id].append(sub_resource)
        self.resources.update({sub_resource.RELATIVE_PATH_TEMPLATE: existing_sub_resources})

Response: Add a sub resource to the resource list


Instruction:
Can you implement a function in Python 3 that
returns the name of the CloudShell model

Response:
def cloudshell_model_name(self):
        """Return the name of the CloudShell model"""
        if self.shell_name:
            return "{shell_name}.{resource_model}".format(shell_name=self.shell_name,
                                                          resource_model=self.RESOURCE_MODEL.replace(" ", ""))
        else:
            return self.RESOURCE_MODEL


Instruction:
Implement a Python 3 function for
slicing a correlation matrix of a y - dimensional dataset and a p - value matrix of a y - dimensional dataset

Response:
def slice_yx(r, pval, ydim=1):
    """slice a correlation and p-value matrix of a (y,X) dataset
    into a (y,x_i) vector and (x_j, x_k) matrices

    Parameters
    ----------
    r : ndarray
        Correlation Matrix of a (y,X) dataset

    pval : ndarray
        p-values

    ydim : int
        Number of target variables y, i.e. the first ydim-th columns
        and rows are (y, x_i) correlations

    Returns
    -------
    y_r : ndarray
        1D vector or ydim-column array with (y,x) correlations

    y_pval : ndarray
        1D vector or ydim-column array with (y,x) p-values

    x_r : ndarray
        correlation matrix (x_j, x_k)

    x_pval : ndarar
        matrix with p-values

    Example
    -------
        import korr
        r, pval = korr.mcc(np.c_[y, X])
        y_r, y_pval, x_r, x_pval = slice_yx(r, pval, ydim=1)
        print(np.c_[y_r, y_pval])
        korr.corrgram(x_r, x_pval)
    """
    if ydim is 1:
        return (
            r[1:, :1].reshape(-1, ), pval[1:, :1].reshape(-1, ),
            r[1:, 1:], pval[1:, 1:])
    else:
        return (
            r[ydim:, :ydim], pval[ydim:, :ydim],
            r[ydim:, ydim:], pval[ydim:, ydim:])


Instruction:
Can you tell what is the following Python 3 function doing
def parse_expression(source: str) -> ExpressionSource:
    '''Returns tuple with expression type and expression body'''
    if not is_expression(source):
        msg = 'Expression is not valid. Expression should be matched with regular expression: {0}'\
              .format(EXPRESSION_REGEX)
        raise ExpressionError(msg, source)
    if not source.startswith('{'):
        [type_, source] = source.split(':', 1)
    elif source.endswith('}}'):
        type_ = 'twoways'
    else:
        type_ = 'oneway'
    return (type_, source[1:-1])

Response: Returns tuple with expression type and expression body


Instruction:
Given the following Python 3 function, write the documentation
def _server_whitelist(self):
        '''
        Returns list of servers that have not errored in the last five minutes.
        If all servers have errored in the last five minutes, returns list with
        one item, the server that errored least recently.
        '''
        whitelist = []
        for server in self.servers:
            if (server not in self.last_error
                    or self.last_error[server] < time.time() - self.PENALTY_BOX_TIME):
                whitelist.append(server)
        if not whitelist:
            whitelist.append(sorted(
                self.last_error.items(), key=lambda kv: kv[1])[0][0])
        return whitelist

Response: Returns a list of servers that have not errored in the last five minutes.


Instruction:
Here you have a function in Python 3, explain what it does
def create_networking_resource_from_context(shell_name, supported_os, context):

    result = GenericNetworkingResource(shell_name=shell_name, name=context.resource.name, supported_os=supported_os)
    result.address = context.resource.address
    result.family = context.resource.family
    result.fullname = context.resource.fullname

    result.attributes = dict(context.resource.attributes)

    return result

Response: Creates an instance of Networking Resource by given context


Instruction:
Can you generate the documentation for the following Python 3 function
def find_worst(rho, pval, m=1, rlim=.10, plim=.35):
    # convert to lists
    n = len(rho)
    r = list(np.abs(rho))
    p = list(pval)
    i = list(range(n))

    # check m
    if m > n:
        warnings.warn(
            'm is bigger than the available correlations in rho and pval.')
        m = n

    # selected indicies
    selected = list()
    # (1) pick the highest/worst p-value
    #   |r| <= r_lim
    #    p  >  p_lim
    it = 0
    while (len(selected) < m) and (it < n):
        temp = p.index(max(p))  # temporary index of the remaining values
        worst = i[temp]  # store original index  as 'worst' before abort loop
        # check
        if (r[temp] <= rlim) and (p[temp] > plim):
            # delete from lists
            r.pop(temp)
            p.pop(temp)
            i.pop(temp)
            # append to abort
            selected.append(worst)
        # next step
        it = it + 1
    # print(selected, i)

    # (2) Just pick the highest/worst p-value of the remaining
    # with bad correlations
    #    |r| <=  r_lim
    it = 0
    n2 = len(i)
    while (len(selected) < m) and (it < n2):
        temp = p.index(max(p))  # temporary index of the remaining values
        worst = i[temp]  # store original index  as 'worst' before abort loop
        # check
        if (r[temp] <= rlim):
            # delete from lists
            r.pop(temp)
            p.pop(temp)
            i.pop(temp)
            # append to abort
            selected.append(worst)
        # next step
        it = it + 1

    # (3) Pick the lowest correlations
    it = 0
    n3 = len(i)
    while (len(selected) < m) and (it < n3):
        # find the smallest p-value
        temp = r.index(min(r))
        worst = i[temp]
        # delete from lists
        r.pop(temp)
        p.pop(temp)
        i.pop(temp)
        # append to abort
        selected.append(worst)
        # next step
        it = it + 1

    return selected

Response: Find the N worst correlations in a given set of correlation coefficients.


Instruction:
Here you have a function in Python 3, explain what it does
def _parse_array(value):
    value = value.lstrip()
    if not value or value[0] not in _bracket_strings:
        return None
    return json.loads(value)

Response: Coerce value into a list.


Instruction:
Create a Python 3 function to
coerce value into a boolean.

Response:
def _parse_boolean(value):
    """Coerce value into an bool.

    :param str value: Value to parse.
    :returns: bool or None if the value is not a boolean string.
    """
    value = value.lower()
    if value in _true_strings:
        return True
    elif value in _false_strings:
        return False
    else:
        return None


Instruction:
Implement a Python 3 function for
coercing value into a dict.

Response:
def _parse_object(value):
    """Coerce value into a dict.

    :param str value: Value to parse.
    :returns: dict or None if the value is not a JSON object
    :raises: TypeError or ValueError if value appears to be an object but can't
        be parsed as JSON.
    """
    value = value.lstrip()
    if not value or value[0] not in _brace_strings:
        return None
    return json.loads(value)


Instruction:
Can you tell what is the following Python 3 function doing
def parse_value(value, allowed_types, name='value'):
    if not isinstance(value, str):
        raise ValueError('value for %r must be a string' % name)
    if isinstance(allowed_types, str):
        allowed_types = [allowed_types]

    # Note that the order of these type considerations is important. Because we
    # have an untyped value that may be one of any given number of types, we
    # need a consistent order of evaluation in cases when there is ambiguity
    # between types.

    if 'null' in allowed_types and value == '':
        return 'null', None

    # For all of these types, we'll pass the value to the function and it will
    # raise a TypeError or ValueError or return None if it can't be parsed as
    # the given type.

    for allowed_type, parser in _parser_funcs:
        if allowed_type in allowed_types:
            try:
                parsed_value = parser(value)
                if parsed_value is not None:
                    return allowed_type, parsed_value
            except (TypeError, ValueError):
                # Ignore any errors, and continue trying other types
                pass

    raise ParseError('%s must be a valid type (%s)' %
                     (name, ', '.join(allowed_types)))

Response: Parses a value into one of the allowed types.


Instruction:
Given the following Python 3 function, write the documentation
def parse_json(value: str, sig_params: List[inspect.Parameter] = None) -> dict:
    try:
        loaded = json.loads(value)
    except Exception as e:
        message = 'Error parsing JSON: %r error: %s' % (value, e)
        logging.debug(message, exc_info=e)
        raise ParseError(message)

    if sig_params is not None:
        return map_param_names(loaded, sig_params)
    return loaded

Response: Parse a value as JSON.


Instruction:
Implement a function in Python 3 to
map request param names to match logic function param names.

Response:
def map_param_names(
        req_params: dict, sig_params: List[inspect.Parameter]) -> dict:
    """Maps request param names to match logic function param names.

    If a doctor type defined a `param_name` attribute for the name of the
    parameter in the request, we should use that as the key when looking up
    the value for the request parameter.

    When we declare a type we can specify what the parameter name
    should be in the request that the annotated type should get mapped to.

    >>> from doctor.types import number
    >>> Latitude = number('The latitude', param_name='location.lat')
    >>> def my_logic(lat: Latitude): pass
    >>> request_params = {'location.lat': 45.2342343}

    In the above example doctor knows to pass the value at key `location.lat`
    to the logic function variable named `lat` since it's annotated by the
    `Latitude` type which specifies what the param_name is on the request.

    :param dict req_params: The parameters specified in the request.
    :param dict sig_params: The logic function's signature parameters.
    :returns: A dict of re-mapped params.
    """
    new_request_params = {}
    for k, param in sig_params.items():
        param_name = getattr(param.annotation, 'param_name', None)
        key = k if param_name is None else param_name
        if key in req_params:
            new_request_params[k] = req_params[key]
    return new_request_params


Instruction:
Here you have a function in Python 3, explain what it does
def parse_form_and_query_params(req_params: dict, sig_params: dict) -> dict:
    # Importing here to prevent circular dependencies.
    from doctor.types import SuperType, UnionType
    errors = {}
    parsed_params = {}
    for param, value in req_params.items():
        # Skip request variables not in the function signature.
        if param not in sig_params:
            continue
        # Skip coercing parameters not annotated by a doctor type.
        if not issubclass(sig_params[param].annotation, SuperType):
            continue

        # Check if the type has a custom parser for the parameter.
        custom_parser = sig_params[param].annotation.parser
        if custom_parser is not None:
            if not callable(custom_parser):
                warnings.warn(
                    'Parser `{}` is not callable, using default parser.'.format(
                        custom_parser))
                custom_parser = None

        try:
            if custom_parser is not None:
                parsed_params[param] = custom_parser(value)
            else:
                if issubclass(sig_params[param].annotation, UnionType):
                    json_type = [
                        _native_type_to_json[_type.native_type]
                        for _type in sig_params[param].annotation.types
                    ]
                else:
                    native_type = sig_params[param].annotation.native_type
                    json_type = [_native_type_to_json[native_type]]
                # If the type is nullable, also add null as an allowed type.
                if sig_params[param].annotation.nullable:
                    json_type.append('null')
                _, parsed_params[param] = parse_value(value, json_type)
        except ParseError as e:
            errors[param] = str(e)

    if errors:
        raise TypeSystemError(errors, errors=errors)

    return parsed_params

Response: Parses the form and query parameters from the input dict.


Instruction:
Given the following Python 3 function, write the documentation
def _handle_task(self):
        headers = self.request.headers

        message = None
        try:
            status_code, output = process_async_task(
                headers, self.request.body)
        except AbortAndRestart as restart:
            # Async retry status code
            status_code = 549
            message = 'Retry Async Task'
            output = str(restart)

        self.response.set_status(status_code, message)
        self.response.out.write(output)

Response: Pass the request info to the async framework.


Instruction:
Can you implement a function in Python 3 that
migrates autoload details. Add namespace for resources and attributes.

Response:
def migrate_autoload_details(autoload_details, shell_name, shell_type):
    """ Migrate autoload details. Add namespace for attributes

    :param autoload_details:
    :param shell_name:
    :param shell_type:
    :return:
    """

    mapping = {}
    for resource in autoload_details.resources:
        resource.model = "{shell_name}.{model}".format(shell_name=shell_name, model=resource.model)
        mapping[resource.relative_address] = resource.model

    for attribute in autoload_details.attributes:

        if not attribute.relative_address:  # Root element
            attribute.attribute_name = "{shell_type}.{attr_name}".format(shell_type=shell_type,
                                                                         attr_name=attribute.attribute_name)
        else:
            attribute.attribute_name = "{model}.{attr_name}".format(model=mapping[attribute.relative_address],
                                                                    attr_name=attribute.attribute_name)

    return autoload_details


Instruction:
Can you tell what is the following Python 3 function doing
def create_firewall_resource_from_context(shell_name, supported_os, context):

    result = GenericFirewallResource(shell_name=shell_name, name=context.resource.name, supported_os=supported_os)
    result.address = context.resource.address
    result.family = context.resource.family
    result.fullname = context.resource.fullname

    result.attributes = dict(context.resource.attributes)

    return result

Response: Creates an instance of FirewallResource from given context


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _get_convert_method(self):
        for type_, method in self._map.items():
            if type(self.value) is bool and type_ is not bool:
                continue
            if isinstance(self.value, type_):
                return method
        if is_sql_instance(self.value):
            return self._raw
        return self._undefined

Response: Get right method to convert the value.


Instruction:
Can you write a function in Python 3 where it
flattens correlation matrix and p - value matrix into a single table.

Response:
def flatten(rho, pval, sortby="cor"):
    """Flatten correlation and p-value matrix

    Parameters:
    -----------
    rho : ndarray
        Correlation Matrix

    pval : ndarray
        Matrix with p-values

    sortby : str
        sort the output table by
        - "cor"  the highest absolute correlation coefficient
        - "pval" the lowest p-value

    Return:
    -------
    tab : ndarray
        Table with (i, j, cor, pval) rows

    Example:
    --------
        from korr import pearson, flatten
        rho, pval = pearson(X)
        tab = flatten(rho, pval, sortby="pval")
        tab.values
    """
    n = rho.shape[0]

    idx = np.triu_indices(n, k=1)

    tab = pd.DataFrame(
        columns=['i', 'j', 'cor', 'pval'],
        data=np.c_[idx[0], idx[1], rho[idx], pval[idx]])
    tab[['i', "j"]] = tab[['i', "j"]].astype(int)

    if sortby == "cor":
        tab['abscor'] = np.abs(tab['cor'])
        tab.sort_values(by='abscor', inplace=True, ascending=False)
    elif sortby == "pval":
        tab.sort_values(by='pval', inplace=True, ascending=True)

    return tab[["i", "j", "cor", "pval"]]


Instruction:
Explain what the following Python 3 code does
def max(self) -> int:
        if self.signed:
            v = (1 << (self.bits - 1)) - 1
        else:
            v = (1 << self.bits) - 1
        if self.lsb0:
            v = v - (v % 2)
        return v

Response: Get the maximum value this immediate can have


Instruction:
Can you generate a brief explanation for the following Python 3 code
def set(self, value: int):
        if not isinstance(value, int):
            raise self.exception("{} is not an integer".format(value))
        if self.lsb0 and self.value % 2 == 1:
            raise self.exception("{} not power of two".format(value))
        if not self.signed and value < 0:
            raise self.exception("{} cannot be negative".format(value))
        if value < self.min() or value > self.max():
            raise self.exception("{} not in allowed range {}-{}".format(value, self.min(), self.max()))

        self.value = value

Response: Sets the immediate value of the entry.


Instruction:
Can you tell what is the following Python 3 function doing
def set_from_bits(self, value: int):
        if self.signed:
            value = -(value & self.tcmask) + (value & ~self.tcmask)
        self.set(value)

Response: Set the immediate value from machine code bits.


Instruction:
Here you have a function in Python 3, explain what it does
def randomize(self):
        self.value = randint(self.min(), self.max())
        if self.lsb0:
            self.value = self.value - (self.value % 2)

Response: Randomize this immediate to a legal value.


Instruction:
Can you generate the documentation for the following Python 3 function
def _init():
    # If there is a context and it is initialized to this request,
    # return, otherwise reinitialize the _local_context.
    if (hasattr(_local_context, '_initialized') and
            _local_context._initialized == os.environ.get('REQUEST_ID_HASH')):
        return

    # Used to track the context object stack.
    _local_context.registry = []

    # Used to provide easy access to the currently running Async job.
    _local_context._executing_async_context = None
    _local_context._executing_async = []

    # So that we do not inadvertently reinitialize the local context.
    _local_context._initialized = os.environ.get('REQUEST_ID_HASH')

    return _local_context

Response: Initialize the furious context and registry.


Instruction:
Create a Python 3 function to
take an async object and executes its job.

Response:
def run_job():
    """Takes an async object and executes its job."""
    async = get_current_async()
    async_options = async.get_options()

    job = async_options.get('job')
    if not job:
        raise Exception('This async contains no job to execute!')

    __, args, kwargs = job

    if args is None:
        args = ()

    if kwargs is None:
        kwargs = {}

    function = async._decorate_job()

    try:
        async.executing = True
        async.result = AsyncResult(payload=function(*args, **kwargs),
                                   status=AsyncResult.SUCCESS)
    except Abort as abort:
        logging.info('Async job was aborted: %r', abort)
        async.result = AsyncResult(status=AsyncResult.ABORT)

        # QUESTION: In this eventuality, we should probably tell the context we
        # are "complete" and let it handle completion checking.
        _handle_context_completion_check(async)
        return
    except AbortAndRestart as restart:
        logging.info('Async job was aborted and restarted: %r', restart)
        raise
    except BaseException as e:
        async.result = AsyncResult(payload=encode_exception(e),
                                   status=AsyncResult.ERROR)

    _handle_results(async_options)
    _handle_context_completion_check(async)


Instruction:
Can you tell what is the following Python 3 function doing
def _handle_results(options):
    results_processor = options.get('_process_results')
    if not results_processor:
        results_processor = _process_results

    processor_result = results_processor()
    if isinstance(processor_result, (Async, Context)):
        processor_result.start()

Response: Process the results of executing the Async s target.


Instruction:
How would you code a function in Python 3 to
encode an exception to a form that can be passed around and serialized.

Response:
def encode_exception(exception):
    """Encode exception to a form that can be passed around and serialized.

    This will grab the stack, then strip off the last two calls which are
    encode_exception and the function that called it.
    """
    import sys
    return AsyncException(unicode(exception),
                          exception.args,
                          sys.exc_info(),
                          exception)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _process_results():
    async = get_current_async()
    callbacks = async.get_callbacks()

    if not isinstance(async.result.payload, AsyncException):
        callback = callbacks.get('success')
    else:
        callback = callbacks.get('error')

        if not callback:
            raise async.result.payload.exception, None, \
                async.result.payload.traceback[2]

    return _execute_callback(async, callback)

Response: Process the results from an Async job.


Instruction:
Can you write a function in Python 3 where it
executes the given callback or insert the Async callback or return the result.

Response:
def _execute_callback(async, callback):
    """Execute the given callback or insert the Async callback, or if no
    callback is given return the async.result.
    """
    from furious.async import Async

    if not callback:
        return async.result.payload

    if isinstance(callback, Async):
        return callback.start()

    return callback()


Instruction:
Write a Python 3 script for
rendering a cURL command for the given request.

Response:
def get_example_curl_lines(method: str, url: str, params: dict,
                           headers: dict) -> List[str]:
    """Render a cURL command for the given request.

    :param str method: HTTP request method (e.g. "GET").
    :param str url: HTTP request URL.
    :param dict params: JSON body, for POST and PUT requests.
    :param dict headers: A dict of HTTP headers.
    :returns: list
    """
    parts = ['curl {}'.format(pipes.quote(url))]
    parts.append('-X {}'.format(method))
    for header in headers:
        parts.append("-H '{}: {}'".format(header, headers[header]))
    if method not in ('DELETE', 'GET'):
        # Don't append a json body if there are no params.
        if params:
            parts.append("-H 'Content-Type: application/json' -d")
            pretty_json = json.dumps(params, separators=(',', ': '), indent=4,
                                     sort_keys=True)
            # add indentation for the closing bracket of the json body
            json_lines = pretty_json.split('\n')
            json_lines[-1] = '   ' + json_lines[-1]
            pretty_json = '\n'.join(json_lines)
            parts.append(pipes.quote(pretty_json))
    wrapped = [parts.pop(0)]
    for part in parts:
        if len(wrapped[-1]) + len(part) < 80:
            wrapped[-1] += ' ' + part
        else:
            wrapped[-1] += ' \\'
            wrapped.append('  ' + part)
    return wrapped


Instruction:
Here you have a function in Python 3, explain what it does
def get_example_lines(headers: Dict[str, str], method: str, url: str,
                      params: Dict[str, Any], response: str) -> List[str]:
    """Render a reStructuredText example for the given request and response.

    :param dict headers: A dict of HTTP headers.
    :param str method: HTTP request method (e.g. "GET").
    :param str url: HTTP request URL.
    :param dict params: Form parameters, for POST and PUT requests.
    :param str response: Text response body.
    :returns: list
    """
    lines = ['', 'Example Request:', '', '.. code-block:: bash', '']
    lines.extend(prefix_lines(
        get_example_curl_lines(method, url, params, headers), '   '))
    lines.extend(['', 'Example Response:', ''])
    try:
        # Try to parse and prettify the response as JSON. If it fails
        # (for whatever reason), we'll treat it as text instead.
        response = json.dumps(json.loads(response), indent=2,
                              separators=(',', ': '), sort_keys=True)
        lines.extend(['.. code-block:: json', ''])
    except Exception:
        lines.extend(['.. code-block:: text', ''])
    lines.extend(prefix_lines(response, '   '))
    return lines

Response: Render a reStructuredText example for the given request and response.


Instruction:
Can you implement a function in Python 3 that
gets an object reference string from the obj instance.

Response:
def get_object_reference(obj: Object) -> str:
    """Gets an object reference string from the obj instance.

    This adds the object type to ALL_RESOURCES so that it gets documented and
    returns a str which contains a sphinx reference to the documented object.

    :param obj: The Object instance.
    :returns: A sphinx docs reference str.
    """
    resource_name = obj.title
    if resource_name is None:
        class_name = obj.__name__
        resource_name = class_name_to_resource_name(class_name)
    ALL_RESOURCES[resource_name] = obj
    return ' See :ref:`resource-{}`.'.format(
        '-'.join(resource_name.split(' ')).lower().strip())


Instruction:
Write a Python 3 function for
returning a string documenting what types the array s items should be documented.

Response:
def get_array_items_description(item: Array) -> str:
    """Returns a description for an array's items.

    :param item: The Array type whose items should be documented.
    :returns: A string documenting what type the array's items should be.
    """
    desc = ''
    if isinstance(item.items, list):
        # This means the type has a list of types where each position is
        # mapped to a different type.  Document what each type should be.
        desc = ''
        item_pos_template = (
            ' *Item {pos} must be*: {description}{enum}{ref}')
        for pos, item in enumerate(item.items):
            _enum = ''
            ref = ''
            if issubclass(item, Enum):
                _enum = ' Must be one of: `{}`'.format(item.enum)
                if item.case_insensitive:
                    _enum += ' (case-insensitive)'
                _enum += '.'
            elif issubclass(item, Object):
                ref = get_object_reference(item)

            desc += item_pos_template.format(
                pos=pos, description=item.description, enum=_enum,
                ref=ref)

    else:
        # Otherwise just document the type assigned to `items`.
        desc = item.items.description
        _enum = ''
        ref = ''
        if issubclass(item.items, Enum):
            _enum = ' Must be one of: `{}`'.format(
                item.items.enum)
            if item.items.case_insensitive:
                _enum += ' (case-insensitive)'
            _enum += '.'
        elif issubclass(item.items, Object):
            ref = get_object_reference(item.items)
        desc = '  *Items must be*: {description}{enum}{ref}'.format(
            description=desc, enum=_enum, ref=ref)
    return desc


Instruction:
Can you implement a function in Python 3 that
returns the json types for the provided annotated type.

Response:
def get_json_types(annotated_type: SuperType) -> List[str]:
    """Returns the json types for the provided annotated type.

    This handles special cases for when we encounter UnionType and an Array.
    UnionType's will have all valid types returned.  An Array will document
    what the items type is by placing that value in brackets, e.g. `list[str]`.

    :param annotated_type: A subclass of SuperType.
    :returns: A list of json types.
    """
    types = []
    if issubclass(annotated_type, UnionType):
        types = [str(t.native_type.__name__) for t in annotated_type.types]
    elif issubclass(annotated_type, Array):
        # Include the type of items in the list if items is defined.
        if annotated_type.items is not None:
            if not isinstance(annotated_type.items, list):
                # items are all of the same type.
                types.append('list[{}]'.format(
                    str(annotated_type.items.native_type.__name__)))
            else:
                # items are different at each index.
                _types = [
                    str(t.native_type.__name__) for t in annotated_type.items]
                types.append('list[{}]'.format(','.join(_types)))
        else:
            types.append('list')
    else:
        types.append(str(annotated_type.native_type.__name__))

    return types


Instruction:
Can you generate the documentation for the following Python 3 function
def get_json_object_lines(annotation: ResourceAnnotation,
                          properties: Dict[str, Any], field: str,
                          url_params: Dict, request: bool = False,
                          object_property: bool = False) -> List[str]:
    """Generate documentation for the given object annotation.

    :param doctor.resource.ResourceAnnotation annotation:
        Annotation object for the associated handler method.
    :param str field: Sphinx field type to use (e.g. '<json').
    :param list url_params: A list of url parameter strings.
    :param bool request: Whether the schema is for the request or not.
    :param bool object_property: If True it indicates this is a property of
        an object that we are documenting.  This is only set to True when
        called recursively when encountering a property that is an object in
        order to document the properties of it.
    :returns: list of strings, one for each line.
    """
    sig_params = annotation.logic._doctor_signature.parameters
    required_lines = []
    lines = []
    default_field = field
    for prop in sorted(properties.keys()):
        annotated_type = properties[prop]
        # If the property is a url parameter override the field to use
        # param so that it's not documented in the json body or query params.
        field = default_field
        if request and prop in url_params:
            field = 'param'

        types = get_json_types(annotated_type)
        description = annotated_type.description
        obj_ref = ''
        if issubclass(annotated_type, Object):
            obj_ref = get_object_reference(annotated_type)
        elif (issubclass(annotated_type, Array) and
                annotated_type.items is not None and
                not isinstance(annotated_type.items, list) and
                issubclass(annotated_type.items, Object)):
            # This means the type is an array of objects, so we want to
            # collect the object as a resource we can document later.
            obj_ref = get_object_reference(annotated_type.items)
        elif (issubclass(annotated_type, Array) and
                isinstance(annotated_type.items, list)):
            # This means the type is array and items is a list of types. Iterate
            # through each type to see if any are objects that we can document.
            for item in annotated_type.items:
                if issubclass(item, Object):
                    # Note: we are just adding them to the global variable
                    # ALL_RESOURCES when calling the function below and not
                    # using the return value as this special case is handled
                    # below in documenting items of an array.
                    get_object_reference(item)

        # Document any enum.
        enum = ''
        if issubclass(annotated_type, Enum):
            enum = ' Must be one of: `{}`'.format(annotated_type.enum)
            if annotated_type.case_insensitive:
                enum += ' (case-insensitive)'
            enum += '.'

        # Document type(s) for an array's items.
        if (issubclass(annotated_type, Array) and
                annotated_type.items is not None):
            array_description = get_array_items_description(annotated_type)
            # Prevents creating a duplicate object reference link in the docs.
            if obj_ref in array_description:
                obj_ref = ''
            description += array_description

        # Document any default value.
        default = ''
        if (request and prop in sig_params and
                sig_params[prop].default != Signature.empty):
            default = ' (Defaults to `{}`) '.format(sig_params[prop].default)

        field_prop = prop
        # If this is a request param and the property is required
        # add required text and append lines to required_lines.  This
        # will make the required properties appear in alphabetical order
        # before the optional.
        line_template = (
            ':{field} {types} {prop}: {description}{enum}{default}{obj_ref}')
        if request and prop in annotation.params.required:
            description = '**Required**.  ' + description
            required_lines.append(line_template.format(
                field=field, types=','.join(types), prop=field_prop,
                description=description, enum=enum, obj_ref=obj_ref,
                default=default))
        else:
            lines.append(line_template.format(
                field=field, types=','.join(types), prop=field_prop,
                description=description, enum=enum, obj_ref=obj_ref,
                default=default))

    return required_lines + lines

Response: Generates the documentation for the given object annotation.


Instruction:
Create a Python 3 function for
generating documentation lines for the given annotation.

Response:
def get_json_lines(annotation: ResourceAnnotation, field: str, route: str,
                   request: bool = False) -> List:
    """Generate documentation lines for the given annotation.

    This only documents schemas of type "object", or type "list" where each
    "item" is an object. Other types are ignored (but a warning is logged).

    :param doctor.resource.ResourceAnnotation annotation:
        Annotation object for the associated handler method.
    :param str field: Sphinx field type to use (e.g. '<json').
    :param str route: The route the annotation is attached to.
    :param bool request: Whether the resource annotation is for the request or
        not.
    :returns: list of strings, one for each line.
    """
    url_params = URL_PARAMS_RE.findall(route)
    if not request:
        return_type = annotation.logic._doctor_signature.return_annotation
        # Check if our return annotation is a Response that supplied a
        # type we can use to document.  If so, use that type for api docs.
        # e.g. def logic() -> Response[MyType]
        if issubclass(return_type, Response):
            if return_type.__args__ is not None:
                return_type = return_type.__args__[0]
        if issubclass(return_type, Array):
            if issubclass(return_type.items, Object):
                properties = return_type.items.properties
                field += 'arr'
            else:
                return []
        elif issubclass(return_type, Object):
            properties = return_type.properties
        else:
            return []
    else:
        # If we defined a req_obj_type for the logic, use that type's
        # properties instead of the function signature.
        if annotation.logic._doctor_req_obj_type:
            properties = annotation.logic._doctor_req_obj_type.properties
        else:
            parameters = annotation.annotated_parameters
            properties = {k: p.annotation for k, p in parameters.items()}
    return get_json_object_lines(annotation, properties, field, url_params,
                                 request)


Instruction:
Make a summary of the following Python 3 code
def get_resource_object_doc_lines() -> List[str]:
    # First loop through  all resources and make sure to add any properties that
    # are objects and not already in `ALL_RESOURCES`.  We iterate over a copy
    # since we will be modifying the dict during the loop.
    for resource_name, a_type in ALL_RESOURCES.copy().items():
        for prop_a_type in a_type.properties.values():
            if issubclass(prop_a_type, Object):
                resource_name = prop_a_type.title
                if resource_name is None:
                    class_name = prop_a_type.__name__
                    resource_name = class_name_to_resource_name(class_name)
                ALL_RESOURCES[resource_name] = prop_a_type
            elif (issubclass(prop_a_type, Array) and
                    prop_a_type.items is not None and
                    not isinstance(prop_a_type.items, list) and
                    issubclass(prop_a_type.items, Object)):
                # This means the type is an array of objects, so we want to
                # collect the object as a resource we can document later.
                resource_name = prop_a_type.items.title
                if resource_name is None:
                    class_name = prop_a_type.items.__name__
                    resource_name = class_name_to_resource_name(class_name)
                ALL_RESOURCES[resource_name] = prop_a_type.items

    # If we don't have any resources to document, just return.
    if not ALL_RESOURCES:
        return []

    lines = ['Resource Objects', '----------------']
    for resource_name in sorted(ALL_RESOURCES.keys()):
        a_type = ALL_RESOURCES[resource_name]
        # First add a reference to the resource
        resource_ref = '_resource-{}'.format(
            '-'.join(resource_name.lower().split(' ')))
        lines.extend(['.. {}:'.format(resource_ref), ''])
        # Add resource name heading
        lines.extend([resource_name, '#' * len(resource_name)])
        # Add resource description
        lines.extend([a_type.description, ''])
        # Only document attributes if it has properties defined.
        if a_type.properties:
            # Add attributes documentation.
            lines.extend(['Attributes', '**********'])
            for prop in a_type.properties:
                prop_a_type = a_type.properties[prop]
                description = a_type.properties[prop].description.strip()
                # Add any object reference if the property is an object or
                # an array of objects.
                obj_ref = ''
                if issubclass(prop_a_type, Object):
                    obj_ref = get_object_reference(prop_a_type)
                elif (issubclass(prop_a_type, Array) and
                        prop_a_type.items is not None and
                        not isinstance(prop_a_type.items, list) and
                        issubclass(prop_a_type.items, Object)):
                    # This means the type is an array of objects.
                    obj_ref = get_object_reference(prop_a_type.items)
                elif (issubclass(prop_a_type, Array) and
                        prop_a_type.items is not None):
                    description += get_array_items_description(prop_a_type)
                native_type = a_type.properties[prop].native_type.__name__
                if prop in a_type.required:
                    description = '**Required**.  ' + description
                lines.append('* **{}** (*{}*) - {}{}'.format(
                    prop, native_type, description, obj_ref).strip())
            lines.append('')
        # Add example of object.
        lines.extend(['Example', '*******'])
        example = a_type.get_example()
        pretty_json = json.dumps(example, separators=(',', ': '), indent=4,
                                 sort_keys=True)
        pretty_json_lines = prefix_lines(pretty_json, '   ')
        lines.extend(['.. code-block:: json', ''])
        lines.extend(pretty_json_lines)
    return lines

Response: Generate documentation lines for all collected resource objects.


Instruction:
Can you tell what is the following Python 3 function doing
def get_name(value) -> str:
    if value.__module__ == '__builtin__':
        return value.__name__
    else:
        return '.'.join((value.__module__, value.__name__))

Response: Return a best guess at the qualified name for a class or function object.


Instruction:
Here you have a function in Python 3, explain what it does
def normalize_route(route: str) -> str:
    normalized_route = str(route).lstrip('^').rstrip('$').rstrip('?')
    normalized_route = normalized_route.replace('<', '(').replace('>', ')')
    return normalized_route

Response: Normalize the given route.


Instruction:
Can you tell what is the following Python 3 function doing
def prefix_lines(lines, prefix):
    if isinstance(lines, bytes):
        lines = lines.decode('utf-8')
    if isinstance(lines, str):
        lines = lines.splitlines()
    return [prefix + line for line in lines]

Response: Add the prefix to each of the lines.


Instruction:
Can you implement a function in Python 3 that
converts a camel case class name to a resource name with spaces.

Response:
def class_name_to_resource_name(class_name: str) -> str:
    """Converts a camel case class name to a resource name with spaces.

    >>> class_name_to_resource_name('FooBarObject')
    'Foo Bar Object'

    :param class_name: The name to convert.
    :returns: The resource name.
    """
    s = re.sub('(.)([A-Z][a-z]+)', r'\1 \2', class_name)
    return re.sub('([a-z0-9])([A-Z])', r'\1 \2', s)


Instruction:
Write a Python 3 function that can
render lines of reStructuredText for items yielded by baseHarness. iter_annotations.

Response:
def _render_rst(self):  # pragma: no cover
        """Render lines of reStructuredText for items yielded by
        :meth:`~doctor.docs.base.BaseHarness.iter_annotations`.
        """
        # Create a mapping of headers to annotations.  We want to group
        # all annotations by a header, but they could be in multiple handlers
        # so we create a map of them here with the heading as the key and
        # the list of associated annotations as a list.  This is so we can
        # sort them alphabetically to make reading the api docs easier.
        heading_to_annotations_map = defaultdict(list)
        for heading, route, handler, annotations in (
                self.harness.iter_annotations()):
            # Set the route and handler as attributes so we can retrieve them
            # when we loop through them all below.
            for annotation in annotations:
                annotation.route = route
                annotation.handler = handler
                heading_to_annotations_map[heading].append(annotation)

        headings = list(heading_to_annotations_map.keys())
        headings.sort()
        previous_heading = None
        for heading in headings:
            annotations = heading_to_annotations_map.get(heading)
            # Sort all the annotations by title.
            annotations.sort(key=lambda a: a.title)
            # Only emit a new heading if the resource has changed.  This
            # esnures that documented endpoints for the same resource all
            # end up under a single heading.
            if previous_heading != heading:
                previous_heading = heading
                yield HEADING_TOKEN + heading
            for annotation in annotations:
                route = annotation.route
                normalized_route = normalize_route(route)
                handler = annotation.handler
                # Adds a title for the endpoint.
                if annotation.title is not None:
                    yield annotation.title
                    yield '#' * len(annotation.title)
                docstring = get_description_lines(getattr(annotation.logic,
                                                          '__doc__', None))

                # Documents the logic function associated with the annotation.
                docstring.append(':Logic Func: :func:`~{}.{}`'.format(
                    annotation.logic.__module__, annotation.logic.__name__))

                field = '<json'
                if annotation.http_method in ('DELETE', 'GET'):
                    field = 'query'
                docstring.extend(get_json_lines(
                    annotation, field=field, route=normalized_route,
                    request=True)
                )

                # Document any request headers.
                defined_headers = list(self.harness._get_headers(
                    str(route), annotation).keys())
                defined_headers.sort()
                for header in defined_headers:
                    definition = self.harness.header_definitions.get(
                        header, '').strip()
                    docstring.append(':reqheader {}: {}'.format(
                        header, definition))

                # Document response if a type was defined.
                if annotation.return_annotation != Parameter.empty:
                    docstring.extend(get_json_lines(
                        annotation, field='>json', route=normalized_route))

                docstring.extend(self._make_example(route, handler, annotation))
                for line in http_directive(annotation.http_method,
                                           normalized_route, docstring):
                    yield line

        # Document resource objects.
        for line in get_resource_object_doc_lines():
            yield line


Instruction:
Create a Python 3 function for
calling by Sphinx to generate documentation for this directive.

Response:
def run(self):  # pragma: no cover
        """Called by Sphinx to generate documentation for this directive."""
        if self.directive_name is None:
            raise NotImplementedError('directive_name must be implemented by '
                                      'subclasses of BaseDirective')
        env, state = self._prepare_env()
        state.doc_names.add(env.docname)
        directive_name = '<{}>'.format(self.directive_name)
        node = nodes.section()
        node.document = self.state.document
        result = ViewList()
        for line in self._render_rst():
            if line.startswith(HEADING_TOKEN):
                # Remove heading token, then append 2 lines, one with
                # the heading text, and the other with the dashes to
                # underline the heading.
                heading = line[HEADING_TOKEN_LENGTH:]
                result.append(heading, directive_name)
                result.append('-' * len(heading), directive_name)
            else:
                result.append(line, directive_name)
        nested_parse_with_titles(self.state, result, node)
        return node.children


Instruction:
Can you generate the documentation for the following Python 3 function
def purge_docs(cls, app, env, docname):  # pragma: no cover
        state = getattr(env, cls.directive_name, None)
        if state and docname in state.doc_names:
            state.doc_names.remove(docname)

Response: Handler for Sphinx s env - purge - doc event.


Instruction:
Write a Python 3 script to
call by Sphinx to setup an extension.

Response:
def setup(cls, app):  # pragma: no cover
        """Called by Sphinx to setup an extension."""
        if cls.directive_name is None:
            raise NotImplementedError('directive_name must be set by '
                                      'subclasses of BaseDirective')
        if not app.registry.has_domain('http'):
            setup_httpdomain(app)
        app.add_config_value('{}_harness'.format(cls.directive_name),
                             None, 'env')
        app.add_directive(cls.directive_name, cls)
        app.connect('builder-inited', cls.run_setup)
        app.connect('build-finished', cls.run_teardown)
        app.connect('env-get-outdated', cls.get_outdated_docs)
        app.connect('env-purge-doc', cls.purge_docs)


Instruction:
Create a Python 3 function to
define header values for a given request.

Response:
def define_header_values(self, http_method, route, values, update=False):
        """Define header values for a given request.

        By default, header values are determined from the class attribute
        `headers`. But if you want to change the headers used in the
        documentation for a specific route, this method lets you do that.

        :param str http_method: An HTTP method, like "get".
        :param str route: The route to match.
        :param dict values: A dictionary of headers for the example request.
        :param bool update: If True, the values will be merged into the default
            headers for the request. If False, the values will replace
            the default headers.
        """
        self.defined_header_values[(http_method.lower(), route)] = {
            'update': update,
            'values': values
        }


Instruction:
How would you explain what the following Python 3 function does
def define_example_values(self, http_method, route, values, update=False):
        self.defined_example_values[(http_method.lower(), route)] = {
            'update': update,
            'values': values
        }

Response: Define example values for a given request.


Instruction:
Can you write a function in Python 3 where it
gets the text for an annotation heading.

Response:
def _get_annotation_heading(self, handler, route, heading=None):
        """Returns the heading text for an annotation.

        Attempts to get the name of the heading from the handler attribute
        `schematic_title` first.

        If `schematic_title` it is not present, it attempts to generate
        the title from the class path.
        This path: advertiser_api.handlers.foo_bar.FooListHandler
        would translate to 'Foo Bar'

        If the file name with the resource is generically named handlers.py
        or it doesn't have a full path then we attempt to get the resource
        name from the class name.
        So FooListHandler and FooHandler would translate to 'Foo'.
        If the handler class name starts with 'Internal', then that will
        be appended to the heading.
        So InternalFooListHandler would translate to 'Foo (Internal)'

        :param mixed handler: The handler class.  Will be a flask resource class
        :param str route: The route to the handler.
        :returns: The text for the heading as a string.
        """
        if hasattr(handler, '_doctor_heading'):
            return handler._doctor_heading

        heading = ''
        handler_path = str(handler)
        try:
            handler_file_name = handler_path.split('.')[-2]
        except IndexError:
            # In the event there is no path and we just have the class name,
            # get heading from the class name by setting us up to enter the
            # first if statement.
            handler_file_name = 'handler'

        # Get heading from class name
        if handler_file_name.startswith('handler'):
            class_name = handler_path.split('.')[-1]
            internal = False
            for word in CAMEL_CASE_RE.findall(class_name):
                if word == 'Internal':
                    internal = True
                    continue
                elif word.startswith(('List', 'Handler', 'Resource')):
                    # We've hit the end of the class name that contains
                    # words we are interested in.
                    break
                heading += '%s ' % (word,)
            if internal:
                heading = heading.strip()
                heading += ' (Internal)'
        # Get heading from handler file name
        else:
            heading = ' '.join(handler_file_name.split('_')).title()
            if 'internal' in route:
                heading += ' (Internal)'
        return heading.strip()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _get_headers(self, route: str, annotation: ResourceAnnotation) -> Dict:
        headers = self.headers.copy()
        defined_header_values = self.defined_header_values.get(
            (annotation.http_method.lower(), str(route)))
        if defined_header_values is not None:
            if defined_header_values['update']:
                headers.update(defined_header_values['values'])
            else:
                headers = defined_header_values['values']
        return headers

Response: Gets the headers for the provided route.


Instruction:
Given the following Python 3 function, write the documentation
def _get_example_values(self, route: str,
                            annotation: ResourceAnnotation) -> Dict[str, Any]:
        """Gets example values for all properties in the annotation's schema.

        :param route: The route to get example values for.
        :type route: werkzeug.routing.Rule for a flask api.
        :param annotation: Schema annotation for the method to be requested.
        :type annotation: doctor.resource.ResourceAnnotation
        :retruns: A dict containing property names as keys and example values
            as values.
        """
        defined_values = self.defined_example_values.get(
            (annotation.http_method.lower(), str(route)))
        if defined_values and not defined_values['update']:
            return defined_values['values']

        # If we defined a req_obj_type for the logic, use that type's
        # example values instead of the annotated parameters.
        if annotation.logic._doctor_req_obj_type:
            values = annotation.logic._doctor_req_obj_type.get_example()
        else:
            values = {
                k: v.annotation.get_example()
                for k, v in annotation.annotated_parameters.items()
            }
        if defined_values:
            values.update(defined_values['values'])

        # If this is a GET route, we need to json dumps any parameters that
        # are lists or dicts.  Otherwise we'll get a 400 error for those params
        if annotation.http_method == 'GET':
            for k, v in values.items():
                if isinstance(v, (list, dict)):
                    values[k] = json.dumps(v)
        return values

Response: Gets the example values for all properties in the schema.


Instruction:
Given the following Python 3 function, write the documentation
def get_device_name(file_name, sys_obj_id, delimiter=":"):

    try:
        with open(file_name, "rb") as csv_file:
            csv_reader = csv.reader(csv_file, delimiter=delimiter)
            for row in csv_reader:
                if len(row) >= 2 and row[0] == sys_obj_id:
                    return row[1]
    except IOError:
        pass  # file does not exists

    return sys_obj_id

Response: Get the device name by its SNMP sysObjectID property from the file map


Instruction:
Make a summary of the following Python 3 code
def has(self, querypart_name, value=None):
        if super().has(querypart_name, value):
            return True
        if not value:
            return super().has('select_options', querypart_name)
        return False

Response: Returns True if querypart_name with value is set.


Instruction:
Can you create a Python 3 function that
groups by a set of attributes.

Response:
def group_by(self, *args, **kwds):
        """
        Default ordering is ``ASC``.

        ``group_by`` accept ``dict`` as you would expect, but note that ``dict``
        does not have same order. Same for named arguments.

        .. code-block:: python

            >>> sqlpuzzle.select('c').from_('t').group_by('a', ('b', 'desc'))
            <Select: SELECT "c" FROM "t" GROUP BY "a", "b" DESC>
        """
        self._group_by.group_by(*args, **kwds)
        return self


Instruction:
Can you generate a brief explanation for the following Python 3 code
def order_by(self, *args, **kwds):
        self._order_by.order_by(*args, **kwds)
        return self

Response: Order by a key.


Instruction:
Make a summary of the following Python 3 code
def context_complete(context_id):
    logging.info('Context %s is.......... DONE.', context_id)

    from furious.context import get_current_async_with_context

    _, context = get_current_async_with_context()

    if not context:
        logging.error("Could not load context")
        return

    for task_id, result in context.result.items():
        logging.info("#########################")
        logging.info("Task Id: %s and Result: %s", task_id, result)

    return context_id

Response: Log out that the context is complete.


Instruction:
Write a Python 3 script to
process the messages pulled from a queue based off the passed in.

Response:
def process_messages(tag, retries=0):
    """Processes the messages pulled fromm a queue based off the tag passed in.
    Will insert another processor if any work was processed or the retry count
    is under the max retry count. Will update a aggregated stats object with
    the data in the payload of the messages processed.

    :param tag: :class: `str` Tag to query the queue on
    :param retry: :class: `int` Number of retries the job has processed
    """
    from furious.batcher import bump_batch
    from furious.batcher import MESSAGE_DEFAULT_QUEUE
    from furious.batcher import MessageIterator
    from furious.batcher import MessageProcessor

    from google.appengine.api import memcache

    # since we don't have a flag for checking complete we'll re-insert a
    # processor task with a retry count to catch any work that may still be
    # filtering in. If we've hit our max retry count we just bail out and
    # consider the job complete.
    if retries > 5:
        logging.info("Process messages hit max retry and is exiting")
        return

    # create a message iteragor for the tag in batches of 500
    message_iterator = MessageIterator(tag, MESSAGE_DEFAULT_QUEUE, 500)

    client = memcache.Client()

    # get the stats object from cache
    stats = client.gets(tag)

    # json decode it if it exists otherwise get the default state.
    stats = json.loads(stats) if stats else get_default_stats()

    work_processed = False

    # loop through the messages pulled from the queue.
    for message in message_iterator:
        work_processed = True

        value = int(message.get("value", 0))
        color = message.get("color").lower()

        # update the total stats with the value pulled
        set_stats(stats["totals"], value)

        # update the specific color status via the value pulled
        set_stats(stats["colors"][color], value)

    # insert the stats back into cache
    json_stats = json.dumps(stats)

    # try and do an add first to see if it's new. We can't trush get due to
    # a race condition.
    if not client.add(tag, json_stats):
        # if we couldn't add than lets do a compare and set to safely
        # update the stats
        if not client.cas(tag, json_stats):
            raise Exception("Transaction Collision.")

    # bump the process batch id
    bump_batch(tag)

    if work_processed:
        # reset the retries as we've processed work
        retries = 0
    else:
        # no work was processed so increment the retries
        retries += 1

    # insert another processor
    processor = MessageProcessor(
        target=process_messages, args=("colors",),
        kwargs={'retries': retries}, tag="colors")

    processor.start()


Instruction:
Make a summary of the following Python 3 code
def set_stats(stats, value):
    stats["total_count"] += 1
    stats["value"] += value
    stats["average"] = stats["value"] / stats["total_count"]

    # this is just a basic example and not the best way to track aggregation.
    # for max and min old there are cases where this will not work correctly.
    if value > stats["max"]:
        stats["max"] = value

    if value < stats["min"] or stats["min"] == 0:
        stats["min"] = value

Response: Updates the stats with the value passed in.


Instruction:
Write a Python 3 function that can
return a : class : dict of the default stats structure.

Response:
def get_default_stats():
    """Returns a :class: `dict` of the default stats structure."""

    default_stats = {
        "total_count": 0,
        "max": 0,
        "min": 0,
        "value": 0,
        "average": 0,
        "last_update": None,
    }

    return {
        "totals": default_stats,
        "colors": {
            "red": default_stats.copy(),
            "blue": default_stats.copy(),
            "yellow": default_stats.copy(),
            "green": default_stats.copy(),
            "black": default_stats.copy(),
        }
    }


Instruction:
Given the following Python 3 function, write the documentation
def serialize_to_json(result, unpicklable=False):

    json = jsonpickle.encode(result, unpicklable=unpicklable)
    result_for_output = str(json)
    return result_for_output

Response: Serializes output as JSON and writes it to console output wrapped with special prefix and suffix


Instruction:
Make a summary of the following Python 3 code
def save(self, folder_path='', configuration_type='running', vrf_management_name=None, return_artifact=False):

        if hasattr(self.resource_config, "vrf_management_name"):
            vrf_management_name = vrf_management_name or self.resource_config.vrf_management_name

        self._validate_configuration_type(configuration_type)
        folder_path = self.get_path(folder_path)
        system_name = re.sub('\s+', '_', self.resource_config.name)[:23]
        time_stamp = time.strftime("%d%m%y-%H%M%S", time.localtime())
        destination_filename = '{0}-{1}-{2}'.format(system_name, configuration_type.lower(), time_stamp)
        full_path = join(folder_path, destination_filename)
        folder_path = self.get_path(full_path)
        self.save_flow.execute_flow(folder_path=folder_path,
                                    configuration_type=configuration_type.lower(),
                                    vrf_management_name=vrf_management_name)

        if return_artifact:
            artifact_type = full_path.split(':')[0]
            identifier = full_path.replace("{0}:".format(artifact_type), "")
            return OrchestrationSavedArtifact(identifier=identifier, artifact_type=artifact_type)

        return destination_filename

Response: Saves the current configuration to a folder


Instruction:
Can you tell what is the following Python 3 function doing
def restore(self, path, configuration_type="running", restore_method="override", vrf_management_name=None):

        if hasattr(self.resource_config, "vrf_management_name"):
            vrf_management_name = vrf_management_name or self.resource_config.vrf_management_name

        self._validate_configuration_type(configuration_type)
        path = self.get_path(path)
        self.restore_flow.execute_flow(path=path,
                                       configuration_type=configuration_type.lower(),
                                       restore_method=restore_method.lower(),
                                       vrf_management_name=vrf_management_name)

Response: Restore configuration on device from provided path


Instruction:
Given the following Python 3 function, write the documentation
def orchestration_save(self, mode="shallow", custom_params=None):

        save_params = {'folder_path': '', 'configuration_type': 'running', 'return_artifact': True}
        params = dict()
        if custom_params:
            params = jsonpickle.decode(custom_params)

        save_params.update(params.get('custom_params', {}))
        save_params['folder_path'] = self.get_path(save_params['folder_path'])

        saved_artifact = self.save(**save_params)

        saved_artifact_info = OrchestrationSavedArtifactInfo(resource_name=self.resource_config.name,
                                                             created_date=datetime.datetime.now(),
                                                             restore_rules=self.get_restore_rules(),
                                                             saved_artifact=saved_artifact)
        save_response = OrchestrationSaveResult(saved_artifacts_info=saved_artifact_info)
        self._validate_artifact_info(saved_artifact_info)

        return serialize_to_json(save_response)

Response:
Orchestration Save command

        :param mode:
        :param custom_params: json with all required action to configure or remove vlans from certain port
        :return Serialized OrchestrationSavedArtifact to json
        :rtype json


Instruction:
Can you generate a brief explanation for the following Python 3 code
def orchestration_restore(self, saved_artifact_info, custom_params=None):

        if saved_artifact_info is None or saved_artifact_info == '':
            raise Exception('ConfigurationOperations', 'saved_artifact_info is None or empty')

        saved_artifact_info = JsonRequestDeserializer(jsonpickle.decode(saved_artifact_info))
        if not hasattr(saved_artifact_info, 'saved_artifacts_info'):
            raise Exception('ConfigurationOperations', 'Saved_artifacts_info is missing')
        saved_config = saved_artifact_info.saved_artifacts_info
        params = None
        if custom_params:
            params = JsonRequestDeserializer(jsonpickle.decode(custom_params))
            _validate_custom_params(params)

        self._validate_artifact_info(saved_config)

        if saved_config.restore_rules.requires_same_resource \
                and saved_config.resource_name.lower() != self.resource_config.name.lower():
            raise Exception('ConfigurationOperations', 'Incompatible resource, expected {}'.format(self.resource_config.name))

        restore_params = {'configuration_type': 'running',
                          'restore_method': 'override',
                          'vrf_management_name': None,
                          'path': '{}:{}'.format(saved_config.saved_artifact.artifact_type,
                                                 saved_config.saved_artifact.identifier)}

        if hasattr(params, 'custom_params'):
            if hasattr(params.custom_params, 'restore_method'):
                restore_params['restore_method'] = params.custom_params.restore_method

            if hasattr(params.custom_params, 'configuration_type'):
                restore_params['configuration_type'] = params.custom_params.configuration_type

            if hasattr(params.custom_params, 'vrf_management_name'):
                restore_params['vrf_management_name'] = params.custom_params.vrf_management_name

        if 'startup' in saved_config.saved_artifact.identifier.split('/')[-1]:
            restore_params['configuration_type'] = 'startup'

        self.restore(**restore_params)

Response: Orchestration restore on the device.


Instruction:
Create a Python 3 function for
validating incoming path and build path url from resource attributes

Response:
def get_path(self, path=''):
        """
        Validate incoming path, if path is empty, build it from resource attributes,
        If path is invalid - raise exception

        :param path: path to remote file storage
        :return: valid path or :raise Exception:
        """

        if not path:
            host = self.resource_config.backup_location
            if ':' not in host:
                scheme = self.resource_config.backup_type
                if not scheme or scheme.lower() == self.DEFAULT_FILE_SYSTEM.lower():
                    scheme = self.file_system
                scheme = re.sub('(:|/+).*$', '', scheme, re.DOTALL)
                host = re.sub('^/+', '', host)
                host = '{}://{}'.format(scheme, host)
            path = host

        url = UrlParser.parse_url(path)

        if url[UrlParser.SCHEME].lower() in AUTHORIZATION_REQUIRED_STORAGE:
            if UrlParser.USERNAME not in url or not url[UrlParser.USERNAME]:
                url[UrlParser.USERNAME] = self.resource_config.backup_user
            if UrlParser.PASSWORD not in url or not url[UrlParser.PASSWORD]:
                url[UrlParser.PASSWORD] = self._api.DecryptPassword(self.resource_config.backup_password).Value
        try:
            result = UrlParser.build_url(url)
        except Exception as e:
            self._logger.error('Failed to build url: {}'.format(e))
            raise Exception('ConfigurationOperations', 'Failed to build path url to remote host')
        return result


Instruction:
Make a summary of the following Python 3 code
def _validate_configuration_type(self, configuration_type):

        if configuration_type.lower() != 'running' and configuration_type.lower() != 'startup':
            raise Exception(self.__class__.__name__, 'Configuration Type is invalid. Should be startup or running')

Response:
Validate configuration type

        :param configuration_type: configuration_type, should be Startup or Running
        :raise Exception:


Instruction:
Given the following Python 3 function, write the documentation
def _validate_artifact_info(self, saved_config):
        is_fail = False
        fail_attribute = ''
        for class_attribute in self.REQUIRED_SAVE_ATTRIBUTES_LIST:
            if type(class_attribute) is tuple:
                if not hasattr(saved_config, class_attribute[0]):
                    is_fail = True
                    fail_attribute = class_attribute[0]
                elif not hasattr(getattr(saved_config, class_attribute[0]), class_attribute[1]):
                    is_fail = True
                    fail_attribute = class_attribute[1]
            else:
                if not hasattr(saved_config, class_attribute):
                    is_fail = True
                    fail_attribute = class_attribute

        if is_fail:
            raise Exception('ConfigurationOperations',
                            'Mandatory field {0} is missing in Saved Artifact Info request json'.format(
                                fail_attribute))

Response: Validate OrchestrationSavedArtifactInfo object for key components


Instruction:
Given the following Python 3 function, write the documentation
def register(key, initializer: callable, param=None):
    '''Adds resolver to global container'''
    get_current_scope().container.register(key, initializer, param)

Response: Adds resolver to global container


Instruction:
Can you tell what is the following Python 3 function doing
def register_single(key, value, param=None):
    '''Generates resolver to return singleton value and adds it to global container'''
    get_current_scope().container.register(key, lambda: value, param)

Response: Generates resolver to return singleton value and adds it to global container


Instruction:
How would you implement a function in Python 3 that
wraps function with scope.

Response:
def wrap_with_scope(func, scope_name=None):
    '''Wraps function with scope. If scope_name is None current scope is used'''
    if scope_name is None:
        scope_name = get_current_scope().name
    return lambda *args, scope=scope_name, **kwargs: \
           _call_with_scope(func, scope, args, kwargs)


Instruction:
Write a Python 3 script to
resolve dependencies using global container and passed it with optional parameters

Response:
def inject(*injections):
    '''Resolves dependencies using global container and passed it with optional parameters'''
    def _decorate(func):
        def _decorated(*args, **kwargs):
            args = list(args)
            keys_to_inject = [name for name in injections if name not in kwargs]
            for key in keys_to_inject:
                kwargs[key] = get_current_scope().container.get(key)
            return func(*args, **kwargs)
        return _decorated
    return _decorate


Instruction:
Can you generate the documentation for the following Python 3 function
def register(self, key, initializer: callable, param=None):
        '''Add resolver to container'''
        if not callable(initializer):
            raise DependencyError('Initializer {0} is not callable'.format(initializer))
        if key not in self._initializers:
            self._initializers[key] = {}
        self._initializers[key][param] = initializer

Response: Add resolver to container