Instruction:
Write a Python 3 script for
updating x from trie / dict E or iterable F

Response:
def update(self, other=None, **kwargs):
        """x.update(E, **F) -> None. update x from trie/dict/iterable E or F.
        If E has a .keys() method, does:     for k in E: x[k] = E[k]
        If E lacks .keys() method, does:     for (k, v) in E: x[k] = v
        In either case, this is followed by: for k in F: x[k] = F[k]"""
        if other is None:
            other = ()

        if hasattr(other, 'keys'):
            for key in other:
                self._update(key, other[key])
        else:
            for key,value in other:
                self._update(key, value)

        for key,value in six.iteritems(kwargs):
            self._update(key, value)


Instruction:
Given the following Python 3 function, write the documentation
def trim(self, prefixes):
        "Prunes any keys beginning with the specified the specified prefixes."
        _prefixes, prefixes = set(map(lambda k:self._prepare_key(k), prefixes)), list()
        for t in lookahead(sorted(_prefixes)):
            if t[1] is not None:
                if t[0] == commonprefix(t):
                    continue
            prefixes.append(t[0])
        length = 0
        for prefix in prefixes:
            length += self._trim(prefix)
        return length

Response: Prunes any keys beginning with the specified the specified prefixes.


Instruction:
Can you write a function in Python 3 where it
writes a compressed representation of script to the Pickler s file object.

Response:
def dump(self, script, file=None):
        "Write a compressed representation of script to the Pickler's file object."
        if file is None:
            file = self._file
        self._dump(script, file, self._protocol, self._version)


Instruction:
Create a Python 3 function for
returning a compressed representation of script as a binary string.

Response:
def dumps(self, script):
        "Return a compressed representation of script as a binary string."
        string = BytesIO()
        self._dump(script, string, self._protocol, self._version)
        return string.getvalue()


Instruction:
Explain what the following Python 3 code does
def load(self, file=None):
        "Read and decompress a compact script from the Pickler's file object."
        if file is None:
            file = self._file
        script_class = self.get_script_class()
        script = self._load(file, self._protocol, self._version)
        return script_class(script)

Response: Read and decompress a compact script from the Pickler s file object.


Instruction:
How would you code a function in Python 3 to
decompress the passed - in compact script and return the result.

Response:
def loads(self, string):
        "Decompress the passed-in compact script and return the result."
        script_class = self.get_script_class()
        script = self._load(BytesIO(string), self._protocol, self._version)
        return script_class(script)


Instruction:
Explain what the following Python 3 code does
def merkle(hashes, func=_merkle_hash256):
    # We use append to duplicate the final item in the iterable of hashes, so
    # we need hashes to be a list-like object, regardless of what we were
    # passed.
    hashes = list(iter(hashes))
    # If the passed-in iterable is empty, allow the constructor to choose our
    # return value:
    if not hashes:
        return func()
    # We must make sure the constructor/compressor is called for the case of
    # a single item as well, in which case the loop below is not entered.
    if len(hashes) == 1:
        return func(*hashes)
    # Build up successive layers of the binary hash tree, starting from the
    # bottom. We've reached the root node when the list has been reduced to
    # one element.
    while len(hashes) > 1:
        # For reasons lost to time, Satoshi decided that any traversal though
        # a bitcoin hash tree will have the same number steps. This is because
        # the last element is repeated when there is an odd number of elements
        # in level, resulting in the right portion of the binary tree being
        # extended into a full tower.
        hashes.append(hashes[-1])
        # By creating an iterator and then duplicating it, we cause two items
        # to be pulled out of the hashes array each time through the generator.
        # The last element is ignored if there is an odd number of elements
        # (meaning there was originally an even number, because of the append
        # operation above).
        hashes = list(func(l,r) for l,r in zip(*(iter(hashes),)*2))
    # Return the root node of the Merkle tree to the caller.
    return hashes[0]

Response:
Convert an iterable of hashes or hashable objects into a binary tree using a passed - in constructor or compression
    function. The default compressor is
    the hash256 function resulting in root - hash for the entire tree.


Instruction:
Here you have a function in Python 3, explain what it does
def iteritems(self):
        "x.iteritems() -> an iterator over the (key, value) items of x in sorted order"
        left_is_hash = isinstance(self.left, numbers.Integral)
        right_is_hash = isinstance(self.right, numbers.Integral)
        if all([left_is_hash, right_is_hash]) or self.right is None:
            if all((self.size>=1, self.length, self.prune is not self.LEFT_NODE)):
                yield(0, self.left)
            if all((self.size>=2, self.length, self.prune is not self.RIGHT_NODE)):
                yield(1, self.right)
        if self.left is not None and not left_is_hash:
            for idx,hash_ in self.left.iteritems():
                yield(idx, hash_)
        if self.right is not None and not right_is_hash:
            offset = 2 ** ((self.size-1).bit_length() - 1)
            for idx,hash_ in self.right.iteritems():
                yield(offset+idx, hash_)

Response: x. iteritems() -> an iterator over the ( key value ) items of x in sorted order


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _get_by_index(self, index, path=None):
        left_size = 2 ** ((self.size-1).bit_length() - 1)
        if index < 0 or self.size <= index:
            return (None, self, index)
        if index < left_size:
            if self.prune is self.LEFT_NODE:
                return (None, self, index)
            if isinstance(self.left, numbers.Integral):
                return (self.left, self, index)
            if path is not None:
                path.append((node, index, self.LEFT_NODE))
            return self.left._get_by_index(index, path)
        else:
            if self.prune is self.RIGHT_NODE:
                return (None, self, index)
            if isinstance(self.right, numbers.Integral):
                return (self.right, self, index)
            if path is not None:
                path.append((node, index, self.RIGHT_NODE))
            return self.right._get_by_index(index-left_size, path)

Response:
Returns the 2 - tuple node idx where node is either a terminal leaf
        node containing the value of that index in either left or right slot and node is the most specific node on the path which would contain the index.


Instruction:
Write a Python 3 script to
encode bytes to a base58 - encoded string.

Response:
def b58encode(b, errors='strict'):
    "Encode bytes to a base58-encoded string."
    len_ = len(b)

    # Convert big-endian bytes to integer
    n = BigInteger.deserialize(BytesIO(b), len_)

    # Divide that integer into base58
    res = []
    while n > 0:
        n, r = divmod (n, 58)
        res.append(b58digits[r])
    res = ''.join(res[::-1])

    # Encode leading zeros as base58 zeros
    pad = 0
    for c in b:
        if c == six.int2byte(0): pad += 1
        else: break
    return (b58digits[0] * pad + res, len_)


Instruction:
How would you code a function in Python 3 to
decode a base58 - encoding string returning bytes.

