Instruction:
Implement a function in Python 3 to
process the extractedurls and ready them for the curses browser or non - interactive output.

Response:
def process_urls(self, extractedurls, dedupe, shorten):
        """Process the 'extractedurls' and ready them for either the curses browser
        or non-interactive output

        Args: extractedurls
              dedupe - Remove duplicate URLs from list

        Returns: items - List of widgets for the ListBox
                 urls - List of all URLs

        """
        cols, _ = urwid.raw_display.Screen().get_cols_rows()
        items = []
        urls = []
        first = True
        for group, usedfirst, usedlast in extractedurls:
            if first:
                first = False
            items.append(urwid.Divider(div_char='-', top=1, bottom=1))
            if dedupe is True:
                # If no unique URLs exist, then skip the group completely
                if not [chunk for chunks in group for chunk in chunks
                        if chunk.url is not None and chunk.url not in urls]:
                    continue
            groupurls = []
            markup = []
            if not usedfirst:
                markup.append(('msgtext:ellipses', '...\n'))
            for chunks in group:
                i = 0
                while i < len(chunks):
                    chunk = chunks[i]
                    i += 1
                    if chunk.url is None:
                        markup.append(('msgtext', chunk.markup))
                    else:
                        if (dedupe is True and chunk.url not in urls) \
                                or dedupe is False:
                            urls.append(chunk.url)
                            groupurls.append(chunk.url)
                        # Collect all immediately adjacent
                        # chunks with the same URL.
                        tmpmarkup = []
                        if chunk.markup:
                            tmpmarkup.append(('msgtext', chunk.markup))
                        while i < len(chunks) and \
                                chunks[i].url == chunk.url:
                            if chunks[i].markup:
                                tmpmarkup.append(chunks[i].markup)
                            i += 1
                        url_idx = urls.index(chunk.url) + 1 if dedupe is True else len(urls)
                        markup += [tmpmarkup or '<URL>',
                                   ('urlref:number:braces', ' ['),
                                   ('urlref:number', repr(url_idx)),
                                   ('urlref:number:braces', ']')]
                markup += '\n'
            if not usedlast:
                markup += [('msgtext:ellipses', '...\n\n')]
            items.append(urwid.Text(markup))

            i = len(urls) - len(groupurls)
            for url in groupurls:
                i += 1
                markup = [(6, urwid.Text([('urlref:number:braces', '['),
                                          ('urlref:number', repr(i)),
                                          ('urlref:number:braces', ']'),
                                          ' '])),
                          urwid.AttrMap(urwid.Button(shorten_url(url,
                                                                 cols,
                                                                 shorten),
                                                     self.mkbrowseto(url),
                                                     user_data=url),
                                        'urlref:url', 'url:sel')]
                items.append(urwid.Columns(markup))

        return items, urls


Instruction:
Can you implement a function in Python 3 that
returns the key file path.

Response:
def _get_key_file_path():
        """Return the key file path."""
        if os.getenv(USER_HOME) is not None and os.access(os.getenv(USER_HOME),
                                                          os.W_OK):
            return os.path.join(os.getenv(USER_HOME), KEY_FILE_NAME)

        return os.path.join(os.getcwd(), KEY_FILE_NAME)


Instruction:
Can you tell what is the following Python 3 function doing
def load_key_file(self):
        self.client_key = None
        if self.key_file_path:
            key_file_path = self.key_file_path
        else:
            key_file_path = self._get_key_file_path()
        key_dict = {}

        logger.debug('load keyfile from %s', key_file_path);

        if os.path.isfile(key_file_path):
            with open(key_file_path, 'r') as f:
                raw_data = f.read()
                if raw_data:
                    key_dict = json.loads(raw_data)

        logger.debug('getting client_key for %s from %s', self.ip, key_file_path);
        if self.ip in key_dict:
            self.client_key = key_dict[self.ip]

Response: Try to load the client key for the current ip.


Instruction:
Make a summary of the following Python 3 code
def save_key_file(self):
        if self.client_key is None:
            return

        if self.key_file_path:
            key_file_path = self.key_file_path
        else:
            key_file_path = self._get_key_file_path()

        logger.debug('save keyfile to %s', key_file_path);

        with open(key_file_path, 'w+') as f:
            raw_data = f.read()
            key_dict = {}

            if raw_data:
                key_dict = json.loads(raw_data)

            key_dict[self.ip] = self.client_key

            f.write(json.dumps(key_dict))

Response: Save the current client key.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _send_register_payload(self, websocket):
        file = os.path.join(os.path.dirname(__file__), HANDSHAKE_FILE_NAME)

        data = codecs.open(file, 'r', 'utf-8')
        raw_handshake = data.read()

        handshake = json.loads(raw_handshake)
        handshake['payload']['client-key'] = self.client_key

        yield from websocket.send(json.dumps(handshake))
        raw_response = yield from websocket.recv()
        response = json.loads(raw_response)

        if response['type'] == 'response' and \
                        response['payload']['pairingType'] == 'PROMPT':
            raw_response = yield from websocket.recv()
            response = json.loads(raw_response)
            if response['type'] == 'registered':
                self.client_key = response['payload']['client-key']
                self.save_key_file()

Response: Send the register payload.


Instruction:
How would you code a function in Python 3 to
pair client with tv.

Response:
def register(self):
        """Pair client with tv."""
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        loop.run_until_complete(self._register())


Instruction:
Make a summary of the following Python 3 code
def _command(self, msg):
        logger.debug('send command to %s', "ws://{}:{}".format(self.ip, self.port));
        try:
            websocket = yield from websockets.connect(
                "ws://{}:{}".format(self.ip, self.port), timeout=self.timeout_connect)
        except:
            logger.debug('command failed to connect to %s', "ws://{}:{}".format(self.ip, self.port));
            return False

        logger.debug('command websocket connected to %s', "ws://{}:{}".format(self.ip, self.port));

        try:
            yield from self._send_register_payload(websocket)

            if not self.client_key:
                raise PyLGTVPairException("Unable to pair")

            yield from websocket.send(json.dumps(msg))

            if msg['type'] == 'request':
                raw_response = yield from websocket.recv()
                self.last_response = json.loads(raw_response)

        finally:
            logger.debug('close command connection to %s', "ws://{}:{}".format(self.ip, self.port));
            yield from websocket.close()

Response: Send a command to the tv.


Instruction:
Can you implement a function in Python 3 that
builds and send a command.

Response:
def command(self, request_type, uri, payload):
        """Build and send a command."""
        self.command_count += 1

        if payload is None:
            payload = {}

        message = {
            'id': "{}_{}".format(type, self.command_count),
            'type': request_type,
            'uri': "ssap://{}".format(uri),
            'payload': payload,
        }

        self.last_response = None

        try:
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)
            loop.run_until_complete(asyncio.wait_for(self._command(message), self.timeout_connect, loop=loop))
        finally:
            loop.close()


Instruction:
Can you tell what is the following Python 3 function doing
def send_message(self, message, icon_path=None):
        icon_encoded_string = ''
        icon_extension = ''

        if icon_path is not None:
            icon_extension = os.path.splitext(icon_path)[1][1:]
            with open(icon_path, 'rb') as icon_file:
                icon_encoded_string = base64.b64encode(icon_file.read()).decode('ascii')

        self.request(EP_SHOW_MESSAGE, {
            'message': message,
            'iconData': icon_encoded_string,
            'iconExtension': icon_extension
        })

Response: Show a floating message.


Instruction:
Given the following Python 3 function, write the documentation
def get_current_app(self):
        self.request(EP_GET_CURRENT_APP_INFO)
        return None if self.last_response is None else self.last_response.get('payload').get('appId')

Response: Get the current app id.


Instruction:
Write a Python 3 script for
returning the current software status.

Response:
def get_software_info(self):
        """Return the current software status."""
        self.request(EP_GET_SOFTWARE_INFO)
        return {} if self.last_response is None else self.last_response.get('payload')


Instruction:
Given the following Python 3 function, write the documentation
def get_audio_status(self):
        self.request(EP_GET_AUDIO_STATUS)
        return {} if self.last_response is None else self.last_response.get('payload')

Response: Get the current audio status


Instruction:
Create a Python 3 function for
getting the current volume.

Response:
def get_volume(self):
        """Get the current volume."""
        self.request(EP_GET_VOLUME)
        return 0 if self.last_response is None else self.last_response.get('payload').get('volume')


Instruction:
Can you generate the documentation for the following Python 3 function
def set_volume(self, volume):
        volume = max(0, volume)
        self.request(EP_SET_VOLUME, {
            'volume': volume
        })

Response: Set the current volume.


Instruction:
Can you tell what is the following Python 3 function doing
def get_channels(self):
        self.request(EP_GET_TV_CHANNELS)
        return {} if self.last_response is None else self.last_response.get('payload').get('channelList')

Response: Get all tv channels.


Instruction:
Write a Python 3 function for
getting the current tv channel.

Response:
def get_current_channel(self):
        """Get the current tv channel."""
        self.request(EP_GET_CURRENT_CHANNEL)
        return {} if self.last_response is None else self.last_response.get('payload')


Instruction:
Can you write a function in Python 3 where it
gets the current channel info.

Response:
def get_channel_info(self):
        """Get the current channel info."""
        self.request(EP_GET_CHANNEL_INFO)
        return {} if self.last_response is None else self.last_response.get('payload')


Instruction:
Here you have a function in Python 3, explain what it does
def _load_plugins_itr(pgroup, safe=True):
    for res in pkg_resources.iter_entry_points(pgroup):
        try:
            yield res.load()
        except ImportError:
            if safe:
                continue
            raise

Response: Load all the ITRs in the given plugin group.


Instruction:
Explain what the following Python 3 code does
def select_by_key(items, sort_fn=sorted):
    itr = anyconfig.utils.concat(((k, v) for k in ks) for ks, v in items)
    return list((k, sort_fn(t[1] for t in g))
                for k, g
                in anyconfig.utils.groupby(itr, operator.itemgetter(0)))

Response: Select items from a list of key - value pairs.


Instruction:
Implement a Python 3 function for
returning a list of Processor objects grouped by type or extensions given item.

Response:
def list_by_x(prs, key):
    """
    :param key: Grouping key, "type" or "extensions"
    :return:
        A list of :class:`Processor` or its children classes grouped by
        given 'item', [(cid, [:class:`Processor`)]] by default
    """
    if key == "type":
        kfn = operator.methodcaller(key)
        res = sorted(((k, sort_by_prio(g)) for k, g
                      in anyconfig.utils.groupby(prs, kfn)),
                     key=operator.itemgetter(0))

    elif key == "extensions":
        res = select_by_key(((p.extensions(), p) for p in prs),
                            sort_fn=sort_by_prio)
    else:
        raise ValueError("Argument 'key' must be 'type' or "
                         "'extensions' but it was '%s'" % key)

    return res


