Best Python code snippet using avocado_python
varianter_yaml_to_mux.py
Source:varianter_yaml_to_mux.py  
...54        return55    if path[-1] != "/":56        path += "/"57    return path58def _handle_control_tag(path, node, value):59    """60    Handling of most YAML control tags (all but "!using")61    :param path: path on the YAML62    :type path: str63    :param node: the node in which to handle control tags64    :type node: instance of :class:`avocado.core.tree.TreeNode` or similar65    :param value: the value of the node66    """67    if value[0].code == YAML_INCLUDE:68        # Include file69        ypath = value[1]70        if not os.path.isabs(ypath):71            ypath = os.path.join(os.path.dirname(path), ypath)72        if not os.path.exists(ypath):73            raise ValueError(74                f"File '{ypath}' included from '{path}' " f"does not exist."75            )76        node.merge(_create_from_yaml("/:" + ypath))77    elif value[0].code in (YAML_REMOVE_NODE, YAML_REMOVE_VALUE):78        value[0].value = value[1]  # set the name79        node.ctrl.append(value[0])  # add "blue pill" of death80    elif value[0].code == YAML_MUX:81        node.multiplex = True82    elif value[0].code == YAML_FILTER_ONLY:83        new_value = _normalize_path(value[1])84        if new_value:85            node.filters[0].append(new_value)86    elif value[0].code == YAML_FILTER_OUT:87        new_value = _normalize_path(value[1])88        if new_value:89            node.filters[1].append(new_value)90def _handle_control_tag_using(path, name, using, value):91    """92    Handling of the "!using" YAML control tag93    :param path: path on the YAML94    :type path: str95    :param name: name to be applied in the "!using" tag96    :type name: str97    :param using: whether using is already being used98    :type using: bool99    :param value: the value of the node100    """101    if using:102        raise ValueError(f"!using can be used only once " f"per node! ({path}:{name})")103    using = value104    if using[0] == "/":105        using = using[1:]106    if using[-1] == "/":107        using = using[:-1]108    return using109def _apply_using(name, using, node):110    """111    Create the structure defined by "!using" and return the new root112    :param name: the tag name to have the "!using" applied to113    :type name: str114    :param using: the new location to put the tag into115    :type using: bool116    :param node: the node in which to handle control tags117    :type node: instance of :class:`avocado.core.tree.TreeNode` or similar118    """119    if name != "":120        for name in using.split("/")[::-1]:121            node = mux.MuxTreeNode(name, children=[node])122    else:123        using = using.split("/")[::-1]124        node.name = using.pop()125        while True:126            if not using:127                break128            name = using.pop()  # 'using' is list pylint: disable=E1101129            node = mux.MuxTreeNode(name, children=[node])130        node = mux.MuxTreeNode("", children=[node])131    return node132def _node_content_from_node(path, node, values, using):133    """Processes node values into the current node content"""134    for value in values:135        if isinstance(value, mux.MuxTreeNode):136            node.add_child(value)137        elif isinstance(value[0], mux.Control):138            if value[0].code == YAML_USING:139                using = _handle_control_tag_using(path, node.name, using, value[1])140            else:141                _handle_control_tag(path, node, value)142        elif isinstance(value[1], collections.OrderedDict):143            child = _tree_node_from_values(144                path, astring.to_text(value[0]), value[1], using145            )146            node.add_child(child)147        else:148            node.value[value[0]] = value[1]149    return using150def _node_content_from_dict(path, node, values, using):151    """Processes dict values into the current node content"""152    for key, value in values.items():153        if isinstance(key, mux.Control):154            if key.code == YAML_USING:155                using = _handle_control_tag_using(path, node.name, using, value)156            else:157                _handle_control_tag(path, node, [key, value])158        elif isinstance(value, collections.OrderedDict) or value is None:159            node.add_child(_tree_node_from_values(path, key, value, using))160        elif value == "null":161            node.value[key] = None162        else:163            node.value[key] = value164    return using165def _tree_node_from_values(path, name, values, using):166    """Create `name` node and add values"""167    # Initialize the node168    node = mux.MuxTreeNode(astring.to_text(name))169    if not values:170        return node171    using = ""...__init__.py
Source:__init__.py  
...70        return71    if path[-1] != '/':72        path += '/'73    return path74def _handle_control_tag(path, cls_node, node, value):75    """76    Handling of most YAML control tags (all but "!using")77    :param path: path on the YAML78    :type path: str79    :param cls_node: the class of the node80    :type cls_node: :class:`avocado.core.tree.TreeNode` or similar81    :param node: the node in which to handle control tags82    :type node: instance of :class:`avocado.core.tree.TreeNode` or similar83    :param value: the value of the node84    """85    if value[0].code == YAML_INCLUDE:86        # Include file87        ypath = value[1]88        if not os.path.isabs(ypath):89            ypath = os.path.join(os.path.dirname(path), ypath)90        if not os.path.exists(ypath):91            raise ValueError("File '%s' included from '%s' does not "92                             "exist." % (ypath, path))93        node.merge(_create_from_yaml('/:' + ypath, cls_node))94    elif value[0].code in (YAML_REMOVE_NODE, YAML_REMOVE_VALUE):95        value[0].value = value[1]   # set the name96        node.ctrl.append(value[0])    # add "blue pill" of death97    elif value[0].code == YAML_MUX:98        node.multiplex = True99    elif value[0].code == YAML_FILTER_ONLY:100        new_value = _normalize_path(value[1])101        if new_value:102            node.filters[0].append(new_value)103    elif value[0].code == YAML_FILTER_OUT:104        new_value = _normalize_path(value[1])105        if new_value:106            node.filters[1].append(new_value)107def _handle_control_tag_using(path, name, using, value):108    """109    Handling of the "!using" YAML control tag110    :param path: path on the YAML111    :type path: str112    :param name: name to be applied in the "!using" tag113    :type name: str114    :param using: whether using is already being used115    :type using: bool116    :param value: the value of the node117    """118    if using:119        raise ValueError("!using can be used only once per "120                         "node! (%s:%s)" % (path, name))121    using = value122    if using[0] == '/':123        using = using[1:]124    if using[-1] == '/':125        using = using[:-1]126    return using127def _apply_using(name, cls_node, using, node):128    """129    Create the structure defined by "!using" and return the new root130    :param name: the tag name to have the "!using" applied to131    :type name: str132    :param cls_node: the class of the node133    :type cls_node: :class:`avocado.core.tree.TreeNode` or similar134    :param using: the new location to put the tag into135    :type using: bool136    :param node: the node in which to handle control tags137    :type node: instance of :class:`avocado.core.tree.TreeNode` or similar138    """139    if name is not '':140        for name in using.split('/')[::-1]:141            node = cls_node(name, children=[node])142    else:143        using = using.split('/')[::-1]144        node.name = using.pop()145        while True:146            if not using:147                break148            name = using.pop()  # 'using' is list pylint: disable=E1101149            node = cls_node(name, children=[node])150        node = cls_node('', children=[node])151    return node152def _create_from_yaml(path, cls_node=mux.MuxTreeNode):153    """Create tree structure from yaml stream"""154    def tree_node_from_values(name, values):155        """Create `name` node and add values"""156        def node_content_from_node(node, values, using):157            """Processes node values into the current node content"""158            for value in values:159                if isinstance(value, cls_node):160                    node.add_child(value)161                elif isinstance(value[0], mux.Control):162                    if value[0].code == YAML_USING:163                        using = _handle_control_tag_using(path, name, using, value[1])164                    else:165                        _handle_control_tag(path, cls_node, node, value)166                elif isinstance(value[1], collections.OrderedDict):167                    child = tree_node_from_values(astring.to_text(value[0]),168                                                  value[1])169                    node.add_child(child)170                else:171                    node.value[value[0]] = value[1]172            return using173        def node_content_from_dict(node, values, using):174            """Processes dict values into the current node content"""175            for key, value in iteritems(values):176                if isinstance(key, mux.Control):177                    if key.code == YAML_USING:178                        using = _handle_control_tag_using(path, name, using, value)179                    else:180                        _handle_control_tag(path, cls_node, node, [key, value])181                elif (isinstance(value, collections.OrderedDict) or182                      value is None):183                    node.add_child(tree_node_from_values(key, value))184                else:185                    node.value[key] = value186            return using187        # Initialize the node188        node = cls_node(astring.to_text(name))189        if not values:190            return node191        using = ''192        # Fill the node content from parsed values193        if isinstance(values, dict):194            using = node_content_from_dict(node, values, using)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
