Best Python code snippet using lemoncheesecake
reorganize.py
Source:reorganize.py  
...349                child_nodes.append(pot_child_node[1])350            elif isLeafWithSubtree(pot_child_node):351                child_nodes.append(pot_child_node[1])352    return child_nodes353def get_hierarchy_links(tree, hierarchy_links=None):354    if hierarchy_links is None:355        hierarchy_links = []356    for node in tree:357        if isLeaf(node):358            pass359        elif isLeafWithSubtree(node):360            child_leafs = getChildNodesThatAreLeafs(node)361            for index in range(0, len(child_leafs)-1):362                hierarchy_links.append(363                    Link(364                        source=child_leafs[index],365                        description=set.DIRECT_SISTER_ZETTEL,366                        target=child_leafs[index + 1]367                    ))368            if len(child_leafs) > 0:369                hierarchy_links.append(370                    Link(371                        source=node[1],372                        description=set.DIRECT_DAUGHTER_ZETTEL,373                        target=child_leafs[0]374                    )375                )376            get_hierarchy_links(node[2], hierarchy_links)377        elif isStructureNode(node):378            child_leafs = getChildNodesThatAreLeafs(node)379            for index in range(0, len(child_leafs)-1):380                hierarchy_links.append(381                    Link(382                        source=child_leafs[index],383                        description=set.DIRECT_SISTER_ZETTEL,384                        target=child_leafs[index + 1]385                    ))386            get_hierarchy_links(node[1], hierarchy_links)387    return hierarchy_links388def reorganize_filenames(tree, path=None, final=None):389    if final is None:390        final = []391    if path is None:392        path = ''393    for node in tree:394        if isinstance(node, list):395            if isLeaf(node):396                final.append(397                    [path + node[0], node[1]])398            elif isLeafWithSubtree(node):399                final.append([path + node[0], node[1]])400                reorganize_filenames(...graph_viz_builder.py
Source:graph_viz_builder.py  
...102            # add cluster to graph103            self._graph.add_subgraph(c)104105106    def add_hierarchy_links(self) -> None:107        for src,dst,_ in self.hierarchy_links:108            edge = EdgeLayout(src=src, dst=dst, color="gray", style="solid")109            self._graph.add_edge(edge)110111112    def add_import_links(self) -> None:113        for src,dst in self.internal_import_links:114            edge = EdgeLayout(src=src, 115                              dst=dst, 116                              color="black", 117                              style="dashed", 118                              constraint=(not ARGS.ignore_imports)119                              )120            self._graph.add_edge(edge)121122123def build_dot_layout(network: nx.DiGraph, 124                     project_path: Path, 125                     dir_as: str = "node",126                     show_interface: bool = False,127                     show_imports: bool = False128                     ) -> pydot.Dot:129    builder = GraphVizBuilder(network=network, 130                              project_path=project_path,131                              rankdir=ARGS.rankdir132                              )133    builder.add_file_nodes(with_interface=show_interface)134135    if dir_as == "node":136        builder.add_dir_nodes()137        builder.add_hierarchy_links()138    elif dir_as == "cluster":139        builder.add_dir_clusters()140    elif dir_as == "empty":141        pass142    else:143        raise ValueError(f"dir_as cannot take value: {dir_as}")144    145    if show_imports:146        builder.add_import_links()147
...hierarchy_to_tree.py
Source:hierarchy_to_tree.py  
1import json 2import ast3global noParent #variable holding the string used when a node does not have a parent4noParent = "None"5def get_children(node, hierarchy): #http  s://stackoverflow.com/questions/18025130/recursively-build-hierarchical-json-tree6    return [x[1] for x in hierarchy if x[0] == node] #finds the child of the node7def get_nodes(node, hierarchy): #https://stackoverflow.com/questions/18025130/recursively-build-hierarchical-json-tree8    d = {}9    d['value'] = node #gets the name of the node10    parent = [x[0] for x in hierarchy if x[1] == node] #gets the parent value of the node from the links list11    if parent == []: d['parent'] = noParent #if the node has no parent, "None" is used instead12    else: d['parent'] = parent[0] #otherwise, write the parent of the node13    children = get_children(node, hierarchy)14    if children:15        d['children'] = [get_nodes(child, hierarchy) for child in children] #finds the children of the current node16    else:17        d['children'] = [] #if the node has no children, it is shown as an empty list18    return d19def json_tree(hierarchy): #https://stackoverflow.com/questions/18025130/recursively-build-hierarchical-json-tree20    # print("Hierarchy :\n", hierarchy)21    parents, children = zip(*hierarchy) #gets all parent and child values22    # print("Parents :\n", parents)23    # print("Children :\n", children)24    root_node = {x for x in parents if x not in children} #the root node is the one without a parent25    # print("Root node :\n", root_node)26    hierarchy.append(('value', root_node))27    tree = get_nodes(list(root_node)[0], hierarchy) #creates the tree28    # print("Tree :\n", tree)29    return json.dumps(tree, indent=4) #writes the tree to the json file30def generate_trees(linksList):31    jsonList = []32    for file in linksList:33        with open(file, "r") as f: #reads the file containing the parent-child links34            hierarchy = ast.literal_eval(f.read())35        gettingName = file.split("/") #gets the name of the hierarchy36        fileName = gettingName[-1][:-4]37        hierarchyFile = fileName + ".json" #turns the hierarchy into a JSON file38        jsonList.append(hierarchyFile)39        jsonFile = "data/" + hierarchyFile40        with open(jsonFile, "w") as jfile: #opens the JSON file and writes the hierarchy in the format of a JSON tree41            tree = json_tree(hierarchy)42            # print("JSON tree :\n", tree)43            jfile.write(tree)44    return jsonList45if __name__ == '__main__':46    HIERARCHY_LINKS = ["./data/patient/state.txt"]47    # Generate the JSON trees from the hierarchy links48    json_trees_list = generate_trees(HIERARCHY_LINKS) ...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!!