Instruction:
How would you explain what the following Python 3 function does
def findall_with_pred(predicate, prs):
    return sorted((p for p in prs if predicate(p)),
                  key=operator.methodcaller("priority"), reverse=True)

Response: Find all the processor classes that satisfy a predicate.


Instruction:
Can you generate the documentation for the following Python 3 function
def maybe_processor(type_or_id, cls=anyconfig.models.processor.Processor):
    if isinstance(type_or_id, cls):
        return type_or_id

    if type(type_or_id) == type(cls) and issubclass(type_or_id, cls):
        return type_or_id()

    return None

Response: Returns the first instance of the specified processor class or None if no processor class is found.


Instruction:
Given the following Python 3 function, write the documentation
def find_by_type_or_id(type_or_id, prs):
    def pred(pcls):
        """Predicate"""
        return pcls.cid() == type_or_id or pcls.type() == type_or_id

    pclss = findall_with_pred(pred, prs)
    if not pclss:
        raise UnknownProcessorTypeError(type_or_id)

    return pclss

Response: Find all the processor classes of a given data type or ID.


Instruction:
Can you tell what is the following Python 3 function doing
def find_by_fileext(fileext, prs):
    def pred(pcls):
        """Predicate"""
        return fileext in pcls.extensions()

    pclss = findall_with_pred(pred, prs)
    if not pclss:
        raise UnknownFileTypeError("file extension={}".format(fileext))

    return pclss

Response: Returns a list of processor classes to processor files with given file extension.


Instruction:
Write a Python 3 function for
finding the list of processor classes that can process a given maybe file.

Response:
def find_by_maybe_file(obj, prs):
    """
    :param obj:
        a file path, file or file-like object, pathlib.Path object or an
        'anyconfig.globals.IOInfo' (namedtuple) object
    :param cps_by_ext: A list of processor classes
    :return: A list of processor classes to process given (maybe) file
    :raises: UnknownFileTypeError
    """
    if not isinstance(obj, IOInfo):
        obj = anyconfig.ioinfo.make(obj)

    return find_by_fileext(obj.extension, prs)


Instruction:
Write a Python 3 function for
finding all the processors in the hierarchy that are of the given type.

Response:
def findall(obj, prs, forced_type=None,
            cls=anyconfig.models.processor.Processor):
    """
    :param obj:
        a file path, file, file-like object, pathlib.Path object or an
        'anyconfig.globals.IOInfo` (namedtuple) object
    :param prs: A list of :class:`anyconfig.models.processor.Processor` classes
    :param forced_type:
        Forced processor type of the data to process or ID of the processor
        class or None
    :param cls: A class object to compare with 'forced_type' later

    :return: A list of instances of processor classes to process 'obj' data
    :raises: ValueError, UnknownProcessorTypeError, UnknownFileTypeError
    """
    if (obj is None or not obj) and forced_type is None:
        raise ValueError("The first argument 'obj' or the second argument "
                         "'forced_type' must be something other than "
                         "None or False.")

    if forced_type is None:
        pclss = find_by_maybe_file(obj, prs)  # :: [Processor], never []
    else:
        pclss = find_by_type_or_id(forced_type, prs)  # Do.

    return pclss


Instruction:
Can you generate the documentation for the following Python 3 function
def find(obj, prs, forced_type=None, cls=anyconfig.models.processor.Processor):
    if forced_type is not None:
        processor = maybe_processor(forced_type, cls=cls)
        if processor is not None:
            return processor

    pclss = findall(obj, prs, forced_type=forced_type, cls=cls)
    return pclss[0]()

Response: Find a specific object in the hierarchy of Processor classes.


Instruction:
How would you explain what the following Python 3 function does
def register(self, *pclss):
        for pcls in pclss:
            if pcls.cid() not in self._processors:
                self._processors[pcls.cid()] = pcls

Response: Registers a list of Processor classes.


Instruction:
Write a Python 3 script to
return a list of all Processor instances in this instance.

Response:
def list(self, sort=False):
        """
        :param sort: Result will be sorted if it's True
        :return: A list of :class:`Processor` or its children classes
        """
        prs = self._processors.values()
        if sort:
            return sorted(prs, key=operator.methodcaller("cid"))

        return prs


Instruction:
Write a Python 3 function for
returning a list of all the Processor s that have the same cid.

Response:
def list_by_cid(self):
        """
        :return:
            A list of :class:`Processor` or its children classes grouped by
            each cid, [(cid, [:class:`Processor`)]]
        """
        prs = self._processors
        return sorted(((cid, [prs[cid]]) for cid in sorted(prs.keys())),
                      key=operator.itemgetter(0))


Instruction:
Can you implement a function in Python 3 that
returns a list of all children classes grouped by item

Response:
def list_by_x(self, item=None):
        """
        :param item: Grouping key, one of "cid", "type" and "extensions"
        :return:
            A list of :class:`Processor` or its children classes grouped by
            given 'item', [(cid, [:class:`Processor`)]] by default
        """
        prs = self._processors

        if item is None or item == "cid":  # Default.
            res = [(cid, [prs[cid]]) for cid in sorted(prs.keys())]

        elif item in ("type", "extensions"):
            res = list_by_x(prs.values(), item)
        else:
            raise ValueError("keyword argument 'item' must be one of "
                             "None, 'cid', 'type' and 'extensions' "
                             "but it was '%s'" % item)
        return res


Instruction:
Implement a function in Python 3 to
return a list of x keys.

Response:
def list_x(self, key=None):
        """
        :param key: Which of key to return from "cid", "type", and "extention"
        :return: A list of x 'key'
        """
        if key in ("cid", "type"):
            return sorted(set(operator.methodcaller(key)(p)
                              for p in self._processors.values()))
        if key == "extension":
            return sorted(k for k, _v in self.list_by_x("extensions"))

        raise ValueError("keyword argument 'key' must be one of "
                         "None, 'cid', 'type' and 'extension' "
                         "but it was '%s'" % key)


Instruction:
Implement a Python 3 function for
finding all the processors in the object obj.

Response:
def findall(self, obj, forced_type=None,
                cls=anyconfig.models.processor.Processor):
        """
        :param obj:
            a file path, file, file-like object, pathlib.Path object or an
            'anyconfig.globals.IOInfo' (namedtuple) object
        :param forced_type: Forced processor type to find
        :param cls: A class object to compare with 'ptype'

        :return: A list of instances of processor classes to process 'obj'
        :raises: ValueError, UnknownProcessorTypeError, UnknownFileTypeError
        """
        return [p() for p in findall(obj, self.list(),
                                     forced_type=forced_type, cls=cls)]


Instruction:
How would you explain what the following Python 3 function does
def find(self, obj, forced_type=None,
             cls=anyconfig.models.processor.Processor):
        """
        :param obj:
            a file path, file, file-like object, pathlib.Path object or an
            'anyconfig.globals.IOInfo' (namedtuple) object
        :param forced_type: Forced processor type to find
        :param cls: A class object to compare with 'ptype'

        :return: an instance of processor class to process 'obj'
        :raises: ValueError, UnknownProcessorTypeError, UnknownFileTypeError
        """
        return find(obj, self.list(), forced_type=forced_type, cls=cls)

Response: Find a specific object in the database.


Instruction:
Can you generate the documentation for the following Python 3 function
def yml_fnc(fname, *args, **options):
    options = common.filter_from_options("ac_dict", options)

    if "ac_safe" in options:
        options["typ"] = "safe"  # Override it.

    iopts = anyconfig.utils.filter_options(_YAML_INIT_KWARGS, options)
    oopts = anyconfig.utils.filter_options(_YAML_INSTANCE_MEMBERS, options)

    yml = ryaml.YAML(**iopts)
    for attr, val in oopts.items():
        setattr(yml, attr, val)  # e.g. yml.preserve_quotes = True

    return getattr(yml, fname)(*args)

Response: Load or dump a single file into a new object.


Instruction:
Can you generate the documentation for the following Python 3 function
def yml_load(stream, container, **options):
    ret = yml_fnc("load", stream, **options)
    if ret is None:
        return container()

    return ret

Response: Load a single YAML file into a container.


Instruction:
How would you implement a function in Python 3 that
avoids bug in python 3.{2,3}. See http://bugs.python.org/issue9257. :param xmlfile: XML file or file-like object

Response:
def _iterparse(xmlfile):
    """
    Avoid bug in python 3.{2,3}. See http://bugs.python.org/issue9257.

    :param xmlfile: XML file or file-like object
    """
    try:
        return ET.iterparse(xmlfile, events=("start-ns", ))
    except TypeError:
        return ET.iterparse(xmlfile, events=(b"start-ns", ))


Instruction:
Make a summary of the following Python 3 code
def _tweak_ns(tag, **options):
    nspaces = options.get("nspaces", None)
    if nspaces is not None:
        matched = _ET_NS_RE.match(tag)
        if matched:
            (uri, tag) = matched.groups()
            prefix = nspaces.get(uri, False)
            if prefix:
                return "%s:%s" % (prefix, tag)

    return tag

Response: Tweaks the namespaces of the given XML tag.


Instruction:
Write a Python 3 function that can
check if all keys of each dict of dics are unique.

Response:
def _dicts_have_unique_keys(dics):
    """
    :param dics: [<dict or dict-like object>], must not be [] or [{...}]
    :return: True if all keys of each dict of 'dics' are unique

    # Enable the followings if to allow dics is [], [{...}]:
    # >>> all(_dicts_have_unique_keys([d]) for [d]
    # ...     in ({}, {'a': 0}, {'a': 1, 'b': 0}))
    # True
    # >>> _dicts_have_unique_keys([{}, {'a': 1}, {'b': 2, 'c': 0}])
    # True

    >>> _dicts_have_unique_keys([{}, {'a': 1}, {'a': 2}])
    False
    >>> _dicts_have_unique_keys([{}, {'a': 1}, {'b': 2}, {'b': 3, 'c': 0}])
    False
    >>> _dicts_have_unique_keys([{}, {}])
    True
    """
    key_itr = anyconfig.compat.from_iterable(d.keys() for d in dics)
    return len(set(key_itr)) == sum(len(d) for d in dics)


Instruction:
Make a summary of the following Python 3 code
def _merge_dicts(dics, container=dict):
    dic_itr = anyconfig.compat.from_iterable(d.items() for d in dics)
    return container(anyconfig.compat.OrderedDict(dic_itr))

Response: Merge a list of dicts into a single container.


Instruction:
Implement a Python 3 function for
parsing the text value.

Response:
def _parse_text(val, **options):
    """
    :return: Parsed value or value itself depends on 'ac_parse_value'
    """
    if val and options.get("ac_parse_value", False):
        return anyconfig.parser.parse_single(val)

    return val


Instruction:
How would you code a function in Python 3 to
process text in the XML element and update dic and subdic with the result.