Response:
def b58decode(s, errors='strict'):
    "Decode a base58-encoding string, returning bytes."
    if not s:
        return (b'', 0)

    # Convert the string to an integer
    n = 0
    for c in s:
        n *= 58
        if c not in b58digits:
            raise InvalidBase58Error(u"character %r is not a valid base58 "
                u"character" % c)
        digit = b58digits.index(c)
        n += digit

    # Convert the integer to bytes
    res = BigInteger(n).serialize((n.bit_length()+7)//8 or 1)

    # Add padding back.
    pad = 0
    for c in s[:-1]:
        if c == b58digits[0]: pad += 1
        else: break
    return (b'\x00' * pad + res, len(s))


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

    my_torrent = Torrent.from_file(torrent_path)

    size = my_torrent.total_size

    click.secho('Name: %s' % my_torrent.name, fg='blue')
    click.secho('Files:')
    for file_tuple in my_torrent.files:
        click.secho(file_tuple.name)

    click.secho('Hash: %s' % my_torrent.info_hash, fg='blue')
    click.secho('Size: %s (%s)' % (humanize_filesize(size), size), fg='blue')
    click.secho('Magnet: %s' % my_torrent.get_magnet(), fg='yellow')

Response: Print out information from. torrent file.


Instruction:
Here you have a function in Python 3, explain what it does
def create(source, dest, tracker, open_trackers, comment, cache):

    source_title = path.basename(source).replace('.', '_').replace(' ', '_')
    dest = '%s.torrent' % path.join(dest, source_title)

    click.secho('Creating torrent from %s ...' % source)

    my_torrent = Torrent.create_from(source)

    if comment:
        my_torrent.comment = comment

    urls = []

    if tracker:
        urls = tracker.split(',')

    if open_trackers:
        click.secho('Fetching an up-to-date open tracker list ...')
        try:
            urls.extend(get_open_trackers_from_remote())
        except RemoteDownloadError:
            click.secho('Failed. Using built-in open tracker list.', fg='red', err=True)
            urls.extend(get_open_trackers_from_local())

    if urls:
        my_torrent.announce_urls = urls

    my_torrent.to_file(dest)

    click.secho('Torrent file created: %s' % dest, fg='green')
    click.secho('Torrent info hash: %s' % my_torrent.info_hash, fg='blue')

    if cache:
        click.secho('Uploading to %s torrent cache service ...')
        try:
            result = upload_to_cache_server(dest)
            click.secho('Cached torrent URL: %s' % result, fg='yellow')

        except RemoteUploadError as e:
            click.secho('Failed: %s' % e, fg='red', err=True)

Response: Create a new torrent file from a single file or a directory.


Instruction:
Here you have a function in Python 3, explain what it does
def files(self):
        files = []
        info = self._struct.get('info')

        if not info:
            return files

        if 'files' in info:
            base = info['name']

            for f in info['files']:
                files.append(TorrentFile(join(base, *f['path']), f['length']))

        else:
            files.append(TorrentFile(info['name'], info['length']))

        return files

Response:
Files in torrent.

        List of namedtuples (filepath, size).

        :rtype: list[TorrentFile]


Instruction:
Can you generate the documentation for the following Python 3 function
def info_hash(self):
        info = self._struct.get('info')

        if not info:
            return None

        return sha1(Bencode.encode(info)).hexdigest()

Response: Hash of torrent file info section. Also known as torrent hash.


Instruction:
Create a Python 3 function to
list of lists of announce URLs.

Response:
def announce_urls(self):
        """List of lists of announce (tracker) URLs.

        First inner list is considered as primary announcers list,
        the following lists as back-ups.

        http://bittorrent.org/beps/bep_0012.html

        """
        urls = self._struct.get('announce-list')

        if not urls:
            urls = self._struct.get('announce')
            if not urls:
                return []
            urls = [[urls]]

        return urls


Instruction:
Implement a function in Python 3 to
return torrent magnet link consisting of BTIH Info Hash and optional other information.

Response:
def get_magnet(self, detailed=True):
        """Returns torrent magnet link, consisting of BTIH (BitTorrent Info Hash) URN
        anr optional other information.

        :param bool|list|tuple|set detailed:
            For boolean - whether additional info (such as trackers) should be included.
            For iterable - expected allowed parameter names:
                tr - trackers
                ws - webseeds

        """
        result = 'magnet:?xt=urn:btih:' + self.info_hash

        def add_tr():
            urls = self.announce_urls
            if not urls:
                return

            trackers = []

            urls = urls[0]  # Only primary announcers are enough.
            for url in urls:
                trackers.append(('tr', url))

            if trackers:
                return urlencode(trackers)

        def add_ws():
            webseeds = [('ws', url) for url in self.webseeds]
            if webseeds:
                return urlencode(webseeds)

        params_map = {
            'tr': add_tr,
            'ws': add_ws,
        }

        if detailed:
            details = []

            if isinstance(detailed, _ITERABLE_TYPES):
                requested_params = detailed
            else:
                requested_params = params_map.keys()

            for param in requested_params:
                param_val = params_map[param]()
                param_val and details.append(param_val)

            if details:
                result += '&%s' % '&'.join(details)

        return result


Instruction:
How would you implement a function in Python 3 that
writes the Torrent object into file.

Response:
def to_file(self, filepath=None):
        """Writes Torrent object into file, either

        :param filepath:
        """
        if filepath is None and self._filepath is None:
            raise TorrentError('Unable to save torrent to file: no filepath supplied.')

        if filepath is not None:
            self._filepath = filepath

        with open(self._filepath, mode='wb') as f:
            f.write(self.to_string())


Instruction:
Can you generate the documentation for the following Python 3 function
def create_from(cls, src_path):
        is_dir = isdir(src_path)
        target_files, size_data = cls._get_target_files_info(src_path)

        SIZE_MIN = 32768  # 32 KiB
        SIZE_DEFAULT = 262144  # 256 KiB
        SIZE_MAX = 1048576  # 1 MiB

        CHUNKS_MIN = 1000  # todo use those limits as advised
        CHUNKS_MAX = 2200

        size_piece = SIZE_MIN
        if size_data > SIZE_MIN:
            size_piece = SIZE_DEFAULT

        if size_piece > SIZE_MAX:
            size_piece = SIZE_MAX

        def read(filepath):
            with open(filepath, 'rb') as f:
                while True:
                    chunk = f.read(size_piece - len(pieces_buffer))
                    chunk_size = len(chunk)
                    if chunk_size == 0:
                        break
                    yield chunk

        pieces = bytearray()
        pieces_buffer = bytearray()

        for fpath, _, _ in target_files:
            for chunk in read(fpath):
                pieces_buffer += chunk

                if len(pieces_buffer) == size_piece:
                    pieces += sha1(pieces_buffer).digest()[:20]
                    pieces_buffer = bytearray()

        if len(pieces_buffer):
            pieces += sha1(pieces_buffer).digest()[:20]
            pieces_buffer = bytearray()

        info = {
            'name': basename(src_path),
            'pieces': bytes(pieces),
            'piece length': size_piece,
        }

        if is_dir:
            files = []

            for _, length, path in target_files:
                files.append({'length': length, 'path': path})

            info['files'] = files

        else:
            info['length'] = target_files[0][1]

        torrent = cls({'info': info})
        torrent.created_by = get_app_version()
        torrent.creation_date = datetime.utcnow()

        return torrent

Response: Returns a Torrent object created from a file or a directory.


Instruction:
Can you generate the documentation for the following Python 3 function
def from_file(cls, filepath):
        torrent = cls(Bencode.read_file(filepath))
        torrent._filepath = filepath
        return torrent

Response: Alternative constructor to get Torrent object from file.


Instruction:
How would you explain what the following Python 3 function does
def humanize_filesize(bytes_size):
    if not bytes_size:
        return '0 B'

    names = ('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB')

    name_idx = int(math.floor(math.log(bytes_size, 1024)))
    size = round(bytes_size / math.pow(1024, name_idx), 2)

    return '%s %s' % (size, names[name_idx])

Response: Returns human readable filesize.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def upload_to_cache_server(fpath):
    url_base = 'http://torrage.info'
    url_upload = '%s/autoupload.php' % url_base
    url_download = '%s/torrent.php?h=' % url_base
    file_field = 'torrent'

    try:
        import requests

        response = requests.post(url_upload, files={file_field: open(fpath, 'rb')}, timeout=REMOTE_TIMEOUT)
        response.raise_for_status()

        info_cache = response.text
        return url_download + info_cache

    except (ImportError, requests.RequestException) as e:

        # Now trace is lost. `raise from` to consider.
        raise RemoteUploadError('Unable to upload to %s: %s' % (url_upload, e))

Response: Uploads a. torrent file to a cache server. Returns the url of the uploaded file.


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

    url_base = 'https://raw.githubusercontent.com/idlesign/torrentool/master/torrentool/repo'
    url = '%s/%s' % (url_base, OPEN_TRACKERS_FILENAME)

    try:
        import requests

        response = requests.get(url, timeout=REMOTE_TIMEOUT)
        response.raise_for_status()

        open_trackers = response.text.splitlines()

    except (ImportError, requests.RequestException) as e:

        # Now trace is lost. `raise from` to consider.
        raise RemoteDownloadError('Unable to download from %s: %s' % (url, e))

    return open_trackers

Response: Returns open trackers announce URLs list from remote repo.


Instruction:
Here you have a function in Python 3, explain what it does
def get_open_trackers_from_local():
    with open(path.join(path.dirname(__file__), 'repo', OPEN_TRACKERS_FILENAME)) as f:
        open_trackers = map(str.strip, f.readlines())

    return list(open_trackers)

Response: Returns open trackers announce URLs list from local backup.


Instruction:
Implement a Python 3 function for
encoding a Python object into a bencoded bytes.

Response:
def encode(cls, value):
        """Encodes a value into bencoded bytes.

        :param value: Python object to be encoded (str, int, list, dict).
        :param str val_encoding: Encoding used by strings in a given object.
        :rtype: bytes
        """
        val_encoding = 'utf-8'

        def encode_str(v):
            try:
                v_enc = encode(v, val_encoding)

            except UnicodeDecodeError:
                if PY3:
                    raise
                else:
                    # Suppose bytestring
                    v_enc = v

            prefix = encode('%s:' % len(v_enc), val_encoding)
            return prefix + v_enc

        def encode_(val):
            if isinstance(val, str_type):
                result = encode_str(val)

            elif isinstance(val, int_types):
                result = encode(('i%se' % val), val_encoding)

            elif isinstance(val, (list, set, tuple)):
                result = encode('l', val_encoding)
                for item in val:
                    result += encode_(item)
                result += encode('e', val_encoding)

            elif isinstance(val, dict):
                result = encode('d', val_encoding)

                # Dictionaries are expected to be sorted by key.
                for k, v in OrderedDict(sorted(val.items(), key=itemgetter(0))).items():
                    result += (encode_str(k) + encode_(v))

                result += encode('e', val_encoding)

            elif isinstance(val, byte_types):
                result = encode('%s:' % len(val), val_encoding)
                result += val

            else:
                raise BencodeEncodingError('Unable to encode `%s` %s' % (type(val), val))

            return result

        return encode_(value)


Instruction:
Make a summary of the following Python 3 code
def decode(cls, encoded):
        def create_dict(items):
            # Let's guarantee that dictionaries are sorted.
            k_v_pair = zip(*[iter(items)] * 2)
            return OrderedDict(sorted(k_v_pair, key=itemgetter(0)))

        def create_list(items):
            return list(items)

        stack_items = []
        stack_containers = []

        def compress_stack():
            target_container = stack_containers.pop()
            subitems = []

            while True:
                subitem = stack_items.pop()
                subitems.append(subitem)
                if subitem is target_container:
                    break

            container_creator = subitems.pop()
            container = container_creator(reversed(subitems))
            stack_items.append(container)

        def parse_forward(till_char, sequence):
            number = ''
            char_sub_idx = 0

            for char_sub_idx, char_sub in enumerate(sequence):
                char_sub = chr_(char_sub)
                if char_sub == till_char:
                    break

                number += char_sub

            number = int(number or 0)
            char_sub_idx += 1

            return number, char_sub_idx

        while encoded:
            char = encoded[0]
            char = chr_(char)

            if char == 'd':  # Dictionary
                stack_items.append(create_dict)
                stack_containers.append(create_dict)
                encoded = encoded[1:]

            elif char == 'l':  # List
                stack_items.append(create_list)
                stack_containers.append(create_list)
                encoded = encoded[1:]

            elif char == 'i':  # Integer
                number, char_sub_idx = parse_forward('e', encoded[1:])
                char_sub_idx += 1

                stack_items.append(number)
                encoded = encoded[char_sub_idx:]

            elif char.isdigit():  # String
                str_len, char_sub_idx = parse_forward(':', encoded)
                last_char_idx = char_sub_idx + str_len

                string = encoded[char_sub_idx:last_char_idx]
                try:
                    string = string.decode('utf-8')
                except UnicodeDecodeError:
                    # Considered bytestring (e.g. `pieces` hashes concatenation).
                    pass

                stack_items.append(string)
                encoded = encoded[last_char_idx:]

            elif char == 'e':  # End of a dictionary or a list.
                compress_stack()
                encoded = encoded[1:]

            else:
                raise BencodeDecodingError('Unable to interpret `%s` char.' % char)

        if len(stack_items) == 1:
            stack_items = stack_items.pop()

        return stack_items

Response: Decodes bencoded data introduced as bytes.


Instruction:
Write a Python 3 script for
decoding a given bencoded string or bytestring.

Response:
def read_string(cls, string):
        """Decodes a given bencoded string or bytestring.

        Returns decoded structure(s).

        :param str string:
        :rtype: list
        """
        if PY3 and not isinstance(string, byte_types):
            string = string.encode()

        return cls.decode(string)


Instruction:
Write a Python 3 script to
decode bencoded data of a given file.

Response:
def read_file(cls, filepath):
        """Decodes bencoded data of a given file.

        Returns decoded structure(s).

        :param str filepath:
        :rtype: list
        """
        with open(filepath, mode='rb') as f:
            contents = f.read()
        return cls.decode(contents)


Instruction:
How would you code a function in Python 3 to
create csp header string

Response:
def create_csp_header(cspDict):
	""" create csp header string """
	policy = ['%s %s' % (k, v) for k, v in cspDict.items() if v != '']
	return '; '.join(policy)


Instruction:
Can you tell what is the following Python 3 function doing
def csp_header(csp={}):
    _csp = csp_default().read()
    _csp.update(csp)

    _header = ''
    if 'report-only' in _csp and _csp['report-only'] is True:
    	_header = 'Content-Security-Policy-Report-Only'
    else:
    	_header = 'Content-Security-Policy'
    if 'report-only' in _csp:
    	del _csp['report-only']
    _headers = {_header: create_csp_header(_csp)}

    def decorator(f):
        @wraps(f)
        def decorated_function(*args, **kwargs):
            resp = make_response(f(*args, **kwargs))
            h = resp.headers
            for header, value in _headers.items():
                h[header] = value
            return resp
        return decorated_function
    return decorator

Response: Decorator to include csp header on app. route wrapper


Instruction:
Write a Python 3 function that can
read default csp settings from json file

Response:
def read(self):
		""" read default csp settings from json file """
		with open(self.default_file) as json_file:
			try:
				return json.load(json_file)
			except Exception as e:
				raise 'empty file'


Instruction:
How would you explain what the following Python 3 function does
def update(self,updates={}):
		try:
			csp = self.read()
		except:
			csp = {'default-src':"'self'"}
			self.write(csp)
		csp.update(updates)
		self.write(csp)

Response: update csp_default. json with update dict


Instruction:
Can you implement a function in Python 3 that
obtains existing ImageSet or create a new one if none is specified.

Response:
def get_image_set(self):
        """
        Obtain existing ImageSet if `pk` is specified, otherwise
        create a new ImageSet for the user.
        """
        image_set_pk = self.kwargs.get("pk", None)
        if image_set_pk is None:
            return self.request.user.image_sets.create()
        return get_object_or_404(self.get_queryset(), pk=image_set_pk)


Instruction:
Can you implement a function in Python 3 that
wraps the Tornado web application initialization so that the TornadoTracing instance is created around an OpenTracing-compatible tracer.

Response:
def tracer_config(__init__, app, args, kwargs):
    """
    Wraps the Tornado web application initialization so that the
    TornadoTracing instance is created around an OpenTracing-compatible tracer.
    """
    __init__(*args, **kwargs)

    tracing = app.settings.get('opentracing_tracing')
    tracer_callable = app.settings.get('opentracing_tracer_callable')
    tracer_parameters = app.settings.get('opentracing_tracer_parameters', {})

    if tracer_callable is not None:
        if not callable(tracer_callable):
            tracer_callable = _get_callable_from_name(tracer_callable)

        tracer = tracer_callable(**tracer_parameters)
        tracing = TornadoTracing(tracer)

    if tracing is None:
        tracing = TornadoTracing()  # fallback to the global tracer

    app.settings['opentracing_tracing'] = tracing

    tracing._trace_all = app.settings.get('opentracing_trace_all',
                                          DEFAULT_TRACE_ALL)
    tracing._trace_client = app.settings.get('opentracing_trace_client',
                                             DEFAULT_TRACE_CLIENT)
    tracing._start_span_cb = app.settings.get('opentracing_start_span_cb',
                                              None)

    httpclient._set_tracing_enabled(tracing._trace_client)
    if tracing._trace_client:
        httpclient._set_tracing_info(tracing._tracer_obj,
                                     tracing._start_span_cb)


Instruction:
How would you code a function in Python 3 to
function decorator that traces functions NOTE: Must be placed before the Tornado decorators @param attributes any number of request attributes (strings) to be set as tags on the created span

Response:
def trace(self, *attributes):
        """
        Function decorator that traces functions
        NOTE: Must be placed before the Tornado decorators
        @param attributes any number of request attributes
        (strings) to be set as tags on the created span
        """

        @wrapt.decorator
        def wrapper(wrapped, instance, args, kwargs):
            if self._trace_all:
                return wrapped(*args, **kwargs)

            handler = instance

            with tracer_stack_context():
                try:
                    self._apply_tracing(handler, list(attributes))

                    # Run the actual function.
                    result = wrapped(*args, **kwargs)

                    # if it has `add_done_callback` it's a Future,
                    # else, a normal method/function.
                    if callable(getattr(result, 'add_done_callback', None)):
                        callback = functools.partial(
                                self._finish_tracing_callback,
                                handler=handler)
                        result.add_done_callback(callback)
                    else:
                        self._finish_tracing(handler)

                except Exception as exc:
                    self._finish_tracing(handler, error=exc)
                    raise

            return result

        return wrapper


Instruction:
Create a Python 3 function to
return a new span from the request with logged attributes and correct operation name from the func.

Response:
def _apply_tracing(self, handler, attributes):
        """
        Helper function to avoid rewriting for middleware and decorator.
        Returns a new span from the request with logged attributes and
        correct operation name from the func.
        """
        operation_name = self._get_operation_name(handler)
        headers = handler.request.headers
        request = handler.request

        # start new span from trace info
        try:
            span_ctx = self._tracer.extract(opentracing.Format.HTTP_HEADERS,
                                            headers)
            scope = self._tracer.start_active_span(operation_name,
                                                   child_of=span_ctx)
        except (opentracing.InvalidCarrierException,
                opentracing.SpanContextCorruptedException):
            scope = self._tracer.start_active_span(operation_name)

        # add span to current spans
        setattr(request, SCOPE_ATTR, scope)

        # log any traced attributes
        scope.span.set_tag(tags.COMPONENT, 'tornado')
        scope.span.set_tag(tags.SPAN_KIND, tags.SPAN_KIND_RPC_SERVER)
        scope.span.set_tag(tags.HTTP_METHOD, request.method)
        scope.span.set_tag(tags.HTTP_URL, request.uri)

        for attr in attributes:
            if hasattr(request, attr):
                payload = str(getattr(request, attr))
                if payload:
                    scope.span.set_tag(attr, payload)

        # invoke the start span callback, if any
        self._call_start_span_cb(scope.span, request)

        return scope


Instruction:
How would you implement a function in Python 3 that
wraps the handler s _execute method to trace incoming requests and apply tracing to the context of the incoming requests.

Response:
def execute(func, handler, args, kwargs):
    """
    Wrap the handler ``_execute`` method to trace incoming requests,
    extracting the context from the headers, if available.
    """
    tracing = handler.settings.get('opentracing_tracing')

    with tracer_stack_context():
        if tracing._trace_all:
            attrs = handler.settings.get('opentracing_traced_attributes', [])
            tracing._apply_tracing(handler, attrs)

        return func(*args, **kwargs)


Instruction:
Explain what the following Python 3 code does
def on_finish(func, handler, args, kwargs):
    tracing = handler.settings.get('opentracing_tracing')
    tracing._finish_tracing(handler)

    return func(*args, **kwargs)

Response:
Wrap the handler s on_finish method to finish the Span for the
    given request.


Instruction:
Can you tell what is the following Python 3 function doing
def log_exception(func, handler, args, kwargs):
    # safe-guard: expected arguments -> log_exception(self, typ, value, tb)
    value = args[1] if len(args) == 3 else None
    if value is None:
        return func(*args, **kwargs)

    tracing = handler.settings.get('opentracing_tracing')
    if not isinstance(value, HTTPError) or 500 <= value.status_code <= 599:
        tracing._finish_tracing(handler, error=value)

    return func(*args, **kwargs)

Response: Wrap the handler log_exception method to finish the Span for the given request.


Instruction:
Explain what the following Python 3 code does
def schema_root(self) -> "SchemaTreeNode":
        sn = self
        while sn.parent:
            sn = sn.parent
        return sn

Response: Return the root node of the receiver s schema.


Instruction:
Given the following Python 3 function, write the documentation
def content_type(self) -> ContentType:
        return self._ctype if self._ctype else self.parent.content_type()

Response: Return receiver s content type.


Instruction:
How would you implement a function in Python 3 that
returns the closest ancestor data node.

Response:
def data_parent(self) -> Optional["InternalNode"]:
        """Return the closest ancestor data node."""
        parent = self.parent
        while parent:
            if isinstance(parent, DataNode):
                return parent
            parent = parent.parent


Instruction:
Can you generate a brief explanation for the following Python 3 code
def iname(self) -> InstanceName:
        dp = self.data_parent()
        return (self.name if dp and self.ns == dp.ns
                else self.ns + ":" + self.name)

Response: Return the instance name corresponding to the receiver.


Instruction:
Here you have a function in Python 3, explain what it does
def data_path(self) -> DataPath:
        dp = self.data_parent()
        return (dp.data_path() if dp else "") + "/" + self.iname()

Response: Return the receiver s data path.


Instruction:
Write a Python 3 script for
returning dictionary of receiver s properties suitable for clients.

Response:
def _node_digest(self) -> Dict[str, Any]:
        """Return dictionary of receiver's properties suitable for clients."""
        res = {"kind": self._yang_class()}
        if self.mandatory:
            res["mandatory"] = True
        if self.description:
            res["description"] = self.description
        return res


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _iname2qname(self, iname: InstanceName) -> QualName:
        p, s, loc = iname.partition(":")
        return (loc, p) if s else (p, self.ns)

Response: Translate instance name to qualified name in the receiver s context.


Instruction:
Write a Python 3 script to
dispatch actions for substatements of stmt.

Response:
def _handle_substatements(self, stmt: Statement, sctx: SchemaContext) -> None:
        """Dispatch actions for substatements of `stmt`."""
        for s in stmt.substatements:
            if s.prefix:
                key = (
                    sctx.schema_data.modules[sctx.text_mid].prefix_map[s.prefix][0]
                    + ":" + s.keyword)
            else:
                key = s.keyword
            mname = SchemaNode._stmt_callback.get(key, "_noop")
            method = getattr(self, mname)
            method(s, sctx)


Instruction:
Can you implement a function in Python 3 that
returns the data node referred to by a leafref path.

Response:
def _follow_leafref(
            self, xpath: "Expr", init: "TerminalNode") -> Optional["DataNode"]:
        """Return the data node referred to by a leafref path.

        Args:
            xpath: XPath expression compiled from a leafref path.
            init: initial context node
        """
        if isinstance(xpath, LocationPath):
            lft = self._follow_leafref(xpath.left, init)
            if lft is None:
                return None
            return lft._follow_leafref(xpath.right, init)
        elif isinstance(xpath, Step):
            if xpath.axis == Axis.parent:
                return self.data_parent()
            elif xpath.axis == Axis.child:
                if isinstance(self, InternalNode) and xpath.qname:
                    qname = (xpath.qname if xpath.qname[1]
                             else (xpath.qname[0], init.ns))
                    return self.get_data_child(*qname)
        elif isinstance(xpath, Root):
            return self.schema_root()
        return None


Instruction:
Explain what the following Python 3 code does
def _tree_line(self, no_type: bool = False) -> str:
        return self._tree_line_prefix() + " " + self.iname()

Response: Return the receiver s contribution to tree diagram.


Instruction:
Write a Python 3 script for
setting NACM default access.

Response:
def _nacm_default_deny_stmt(self, stmt: Statement, sctx: SchemaContext) -> None:
        """Set NACM default access."""
        if not hasattr(self, 'default_deny'):
            return
        if stmt.keyword == "default-deny-all":
            self.default_deny = DefaultDeny.all
        elif stmt.keyword == "default-deny-write":
            self.default_deny = DefaultDeny.write


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_child(self, name: YangIdentifier,
                  ns: YangIdentifier = None) -> Optional[SchemaNode]:
        """Return receiver's schema child.

        Args:
            name: Child's name.
            ns: Child's namespace (= `self.ns` if absent).
        """
        ns = ns if ns else self.ns
        todo = []
        for child in self.children:
            if child.name is None:
                todo.append(child)
            elif child.name == name and child.ns == ns:
                return child
        for c in todo:
            grandchild = c.get_child(name, ns)
            if grandchild is not None:
                return grandchild

Response: Return receiver s schema child.


Instruction:
How would you explain what the following Python 3 function does
def get_schema_descendant(
            self, route: SchemaRoute) -> Optional[SchemaNode]:
        """Return descendant schema node or ``None`` if not found.

        Args:
            route: Schema route to the descendant node
                   (relative to the receiver).
        """
        node = self
        for p in route:
            node = node.get_child(*p)
            if node is None:
                return None
        return node

Response: Returns the descendant schema node or None if not found.


Instruction:
Given the following Python 3 function, write the documentation
def get_data_child(self, name: YangIdentifier,
                       ns: YangIdentifier = None) -> Optional["DataNode"]:
        """Return data node directly under the receiver."""
        ns = ns if ns else self.ns
        todo = []
        for child in self.children:
            if child.name == name and child.ns == ns:
                if isinstance(child, DataNode):
                    return child
                todo.insert(0, child)
            elif not isinstance(child, DataNode):
                todo.append(child)
        for c in todo:
            res = c.get_data_child(name, ns)
            if res:
                return res

Response: Return data node directly under the receiver.


Instruction:
Implement a function in Python 3 to
return receiver s children based on content type.

Response:
def filter_children(self, ctype: ContentType = None) -> List[SchemaNode]:
        """Return receiver's children based on content type.

        Args:
            ctype: Content type.
        """
        if ctype is None:
            ctype = self.content_type()
        return [c for c in self.children if
                not isinstance(c, (RpcActionNode, NotificationNode)) and
                c.content_type().value & ctype.value != 0]


Instruction:
Make a summary of the following Python 3 code
def data_children(self) -> List["DataNode"]:
        res = []
        for child in self.children:
            if isinstance(child, DataNode):
                res.append(child)
            elif not isinstance(child, SchemaTreeNode):
                res.extend(child.data_children())
        return res

Response: Return the set of all data nodes directly under the receiver.


Instruction:
Implement a Python 3 function for
overriding the superclass method.

Response:
def from_raw(self, rval: RawObject, jptr: JSONPointer = "") -> ObjectValue:
        """Override the superclass method."""
        if not isinstance(rval, dict):
            raise RawTypeError(jptr, "object")
        res = ObjectValue()
        for qn in rval:
            if qn.startswith("@"):
                if qn != "@":
                    tgt = qn[1:]
                    if tgt not in rval:
                        raise MissingAnnotationTarget(jptr, tgt)
                    jptr += '/' + tgt
                res[qn] = self._process_metadata(rval[qn], jptr)
            else:
                cn = self._iname2qname(qn)
                ch = self.get_data_child(*cn)
                npath = jptr + "/" + qn
                if ch is None:
                    raise RawMemberError(npath)
                res[ch.iname()] = ch.from_raw(rval[qn], npath)
        return res


Instruction:
Explain what the following Python 3 code does
def _validate(self, inst: "InstanceNode", scope: ValidationScope,
                  ctype: ContentType) -> None:
        """Extend the superclass method."""
        if scope.value & ValidationScope.syntax.value:   # schema
            self._check_schema_pattern(inst, ctype)
        for m in inst:
            inst._member(m).validate(scope, ctype)
        super()._validate(inst, scope, ctype)

Response: Extend the superclass method.


Instruction:
Create a Python 3 function to
return the set of instance names under the receiver.

Response:
def _child_inst_names(self) -> Set[InstanceName]:
        """Return the set of instance names under the receiver."""
        return frozenset([c.iname() for c in self.data_children()])


Instruction:
Can you implement a function in Python 3 that
builds the schema pattern for the receiver and its data descendants.

Response:
def _make_schema_patterns(self) -> None:
        """Build schema pattern for the receiver and its data descendants."""
        self.schema_pattern = self._schema_pattern()
        for dc in self.data_children():
            if isinstance(dc, InternalNode):
                dc._make_schema_patterns()


Instruction:
Write a Python 3 function for
adding child node to the receiver and handle substatements.

Response:
def _handle_child(
            self, node: SchemaNode, stmt: Statement, sctx: SchemaContext) -> None:
        """Add child node to the receiver and handle substatements."""
        if not sctx.schema_data.if_features(stmt, sctx.text_mid):
            return
        node.name = stmt.argument
        node.ns = sctx.default_ns
        node._get_description(stmt)
        self._add_child(node)
        node._handle_substatements(stmt, sctx)


Instruction:
Given the following Python 3 function, write the documentation
def _augment_stmt(self, stmt: Statement, sctx: SchemaContext) -> None:
        if not sctx.schema_data.if_features(stmt, sctx.text_mid):
            return
        path = sctx.schema_data.sni2route(stmt.argument, sctx)
        target = self.get_schema_descendant(path)
        if stmt.find1("when"):
            gr = GroupNode()
            target._add_child(gr)
            target = gr
        target._handle_substatements(stmt, sctx)

Response: Handle an augment statement.


Instruction:
Can you write a function in Python 3 where it
handles the refine statement.

Response:
def _refine_stmt(self, stmt: Statement, sctx: SchemaContext) -> None:
        """Handle **refine** statement."""
        target = self.get_schema_descendant(
            sctx.schema_data.sni2route(stmt.argument, sctx))
        if not sctx.schema_data.if_features(stmt, sctx.text_mid):
            target.parent.children.remove(target)
        else:
            target._handle_substatements(stmt, sctx)


Instruction:
Write a Python 3 script for
handling leaf - list statement.

Response:
def _leaf_list_stmt(self, stmt: Statement, sctx: SchemaContext) -> None:
        """Handle leaf-list statement."""
        node = LeafListNode()
        node.type = DataType._resolve_type(
            stmt.find1("type", required=True), sctx)
        self._handle_child(node, stmt, sctx)


Instruction:
Explain what the following Python 3 code does
def _rpc_action_stmt(self, stmt: Statement, sctx: SchemaContext) -> None:
        self._handle_child(RpcActionNode(), stmt, sctx)

Response: Handle RPC or action statement.


Instruction:
Here you have a function in Python 3, explain what it does
def _ascii_tree(self, indent: str, no_types: bool, val_count: bool) -> str:
        def suffix(sn):
            return f" {{{sn.val_count}}}\n" if val_count else "\n"
        if not self.children:
            return ""
        cs = []
        for c in self.children:
            cs.extend(c._flatten())
        cs.sort(key=lambda x: x.qual_name)
        res = ""
        for c in cs[:-1]:
            res += (indent + c._tree_line(no_types) + suffix(c) +
                    c._ascii_tree(indent + "|  ", no_types, val_count))
        return (res + indent + cs[-1]._tree_line(no_types) + suffix(cs[-1]) +
                cs[-1]._ascii_tree(indent + "   ", no_types, val_count))

Response: Return the receiver s subtree as ASCII art.


Instruction:
Here you have a function in Python 3, explain what it does
def orphan_instance(self, rval: RawValue) -> "ObjectMember":
        val = self.from_raw(rval)
        return ObjectMember(self.iname(), {}, val, None, self, datetime.now())

Response: Return an isolated instance of the receiver.


Instruction:
Can you tell what is the following Python 3 function doing
def split_instance_route(self, route: "InstanceRoute") -> Optional[Tuple[
            "InstanceRoute", "InstanceRoute"]]:
        """Split `route` into the part up to receiver and the rest.

        Args:
            route: Absolute instance route (the receiver should correspond to an
                instance node on this route).

        Returns:
            A tuple consisting of
                - the part of `route` from the root up to and including the
                  instance whose schema node is the receiver, and
                - the rest of `route`.
                ``None`` is returned if the receiver is not on the route.
        """
        sroute = []
        sn = self
        while sn:
            sroute.append(sn.iname())
            sn = sn.data_parent()
        i = 0
        while True:
            if not sroute:
                break
            inst = sroute.pop()
            if inst != route[i].iname():
                return None
            while True:         # skip up to next MemberName
                i += 1
                if i >= len(route) or isinstance(route[i], MemberName):
                    break
            if not sroute:
                return (InstanceRoute(route[:i]), InstanceRoute(route[i:]))
            if i >= len(route):
                return None

Response: Splits the route into the part up to receiver and the rest of the route.


Instruction:
Can you generate the documentation for the following Python 3 function
def _validate(self, inst: "InstanceNode", scope: ValidationScope,
                  ctype: ContentType) -> None:
        """Extend the superclass method."""
        if scope.value & ValidationScope.semantics.value:
            self._check_must(inst)        # must expressions
        super()._validate(inst, scope, ctype)

Response: Extend the superclass method to check that the instance is valid.


Instruction:
Can you generate the documentation for the following Python 3 function
def content_type(self) -> ContentType:
        if self._ctype:
            return self._ctype
        return (ContentType.config if self.parent.config else
                ContentType.nonconfig)

Response: Return the content type of the resource.


Instruction:
Here you have a function in Python 3, explain what it does
def from_raw(self, rval: RawScalar, jptr: JSONPointer = "") -> ScalarValue:
        res = self.type.from_raw(rval)
        if res is None:
            raise RawTypeError(jptr, self.type.yang_type() + " value")
        return res

Response: Override the superclass method.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _validate(self, inst: "InstanceNode", scope: ValidationScope,
                  ctype: ContentType) -> None:
        """Extend the superclass method."""
        if (scope.value & ValidationScope.syntax.value and
                inst.value not in self.type):
            raise YangTypeError(inst.json_pointer(), self.type.error_tag,
                                self.type.error_message)
        if (isinstance(self.type, LinkType) and        # referential integrity
                scope.value & ValidationScope.semantics.value and
                self.type.require_instance):
            try:
                tgt = inst._deref()
            except YangsonException:
                tgt = []
            if not tgt:
                raise SemanticError(inst.json_pointer(), "instance-required")
        super()._validate(inst, scope, ctype)

Response: Extend the superclass method.


Instruction:
Write a Python 3 function that can
return the receiver s contribution to tree diagram.

Response:
def _tree_line(self, no_type: bool = False) -> str:
        """Return the receiver's contribution to tree diagram."""
        return super()._tree_line() + ("!" if self.presence else "")


Instruction:
How would you explain what the following Python 3 function does
def _validate(self, inst: "InstanceNode", scope: ValidationScope,
                  ctype: ContentType) -> None:
        """Extend the superclass method."""
        if isinstance(inst, ArrayEntry):
            super()._validate(inst, scope, ctype)
        else:
            if scope.value & ValidationScope.semantics.value:
                self._check_list_props(inst)
                self._check_cardinality(inst)
            for e in inst:
                super()._validate(e, scope, ctype)

Response: Extend the superclass method.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def from_raw(self, rval: RawList, jptr: JSONPointer = "") -> ArrayValue:
        if not isinstance(rval, list):
            raise RawTypeError(jptr, "array")
        res = ArrayValue()
        i = 0
        for en in rval:
            i += 1
            res.append(self.entry_from_raw(en, f"{jptr}/{i}"))
        return res

Response: Override the superclass method.


Instruction:
Implement a Python 3 function for
transforming a raw ( leaf - list entry into the cooked form.

Response:
def entry_from_raw(self, rval: RawEntry, jptr: JSONPointer = "") -> EntryValue:
        """Transform a raw (leaf-)list entry into the cooked form.

        Args:
            rval: raw entry (scalar or object)
            jptr: JSON pointer of the entry

        Raises:
            NonexistentSchemaNode: If a member inside `rval` is not defined
                in the schema.
            RawTypeError: If a scalar value inside `rval` is of incorrect type.
        """
        return super().from_raw(rval, jptr)


Instruction:
Make a summary of the following Python 3 code
def _check_list_props(self, inst: "InstanceNode") -> None:
        if self.keys:
            self._check_keys(inst)
        for u in self.unique:
            self._check_unique(u, inst)

Response: Check uniqueness of keys and unique properties if applicable.


Instruction:
Can you generate the documentation for the following Python 3 function
def _tree_line(self, no_type: bool = False) -> str:
        keys = (" [" + " ".join([k[0] for k in self.keys]) + "]"
                if self.keys else "")
        return super()._tree_line() + keys

Response: Return the receiver s contribution to tree diagram.


Instruction:
Create a Python 3 function for
returning an isolated entry of the receiver.

Response:
def orphan_entry(self, rval: RawObject) -> "ArrayEntry":
        """Return an isolated entry of the receiver.

        Args:
            rval: Raw object to be used for the returned entry.
        """
        val = self.entry_from_raw(rval)
        return ArrayEntry(0, EmptyList(), EmptyList(), val, None, self,
                          val.timestamp)


Instruction:
Create a Python 3 function to
return receiver s case that s active in an instance node value.

Response:
def _active_case(self, value: ObjectValue) -> Optional["CaseNode"]:
        """Return receiver's case that's active in an instance node value."""
        for c in self.children:
            for cc in c.data_children():
                if cc.iname() in value:
                    return c


Instruction:
Make a summary of the following Python 3 code
def default(self) -> Optional[ScalarValue]:
        if self.mandatory:
            return None
        if self._default is not None:
            return self._default
        return (None if self.type.default is None
                else ArrayValue([self.type.default]))

Response: Default value of the receiver if any.


Instruction:
Can you generate the documentation for the following Python 3 function
def from_raw(self, rval: RawValue, jptr: JSONPointer = "") -> Value:
        def convert(val):
            if isinstance(val, list):
                res = ArrayValue([convert(x) for x in val])
            elif isinstance(val, dict):
                res = ObjectValue({x: convert(val[x]) for x in val})
            else:
                res = val
            return res
        return convert(rval)

Response: Convert raw value to a new value.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _input_stmt(self, stmt: Statement, sctx: SchemaContext) -> None:
        self.get_child("input")._handle_substatements(stmt, sctx)

Response: Handle RPC or action input statement.


Instruction:
Create a Python 3 function for
handling RPC or action output statement.

Response:
def _output_stmt(self, stmt: Statement, sctx: SchemaContext) -> None:
        """Handle RPC or action output statement."""
        self.get_child("output")._handle_substatements(stmt, sctx)


Instruction:
Implement a function in Python 3 to
create an instance from a list of values.

Response:
def from_list(cls, vals: List[Value] = [], reverse: bool = False) -> "LinkedList":
        """Create an instance from a standard list.

        Args:
            vals: Python list of instance values.
        """
        res = EmptyList()
        for v in (vals if reverse else vals[::-1]):
            res = cls(v, res)
        return res


Instruction:
Given the following Python 3 function, write the documentation
def path(self) -> Tuple[InstanceKey]:
        res = []
        inst: InstanceNode = self
        while inst.parinst:
            res.insert(0, inst._key)
            inst = inst.parinst
        return tuple(res)

Response: Return the list of keys on the path from root to receiver.


Instruction:
Write a Python 3 script for
returning receiver s member with a new value.

Response:
def put_member(self, name: InstanceName, value: Value,
                   raw: bool = False) -> "InstanceNode":
        """Return receiver's member with a new value.

        If the member is permitted by the schema but doesn't exist, it
        is created.

        Args:
            name: Instance name of the member.
            value: New value of the member.
            raw: Flag to be set if `value` is raw.

        Raises:
            NonexistentSchemaNode: If member `name` is not permitted by the
                schema.
            InstanceValueError: If the receiver's value is not an object.
        """
        if not isinstance(self.value, ObjectValue):
            raise InstanceValueError(self.json_pointer(), "member of non-object")
        csn = self._member_schema_node(name)
        newval = self.value.copy()
        newval[name] = csn.from_raw(value, self.json_pointer()) if raw else value
        return self._copy(newval)._member(name)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def delete_item(self, key: InstanceKey) -> "InstanceNode":
        if not isinstance(self.value, StructuredValue):
            raise InstanceValueError(self.json_pointer(), "scalar value")
        newval = self.value.copy()
        try:
            del newval[key]
        except (KeyError, IndexError, TypeError):
            raise NonexistentInstance(self.json_pointer(),
                                      f"item '{key}'") from None
        return self._copy(newval)

Response: Delete an item from receiver s value.


Instruction:
Implement a Python 3 function for
returning an instance node corresponding to the receiver s parent.

Response:
def up(self) -> "InstanceNode":
        """Return an instance node corresponding to the receiver's parent.

        Raises:
            NonexistentInstance: If there is no parent.
        """
        ts = max(self.timestamp, self.parinst.timestamp)
        return self.parinst._copy(self._zip(), ts)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def top(self) -> "InstanceNode":
        inst = self
        while inst.parinst:
            inst = inst.up()
        return inst

Response: Return an instance node corresponding to the root of the data tree.


Instruction:
How would you explain what the following Python 3 function does
def update(self, value: Union[RawValue, Value],
               raw: bool = False) -> "InstanceNode":
        """Update the receiver's value.

        Args:
            value: New value.
            raw: Flag to be set if `value` is raw.

        Returns:
            Copy of the receiver with the updated value.
        """
        newval = self.schema_node.from_raw(
            value, self.json_pointer()) if raw else value
        return self._copy(newval)

Response: Update the receiver s value.


Instruction:
Write a Python 3 function for
moving focus to an instance inside the receiver s value.

Response:
def goto(self, iroute: "InstanceRoute") -> "InstanceNode":
        """Move the focus to an instance inside the receiver's value.

        Args:
            iroute: Instance route (relative to the receiver).

        Returns:
            The instance node corresponding to the target instance.

        Raises:
            InstanceValueError: If `iroute` is incompatible with the receiver's
                value.
            NonexistentInstance: If the instance node doesn't exist.
            NonDataNode: If an instance route addresses a non-data node
                (rpc/action/notification).
        """
        inst = self
        for sel in iroute:
            inst = sel.goto_step(inst)
        return inst


Instruction:
Write a Python 3 script for
returning a value within the receiver s subtree.

Response:
def peek(self, iroute: "InstanceRoute") -> Optional[Value]:
        """Return a value within the receiver's subtree.

        Args:
            iroute: Instance route (relative to the receiver).
        """
        val = self.value
        sn = self.schema_node
        for sel in iroute:
            val, sn = sel.peek_step(val, sn)
            if val is None:
                return None
        return val


Instruction:
How would you explain what the following Python 3 function does
def validate(self, scope: ValidationScope = ValidationScope.all,
                 ctype: ContentType = ContentType.config) -> None:
        """Validate the receiver's value.

        Args:
            scope: Scope of the validation (syntax, semantics or all).
            ctype: Receiver's content type.

        Raises:
            SchemaError: If the value doesn't conform to the schema.
            SemanticError: If the value violates a semantic constraint.
            YangTypeError: If the value is a scalar of incorrect type.
        """
        self.schema_node._validate(self, scope, ctype)

Response: Validate the receiver s value.


Instruction:
Implement a function in Python 3 to
return the receiver with defaults added recursively to its value.

Response:
def add_defaults(self, ctype: ContentType = None) -> "InstanceNode":
        """Return the receiver with defaults added recursively to its value.

        Args:
            ctype: Content type of the defaults to be added. If it is
                ``None``, the content type will be the same as receiver's.
        """
        val = self.value
        if not (isinstance(val, StructuredValue) and self.is_internal()):
            return self
        res = self
        if isinstance(val, ObjectValue):
            if val:
                for mn in self._member_names():
                    m = res._member(mn) if res is self else res.sibling(mn)
                    res = m.add_defaults(ctype)
                res = res.up()
            return self.schema_node._add_defaults(res, ctype)
        if not val:
            return res
        en = res[0]
        while True:
            res = en.add_defaults(ctype)
            try:
                en = res.next()
            except NonexistentInstance:
                break
        return res.up()


Instruction:
Write a Python 3 function that can
return receiver s value in a raw form ( ready for JSON encoding ).

Response:
def raw_value(self) -> RawValue:
        """Return receiver's value in a raw form (ready for JSON encoding)."""
        if isinstance(self.value, ObjectValue):
            return {m: self._member(m).raw_value() for m in self.value}
        if isinstance(self.value, ArrayValue):
            return [en.raw_value() for en in self]
        return self.schema_node.type.to_raw(self.value)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _node_set(self) -> List["InstanceNode"]:
        return list(self) if isinstance(self.value, ArrayValue) else [self]

Response: XPath - return the list of all receiver s nodes.


Instruction:
Here you have a function in Python 3, explain what it does
def _children(self, qname:
                  Union[QualName, bool] = None) -> List["InstanceNode"]:
        """XPath - return the list of receiver's children."""
        sn = self.schema_node
        if not isinstance(sn, InternalNode):
            return []
        if qname:
            cn = sn.get_data_child(*qname)
            if cn is None:
                return []
            iname = cn.iname()
            if iname in self.value:
                return self._member(iname)._node_set()
            wd = cn._default_instance(self, ContentType.all, lazy=True)
            if iname not in wd.value:
                return []
            while True:
                cn = cn.parent
                if cn is sn:
                    return wd._member(iname)._node_set()
                if (cn.when and not cn.when.evaluate(self) or
                    isinstance(cn, CaseNode) and
                        cn.qual_name != cn.parent.default_case):
                    return []
        res = []
        wd = sn._add_defaults(self, ContentType.all, lazy=True)
        for mn in wd.value:
            res.extend(wd._member(mn)._node_set())
        return res

Response: XPath - return the list of receiver s children.


Instruction:
Given the following Python 3 function, write the documentation
def _deref(self) -> List["InstanceNode"]:
        return ([] if self.is_internal() else
                self.schema_node.type._deref(self))

Response: XPath - returns the list of nodes that the receiver refers to.


Instruction:
Here you have a function in Python 3, explain what it does
def qual_name(self) -> QualName:
        p, s, loc = self._key.partition(":")
        return (loc, p) if s else (p, self.namespace)

Response: Return the receiver s qualified name.


Instruction:
Can you implement a function in Python 3 that
returns an instance node corresponding to a sibling member.

Response:
def sibling(self, name: InstanceName) -> "ObjectMember":
        """Return an instance node corresponding to a sibling member.

        Args:
            name: Instance name of the sibling member.

        Raises:
            NonexistentSchemaNode: If member `name` is not permitted by the
                schema.
            NonexistentInstance: If sibling member `name` doesn't exist.
        """
        ssn = self.parinst._member_schema_node(name)
        try:
            sibs = self.siblings.copy()
            newval = sibs.pop(name)
            sibs[self.name] = self.value
            return ObjectMember(name, sibs, newval, self.parinst,
                                ssn, self.timestamp)
        except KeyError:
            raise NonexistentInstance(self.json_pointer(),
                                      f"member '{name}'") from None


Instruction:
Can you create a Python 3 function that
looks up an entry in YANG list with the given keys.

Response:
def look_up(self, **keys: Dict[InstanceName, ScalarValue]) -> "ArrayEntry":
        """Return the entry with matching keys.

        Args:
            keys: Keys and values specified as keyword arguments.

        Raises:
            InstanceValueError: If the receiver's value is not a YANG list.
            NonexistentInstance: If no entry with matching keys exists.
        """
        if not isinstance(self.schema_node, ListNode):
            raise InstanceValueError(self.json_pointer(), "lookup on non-list")
        try:
            for i in range(len(self.value)):
                en = self.value[i]
                flag = True
                for k in keys:
                    if en[k] != keys[k]:
                        flag = False
                        break
                if flag:
                    return self._entry(i)
            raise NonexistentInstance(self.json_pointer(), "entry lookup failed")
        except KeyError:
            raise NonexistentInstance(self.json_pointer(), "entry lookup failed") from None
        except TypeError:
            raise InstanceValueError(self.json_pointer(), "lookup on non-list") from None


Instruction:
Make a summary of the following Python 3 code
def _zip(self) -> ObjectValue:
        res = ObjectValue(self.siblings.copy(), self.timestamp)
        res[self.name] = self.value
        return res

Response: Zip the receiver into an object and return it.


Instruction:
Can you write a function in Python 3 where it
updates the receiver s value.

Response:
def update(self, value: Union[RawValue, Value],
               raw: bool = False) -> "ArrayEntry":
        """Update the receiver's value.

        This method overrides the superclass method.
        """
        return super().update(self._cook_value(value, raw), False)


Instruction:
Create a Python 3 function to
return an instance node corresponding to the previous entry in the array.

Response:
def previous(self) -> "ArrayEntry":
        """Return an instance node corresponding to the previous entry.

        Raises:
            NonexistentInstance: If the receiver is the first entry of the
                parent array.
        """
        try:
            newval, nbef = self.before.pop()
        except IndexError:
            raise NonexistentInstance(self.json_pointer(), "previous of first") from None
        return ArrayEntry(
            self.index - 1, nbef, self.after.cons(self.value), newval,
            self.parinst, self.schema_node, self.timestamp)


Instruction:
Implement a function in Python 3 to
return the next entry in the array.

Response:
def next(self) -> "ArrayEntry":
        """Return an instance node corresponding to the next entry.

        Raises:
            NonexistentInstance: If the receiver is the last entry of the parent array.
        """
        try:
            newval, naft = self.after.pop()
        except IndexError:
            raise NonexistentInstance(self.json_pointer(), "next of last") from None
        return ArrayEntry(
            self.index + 1, self.before.cons(self.value), naft, newval,
            self.parinst, self.schema_node, self.timestamp)


Instruction:
Create a Python 3 function to
insert a new entry before receiver.

Response:
def insert_before(self, value: Union[RawValue, Value],
                      raw: bool = False) -> "ArrayEntry":
        """Insert a new entry before the receiver.

        Args:
            value: The value of the new entry.
            raw: Flag to be set if `value` is raw.

        Returns:
            An instance node of the new inserted entry.
        """
        return ArrayEntry(self.index, self.before, self.after.cons(self.value),
                          self._cook_value(value, raw), self.parinst,
                          self.schema_node, datetime.now())


Instruction:
Write a Python 3 script for
zipping the receiver into an array and return it.

Response:
def _zip(self) -> ArrayValue:
        """Zip the receiver into an array and return it."""
        res = list(self.before)
        res.reverse()
        res.append(self.value)
        res.extend(list(self.after))
        return ArrayValue(res, self.timestamp)


Instruction:
Explain what the following Python 3 code does
def _ancestors(
            self, qname: Union[QualName, bool] = None) -> List[InstanceNode]:
        """XPath - return the list of receiver's ancestors."""
        return self.up()._ancestors(qname)

Response: XPath - return the list of receiver s ancestors.


Instruction:
Make a summary of the following Python 3 code
def _preceding_siblings(
            self, qname: Union[QualName, bool] = None) -> List[InstanceNode]:
        """XPath - return the list of receiver's preceding siblings."""
        if qname and self.qual_name != qname:
            return []
        res = []
        en = self
        for _ in self.before:
            en = en.previous()
            res.append(en)
        return res

Response: XPath - return the list of receiver s preceding siblings.


Instruction:
Given the following Python 3 function, write the documentation
def _following_siblings(
            self, qname: Union[QualName, bool] = None) -> List[InstanceNode]:
        """XPath - return the list of receiver's following siblings."""
        if qname and self.qual_name != qname:
            return []
        res = []
        en = self
        for _ in self.after:
            en = en.next()
            res.append(en)
        return res

Response: XPath - return the list of receiver s following siblings.


Instruction:
Can you create a Python 3 function that
returns member value addressed by the receiver + its schema node.

Response:
def peek_step(self, val: ObjectValue,
                  sn: "DataNode") -> Tuple[Value, "DataNode"]:
        """Return member value addressed by the receiver + its schema node.

        Args:
            val: Current value (object).
            sn:  Current schema node.
        """
        cn = sn.get_data_child(self.name, self.namespace)
        try:
            return (val[cn.iname()], cn)
        except (IndexError, KeyError, TypeError):
            return (None, cn)


Instruction:
Explain what the following Python 3 code does
def peek_step(self, val: ObjectValue,
                  sn: "DataNode") -> Tuple[None, "DataNode"]:
        """Fail because there is no action instance."""
        cn = sn.get_child(self.name, self.namespace)
        return (None, cn)

Response: Fail because there is no action instance.


Instruction:
Write a Python 3 function that can
return the next entry value in the receiver + its schema node.

Response:
def peek_step(self, val: ArrayValue,
                  sn: "DataNode") -> Tuple[Optional[Value], "DataNode"]:
        """Return entry value addressed by the receiver + its schema node.

        Args:
            val: Current value (array).
            sn:  Current schema node.
        """
        try:
            return val[self.index], sn
        except (IndexError, KeyError, TypeError):
            return None, sn


Instruction:
How would you implement a function in Python 3 that
lets schema node s type parse the receiver s value.

Response:
def parse_value(self, sn: "DataNode") -> ScalarValue:
        """Let schema node's type parse the receiver's value."""
        res = sn.type.parse_value(self.value)
        if res is None:
            raise InvalidKeyValue(self.value)
        return res


Instruction:
Can you write a function in Python 3 where it
returns the next entry value in the receiver + its schema node.

Response:
def peek_step(self, val: ArrayValue,
                  sn: "DataNode") -> Tuple[Value, "DataNode"]:
        """Return entry value addressed by the receiver + its schema node.

        Args:
            val: Current value (array).
            sn:  Current schema node.
        """
        try:
            return (val[val.index(self.parse_value(sn))], sn)
        except ValueError:
            return None, sn


Instruction:
Create a Python 3 function for
returning member instance of inst addressed by the receiver.

Response:
def goto_step(self, inst: InstanceNode) -> InstanceNode:
        """Return member instance of `inst` addressed by the receiver.

        Args:
            inst: Current instance.
        """
        try:
            return inst._entry(
                inst.value.index(self.parse_value(inst.schema_node)))
        except ValueError:
            raise NonexistentInstance(inst.json_pointer(),
                                      f"entry '{self.value!s}'") from None


Instruction:
How would you implement a function in Python 3 that
parses the keys of a list.

Response:
def parse_keys(self, sn: "DataNode") -> Dict[InstanceName, ScalarValue]:
        """Parse key dictionary in the context of a schema node.

        Args:
            sn: Schema node corresponding to a list.
        """
        res = {}
        for k in self.keys:
            knod = sn.get_data_child(*k)
            if knod is None:
                raise NonexistentSchemaNode(sn.qual_name, *k)
            kval = knod.type.parse_value(self.keys[k])
            if kval is None:
                raise InvalidKeyValue(self.keys[k])
            res[knod.iname()] = kval
        return res


Instruction:
Can you write a function in Python 3 where it
returns the entry addressed by the receiver + its schema node.

Response:
def peek_step(self, val: ArrayValue,
                  sn: "DataNode") -> Tuple[ObjectValue, "DataNode"]:
        """Return the entry addressed by the receiver + its schema node.

        Args:
            val: Current value (array).
            sn:  Current schema node.
        """
        keys = self.parse_keys(sn)
        for en in val:
            flag = True
            try:
                for k in keys:
                    if en[k] != keys[k]:
                        flag = False
                        break
            except KeyError:
                continue
            if flag:
                return (en, sn)
        return (None, sn)


Instruction:
Make a summary of the following Python 3 code
def goto_step(self, inst: InstanceNode) -> InstanceNode:
        return inst.look_up(**self.parse_keys(inst.schema_node))

Response: Return member instance of inst addressed by the receiver.


Instruction:
Can you implement a function in Python 3 that
parses the resource identifier.

Response:
def parse(self) -> InstanceRoute:
        """Parse resource identifier."""
        res = InstanceRoute()
        if self.at_end():
            return res
        if self.peek() == "/":
            self.offset += 1
        if self.at_end():
            return res
        sn = self.schema_node
        while True:
            name, ns = self.prefixed_name()
            cn = sn.get_data_child(name, ns)
            if cn is None:
                for cn in sn.children:
                    if (isinstance(cn, RpcActionNode) and cn.name == name and
                            (ns is None or cn.ns == ns)):
                        res.append(ActionName(name, ns))
                        return res
                raise NonexistentSchemaNode(sn.qual_name, name, ns)
            res.append(MemberName(name, ns))
            if self.at_end():
                return res
            if isinstance(cn, SequenceNode):
                self.char("=")
                res.append(self._key_values(cn))
                if self.at_end():
                    return res
            else:
                self.char("/")
            sn = cn


Instruction:
How would you explain what the following Python 3 function does
def _key_values(self, sn: "SequenceNode") -> Union[EntryKeys, EntryValue]:
        try:
            keys = self.up_to("/")
        except EndOfInput:
            keys = self.remaining()
        if not keys:
            raise UnexpectedInput(self, "entry value or keys")
        if isinstance(sn, LeafListNode):
            return EntryValue(unquote(keys))
        ks = keys.split(",")
        try:
            if len(ks) != len(sn.keys):
                raise UnexpectedInput(self, f"exactly {len(sn.keys)} keys")
        except AttributeError:
            raise BadSchemaNodeType(sn.qual_name, "list")
        sel = {}
        for j in range(len(ks)):
            knod = sn.get_data_child(*sn.keys[j])
            val = unquote(ks[j])
            sel[(knod.name, None if knod.ns == sn.ns else knod.ns)] = val
        return EntryKeys(sel)

Response: Parse leaf - list value or list keys.


Instruction:
Make a summary of the following Python 3 code
def find1(self, kw: YangIdentifier, arg: str = None,
              pref: YangIdentifier = None,
              required: bool = False) -> Optional["Statement"]:
        """Return first substatement with the given parameters.

        Args:
            kw: Statement keyword (local part for extensions).
            arg: Argument (all arguments will match if ``None``).
            pref: Keyword prefix (``None`` for built-in statements).
            required: Should an exception be raised on failure?

        Raises:
            StatementNotFound: If `required` is ``True`` and the
                statement is not found.
        """
        for sub in self.substatements:
            if (sub.keyword == kw and sub.prefix == pref and
                    (arg is None or sub.argument == arg)):
                return sub
        if required:
            raise StatementNotFound(str(self), kw)

Response: Return first substatement with the given parameters.


Instruction:
Explain what the following Python 3 code does
def find_all(self, kw: YangIdentifier,
                 pref: YangIdentifier = None) -> List["Statement"]:
        """Return the list all substatements with the given keyword and prefix.

        Args:
            kw: Statement keyword (local part for extensions).
            pref: Keyword prefix (``None`` for built-in statements).
        """
        return [c for c in self.substatements
                if c.keyword == kw and c.prefix == pref]

Response: Return the list all substatements with the given keyword and prefix.


Instruction:
Make a summary of the following Python 3 code
def get_definition(self, name: YangIdentifier,
                       kw: YangIdentifier) -> Optional["Statement"]:
        """Search ancestor statements for a definition.

        Args:
            name: Name of a grouping or datatype (with no prefix).
            kw: ``grouping`` or ``typedef``.

        Raises:
            DefinitionNotFound: If the definition is not found.
        """
        stmt = self.superstmt
        while stmt:
            res = stmt.find1(kw, name)
            if res:
                return res
            stmt = stmt.superstmt
        return None

Response: Search ancestor statements for a definition.


Instruction:
Can you create a Python 3 function that
returns receiver s error tag and error message if present.

Response:
def get_error_info(self) -> Tuple[Optional[str], Optional[str]]:
        """Return receiver's error tag and error message if present."""
        etag = self.find1("error-app-tag")
        emsg = self.find1("error-message")
        return (etag.argument if etag else None, emsg.argument if emsg else None)


Instruction:
Can you generate the documentation for the following Python 3 function
def parse(self) -> Statement:
        self.opt_separator()
        start = self.offset
        res = self.statement()
        if res.keyword not in ["module", "submodule"]:
            self.offset = start
            raise UnexpectedInput(self, "'module' or 'submodule'")
        if self.name is not None and res.argument != self.name:
            raise ModuleNameMismatch(res.argument, self.name)
        if self.rev:
            revst = res.find1("revision")
            if revst is None or revst.argument != self.rev:
                raise ModuleRevisionMismatch(revst.argument, self.rev)
        try:
            self.opt_separator()
        except EndOfInput:
            return res
        raise UnexpectedInput(self, "end of input")

Response: Parse a complete YANG module or submodule.


Instruction:
Explain what the following Python 3 code does
def unescape(cls, text: str) -> str:
        chop = text.split("\\", 1)
        try:
            return (chop[0] if len(chop) == 1
                    else chop[0] + cls.unescape_map[chop[1][0]] +
                    cls.unescape(chop[1][1:]))
        except KeyError:
            raise InvalidArgument(text) from None

Response: Unescape the escape sequence with corresponding characters.


Instruction:
Can you generate the documentation for the following Python 3 function
def opt_separator(self) -> bool:
        start = self.offset
        self.dfa([
            {  # state 0: whitespace
                "": lambda: -1,
                " ": lambda: 0,
                "\t": lambda: 0,
                "\n": lambda: 0,
                "\r": lambda: 1,
                "/": lambda: 2
            },
            {  # state 1: CR/LF?
                "": self._back_break,
                "\n": lambda: 0
            },
            {  # state 2: start comment?
                "": self._back_break,
                "/": lambda: 3,
                "*": lambda: 4
            },
            {  # state 3: line comment
                "": lambda: 3,
                "\n": lambda: 0
            },
            {  # state 4: block comment
                "": lambda: 4,
                "*": lambda: 5
            },
            {  # state 5: end block comment?
                "": lambda: 4,
                "/": lambda: 0,
                "*": lambda: 5
            }])
        return start < self.offset

Response: Parse an optional separator and return True if found.


Instruction:
Can you implement a function in Python 3 that
parses a YANG statement keyword.

Response:
def keyword(self) -> Tuple[Optional[str], str]:
        """Parse a YANG statement keyword.

        Raises:
            EndOfInput: If past the end of input.
            UnexpectedInput: If no syntactically correct keyword is found.
        """
        i1 = self.yang_identifier()
        if self.peek() == ":":
            self.offset += 1
            i2 = self.yang_identifier()
            return (i1, i2)
        return (None, i1)


Instruction:
Write a Python 3 script to
parse YANG statement. Raises: EndOfInput: If past the end of input. UnexpectedInput: If no syntactically correct statement is found.

Response:
def statement(self) -> Statement:
        """Parse YANG statement.

        Raises:
            EndOfInput: If past the end of input.
            UnexpectedInput: If no syntactically correct statement is found.
        """
        pref, kw = self.keyword()
        pres = self.opt_separator()
        next = self.peek()
        if next == ";":
            arg = None
            sub = False  # type: bool
        elif next == "{":
            arg = None
            sub = True
        elif not pres:
            raise UnexpectedInput(self, "separator")
        else:
            self._arg = ""
            sub = self.argument()
            arg = self._arg
        self.offset += 1
        res = Statement(kw, arg, pref=pref)
        if sub:
            res.substatements = self.substatements()
            for sub in res.substatements:
                sub.superstmt = res
        return res


Instruction:
Given the following Python 3 function, write the documentation
def argument(self) -> bool:
        next = self.peek()
        if next == "'":
            quoted = True
            self.sq_argument()
        elif next == '"':
            quoted = True
            self.dq_argument()
        elif self._arg == "":
            quoted = False
            self.unq_argument()
        else:
            raise UnexpectedInput(self, "single or double quote")
        self.opt_separator()
        next = self.peek()
        if next == ";":
            return False
        if next == "{":
            return True
        elif quoted and next == "+":
            self.offset += 1
            self.opt_separator()
            return self.argument()
        else:
            raise UnexpectedInput(self, "';', '{'" +
                                  (" or '+'" if quoted else ""))

Response: Parse the next argument in the string.


Instruction:
Can you implement a function in Python 3 that
parses double - quoted argument.

Response:
def dq_argument(self) -> str:
        """Parse double-quoted argument.

        Raises:
            EndOfInput: If past the end of input.
        """
        def escape():
            self._escape = True
            return 1
        self._escape = False                 # any escaped chars?
        self.offset += 1
        start = self.offset
        self.dfa([
            {  # state 0: argument
                "": lambda: 0,
                '"': lambda: -1,
                "\\": escape
            },
            {  # state 1: after escape
                "": lambda: 0
            }])
        self._arg += (self.unescape(self.input[start:self.offset])
                      if self._escape else self.input[start:self.offset])
        self.offset += 1


Instruction:
Implement a function in Python 3 to
parse an unquoted argument.

Response:
def unq_argument(self) -> str:
        """Parse unquoted argument.

        Raises:
            EndOfInput: If past the end of input.
        """
        start = self.offset
        self.dfa([
            {  # state 0: argument
                "": lambda: 0,
                ";": lambda: -1,
                " ": lambda: -1,
                "\t": lambda: -1,
                "\r": lambda: -1,
                "\n": lambda: -1,
                "{": lambda: -1,
                '/': lambda: 1
            },
            {  # state 1: comment?
                "": lambda: 0,
                "/": self._back_break,
                "*": self._back_break
            }])
        self._arg = self.input[start:self.offset]


Instruction:
Can you generate a brief explanation for the following Python 3 code
def substatements(self) -> List[Statement]:
        res = []
        self.opt_separator()
        while self.peek() != "}":
            res.append(self.statement())
            self.opt_separator()
        self.offset += 1
        return res

Response:
Parse substatements.

        Raises:
            EndOfInput: If past the end of input.


Instruction:
Here you have a function in Python 3, explain what it does
def _from_yang_library(self, yang_lib: Dict[str, Any]) -> None:
        try:
            for item in yang_lib["ietf-yang-library:modules-state"]["module"]:
                name = item["name"]
                rev = item["revision"]
                mid = (name, rev)
                mdata = ModuleData(mid)
                self.modules[mid] = mdata
                if item["conformance-type"] == "implement":
                    if name in self.implement:
                        raise MultipleImplementedRevisions(name)
                    self.implement[name] = rev
                mod = self._load_module(name, rev)
                mdata.statement = mod
                if "feature" in item:
                    mdata.features.update(item["feature"])
                locpref = mod.find1("prefix", required=True).argument
                mdata.prefix_map[locpref] = mid
                if "submodule" in item:
                    for s in item["submodule"]:
                        sname = s["name"]
                        smid = (sname, s["revision"])
                        sdata = ModuleData(mid)
                        self.modules[smid] = sdata
                        mdata.submodules.add(smid)
                        submod = self._load_module(*smid)
                        sdata.statement = submod
                        bt = submod.find1("belongs-to", name, required=True)
                        locpref = bt.find1("prefix", required=True).argument
                        sdata.prefix_map[locpref] = mid
        except KeyError as e:
            raise BadYangLibraryData("missing " + str(e)) from None
        self._process_imports()
        self._check_feature_dependences()

Response: Set the schema structures from YANG library data.


Instruction:
How would you code a function in Python 3 to
read and parse a YANG module or submodule.

Response:
def _load_module(self, name: YangIdentifier,
                     rev: RevisionDate) -> Statement:
        """Read and parse a YANG module or submodule."""
        for d in self.module_search_path:
            run = 0
            while run < 2:
                fn = f"{d}/{name}"
                if rev and run == 0:
                    fn += "@" + rev
                fn += ".yang"
                try:
                    with open(fn, encoding='utf-8') as infile:
                        res = ModuleParser(infile.read(), name, rev).parse()
                except (FileNotFoundError, PermissionError, ModuleContentMismatch):
                    run += 1
                    continue
                return res
        raise ModuleNotFound(name, rev)


Instruction:
Can you tell what is the following Python 3 function doing
def namespace(self, mid: ModuleId) -> YangIdentifier:
        try:
            mdata = self.modules[mid]
        except KeyError:
            raise ModuleNotRegistered(*mid) from None
        return mdata.main_module[0]

Response: Return the namespace corresponding to a module or submodule.


Instruction:
Write a Python 3 function that can
return the last revision of a module or submodule.

Response:
def last_revision(self, mod: YangIdentifier) -> ModuleId:
        """Return the last revision of a module that's part of the data model.

        Args:
            mod: Name of a module or submodule.

        Raises:
            ModuleNotRegistered: If the module `mod` is not present in the
                data model.
        """
        revs = [mn for mn in self.modules if mn[0] == mod]
        if not revs:
            raise ModuleNotRegistered(mod)
        return sorted(revs, key=lambda x: x[1])[-1]


Instruction:
Given the following Python 3 function, write the documentation
def prefix2ns(self, prefix: YangIdentifier, mid: ModuleId) -> YangIdentifier:
        try:
            mdata = self.modules[mid]
        except KeyError:
            raise ModuleNotRegistered(*mid) from None
        try:
            return mdata.prefix_map[prefix][0]
        except KeyError:
            raise UnknownPrefix(prefix, mid) from None

Response: Return the namespace corresponding to a prefix.


Instruction:
Create a Python 3 function to
resolve a name with an optional prefix.

Response:
def resolve_pname(self, pname: PrefName,
                      mid: ModuleId) -> Tuple[YangIdentifier, ModuleId]:
        """Return the name and module identifier in which the name is defined.

        Args:
            pname: Name with an optional prefix.
            mid: Identifier of the module in which `pname` appears.

        Raises:
            ModuleNotRegistered: If `mid` is not registered in the data model.
            UnknownPrefix: If the prefix specified in `pname` is not declared.
        """
        p, s, loc = pname.partition(":")
        try:
            mdata = self.modules[mid]
        except KeyError:
            raise ModuleNotRegistered(*mid) from None
        try:
            return (loc, mdata.prefix_map[p]) if s else (p, mdata.main_module)
        except KeyError:
            raise UnknownPrefix(p, mid) from None


Instruction:
Can you create a Python 3 function that
translates a prefixed name to a qualified name.

Response:
def translate_pname(self, pname: PrefName, mid: ModuleId) -> QualName:
        """Translate a prefixed name to a qualified name.
        Args:
            pname: Name with an optional prefix.
            mid: Identifier of the module in which `pname` appears.
        Raises:
            ModuleNotRegistered: If `mid` is not registered in the data model.
            UnknownPrefix: If the prefix specified in `pname` is not declared.
        """
        loc, nid = self.resolve_pname(pname, mid)
        return (loc, self.namespace(nid))


Instruction:
Can you generate the documentation for the following Python 3 function
def translate_node_id(self, ni: PrefName, sctx: SchemaContext) -> QualName:
        p, s, loc = ni.partition(":")
        if not s:
            return (ni, sctx.default_ns)
        try:
            mdata = self.modules[sctx.text_mid]
        except KeyError:
            raise ModuleNotRegistered(*sctx.text_mid) from None
        try:
            return (loc, self.namespace(mdata.prefix_map[p]))
        except KeyError:
            raise UnknownPrefix(p, sctx.text_mid) from None

Response: Translate a node identifier to a qualified name.


Instruction:
Given the following Python 3 function, write the documentation
def prefix(self, imod: YangIdentifier, mid: ModuleId) -> YangIdentifier:
        try:
            did = (imod, self.implement[imod])
        except KeyError:
            raise ModuleNotImplemented(imod) from None
        try:
            pmap = self.modules[mid].prefix_map
        except KeyError:
            raise ModuleNotRegistered(*mid) from None
        for p in pmap:
            if pmap[p] == did:
                return p
        raise ModuleNotImported(imod, mid)

Response: Return the prefix corresponding to an implemented module.


Instruction:
Can you generate the documentation for the following Python 3 function
def sni2route(self, sni: SchemaNodeId, sctx: SchemaContext) -> SchemaRoute:
        nlist = sni.split("/")
        res = []
        for qn in (nlist[1:] if sni[0] == "/" else nlist):
            res.append(self.translate_node_id(qn, sctx))
        return res

Response: Translate a schema node identifier to a schema route.


Instruction:
Here you have a function in Python 3, explain what it does
def path2route(path: SchemaPath) -> SchemaRoute:
        if path == "/" or path == "":
            return []
        nlist = path.split("/")
        prevns = None
        res = []
        for n in (nlist[1:] if path[0] == "/" else nlist):
            p, s, loc = n.partition(":")
            if s:
                if p == prevns:
                    raise InvalidSchemaPath(path)
                res.append((loc, p))
                prevns = p
            elif prevns:
                res.append((p, prevns))
            else:
                raise InvalidSchemaPath(path)
        return res

Response: Translate a schema path to a schema route.


Instruction:
Given the following Python 3 function, write the documentation
def get_definition(self, stmt: Statement,
                       sctx: SchemaContext) -> Tuple[Statement, SchemaContext]:
        """Find the statement defining a grouping or derived type.

        Args:
            stmt: YANG "uses" or "type" statement.
            sctx: Schema context where the definition is used.

        Returns:
            A tuple consisting of the definition statement ('grouping' or
            'typedef') and schema context of the definition.

        Raises:
            ValueError: If `stmt` is neither "uses" nor "type" statement.
            ModuleNotRegistered: If `mid` is not registered in the data model.
            UnknownPrefix: If the prefix specified in the argument of `stmt`
                is not declared.
            DefinitionNotFound: If the corresponding definition is not found.
        """
        if stmt.keyword == "uses":
            kw = "grouping"
        elif stmt.keyword == "type":
            kw = "typedef"
        else:
            raise ValueError("not a 'uses' or 'type' statement")
        loc, did = self.resolve_pname(stmt.argument, sctx.text_mid)
        if did == sctx.text_mid:
            dstmt = stmt.get_definition(loc, kw)
            if dstmt:
                return (dstmt, sctx)
        else:
            dstmt = self.modules[did].statement.find1(kw, loc)
            if dstmt:
                return (dstmt, SchemaContext(sctx.schema_data, sctx.default_ns, did))
        for sid in self.modules[did].submodules:
            dstmt = self.modules[sid].statement.find1(kw, loc)
            if dstmt:
                return (
                    dstmt, SchemaContext(sctx.schema_data, sctx.default_ns, sid))
        raise DefinitionNotFound(kw, stmt.argument)

Response: Find the statement defining a grouping or derived type.


Instruction:
Given the following Python 3 function, write the documentation
def is_derived_from(self, identity: QualName, base: QualName) -> bool:
        try:
            bases = self.identity_adjs[identity].bases
        except KeyError:
            return False
        if base in bases:
            return True
        for ib in bases:
            if self.is_derived_from(ib, base):
                return True
        return False

Response: Return True if identity is derived from base.


Instruction:
Given the following Python 3 function, write the documentation
def derived_from(self, identity: QualName) -> MutableSet[QualName]:
        try:
            res = self.identity_adjs[identity].derivs
        except KeyError:
            return set()
        for id in res.copy():
            res |= self.derived_from(id)
        return res

Response: Return list of identities transitively derived from identity.