Response:
def _process_elem_text(elem, dic, subdic, text="@text", **options):
    """
    :param elem: ET Element object which has elem.text
    :param dic: <container> (dict[-like]) object converted from elem
    :param subdic: Sub <container> object converted from elem
    :param options:
        Keyword options, see the description of :func:`elem_to_container` for
        more details.

    :return: None but updating elem.text, dic and subdic as side effects
    """
    elem.text = elem.text.strip()
    if elem.text:
        etext = _parse_text(elem.text, **options)
        if len(elem) or elem.attrib:
            subdic[text] = etext
        else:
            dic[elem.tag] = etext


Instruction:
Can you implement a function in Python 3 that
parses the attributes of the element and returns a container object.

Response:
def _parse_attrs(elem, container=dict, **options):
    """
    :param elem: ET Element object has attributes (elem.attrib)
    :param container: callble to make a container object
    :return: Parsed value or value itself depends on 'ac_parse_value'
    """
    adic = dict((_tweak_ns(a, **options), v) for a, v in elem.attrib.items())
    if options.get("ac_parse_value", False):
        return container(dict((k, anyconfig.parser.parse_single(v))
                              for k, v in adic.items()))

    return container(adic)


Instruction:
Can you tell what is the following Python 3 function doing
def _process_elem_attrs(elem, dic, subdic, container=dict, attrs="@attrs",
                        **options):
    """
    :param elem: ET Element object or None
    :param dic: <container> (dict[-like]) object converted from elem
    :param subdic: Sub <container> object converted from elem
    :param options:
        Keyword options, see the description of :func:`elem_to_container` for
        more details.

    :return: None but updating dic and subdic as side effects
    """
    adic = _parse_attrs(elem, container=container, **options)
    if not elem.text and not len(elem) and options.get("merge_attrs"):
        dic[elem.tag] = adic
    else:
        subdic[attrs] = adic

Response:
Process the attributes of the given ET Element and update dic and subdic as side effects
   .


Instruction:
Write a Python 3 function that can
process children elements in elem into dic and subdic.

Response:
def _process_children_elems(elem, dic, subdic, container=dict,
                            children="@children", **options):
    """
    :param elem: ET Element object or None
    :param dic: <container> (dict[-like]) object converted from elem
    :param subdic: Sub <container> object converted from elem
    :param container: callble to make a container object
    :param children: Tag for children nodes
    :param options:
        Keyword options, see the description of :func:`elem_to_container` for
        more details.

    :return: None but updating dic and subdic as side effects
    """
    cdics = [elem_to_container(c, container=container, **options)
             for c in elem]
    merge_attrs = options.get("merge_attrs", False)
    sdics = [container(elem.attrib) if merge_attrs else subdic] + cdics

    if _dicts_have_unique_keys(sdics):  # ex. <a><b>1</b><c>c</c></a>
        dic[elem.tag] = _merge_dicts(sdics, container)
    elif not subdic:  # There are no attrs nor text and only these children.
        dic[elem.tag] = cdics
    else:
        subdic[children] = cdics


Instruction:
Create a Python 3 function to
convert an XML ElementTree Element to a collection of container objects.

Response:
def elem_to_container(elem, container=dict, **options):
    """
    Convert XML ElementTree Element to a collection of container objects.

    Elements are transformed to a node under special tagged nodes, attrs, text
    and children, to store the type of these elements basically, however, in
    some special cases like the followings, these nodes are attached to the
    parent node directly for later convenience.

    - There is only text element
    - There are only children elements each has unique keys among all

    :param elem: ET Element object or None
    :param container: callble to make a container object
    :param options: Keyword options

        - nspaces: A namespaces dict, {uri: prefix} or None
        - attrs, text, children: Tags for special nodes to keep XML info
        - merge_attrs: Merge attributes and mix with children nodes, and the
          information of attributes are lost after its transformation.
    """
    dic = container()
    if elem is None:
        return dic

    elem.tag = _tweak_ns(elem.tag, **options)  # {ns}tag -> ns_prefix:tag
    subdic = dic[elem.tag] = container()
    options["container"] = container

    if elem.text:
        _process_elem_text(elem, dic, subdic, **options)

    if elem.attrib:
        _process_elem_attrs(elem, dic, subdic, **options)

    if len(elem):
        _process_children_elems(elem, dic, subdic, **options)
    elif not elem.text and not elem.attrib:  # ex. <tag/>.
        dic[elem.tag] = None

    return dic


Instruction:
Can you tell what is the following Python 3 function doing
def _complement_tag_options(options):
    if not all(nt in options for nt in _TAGS):
        tags = options.get("tags", {})
        for ntype, tag in _TAGS.items():
            options[ntype] = tags.get(ntype, tag)

    return options

Response: Returns a dict of options that can be used to complement the tags.


Instruction:
Can you implement a function in Python 3 that
converts XML ElementTree Root Element to a collection of container objects.

Response:
def root_to_container(root, container=dict, nspaces=None, **options):
    """
    Convert XML ElementTree Root Element to a collection of container objects.

    :param root: etree root object or None
    :param container: callble to make a container object
    :param nspaces: A namespaces dict, {uri: prefix} or None
    :param options: Keyword options,

        - tags: Dict of tags for special nodes to keep XML info, attributes,
          text and children nodes, e.g. {"attrs": "@attrs", "text": "#text"}
    """
    tree = container()
    if root is None:
        return tree

    if nspaces is not None:
        for uri, prefix in nspaces.items():
            root.attrib["xmlns:" + prefix if prefix else "xmlns"] = uri

    return elem_to_container(root, container=container, nspaces=nspaces,
                             **_complement_tag_options(options))


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

Response:
def _elem_set_attrs(obj, parent, to_str):
    """
    :param obj: Container instance gives attributes of XML Element
    :param parent: XML ElementTree parent node object
    :param to_str: Callable to convert value to string or None
    :param options: Keyword options, see :func:`container_to_etree`

    :return: None but parent will be modified
    """
    for attr, val in anyconfig.compat.iteritems(obj):
        parent.set(attr, to_str(val))


Instruction:
Can you generate the documentation for the following Python 3 function
def _get_or_update_parent(key, val, to_str, parent=None, **options):
    elem = ET.Element(key)

    vals = val if anyconfig.utils.is_iterable(val) else [val]
    for val_ in vals:
        container_to_etree(val_, parent=elem, to_str=to_str, **options)

    if parent is None:  # 'elem' is the top level etree.
        return elem

    parent.append(elem)
    return parent

Response: Helper function to update the parent node of the current child.


Instruction:
How would you explain what the following Python 3 function does
def container_to_etree(obj, parent=None, to_str=None, **options):
    if to_str is None:
        to_str = _to_str_fn(**options)

    if not anyconfig.utils.is_dict_like(obj):
        if parent is not None and obj:
            parent.text = to_str(obj)  # Parent is a leaf text node.
        return parent  # All attributes and text should be set already.

    options = _complement_tag_options(options)
    (attrs, text, children) = operator.itemgetter(*_ATC)(options)

    for key, val in anyconfig.compat.iteritems(obj):
        if key == attrs:
            _elem_set_attrs(val, parent, to_str)
        elif key == text:
            parent.text = to_str(val)
        elif key == children:
            for celem in _elem_from_descendants(val, **options):
                parent.append(celem)
        else:
            parent = _get_or_update_parent(key, val, to_str, parent=parent,
                                           **options)

    return ET.ElementTree(parent)

Response: Convert a dict - like object to XML ElementTree.


Instruction:
Make a summary of the following Python 3 code
def etree_write(tree, stream):
    try:
        tree.write(stream, encoding="utf-8", xml_declaration=True)
    except TypeError:
        tree.write(stream, encoding="unicode", xml_declaration=True)

Response: Writes XML ElementTree root content into stream.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def load_from_string(self, content, container, **opts):
        root = ET.fromstring(content)
        if anyconfig.compat.IS_PYTHON_3:
            stream = BytesIO(content)
        else:
            stream = anyconfig.compat.StringIO(content)
        nspaces = _namespaces_from_file(stream)
        return root_to_container(root, container=container,
                                 nspaces=nspaces, **opts)

Response: Load config from a string content.


Instruction:
Can you implement a function in Python 3 that
loads a config from a file path.

Response:
def load_from_path(self, filepath, container, **opts):
        """
        :param filepath: XML file path
        :param container: callble to make a container object
        :param opts: optional keyword parameters to be sanitized

        :return: Dict-like object holding config parameters
        """
        root = ET.parse(filepath).getroot()
        nspaces = _namespaces_from_file(filepath)
        return root_to_container(root, container=container,
                                 nspaces=nspaces, **opts)


Instruction:
Can you generate the documentation for the following Python 3 function
def load_from_stream(self, stream, container, **opts):
        root = ET.parse(stream).getroot()
        path = anyconfig.utils.get_path_from_stream(stream)
        nspaces = _namespaces_from_file(path)
        return root_to_container(root, container=container,
                                 nspaces=nspaces, **opts)

Response: Load a configuration from a stream.


Instruction:
How would you explain what the following Python 3 function does
def dump_to_string(self, cnf, **opts):
        tree = container_to_etree(cnf, **opts)
        buf = BytesIO()
        etree_write(tree, buf)
        return buf.getvalue()

Response: Dump the configuration to a string


Instruction:
Here you have a function in Python 3, explain what it does
def dump_to_stream(self, cnf, stream, **opts):
        tree = container_to_etree(cnf, **opts)
        etree_write(tree, stream)

Response: Dump the configuration data to a file - like object stream


Instruction:
Make a summary of the following Python 3 code
def filter_from_options(key, options):
    return anyconfig.utils.filter_options([k for k in options.keys()
                                           if k != key], options)

Response: Filter the options in which the item with key is not in the options dict.


Instruction:
Can you write a function in Python 3 where it
creates or update a loader with customization for the given container.

Response:
def _customized_loader(container, loader=Loader, mapping_tag=_MAPPING_TAG):
    """
    Create or update loader with making given callble 'container' to make
    mapping objects such as dict and OrderedDict, used to construct python
    object from yaml mapping node internally.

    :param container: Set container used internally
    """
    def construct_mapping(loader, node, deep=False):
        """Construct python object from yaml mapping node, based on
        :meth:`yaml.BaseConstructor.construct_mapping` in PyYAML (MIT).
        """
        loader.flatten_mapping(node)
        if not isinstance(node, yaml.MappingNode):
            msg = "expected a mapping node, but found %s" % node.id
            raise yaml.constructor.ConstructorError(None, None, msg,
                                                    node.start_mark)
        mapping = container()
        for key_node, value_node in node.value:
            key = loader.construct_object(key_node, deep=deep)
            try:
                hash(key)
            except TypeError as exc:
                eargs = ("while constructing a mapping",
                         node.start_mark,
                         "found unacceptable key (%s)" % exc,
                         key_node.start_mark)
                raise yaml.constructor.ConstructorError(*eargs)
            value = loader.construct_object(value_node, deep=deep)
            mapping[key] = value

        return mapping

    tag = "tag:yaml.org,2002:python/unicode"

    def construct_ustr(loader, node):
        """Unicode string constructor"""
        return loader.construct_scalar(node)

    try:
        loader.add_constructor(tag, construct_ustr)
    except NameError:
        pass

    if type(container) != dict:
        loader.add_constructor(mapping_tag, construct_mapping)
    return loader


Instruction:
How would you explain what the following Python 3 function does
def _customized_dumper(container, dumper=Dumper):
    def container_representer(dumper, data, mapping_tag=_MAPPING_TAG):
        """Container representer.
        """
        return dumper.represent_mapping(mapping_tag, data.items())

    def ustr_representer(dumper, data):
        """Unicode string representer"""
        tag = "tag:yaml.org,2002:python/unicode"
        return dumper.represent_scalar(tag, data)

    try:
        dumper.add_representer(unicode, ustr_representer)
    except NameError:
        pass

    if type(container) != dict:
        dumper.add_representer(container, container_representer)
    return dumper

Response: Customized dumper for the given container.


Instruction:
Given the following Python 3 function, write the documentation
def yml_fnc(fname, *args, **options):
    key = "ac_safe"
    fnc = getattr(yaml, r"safe_" + fname if options.get(key) else fname)
    return fnc(*args, **common.filter_from_options(key, options))

Response: Wrapper of yaml. safe_load yaml. load and yaml. dump.


Instruction:
Create a Python 3 function to
wrap of yaml. safe_load and yaml. load.

Response:
def yml_load(stream, container, yml_fnc=yml_fnc, **options):
    """An wrapper of yaml.safe_load and yaml.load.

    :param stream: a file or file-like object to load YAML content
    :param container: callble to make a container object

    :return: Mapping object
    """
    if options.get("ac_safe", False):
        options = {}  # yaml.safe_load does not process Loader opts.
    elif not options.get("Loader"):
        maybe_container = options.get("ac_dict", False)
        if maybe_container and callable(maybe_container):
            container = maybe_container

        options["Loader"] = _customized_loader(container)

    ret = yml_fnc("load", stream,
                  **common.filter_from_options("ac_dict", options))
    if ret is None:
        return container()

    return ret


Instruction:
Given the following Python 3 function, write the documentation
def yml_dump(data, stream, yml_fnc=yml_fnc, **options):
    _is_dict = anyconfig.utils.is_dict_like(data)

    if options.get("ac_safe", False):
        options = {}
    elif not options.get("Dumper", False) and _is_dict:
        # TODO: Any other way to get its constructor?
        maybe_container = options.get("ac_dict", type(data))
        options["Dumper"] = _customized_dumper(maybe_container)

    if _is_dict:
        # Type information and the order of items are lost on dump currently.
        data = anyconfig.dicts.convert_to(data, ac_dict=dict)
        options = common.filter_from_options("ac_dict", options)

    return yml_fnc("dump", data, stream, **options)

Response: A wrapper of yaml. safe_dump and yaml. dump.


Instruction:
How would you explain what the following Python 3 function does
def _split_path(path, seps=PATH_SEPS):
    if not path:
        return []

    for sep in seps:
        if sep in path:
            if path == sep:  # Special case, '/' or '.' only.
                return ['']
            return [x for x in path.split(sep) if x]

    return [path]

Response: Parse a path expression and return a list of items.


Instruction:
Can you generate the documentation for the following Python 3 function
def mk_nested_dic(path, val, seps=PATH_SEPS):
    ret = None
    for key in reversed(_split_path(path, seps)):
        ret = {key: val if ret is None else ret.copy()}

    return ret

Response: Make a nested dict iteratively.


Instruction:
How would you explain what the following Python 3 function does
def set_(dic, path, val, seps=PATH_SEPS):
    merge(dic, mk_nested_dic(path, val, seps), ac_merge=MS_DICTS)

Response: setter for nested dicts.


Instruction:
Create a Python 3 function to
update self with other with value val.

Response:
def _update_with_replace(self, other, key, val=None, **options):
    """
    Replace value of a mapping object 'self' with 'other' has if both have same
    keys on update. Otherwise, just keep the value of 'self'.

    :param self: mapping object to update with 'other'
    :param other: mapping object to update 'self'
    :param key: key of mapping object to update
    :param val: value to update self alternatively

    :return: None but 'self' will be updated
    """
    self[key] = other[key] if val is None else val


Instruction:
Make a summary of the following Python 3 code
def _merge_list(self, key, lst):
    self[key] += [x for x in lst if x not in self[key]]

Response: Merge list with another list


Instruction:
Can you create a Python 3 function that
updates the value of self with other.

Response:
def _update_with_merge(self, other, key, val=None, merge_lists=False,
                       **options):
    """
    Merge the value of self with other's recursively. Behavior of merge will be
    vary depends on types of original and new values.

    - mapping vs. mapping -> merge recursively
    - list vs. list -> vary depends on 'merge_lists'. see its description.

    :param other: a dict[-like] object or a list of (key, value) tuples
    :param key: key of mapping object to update
    :param val: value to update self[key]
    :param merge_lists:
        Merge not only dicts but also lists. For example,

        [1, 2, 3], [3, 4] ==> [1, 2, 3, 4]
        [1, 2, 2], [2, 4] ==> [1, 2, 2, 4]

    :return: None but 'self' will be updated
    """
    if val is None:
        val = other[key]

    if key in self:
        val0 = self[key]  # Original value
        if anyconfig.utils.is_dict_like(val0):  # It needs recursive updates.
            merge(self[key], val, merge_lists=merge_lists, **options)
        elif merge_lists and _are_list_like(val, val0):
            _merge_list(self, key, val)
        else:
            _merge_other(self, key, val)
    else:
        self[key] = val


Instruction:
Write a Python 3 function for
returning a function to update objects of the object.

Response:
def _get_update_fn(strategy):
    """
    Select dict-like class based on merge strategy and orderness of keys.

    :param merge: Specify strategy from MERGE_STRATEGIES of how to merge dicts.
    :return: Callable to update objects
    """
    if strategy is None:
        strategy = MS_DICTS
    try:
        return _MERGE_FNS[strategy]
    except KeyError:
        if callable(strategy):
            return strategy

        raise ValueError("Wrong merge strategy: %r" % strategy)


Instruction:
Can you implement a function in Python 3 that
updates self with other mapping object or an iterable of dicts or tuples based on merge strategy ac_merge.

Response:
def merge(self, other, ac_merge=MS_DICTS, **options):
    """
    Update (merge) a mapping object 'self' with other mapping object or an
    iterable yields (key, value) tuples based on merge strategy 'ac_merge'.

    :param others: a list of dict[-like] objects or (key, value) tuples
    :param another: optional keyword arguments to update self more
    :param ac_merge: Merge strategy to choose
    """
    _update_fn = _get_update_fn(ac_merge)

    if hasattr(other, "keys"):
        for key in other:
            _update_fn(self, other, key, **options)
    else:
        try:
            for key, val in other:
                _update_fn(self, other, key, val=val, **options)
        except (ValueError, TypeError) as exc:  # Re-raise w/ info.
            raise type(exc)("%s other=%r" % (str(exc), other))


Instruction:
Here you have a function in Python 3, explain what it does
def _make_recur(obj, make_fn, ac_ordered=False, ac_dict=None, **options):
    if ac_dict is None:
        ac_dict = anyconfig.compat.OrderedDict if ac_ordered else dict

    return ac_dict((k, None if v is None else make_fn(v, **options))
                   for k, v in obj.items())

Response: Make a recur object from a dict or OrderedDict.


Instruction:
Create a Python 3 function to
make an iterator over the object.

Response:
def _make_iter(obj, make_fn, **options):
    """
    :param obj: A mapping objects or other primitive object
    :param make_fn: Function to make/convert to
    :param options: Optional keyword arguments.

    :return: Mapping object
    """
    return type(obj)(make_fn(v, **options) for v in obj)


Instruction:
Here you have a function in Python 3, explain what it does
def convert_to(obj, ac_ordered=False, ac_dict=None, **options):
    options.update(ac_ordered=ac_ordered, ac_dict=ac_dict)
    if anyconfig.utils.is_dict_like(obj):
        return _make_recur(obj, convert_to, **options)
    if anyconfig.utils.is_list_like(obj):
        return _make_iter(obj, convert_to, **options)

    return obj

Response: Convert a mapping object to a dict or object of to_type recursively.


Instruction:
Write a Python 3 function that can
parse a line of Java properties file.

Response:
def _parseline(line):
    """
    Parse a line of Java properties file.

    :param line:
        A string to parse, must not start with ' ', '#' or '!' (comment)
    :return: A tuple of (key, value), both key and value may be None

    >>> _parseline(" ")
    (None, '')
    >>> _parseline("aaa:")
    ('aaa', '')
    >>> _parseline(" aaa:")
    ('aaa', '')
    >>> _parseline("aaa")
    ('aaa', '')
    >>> _parseline("url = http://localhost")
    ('url', 'http://localhost')
    >>> _parseline("calendar.japanese.type: LocalGregorianCalendar")
    ('calendar.japanese.type', 'LocalGregorianCalendar')
    """
    pair = re.split(r"(?:\s+)?(?:(?<!\\)[=:])", line.strip(), 1)
    key = pair[0].rstrip()

    if len(pair) < 2:
        LOGGER.warning("Invalid line found: %s", line)
        return (key or None, '')

    return (key, pair[1].strip())


Instruction:
Make a summary of the following Python 3 code
def load(stream, container=dict, comment_markers=_COMMENT_MARKERS):
    ret = container()
    prev = ""

    for line in stream.readlines():
        line = _pre_process_line(prev + line.strip().rstrip(),
                                 comment_markers)
        # I don't think later case may happen but just in case.
        if line is None or not line:
            continue

        prev = ""  # re-initialize for later use.

        if line.endswith("\\"):
            prev += line.rstrip(" \\")
            continue

        (key, val) = _parseline(line)
        if key is None:
            LOGGER.warning("Failed to parse the line: %s", line)
            continue

        ret[key] = unescape(val)

    return ret

Response: Load and parse Java properties file given as a fiel or file - like object stream.


Instruction:
Can you tell what is the following Python 3 function doing
def copen(filepath, flag='r', encoding=None):

    """
    FIXME: How to test this ?

    >>> c = copen(__file__)
    >>> c is not None
    True
    """
    if encoding is None:
        encoding = locale.getdefaultlocale()[1]

    return codecs.open(filepath, flag, encoding)

Response: Open a file with the specified encoding.


Instruction:
Write a Python 3 script for
making a list of template search paths from given template_file and paths.

Response:
def make_template_paths(template_file, paths=None):
    """
    Make up a list of template search paths from given 'template_file'
    (absolute or relative path to the template file) and/or 'paths' (a list of
    template search paths given by user).

    NOTE: User-given 'paths' will take higher priority over a dir of
    template_file.

    :param template_file: Absolute or relative path to the template file
    :param paths: A list of template search paths
    :return: List of template paths ([str])

    >>> make_template_paths("/path/to/a/template")
    ['/path/to/a']
    >>> make_template_paths("/path/to/a/template", ["/tmp"])
    ['/tmp', '/path/to/a']
    >>> os.chdir("/tmp")
    >>> make_template_paths("./path/to/a/template")
    ['/tmp/path/to/a']
    >>> make_template_paths("./path/to/a/template", ["/tmp"])
    ['/tmp', '/tmp/path/to/a']
    """
    tmpldir = os.path.abspath(os.path.dirname(template_file))
    return [tmpldir] if paths is None else paths + [tmpldir]


Instruction:
Given the following Python 3 function, write the documentation
def render_s(tmpl_s, ctx=None, paths=None, filters=None):
    if paths is None:
        paths = [os.curdir]

    env = tmpl_env(paths)

    if env is None:
        return tmpl_s

    if filters is not None:
        env.filters.update(filters)

    if ctx is None:
        ctx = {}

    return tmpl_env(paths).from_string(tmpl_s).render(**ctx)

Response: Compile and render given template string tmpl_s with context dict ctx.


Instruction:
Can you create a Python 3 function that
renders a single template file into a string.

Response:
def render_impl(template_file, ctx=None, paths=None, filters=None):
    """
    :param template_file: Absolute or relative path to the template file
    :param ctx: Context dict needed to instantiate templates
    :param filters: Custom filters to add into template engine
    :return: Compiled result (str)
    """
    env = tmpl_env(make_template_paths(template_file, paths))

    if env is None:
        return copen(template_file).read()

    if filters is not None:
        env.filters.update(filters)

    if ctx is None:
        ctx = {}

    return env.get_template(os.path.basename(template_file)).render(**ctx)


Instruction:
Explain what the following Python 3 code does
def render(filepath, ctx=None, paths=None, ask=False, filters=None):
    try:
        return render_impl(filepath, ctx, paths, filters)
    except TemplateNotFound as mtmpl:
        if not ask:
            raise

        usr_tmpl = anyconfig.compat.raw_input(os.linesep + ""
                                              "*** Missing template "
                                              "'%s'. Please enter absolute "
                                              "or relative path starting from "
                                              "'.' to the template file: " %
                                              mtmpl)
        usr_tmpl = os.path.normpath(usr_tmpl.strip())
        paths = make_template_paths(usr_tmpl, paths)

        return render_impl(usr_tmpl, ctx, paths, filters)

Response: Compile and render a template file and return the result as a string.


Instruction:
Write a Python 3 script to
compile and render a template and return the result as a string.

Response:
def try_render(filepath=None, content=None, **options):
    """
    Compile and render template and return the result as a string.

    :param filepath: Absolute or relative path to the template file
    :param content: Template content (str)
    :param options: Keyword options passed to :func:`render` defined above.
    :return: Compiled result (str) or None
    """
    if filepath is None and content is None:
        raise ValueError("Either 'path' or 'content' must be some value!")

    tmpl_s = filepath or content[:10] + " ..."
    LOGGER.debug("Compiling: %s", tmpl_s)
    try:
        if content is None:
            render_opts = anyconfig.utils.filter_options(RENDER_OPTS, options)
            return render(filepath, **render_opts)
        render_s_opts = anyconfig.utils.filter_options(RENDER_S_OPTS, options)
        return render_s(content, **render_s_opts)
    except Exception as exc:
        LOGGER.warning("Failed to compile '%s'. It may not be a template.%s"
                       "exc=%r", tmpl_s, os.linesep, exc)
        return None


Instruction:
How would you code a function in Python 3 to
parse a string into a list of strings.

Response:
def _parse(val_s, sep=_SEP):
    """
    FIXME: May be too naive implementation.

    :param val_s: A string represents some value to parse
    :param sep: separator between values

    >>> _parse(r'"foo string"')
    'foo string'
    >>> _parse("a, b, c")
    ['a', 'b', 'c']
    >>> _parse("aaa")
    'aaa'
    """
    if (val_s.startswith('"') and val_s.endswith('"')) or \
            (val_s.startswith("'") and val_s.endswith("'")):
        return val_s[1:-1]
    if sep in val_s:
        return [P.parse(x) for x in P.parse_list(val_s)]

    return P.parse(val_s)


Instruction:
Can you tell what is the following Python 3 function doing
def _to_s(val, sep=", "):
    if anyconfig.utils.is_iterable(val):
        return sep.join(str(x) for x in val)

    return str(val)

Response: Convert any to string.


Instruction:
Write a Python 3 function that can
yield the items in the order they appear in the order they appear in the order they appear in the order they appear.

Response:
def _parsed_items(items, sep=_SEP, **options):
    """
    :param items: List of pairs, [(key, value)], or generator yields pairs
    :param sep: Seprator string
    :return: Generator to yield (key, value) pair of 'dic'
    """
    parse = _parse if options.get("ac_parse_value") else anyconfig.utils.noop
    for key, val in items:
        yield (key, parse(val, sep))


Instruction:
Here you have a function in Python 3, explain what it does
def _make_parser(**kwargs):
    # Optional arguements for configparser.SafeConfigParser{,readfp}
    kwargs_0 = filter_options(("defaults", "dict_type", "allow_no_value"),
                              kwargs)
    kwargs_1 = filter_options(("filename", ), kwargs)

    try:
        parser = configparser.SafeConfigParser(**kwargs_0)
    except TypeError:
        # .. note::
        #    It seems ConfigParser.*ConfigParser in python 2.6 does not support
        #    'allow_no_value' option parameter, and TypeError will be thrown.
        kwargs_0 = filter_options(("defaults", "dict_type"), kwargs)
        parser = configparser.SafeConfigParser(**kwargs_0)

    return (kwargs_1, parser)

Response: Create a ConfigParser. SafeConfigParser object for the current node.


Instruction:
Can you write a function in Python 3 where it
loads configuration from file or file - like object.

Response:
def _load(stream, container, sep=_SEP, dkey=DEFAULTSECT, **kwargs):
    """
    :param stream: File or file-like object provides ini-style conf
    :param container: any callable to make container
    :param sep: Seprator string
    :param dkey: Default section name

    :return: Dict or dict-like object represents config values
    """
    (kwargs_1, psr) = _make_parser(**kwargs)
    if IS_PYTHON_3:
        psr.read_file(stream, **kwargs_1)
    else:
        psr.readfp(stream, **kwargs_1)

    cnf = container()
    kwargs["sep"] = sep

    defaults = psr.defaults()
    if defaults:
        cnf[dkey] = container(_parsed_items(iteritems(defaults), **kwargs))

    for sect in psr.sections():
        cnf[sect] = container(_parsed_items(psr.items(sect), **kwargs))

    return cnf


Instruction:
Can you tell what is the following Python 3 function doing
def _dumps_itr(cnf, dkey=DEFAULTSECT):
    for sect, params in iteritems(cnf):
        yield "[%s]" % sect

        for key, val in iteritems(params):
            if sect != dkey and dkey in cnf and cnf[dkey].get(key) == val:
                continue  # It should be in [DEFAULT] section.

            yield "%s = %s" % (key, _to_s(val))

        yield ''

Response: Dump the configuration data in a list of dicts.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _dumps(cnf, **kwargs):
    return os.linesep.join(l for l in _dumps_itr(cnf))

Response: Dump the configuration data to INI format


Instruction:
Make a summary of the following Python 3 code
def query(data, **options):
    expression = options.get("ac_query", None)
    if expression is None or not expression:
        return data

    try:
        pexp = jmespath.compile(expression)
        return pexp.search(data)
    except ValueError as exc:  # jmespath.exceptions.*Error inherit from it.
        LOGGER.warning("Failed to compile or search: exp=%s, exc=%r",
                       expression, exc)
    except (NameError, AttributeError):
        LOGGER.warning("Filter module (jmespath) is not available. "
                       "Do nothing.")

    return data

Response: Query data with given JMESPath expression.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_file_extension(file_path):
    _ext = os.path.splitext(file_path)[-1]
    if _ext:
        return _ext[1:] if _ext.startswith('.') else _ext

    return ""

Response: Get the file extension


Instruction:
How would you implement a function in Python 3 that
checks if an object is iterable.

Response:
def is_iterable(obj):
    """
    >>> is_iterable([])
    True
    >>> is_iterable(())
    True
    >>> is_iterable([x for x in range(10)])
    True
    >>> is_iterable((1, 2, 3))
    True
    >>> g = (x for x in range(10))
    >>> is_iterable(g)
    True
    >>> is_iterable("abc")
    False
    >>> is_iterable(0)
    False
    >>> is_iterable({})
    False
    """
    return isinstance(obj, (list, tuple, types.GeneratorType)) or \
        (not isinstance(obj, (int, str, dict)) and
         bool(getattr(obj, "next", False)))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def is_ioinfo(obj, keys=None):
    if keys is None:
        keys = anyconfig.globals.IOI_KEYS

    if isinstance(obj, tuple) and getattr(obj, "_asdict", False):
        return all(k in obj._asdict() for k in keys)

    return False

Response: Check if given object is a namedtuple object.


Instruction:
Explain what the following Python 3 code does
def is_path_like_object(obj, marker='*'):
    return ((is_path(obj) and marker not in obj) or
            (is_path_obj(obj) and marker not in obj.as_posix()) or
            is_file_stream(obj) or is_ioinfo(obj))

Response: Check if the object is a path string a pathlib. Path object a file - like namedtuple object?


Instruction:
How would you implement a function in Python 3 that
is given object maybe_paths consist of path or path pattern strings?

Response:
def is_paths(maybe_paths, marker='*'):
    """
    Does given object 'maybe_paths' consist of path or path pattern strings?
    """
    return ((is_path(maybe_paths) and marker in maybe_paths) or  # Path str
            (is_path_obj(maybe_paths) and marker in maybe_paths.as_posix()) or
            (is_iterable(maybe_paths) and
             all(is_path(p) or is_ioinfo(p) for p in maybe_paths)))


Instruction:
How would you explain what the following Python 3 function does
def get_path_from_stream(strm):
    if not is_file_stream(strm):
        raise ValueError("Given object does not look a file/file-like "
                         "object: %r" % strm)

    path = getattr(strm, "name", None)
    if path is not None:
        try:
            return normpath(path)
        except (TypeError, ValueError):
            pass

    return None

Response: Try to get file path from given file or file - like object strm.


Instruction:
Given the following Python 3 function, write the documentation
def _try_to_get_extension(obj):
    if is_path(obj):
        path = obj

    elif is_path_obj(obj):
        return obj.suffix[1:]

    elif is_file_stream(obj):
        try:
            path = get_path_from_stream(obj)
        except ValueError:
            return None

    elif is_ioinfo(obj):
        path = obj.path

    else:
        return None

    if path:
        return get_file_extension(path)

    return None

Response: Try to get file extension from given path or file - like object.


Instruction:
How would you explain what the following Python 3 function does
def are_same_file_types(objs):
    if not objs:
        return False

    ext = _try_to_get_extension(objs[0])
    if ext is None:
        return False

    return all(_try_to_get_extension(p) == ext for p in objs[1:])

Response: Tests if a list of file objects are given as a list of file paths or file - like objects.


Instruction:
How would you explain what the following Python 3 function does
def _expand_paths_itr(paths, marker='*'):
    for path in paths:
        if is_path(path):
            if marker in path:  # glob path pattern
                for ppath in sglob(path):
                    yield ppath
            else:
                yield path  # a simple file path
        elif is_path_obj(path):
            if marker in path.as_posix():
                for ppath in sglob(path.as_posix()):
                    yield normpath(ppath)
            else:
                yield normpath(path.as_posix())
        elif is_ioinfo(path):
            yield path.path
        else:  # A file or file-like object
            yield path

Response: Iterator version of expand_paths.


Instruction:
Explain what the following Python 3 code does
def expand_paths(paths, marker='*'):
    if is_path(paths) and marker in paths:
        return sglob(paths)

    if is_path_obj(paths) and marker in paths.as_posix():
        # TBD: Is it better to return [p :: pathlib.Path] instead?
        return [normpath(p) for p in sglob(paths.as_posix())]

    return list(_expand_paths_itr(paths, marker=marker))

Response: Expand a list of paths to a list of paths.


Instruction:
Write a Python 3 script for
checking if the object is a list - like object.

Response:
def is_list_like(obj):
    """
    >>> is_list_like([])
    True
    >>> is_list_like(())
    True
    >>> is_list_like([x for x in range(10)])
    True
    >>> is_list_like((1, 2, 3))
    True
    >>> g = (x for x in range(10))
    >>> is_list_like(g)
    True
    >>> is_list_like("abc")
    False
    >>> is_list_like(0)
    False
    >>> is_list_like({})
    False
    """
    return isinstance(obj, _LIST_LIKE_TYPES) and \
        not (isinstance(obj, anyconfig.compat.STR_TYPES) or is_dict_like(obj))


Instruction:
Create a Python 3 function to
filter options with given keys.

Response:
def filter_options(keys, options):
    """
    Filter 'options' with given 'keys'.

    :param keys: key names of optional keyword arguments
    :param options: optional keyword arguments to filter with 'keys'

    >>> filter_options(("aaa", ), dict(aaa=1, bbb=2))
    {'aaa': 1}
    >>> filter_options(("aaa", ), dict(bbb=2))
    {}
    """
    return dict((k, options[k]) for k in keys if k in options)


Instruction:
Can you generate the documentation for the following Python 3 function
def _try_validate(cnf, schema, **options):
    valid = True
    if schema:
        (valid, msg) = validate(cnf, schema, **options)
        if msg:
            LOGGER.warning(msg)

    if valid:
        return cnf

    return None

Response: Try to validate cnf with schema.


Instruction:
Create a Python 3 function for
loading the configuration file if it is a template file and try to validate it AAR

Response:
def _maybe_schema(**options):
    """
    :param options: Optional keyword arguments such as

        - ac_template: Assume configuration file may be a template file and try
          to compile it AAR if True
        - ac_context: Mapping object presents context to instantiate template
        - ac_schema: JSON schema file path to validate configuration files

    :return: Mapping object or None means some errors
    """
    ac_schema = options.get("ac_schema", None)
    if ac_schema is not None:
        # Try to detect the appropriate parser to load the schema data as it
        # may be different from the original config file's format, perhaps.
        options["ac_parser"] = None
        options["ac_schema"] = None  # Avoid infinite loop.
        LOGGER.info("Loading schema: %s", ac_schema)
        return load(ac_schema, **options)

    return None


Instruction:
Can you generate the documentation for the following Python 3 function
def open(path, mode=None, ac_parser=None, **options):
    psr = find(path, forced_type=ac_parser)

    if mode is not None and mode.startswith('w'):
        return psr.wopen(path, **options)

    return psr.ropen(path, **options)

Response: Open given configuration file with appropriate open flag.


Instruction:
Here you have a function in Python 3, explain what it does
def _single_load(input_, ac_parser=None, ac_template=False,
                 ac_context=None, **options):
    """
    :param input_:
        File path or file or file-like object or pathlib.Path object represents
        the file or a namedtuple 'anyconfig.globals.IOInfo' object represents
        some input to load some data from
    :param ac_parser: Forced parser type or parser object itself
    :param ac_template:
        Assume configuration file may be a template file and try to compile it
        AAR if True
    :param ac_context: A dict presents context to instantiate template
    :param options:
        Optional keyword arguments :func:`single_load` supports except for
        ac_schema and ac_query

    :return: Mapping object
    :raises: ValueError, UnknownProcessorTypeError, UnknownFileTypeError
    """
    ioi = anyconfig.ioinfo.make(input_)
    psr = find(ioi, forced_type=ac_parser)
    filepath = ioi.path

    # .. note::
    #    This will be kept for backward compatibility until 'ignore_missing'
    #    option is deprecated and removed completely.
    if "ignore_missing" in options:
        warnings.warn("keyword option 'ignore_missing' is deprecated, use "
                      "'ac_ignore_missing' instead", DeprecationWarning)
        options["ac_ignore_missing"] = options["ignore_missing"]

    LOGGER.info("Loading: %s", filepath)
    if ac_template and filepath is not None:
        content = anyconfig.template.try_render(filepath=filepath,
                                                ctx=ac_context, **options)
        if content is not None:
            return psr.loads(content, **options)

    return psr.load(ioi, **options)

Response: Load a single item from a file.


Instruction:
How would you explain what the following Python 3 function does
def single_load(input_, ac_parser=None, ac_template=False,
                ac_context=None, **options):
    r"""
    Load single configuration file.

    .. note::

       :func:`load` is a preferable alternative and this API should be used
       only if there is a need to emphasize given input 'input\_' is single
       one.

    :param input\_:
        File path or file or file-like object or pathlib.Path object represents
        the file or a namedtuple 'anyconfig.globals.IOInfo' object represents
        some input to load some data from
    :param ac_parser: Forced parser type or parser object itself
    :param ac_template:
        Assume configuration file may be a template file and try to compile it
        AAR if True
    :param ac_context: A dict presents context to instantiate template
    :param options: Optional keyword arguments such as:

        - Options common in :func:`single_load`, :func:`multi_load`,
          :func:`load` and :func:`loads`:

          - ac_dict: callable (function or class) to make mapping objects from
            loaded data if the selected backend can customize that such as JSON
            which supports that with 'object_pairs_hook' option, or None. If
            this option was not given or None, dict or
            :class:`collections.OrderedDict` will be used to make result as
            mapping object depends on if ac_ordered (see below) is True and
            selected backend can keep the order of items loaded. See also
            :meth:`_container_factory` of
            :class:`anyconfig.backend.base.Parser` for more implementation
            details.

          - ac_ordered: True if you want to keep resuls ordered. Please note
            that order of items may be lost depends on the selected backend.

          - ac_schema: JSON schema file path to validate given config file
          - ac_query: JMESPath expression to query data

        - Common backend options:

          - ac_ignore_missing:
            Ignore and just return empty result if given file 'input\_' does
            not exist actually.

        - Backend specific options such as {"indent": 2} for JSON backend

    :return: Mapping object
    :raises: ValueError, UnknownProcessorTypeError, UnknownFileTypeError
    """
    cnf = _single_load(input_, ac_parser=ac_parser, ac_template=ac_template,
                       ac_context=ac_context, **options)
    schema = _maybe_schema(ac_template=ac_template, ac_context=ac_context,
                           **options)
    cnf = _try_validate(cnf, schema, **options)
    return anyconfig.query.query(cnf, **options)

Response: r Loads a single configuration file.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def load(path_specs, ac_parser=None, ac_dict=None, ac_template=False,
         ac_context=None, **options):
    r"""
    Load single or multiple config files or multiple config files specified in
    given paths pattern or pathlib.Path object represents config files or a
    namedtuple 'anyconfig.globals.IOInfo' object represents some inputs.

    :param path_specs:
        A list of file path or a glob pattern such as r'/a/b/\*.json'to list of
        files, file or file-like object or pathlib.Path object represents the
        file or a namedtuple 'anyconfig.globals.IOInfo' object represents some
        inputs to load some data from.
    :param ac_parser: Forced parser type or parser object
    :param ac_dict:
        callable (function or class) to make mapping object will be returned as
        a result or None. If not given or ac_dict is None, default mapping
        object used to store resutls is dict or
        :class:`collections.OrderedDict` if ac_ordered is True and selected
        backend can keep the order of items in mapping objects.

    :param ac_template: Assume configuration file may be a template file and
        try to compile it AAR if True
    :param ac_context: A dict presents context to instantiate template
    :param options:
        Optional keyword arguments. See also the description of 'options' in
        :func:`single_load` and :func:`multi_load`

    :return: Mapping object or any query result might be primitive objects
    :raises: ValueError, UnknownProcessorTypeError, UnknownFileTypeError
    """
    marker = options.setdefault("ac_marker", options.get("marker", '*'))

    if anyconfig.utils.is_path_like_object(path_specs, marker):
        return single_load(path_specs, ac_parser=ac_parser, ac_dict=ac_dict,
                           ac_template=ac_template, ac_context=ac_context,
                           **options)

    if not anyconfig.utils.is_paths(path_specs, marker):
        raise ValueError("Possible invalid input %r" % path_specs)

    return multi_load(path_specs, ac_parser=ac_parser, ac_dict=ac_dict,
                      ac_template=ac_template, ac_context=ac_context,
                      **options)

Response: r Loads a single or multiple config files or multiple config files specified in path_specs.


Instruction:
How would you code a function in Python 3 to
load a configuration file into a mapping object.

Response:
def loads(content, ac_parser=None, ac_dict=None, ac_template=False,
          ac_context=None, **options):
    """
    :param content: Configuration file's content (a string)
    :param ac_parser: Forced parser type or ID or parser object
    :param ac_dict:
        callable (function or class) to make mapping object will be returned as
        a result or None. If not given or ac_dict is None, default mapping
        object used to store resutls is dict or
        :class:`collections.OrderedDict` if ac_ordered is True and selected
        backend can keep the order of items in mapping objects.
    :param ac_template: Assume configuration file may be a template file and
        try to compile it AAR if True
    :param ac_context: Context dict to instantiate template
    :param options:
        Optional keyword arguments. See also the description of 'options' in
        :func:`single_load` function.

    :return: Mapping object or any query result might be primitive objects
    :raises: ValueError, UnknownProcessorTypeError
    """
    if ac_parser is None:
        LOGGER.warning("ac_parser was not given but it's must to find correct "
                       "parser to load configurations from string.")
        return None

    psr = find(None, forced_type=ac_parser)
    schema = None
    ac_schema = options.get("ac_schema", None)
    if ac_schema is not None:
        options["ac_schema"] = None
        schema = loads(ac_schema, ac_parser=psr, ac_dict=ac_dict,
                       ac_template=ac_template, ac_context=ac_context,
                       **options)

    if ac_template:
        compiled = anyconfig.template.try_render(content=content,
                                                 ctx=ac_context, **options)
        if compiled is not None:
            content = compiled

    cnf = psr.loads(content, ac_dict=ac_dict, **options)
    cnf = _try_validate(cnf, schema, **options)
    return anyconfig.query.query(cnf, **options)


Instruction:
Implement a function in Python 3 to
dump data to out.

Response:
def dump(data, out, ac_parser=None, **options):
    """
    Save 'data' to 'out'.

    :param data: A mapping object may have configurations data to dump
    :param out:
        An output file path, a file, a file-like object, :class:`pathlib.Path`
        object represents the file or a namedtuple 'anyconfig.globals.IOInfo'
        object represents output to dump some data to.
    :param ac_parser: Forced parser type or parser object
    :param options:
        Backend specific optional arguments, e.g. {"indent": 2} for JSON
        loader/dumper backend

    :raises: ValueError, UnknownProcessorTypeError, UnknownFileTypeError
    """
    ioi = anyconfig.ioinfo.make(out)
    psr = find(ioi, forced_type=ac_parser)
    LOGGER.info("Dumping: %s", ioi.path)
    psr.dump(data, ioi, **options)


Instruction:
Can you write a function in Python 3 where it
dumps data in forced type format.

Response:
def dumps(data, ac_parser=None, **options):
    """
    Return string representation of 'data' in forced type format.

    :param data: Config data object to dump
    :param ac_parser: Forced parser type or ID or parser object
    :param options: see :func:`dump`

    :return: Backend-specific string representation for the given data
    :raises: ValueError, UnknownProcessorTypeError
    """
    psr = find(None, forced_type=ac_parser)
    return psr.dumps(data, **options)


Instruction:
Can you write a function in Python 3 where it
queries the JMES API for the given data and JMESPath expression.

Response:
def query(data, expression, **options):
    """
    API just wraps :func:`anyconfig.query.query`.

    :param data: Config data object to query
    :param expression: JMESPath expression to query data
    :param options: Ignored in current implementation

    :return: Query result object may be a primitive (int, str, etc.) or dict.
    """
    return anyconfig.query.query(data, ac_query=expression)


Instruction:
How would you code a function in Python 3 to
return a logger with a NullHandler.

Response:
def getLogger(name="anyconfig"):
    """
    See: "Configuring Logging for a Library" in python standard logging howto,
    e.g. https://docs.python.org/2/howto/logging.html#library-config.
    """
    logger = logging.getLogger(name)
    logger.addHandler(anyconfig.compat.NullHandler())

    return logger


Instruction:
Can you generate the documentation for the following Python 3 function
def parse_single(str_):
    if str_ is None:
        return ''

    str_ = str_.strip()

    if not str_:
        return ''

    if BOOL_PATTERN.match(str_) is not None:
        return bool(str_)

    if INT_PATTERN.match(str_) is not None:
        return int(str_)

    if STR_PATTERN.match(str_) is not None:
        return str_[1:-1]

    return str_

Response: A simple parser to parse a single string into a sequence of Int Bool String


Instruction:
Can you generate the documentation for the following Python 3 function
def parse(str_, lsep=",", avsep=":", vssep=",", avssep=";"):
    if avsep in str_:
        return parse_attrlist(str_, avsep, vssep, avssep)
    if lsep in str_:
        return parse_list(str_, lsep)

    return parse_single(str_)

Response: Generic parser for the XML file.


Instruction:
How would you implement a function in Python 3 that
converts logging level to log level.

Response:
def to_log_level(level):
    """
    :param level: Logging level in int = 0 .. 2

    >>> to_log_level(0) == logging.WARN
    True
    >>> to_log_level(5)  # doctest: +IGNORE_EXCEPTION_DETAIL, +ELLIPSIS
    Traceback (most recent call last):
        ...
    ValueError: wrong log level passed: 5
    >>>
    """
    if level < 0 or level >= 3:
        raise ValueError("wrong log level passed: " + str(level))

    return [logging.WARN, logging.INFO, logging.DEBUG][level]


Instruction:
Can you tell what is the following Python 3 function doing
def make_parser(defaults=None):
    if defaults is None:
        defaults = DEFAULTS

    ctypes = API.list_types()
    ctypes_s = ", ".join(ctypes)
    type_help = "Select type of %s config files from " + \
        ctypes_s + " [Automatically detected by file ext]"

    mts = API.MERGE_STRATEGIES
    mts_s = ", ".join(mts)
    mt_help = "Select strategy to merge multiple configs from " + \
        mts_s + " [%(merge)s]" % defaults

    parser = argparse.ArgumentParser(usage=USAGE)
    parser.set_defaults(**defaults)

    parser.add_argument("inputs", type=str, nargs='*', help="Input files")
    parser.add_argument("--version", action="version",
                        version="%%(prog)s %s" % anyconfig.globals.VERSION)

    lpog = parser.add_argument_group("List specific options")
    lpog.add_argument("-L", "--list", action="store_true",
                      help="List supported config types")

    spog = parser.add_argument_group("Schema specific options")
    spog.add_argument("--validate", action="store_true",
                      help="Only validate input files and do not output. "
                           "You must specify schema file with -S/--schema "
                           "option.")
    spog.add_argument("--gen-schema", action="store_true",
                      help="Generate JSON schema for givne config file[s] "
                           "and output it instead of (merged) configuration.")

    gspog = parser.add_argument_group("Query/Get/set options")
    gspog.add_argument("-Q", "--query", help=_QUERY_HELP)
    gspog.add_argument("--get", help=_GET_HELP)
    gspog.add_argument("--set", help=_SET_HELP)

    parser.add_argument("-o", "--output", help="Output file path")
    parser.add_argument("-I", "--itype", choices=ctypes, metavar="ITYPE",
                        help=(type_help % "Input"))
    parser.add_argument("-O", "--otype", choices=ctypes, metavar="OTYPE",
                        help=(type_help % "Output"))
    parser.add_argument("-M", "--merge", choices=mts, metavar="MERGE",
                        help=mt_help)
    parser.add_argument("-A", "--args", help="Argument configs to override")
    parser.add_argument("--atype", choices=ctypes, metavar="ATYPE",
                        help=_ATYPE_HELP_FMT % ctypes_s)

    cpog = parser.add_argument_group("Common options")
    cpog.add_argument("-x", "--ignore-missing", action="store_true",
                      help="Ignore missing input files")
    cpog.add_argument("-T", "--template", action="store_true",
                      help="Enable template config support")
    cpog.add_argument("-E", "--env", action="store_true",
                      help="Load configuration defaults from "
                           "environment values")
    cpog.add_argument("-S", "--schema", help="Specify Schema file[s] path")
    cpog.add_argument("-e", "--extra-opts",
                      help="Extra options given to the API call, "
                           "--extra-options indent:2 (specify the "
                           "indent for pretty-printing of JSON outputs) "
                           "for example")
    cpog.add_argument("-v", "--verbose", action="count", dest="loglevel",
                      help="Verbose mode; -v or -vv (more verbose)")
    return parser

Response: Create an argument parser for the base command line interface.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _exit_with_output(content, exit_code=0):
    (sys.stdout if exit_code == 0 else sys.stderr).write(content + os.linesep)
    sys.exit(exit_code)

Response: Exit the program with printing out messages.


Instruction:
Write a Python 3 function for
showing list of info of parsers available

Response:
def _show_psrs():
    """Show list of info of parsers available
    """
    sep = os.linesep

    types = "Supported types: " + ", ".join(API.list_types())
    cids = "IDs: " + ", ".join(c for c, _ps in API.list_by_cid())

    x_vs_ps = ["  %s: %s" % (x, ", ".join(p.cid() for p in ps))
               for x, ps in API.list_by_extension()]
    exts = "File extensions:" + sep + sep.join(x_vs_ps)

    _exit_with_output(sep.join([types, exts, cids]))


Instruction:
How would you explain what the following Python 3 function does
def _parse_args(argv):
    parser = make_parser()
    args = parser.parse_args(argv)
    LOGGER.setLevel(to_log_level(args.loglevel))

    if args.inputs:
        if '-' in args.inputs:
            args.inputs = sys.stdin
    else:
        if args.list:
            _show_psrs()
        elif args.env:
            cnf = os.environ.copy()
            _output_result(cnf, args.output, args.otype or "json", None, None)
            sys.exit(0)
        else:
            parser.print_usage()
            sys.exit(1)

    if args.validate and args.schema is None:
        _exit_with_output("--validate option requires --scheme option", 1)

    return args

Response: Parse command line arguments and show supported config format types or usage.


Instruction:
Here you have a function in Python 3, explain what it does
def _do_get(cnf, get_path):
    (cnf, err) = API.get(cnf, get_path)
    if cnf is None:  # Failed to get the result.
        _exit_with_output("Failed to get result: err=%s" % err, 1)

    return cnf

Response: Get a single object from the given configuration object.


Instruction:
Explain what the following Python 3 code does
def _output_type_by_input_path(inpaths, itype, fmsg):
    msg = ("Specify inpath and/or outpath type[s] with -I/--itype "
           "or -O/--otype option explicitly")
    if itype is None:
        try:
            otype = API.find(inpaths[0]).type()
        except API.UnknownFileTypeError:
            _exit_with_output((fmsg % inpaths[0]) + msg, 1)
        except (ValueError, IndexError):
            _exit_with_output(msg, 1)
    else:
        otype = itype

    return otype

Response: Return the type of the object given the input file paths and the input type.


Instruction:
How would you implement a function in Python 3 that
tries to dump a single object to outpath.

Response:
def _try_dump(cnf, outpath, otype, fmsg, extra_opts=None):
    """
    :param cnf: Configuration object to print out
    :param outpath: Output file path or None
    :param otype: Output type or None
    :param fmsg: message if it cannot detect otype by 'inpath'
    :param extra_opts: Map object will be given to API.dump as extra options
    """
    if extra_opts is None:
        extra_opts = {}
    try:
        API.dump(cnf, outpath, otype, **extra_opts)
    except API.UnknownFileTypeError:
        _exit_with_output(fmsg % outpath, 1)
    except API.UnknownProcessorTypeError:
        _exit_with_output("Invalid output type '%s'" % otype, 1)


Instruction:
Given the following Python 3 function, write the documentation
def _output_result(cnf, outpath, otype, inpaths, itype,
                   extra_opts=None):
    """
    :param cnf: Configuration object to print out
    :param outpath: Output file path or None
    :param otype: Output type or None
    :param inpaths: List of input file paths
    :param itype: Input type or None
    :param extra_opts: Map object will be given to API.dump as extra options
    """
    fmsg = ("Uknown file type and cannot detect appropriate backend "
            "from its extension, '%s'")

    if not anyconfig.utils.is_dict_like(cnf):
        _exit_with_output(str(cnf))  # Print primitive types as it is.

    if not outpath or outpath == "-":
        outpath = sys.stdout
        if otype is None:
            otype = _output_type_by_input_path(inpaths, itype, fmsg)

    _try_dump(cnf, outpath, otype, fmsg, extra_opts=extra_opts)

Response: Output the result of the dump of a single object.


Instruction:
Can you generate the documentation for the following Python 3 function
def _load_diff(args, extra_opts):
    try:
        diff = API.load(args.inputs, args.itype,
                        ac_ignore_missing=args.ignore_missing,
                        ac_merge=args.merge,
                        ac_template=args.template,
                        ac_schema=args.schema,
                        **extra_opts)
    except API.UnknownProcessorTypeError:
        _exit_with_output("Wrong input type '%s'" % args.itype, 1)
    except API.UnknownFileTypeError:
        _exit_with_output("No appropriate backend was found for given file "
                          "'%s'" % args.itype, 1)
    _exit_if_load_failure(diff,
                          "Failed to load: args=%s" % ", ".join(args.inputs))

    return diff

Response: Load the object from the given file and return the object that was loaded.


Instruction:
Implement a function in Python 3 to
filter the object cnf according to the given arguments

Response:
def _do_filter(cnf, args):
    """
    :param cnf: Mapping object represents configuration data
    :param args: :class:`argparse.Namespace` object
    :return: 'cnf' may be updated
    """
    if args.query:
        cnf = API.query(cnf, args.query)
    elif args.get:
        cnf = _do_get(cnf, args.get)
    elif args.set:
        (key, val) = args.set.split('=')
        API.set_(cnf, key, anyconfig.parser.parse(val))

    return cnf


Instruction:
Can you generate the documentation for the following Python 3 function
def _validate_all(data, schema):
    vldtr = jsonschema.Draft4Validator(schema)  # :raises: SchemaError, ...
    errors = list(vldtr.iter_errors(data))

    return (not errors, [err.message for err in errors])

Response: Validate all data according to schema.


Instruction:
Write a Python 3 script for
validating data with given schema.

Response:
def _validate(data, schema, ac_schema_safe=True, **options):
    """
    See the descritpion of :func:`validate` for more details of parameters and
    return value.

    Validate target object 'data' with given schema object.
    """
    try:
        jsonschema.validate(data, schema, **options)

    except (jsonschema.ValidationError, jsonschema.SchemaError,
            Exception) as exc:
        if ac_schema_safe:
            return (False, str(exc))  # Validation was failed.
        raise

    return (True, '')


Instruction:
Create a Python 3 function for
validating target object with given schema object loaded from JSON schema.

Response:
def validate(data, schema, ac_schema_safe=True, ac_schema_errors=False,
             **options):
    """
    Validate target object with given schema object, loaded from JSON schema.

    See also: https://python-jsonschema.readthedocs.org/en/latest/validate/

    :parae data: Target object (a dict or a dict-like object) to validate
    :param schema: Schema object (a dict or a dict-like object)
        instantiated from schema JSON file or schema JSON string
    :param options: Other keyword options such as:

        - ac_schema_safe: Exception (jsonschema.ValidationError or
          jsonschema.SchemaError or others) will be thrown during validation
          process due to any validation or related errors. However, these will
          be catched by default, and will be re-raised if 'ac_safe' is False.

        - ac_schema_errors: Lazily yield each of the validation errors and
          returns all of them if validation fails.

    :return: (True if validation succeeded else False, error message[s])
    """
    if not JSONSCHEMA_IS_AVAIL:
        return (True, _NA_MSG)

    options = anyconfig.utils.filter_options(("cls", ), options)
    if ac_schema_errors:
        return _validate_all(data, schema, **options)

    return _validate(data, schema, ac_schema_safe, **options)


Instruction:
Can you create a Python 3 function that
generates a JSON schema object with type annotation added for given object.

Response:
def array_to_schema(arr, **options):
    """
    Generate a JSON schema object with type annotation added for given object.

    :param arr: Array of mapping objects like dicts
    :param options: Other keyword options such as:

        - ac_schema_strict: True if more strict (precise) schema is needed
        - ac_schema_typemap: Type to JSON schema type mappings

    :return: Another mapping objects represents JSON schema of items
    """
    (typemap, strict) = _process_options(**options)

    arr = list(arr)
    scm = dict(type=typemap[list],
               items=gen_schema(arr[0] if arr else "str", **options))
    if strict:
        nitems = len(arr)
        scm["minItems"] = nitems
        scm["uniqueItems"] = len(set(arr)) == nitems

    return scm


Instruction:
Make a summary of the following Python 3 code
def object_to_schema(obj, **options):
    (typemap, strict) = _process_options(**options)

    props = dict((k, gen_schema(v, **options)) for k, v in obj.items())
    scm = dict(type=typemap[dict], properties=props)
    if strict:
        scm["required"] = sorted(props.keys())

    return scm

Response: Generate a node represents JSON schema object for given object.


Instruction:
Write a Python 3 function that can
generate a node represents JSON schema object for given configuration data object.

Response:
def gen_schema(data, **options):
    """
    Generate a node represents JSON schema object with type annotation added
    for given object node.

    :param data: Configuration data object (dict[-like] or namedtuple)
    :param options: Other keyword options such as:

        - ac_schema_strict: True if more strict (precise) schema is needed
        - ac_schema_typemap: Type to JSON schema type mappings

    :return: A dict represents JSON schema of this node
    """
    if data is None:
        return dict(type="null")

    _type = type(data)

    if _type in _SIMPLE_TYPES:
        typemap = options.get("ac_schema_typemap", _SIMPLETYPE_MAP)
        scm = dict(type=typemap[_type])

    elif anyconfig.utils.is_dict_like(data):
        scm = object_to_schema(data, **options)

    elif anyconfig.utils.is_list_like(data):
        scm = array_to_schema(data, **options)

    return scm


Instruction:
Can you generate the documentation for the following Python 3 function
def ensure_outdir_exists(filepath):
    outdir = os.path.dirname(filepath)

    if outdir and not os.path.exists(outdir):
        LOGGER.debug("Making output dir: %s", outdir)
        os.makedirs(outdir)

Response: Make dir to dump filepath if that dir does not exist.


Instruction:
How would you explain what the following Python 3 function does
def to_method(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        """Wrapper function.
        """
        return func(*args[1:], **kwargs)

    return wrapper

Response: Converts a function to a method that will be called with the first argument as the first argument.


Instruction:
Make a summary of the following Python 3 code
def load_with_fn(load_fn, content_or_strm, container, allow_primitives=False,
                 **options):
    """
    Load data from given string or stream 'content_or_strm'.

    :param load_fn: Callable to load data
    :param content_or_strm: data content or stream provides it
    :param container: callble to make a container object
    :param allow_primitives:
        True if the parser.load* may return objects of primitive data types
        other than mapping types such like JSON parser
    :param options: keyword options passed to 'load_fn'

    :return: container object holding data
    """
    ret = load_fn(content_or_strm, **options)
    if anyconfig.utils.is_dict_like(ret):
        return container() if (ret is None or not ret) else container(ret)

    return ret if allow_primitives else container(ret)

Response: Load data from given string or stream.


Instruction:
Make a summary of the following Python 3 code
def dump_with_fn(dump_fn, data, stream, **options):
    if stream is None:
        return dump_fn(data, **options)

    return dump_fn(data, stream, **options)

Response: Dump data to a string if stream is None or dump data to a file or file - like object stream.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _container_factory(self, **options):
        ac_dict = options.get("ac_dict", False)
        _dicts = [x for x in (options.get(o) for o in self.dict_options())
                  if x]

        if self.dict_options() and ac_dict and callable(ac_dict):
            return ac_dict  # Higher priority than ac_ordered.
        if _dicts and callable(_dicts[0]):
            return _dicts[0]
        if self.ordered() and options.get("ac_ordered", False):
            return anyconfig.compat.OrderedDict

        return dict

Response: Returns a class or function that returns the appropriate container class.


Instruction:
Explain what the following Python 3 code does
def _load_options(self, container, **options):
        # Force set dict option if available in backend. For example,
        # options["object_hook"] will be OrderedDict if 'container' was
        # OrderedDict in JSON backend.
        for opt in self.dict_options():
            options.setdefault(opt, container)

        return anyconfig.utils.filter_options(self._load_opts, options)

Response: Select backend specific loading options.


Instruction:
Make a summary of the following Python 3 code
def load_from_string(self, content, container, **kwargs):
        _not_implemented(self, content, container, **kwargs)

Response: Load config from given string content.


Instruction:
Write a Python 3 function for
loading config from given filepath.

Response:
def load_from_path(self, filepath, container, **kwargs):
        """
        Load config from given file path 'filepath`.

        :param filepath: Config file path
        :param container: callble to make a container object later
        :param kwargs: optional keyword parameters to be sanitized :: dict

        :return: Dict-like object holding config parameters
        """
        _not_implemented(self, filepath, container, **kwargs)


Instruction:
Can you generate the documentation for the following Python 3 function
def load_from_stream(self, stream, container, **kwargs):
        _not_implemented(self, stream, container, **kwargs)

Response: Load config from given stream.


Instruction:
Write a Python 3 function for
loading config from given string content.

Response:
def loads(self, content, **options):
        """
        Load config from given string 'content' after some checks.

        :param content:  Config file content
        :param options:
            options will be passed to backend specific loading functions.
            please note that options have to be sanitized w/
            :func:`anyconfig.utils.filter_options` later to filter out options
            not in _load_opts.

        :return: dict or dict-like object holding configurations
        """
        container = self._container_factory(**options)
        if not content or content is None:
            return container()

        options = self._load_options(container, **options)
        return self.load_from_string(content, container, **options)


Instruction:
How would you implement a function in Python 3 that
loads config from a file path or file - like object which contains a namedtuple object containing the configuration and the options.

Response:
def load(self, ioi, ac_ignore_missing=False, **options):
        """
        Load config from a file path or a file / file-like object which 'ioi'
        refering after some checks.

        :param ioi:
            'anyconfig.globals.IOInfo' namedtuple object provides various info
            of input object to load data from

        :param ac_ignore_missing:
            Ignore and just return empty result if given `ioi` object does not
            exist in actual.
        :param options:
            options will be passed to backend specific loading functions.
            please note that options have to be sanitized w/
            :func:`anyconfig.utils.filter_options` later to filter out options
            not in _load_opts.

        :return: dict or dict-like object holding configurations
        """
        container = self._container_factory(**options)
        options = self._load_options(container, **options)

        if not ioi:
            return container()

        if anyconfig.utils.is_stream_ioinfo(ioi):
            cnf = self.load_from_stream(ioi.src, container, **options)
        else:
            if ac_ignore_missing and not os.path.exists(ioi.path):
                return container()

            cnf = self.load_from_path(ioi.path, container, **options)

        return cnf