Ejemplo n.º 1
0
    def remove_subtree(self, nid):
        """
        Return a subtree deleted from this tree. If nid is None, an
        empty tree is returned.
        For the original tree, this method is similar to
        `remove_node(self,nid)`, because given node and its children
        are removed from the original tree in both methods.
        For the returned value and performance, these two methods are
        different:

            `remove_node` returns the number of deleted nodes;
            `remove_subtree` returns a subtree of deleted nodes;

        You are always suggested to use `remove_node` if your only to
        delete nodes from a tree, as the other one need memory
        allocation to store the new tree.
        """
        st = Tree()
        if nid is None:
            return st

        if not self.contains(nid):
            raise NodeIDAbsentError("Node '%s' is not in the tree" % nid)
        st.root = nid

        parent = self[nid].bpointer
        self[nid].bpointer = None  # reset root parent for the new tree
        removed = []
        for id in self.expand_tree(nid):
            removed.append(id)
        for id in removed:
            st._nodes.update({id: self._nodes.pop(id)})
        # Update its parent info
        self.__update_fpointer(parent, nid, Node.DELETE)
        return st
class Conversation:
    def __init__(self, tweet):
        self.root_tweet = tweet
        self.conversation_tree = Tree()
        self.conversation_tree.create_node(tweet, tweet)
        self.depth = int()
        self.tweets_id = list()
        self.tweets_id.append(tweet)
        self.width = int()

    def add_replay(self, tweet, parent_tweet):
        self.conversation_tree.create_node(tweet, tweet, parent=parent_tweet)
        self.tweets_id.append(tweet)

    def set_depth(self):
        self.depth = self.conversation_tree.depth() + 1

    def find_depth(self):
        return self.depth

    def get_tweets_id(self):
        return self.tweets_id

    def set_width(self):
        self.width = len(self.tweets_id)

    def find_width(self):
        return self.width

    def get_conversation_tree(self):
        return self.conversation_tree
Ejemplo n.º 3
0
 def test_paste_tree(self):
     new_tree = Tree()
     new_tree.create_node("Jill", "jill")
     new_tree.create_node("Mark", "mark", parent="jill")
     self.tree.paste("jane", new_tree)
     self.assertEqual("jill" in self.tree.is_branch("jane"), True)
     self.tree.remove_node("jill")
Ejemplo n.º 4
0
def main():
  try:
    conf = open(args.config, 'r')
    tempConf = yaml.load_all(conf)

    for line in tempConf:
      list_path = line["ListPath"]
      write_missed = line["WriteMissed"]

    pack_list_file = open(list_path, "r+")
    pack_list = json.load(pack_list_file)

    checked = check(pack_list, write_missed)

    tree = Tree()
    tree.create_node(cur_time, "root")
    generate_tree(checked, tree, "root")

    print "\n"
    tree.show()
    print "\n"

  except KeyboardInterrupt:
    print '\nThe process was interrupted by the user'
    raise SystemExit
	def test_02_get_hierarchy_for_module_returns_single_node_when_nothing_depend_on_module(self, mock_client):
		"""
		Test that get_hierarchy_for_module returns a single node tree structure if no dependent modules are found
		:param mock_client: A mocked out version of erppeek.Client
		:return:
		"""
		# Mock Up
		mock_dp = DependencyGraph
		orig_mod_search = mock_dp.module_search
		orig_dep_search = mock_dp.dependency_search
		orig_client_search = mock_client.search
		mock_dp.module_search = MagicMock(return_value=[666])
		mock_dp.dependency_search = MagicMock(return_value=[])

		mock_dg = mock_dp('valid_module')
		test_hierarchy = Tree()
		test_hierarchy.create_node('valid_module', 'valid_module')
		self.assertEqual(mock_dg.hierarchy.to_json(), test_hierarchy.to_json(), 'get_hierarchy_for_module did not return [] when finding no dependent modules')

		# Mock Down
		mock_client.stop()
		mock_dp.module_search.stop()
		mock_client.search.stop()
		mock_client.search = orig_client_search
		mock_dp.dependency_search.stop()
		mock_dp.module_search = orig_mod_search
		mock_dp.dependency_search = orig_dep_search
Ejemplo n.º 6
0
 def print_prob_val(self, fname="OutTree.txt"):
     n_tree = Tree(tree=self.tree)
     for node in n_tree.nodes:
         node = n_tree.get_node(node)
         node.tag = "{tag} - {val} - {prob}".format(tag=node.tag, val=node.data[0], prob=node.data[1])
     n_tree.save2file(fname)
     self.tree =  None
Ejemplo n.º 7
0
class Scansion(object):
    """
        .src    : list of strings
    """

    #///////////////////////////////////////////////////////////////////////////
    def __init__(self, source_file):
        """
                Scansion.__init__

                source_file     : (src) source file's name.
        """
        self.htree = Tree()
        self.src = []

        # creating root node (level 0) :
        self.htree.create_node(tag           = "root",
                               identifier    = "root",
                               data          = Hypothesis(htree = self.htree,
                                                          level=0,
                                                          language=None,
                                                          src=source_file))

        # calling root node :
        msg(0, "Calling the root node.")
        stop = False
        while not stop:
            leaves_to_be_extended = [leave for leave in self.htree.leaves() if not leave.data.dead]

            for leave in leaves_to_be_extended:
                leave.data.go_on()

            if len(leaves_to_be_extended)==0:
                stop = True
Ejemplo n.º 8
0
class TreePipeline(object):

    def open_spider(self, spider):
        self.tree = Tree()
        self.tree.create_node("root", "root")

    def process_item(self, item, spider):
        lst = item['text']
        lst = [x.strip() for x in [y.replace('...', '') for y in lst]]
        item['pagetitle'] = item['pagetitle'].replace('...', '')
        lst[-1] = item['pagetitle']
        for idx, elem in enumerate(lst):
            if idx == 0:
                previous = "root"
            else:
                previous = "|".join(lst[:idx])
            elem = "|".join(lst[:idx + 1])
            # elem = elem.replace('...', '')
            elem = elem.encode('utf-8').decode('utf-8')
            if not self.tree.contains(elem):
                print "Adding node %s" % elem
                self.tree.create_node(elem, elem, parent=previous)
                # self.tree.show()
        return item

    def close_spider(self, spider):
        self.tree.show()
        with open(makepath('data/cats/tree.json'), 'w') as outfile:
            outfile.write(self.tree.to_json())
        self.tree.save2file(makepath('data/cats/tree.tree'))
Ejemplo n.º 9
0
def parse_xml(path):
    tree = ET.parse(path)
    bill_list = []
    destination = "console"

    # eg:       (cox, "*****@*****.**")                  (rent, 2000)
    #          /                                 \            &    /            \
    #      (Evan, 0.5)                        (Jason, 0.5)       (Evan, 0.45)   (Jason, 0.55)
    for bill in tree.findall("bill"):
        billname = bill.get("name")
        bill_tree = Tree()
        bill_value = bill.get("fixed")
        if bill_value is None:
            bill_value = bill.get("from_email")
        bill_tree.create_node(tag=billname, identifier=billname, data=bill_value)

        for user in bill.findall("user"):
            username = user.get("name")
            ratio = user.get("ratio")
            bill_tree.create_node(tag=username, identifier=username, parent=billname, data=ratio)
        bill_list.append(bill_tree)

    # Get the location to dump our results
    for d in tree.findall("output"):
        destination = d.get("destination")

    return (bill_list, destination)
Ejemplo n.º 10
0
 def test_show_data_property(self):
     new_tree = Tree()
     class Flower(object):
         def __init__(self, color):
             self.color = color
     new_tree.create_node("Jill", "jill", data=Flower("white"))
     new_tree.show(data_property="color")
Ejemplo n.º 11
0
def build_directory_tree(service):
    print colored("*** Building directory tree ***", 'blue')
    
    # initialize a new directory structure
    directory = Tree()
    directory.create_node("Root", "root")
    page_token = None
    while True:
        try:
            param = {}
            if page_token:
                param['pageToken'] = page_token
            
            # Get children of folderID
            children = service.children().list(folderId='root', **param).execute()

            # For each child in folder, get ID, name and Type
            # and write to the directory tree
            for child in children.get('items', []):
                try:
                    file__ = service.files().get(fileId=child['id']).execute()
                    directory.create_node(file__['title'], child['id'], parent = 'root', data=node('root', child['id'], file__['title'], file__['mimeType']))
                except errors.HttpError, error:
                    print 'An error occurred: %s' % error
            
            # Get next page token for current folderID
            page_token = children.get('nextPageToken')
            if not page_token:
                break
        except errors.HttpError, error:
            print colored('An error occurred: %s', 'red') % error
            break
Ejemplo n.º 12
0
 def visit_root(self, node, tree=None):
     tree = Tree()
     root = repr(node)
     tree.create_node(root, root)
     for child in node.children:
         tree = self.visit(child, tree=tree)
     return tree
Ejemplo n.º 13
0
 def _get_random_tree(self, start, max_depth=999):
     """
     Returns a random tree from PCFG starting with symbol 'start'
     depth: the maximum depth of tree
     """
     t = Tree()
     t.create_node(ParseNode(start,''))
     
     # get ids of not expanded nonterminals in tree
     nodes_to_expand, depth = self.__get_nodes_to_expand_and_depth(t)
     
     while len(nodes_to_expand) > 0:
         # for each non terminal, choose a random rule and apply it
         for node in nodes_to_expand:
             symbol = t[node].tag.symbol
             
             # if tree exceeded the allowed depth, expand nonterminals
             # using rules from terminating_rule_ids
             if depth >= (max_depth-1):
                 # choose from rules for nonterminal from terminating_rule_ids
                 rhsix = np.random.choice(self.grammar.terminating_rule_ids[symbol], size=1)
             else:
                 # choose from rules for nonterminal according to production probabilities
                 rhsix = np.random.choice(len(self.grammar.rules[symbol]), p=self.grammar.prod_probabilities[symbol], size=1)
             
             t[node].tag.rule = rhsix[0] # index of production rule used when expanding this node
             rhs = self.grammar.rules[symbol][rhsix[0]]
             for s in rhs:
                 t.create_node(tag=ParseNode(s,''), parent=node)
         
         nodes_to_expand, depth = self.__get_nodes_to_expand_and_depth(t)
     
     return t
    def __init__(self, id, opcode, op_array, context):
        """ Create a tree representaiton of an Opcode

            Arguments :
                id : The id representing the new OPcode
                opcode : An OPcode struct
                op_array : The op_array of the OPcode
                context : An OPcacheParser instance
        """

        Tree.__init__(self)

        # Identifier to be used by the tree and nodes
        id_with_hash = str(hash(str(op_array))) + "_" + id

        # OP name
        op = OPcodeParser.get_opcode_name(opcode['opcode'])

        # Parser
        context = OPcodeParser(context)

        # Parse operands and result
        (op1, op2, result) = context.parse_operands(opcode, op_array)

        # Create nodes
        op1_node = Node("Operand 1: " + op1, id_with_hash + "_op1")
        op2_node = Node("Operand 2: " + op2, id_with_hash + "_op2")
        result_node = Node("Result: " + result, id_with_hash + "_result")

        # Link nodes to tree
        self.create_node(id + ": " + op, id_with_hash + "_opcode")
        self.add_node(op1_node, parent=id_with_hash + "_opcode")
        self.add_node(op2_node, parent=id_with_hash + "_opcode")
        self.add_node(result_node, parent=id_with_hash + "_opcode")
Ejemplo n.º 15
0
Archivo: cky.py Proyecto: dasolma/pyCKY
def create_trees(BP, Roots, NT, n):

    for bp in BP[n-1,0]:
        if bp.name in Roots:
            print '\nTree'
            t = Tree()
            create_tree(t, bp, 0, 1)
            t.show(key=lambda x: x.identifier)
Ejemplo n.º 16
0
def merge_trees(t1, t2, tick):
  t = Tree()
  identifier = -tick # using negative numbers as identifiers, positive numbers are ids for the leaf nodes
  name = "new_cluster_%s" % tick
  t.create_node(name, identifier)
  t.paste(identifier, t1)
  t.paste(identifier, t2)
  return t, name
Ejemplo n.º 17
0
def create_tree(indexed_titles, root, children=None):
  t = Tree()
  identifier = indexed_titles[root]
  t.create_node(root, identifier)
  if children:
    for sub_tree in children:
      t.paste(identifier, sub_tree)
  return t
def build_conversation(cursor):
    conversation_roots = postgres_queries.find_conversations_root(cursor)
    conversation_list = list()
    for root in conversation_roots:
        conversation_tree = Tree()
        conversation_tree.create_node(root[0], root[0])
        search_children(root[0], conversation_tree, cursor)
        conversation_list.append(conversation_tree)
    return conversation_list
 def compare_actual_folder_with_tree(self, root: path, tree: Tree):
     root_name = tree.root
     root_path = root.joinpath(root_name)
     print(root_path)
     self.assertTrue(root_path.exists(), "The path {} should exist, but doesn't".format(root_path))
     children = tree.children(root_name)
     for children in children:
         subtree = tree.subtree(children.identifier)
         self.compare_actual_folder_with_tree(root_path, subtree)
Ejemplo n.º 20
0
 def create_powerset_tree(self):
     tree = Tree(None)
     for idx, s in enumerate(self.object_sets):
         if idx == 0:
             tree.create_node((s, self.get_max_timeset(s)), s)
         else:
             parent_index = frozenset(sorted(list(s)[0:-1]))
             tree.create_node((s, self.get_max_timeset(s)), s, parent_index)
     return tree
Ejemplo n.º 21
0
 def setUp(self):
     tree = Tree()
     tree.create_node("Hárry", "hárry")
     tree.create_node("Jane", "jane", parent="hárry")
     tree.create_node("Bill", "bill", parent="hárry")
     tree.create_node("Diane", "diane", parent="jane")
     tree.create_node("George", "george", parent="bill")
     self.tree = tree
Ejemplo n.º 22
0
 def test_all_nodes_itr(self):
     """
     tests: Tree.all_nodes_iter
     Added by: William Rusnack
     """
     new_tree = Tree()
     self.assertEqual(len(new_tree.all_nodes_itr()), 0)
     nodes = list()
     nodes.append(new_tree.create_node('root_node'))
     nodes.append(new_tree.create_node('second', parent=new_tree.root))
     for nd in new_tree.all_nodes_itr():
         self.assertTrue(nd in nodes)
Ejemplo n.º 23
0
def to_tree(ts):
    """

    :param ts: list of stuff to be treed
    :return: treelib.Tree with the stuff after zf78 and putted in the tree.
    """
    tree = Tree()
    tree.create_node("root", 0, data=list([0,0.0]))
    coded = encode(ts)
    for i, word in enumerate(coded):
        tree.create_node(word[1],i+1,parent=word[0], data=list([0,0.0]))
    return tree
 def load(self, path2file):
     self.id_EDUs = []
     self.EDU = {}
     self.treeNS = Tree()
     self.tree = Tree()
     # nombre max d'espace pour init id_parents
     with open(path2file, "r") as f:
         max_space = 0
         nb_line = 0
         for i, line in enumerate(f):
             nb_space = 0
             for c in line:
                 if c == " ":
                     nb_space += 1
                 else:
                     break
             if nb_space > max_space:
                 max_space = nb_space
             nb_line += 1
     with open(path2file, "r") as f:
         id_parents = [0] * max_space
         NS_parents = [0] * max_space
         for i, line in enumerate(f):
             # nombre d'espace détermine le parent
             nb_space = 0
             for c in line:
                 if c == " ":
                     nb_space += 1
                 else:
                     break
             space = nb_space / 2
             id_parents[space] = i
             parent = id_parents[space - 1]
             reg = "\(([\w\-\[\]]+)|(_!.+!_)"  # récupération du contenu
             match = re.findall(reg, line)[0]
             if match[0] == "":
                 content = match[1]  # feuille EDU
                 self.id_EDUs.append(i)
                 # print content
                 self.EDU[i] = re.findall("_!(.*)!_", content)
             else:
                 content = match[0]
                 reg2 = "\[(N|S)\]"  # récupération NS
                 match2 = re.findall(reg2, content)
                 NS_parents[space] = match2  # ['N','S']
             # création du noeud
             if i == 0:
                 self.tree.create_node(content, 0)
                 self.treeNS.create_node("Root", 0)
             else:
                 id_NS = len(self.tree.is_branch(parent))  # 0 ou 1 car arbre binaire
                 self.tree.create_node(content, i, parent=parent)
                 self.treeNS.create_node(NS_parents[space - 1][id_NS], i, parent=parent)
Ejemplo n.º 25
0
    def build_header_tree(self):
        header_tree = Tree()

        root_field = ReportDefinitionField()
        root_field.alias = 'root'
        root_field.header = 'Root'
        root_field.sub_fields = self.report_definition.fields

        self.__build_header_tree(header_tree, None, root_field)
        self.__prepare_header_data(header_tree, header_tree.get_node(header_tree.root))

        return header_tree
Ejemplo n.º 26
0
def types_of_conversation():
    conversation_amount = postgres_queries.find_annotated_conversation_number()
    conversation_list = list()
    depth_dict = dict()
    depth_dict_long = dict()
    depth_dict_short = dict()
    number_of_tweets_dict = dict()
    for i in range (0, conversation_amount + 1, 1):
        conversation_tree = Tree()
        converastion = postgres_queries.find_conversation(i)
        for tweet in converastion:
            if tweet[1] is None:
                conversation_tree.create_node(tweet[0], tweet[0])
                build_conversation(tweet[0], converastion, conversation_tree)
                depth = conversation_tree.depth() + 1
                number_of_tweets = len(conversation_tree.all_nodes())
                #short/long
                if number_of_tweets >=20:
                    if depth in depth_dict_long:
                        depth_dict_long[depth] += 1
                    else:
                        depth_dict_long[depth] = 1
                else:
                    if depth in depth_dict_short:
                        depth_dict_short[depth] += 1
                    else:
                        depth_dict_short[depth] = 1

                if number_of_tweets in number_of_tweets_dict:
                    number_of_tweets_dict[number_of_tweets] += 1
                else:
                     number_of_tweets_dict[number_of_tweets] = 1
                if depth in depth_dict:
                    depth_dict[depth] += 1
                else:
                    depth_dict[depth] = 1
        conversation_list.append(conversation_tree)
    #print depth_dict
    print 'Depth of a conversation'
    for depth, count in depth_dict.iteritems():
        print depth, '\t', count
    print 'Number of tweets in a conversation'
    for number, count in number_of_tweets_dict.iteritems():
        print number, '\t', count
    print 'Depth of a long conversation'
    for depth, count in depth_dict_long.iteritems():
        print depth, '\t', count
    print 'Depth of a short conversation'
    for depth, count in depth_dict_short.iteritems():
        print depth, '\t', count

    return conversation_list
Ejemplo n.º 27
0
    def test_export_with_minus_in_filename(self):
        tree = Tree()
        tree.create_node('Example Node', 'example-node')
        expected = """\
digraph tree {
\t"example-node" [label="Example Node", shape=circle]
}"""

        export_to_dot(tree, 'id_with_minus.dot')
        self.assertTrue(os.path.isfile('id_with_minus.dot'), "The file id_with_minus.dot could not be found.")
        generated = self.read_generated_output('id_with_minus.dot')
        self.assertEqual(expected, generated, "The generated file content is not the expected one")
        os.remove('id_with_minus.dot')
Ejemplo n.º 28
0
    def test_unicode_filename(self):
        tree = Tree()
        tree.create_node('Node 1', 'node_1')
        export_to_dot(tree, 'ŕʩϢ.dot')

        expected = """\
digraph tree {
\t"node_1" [label="Node 1", shape=circle]
}"""
        self.assertTrue(os.path.isfile('ŕʩϢ.dot'), "The file ŕʩϢ.dot could not be found.")
        generated = self.read_generated_output('ŕʩϢ.dot')
        self.assertEqual(expected, generated, "The generated file content is not the expected one")
        os.remove('ŕʩϢ.dot')
Ejemplo n.º 29
0
def create_tree(adict):
    tree = Tree()
    if type(adict) is dict:
        # print ("procdessing dict to tree...")
        root = list(adict.keys())[0]
        # print (root)
        tree.create_node(timestamp_node(root), root, data=time())
        for node in list(adict.values()):
            # print ("processing a node of the dict values")
            if type(node) is dict:
                newTree = create_tree(node)
                tree.paste(root, newTree)
            elif type(node) is list:
                for item in node:
                    newTree = create_tree(item)
                    tree.paste(root, newTree)
            else:
                tree.create_node(timestamp_node(node),
                                 node,
                                 parent=root,
                                 data=time())
    else:
        tree.create_node(timestamp_node(adict), adict, data=time())
    return tree
Ejemplo n.º 30
0
def tree_build_from_list(containers):
    """
    Build a tree based on a unsorted list.

    Build a tree of containers based on an unsorted list of containers.

    Example:
    --------
        >>> containers = [
            {
                "childContainerKey": null,
                "configlets": [],
                "devices": [],
                "imageBundle": "",
                "key": "root",
                "name": "Tenant",
                "parentName": null
            },
            {
                "childContainerKey": null,
                "configlets": [
                    "veos3-basic-configuration"
                ],
                "devices": [
                    "veos-1"
                ],
                "imageBundle": "",
                "key": "container_43_840035860469981",
                "name": "staging",
                "parentName": "Tenant"
            }]
        >>> print(tree_build_from_list(containers=containers))
            {"Tenant": {"children": [{"Fabric": {"children": [{"Leaves": {"children": ["MLAG01", "MLAG02"]}}, "Spines"]}}]}}
    Parameters
    ----------
    containers : dict, optional
        Container topology to create on CVP, by default None
    
    Returns
    -------
    json
        tree topology
    """
    # Create tree object
    tree = Tree()  # Create the base node
    previously_created = list()
    # Create root node to mimic CVP behavior
    tree.create_node("Tenant", "Tenant")
    # Iterate for first level of containers directly attached under root.
    for cvp_container in containers:
        if cvp_container['parentName'] == None:
            continue
        elif cvp_container['parentName'] in ['Tenant']:
            previously_created.append(cvp_container['name'])
            tree.create_node(cvp_container['name'],
                             cvp_container['name'],
                             parent=cvp_container['parentName'])
    # Loop since expected tree is not equal to number of entries in container topology
    while len(tree.all_nodes()) < len(containers):
        for cvp_container in containers:
            if tree.contains(
                    cvp_container['parentName']
            ):  #and cvp_container['parentName'] not in ['Tenant']
                try:
                    tree.create_node(cvp_container['name'],
                                     cvp_container['name'],
                                     parent=cvp_container['parentName'])
                except:
                    continue
    return tree.to_json()
Ejemplo n.º 31
0
# https://treelib.readthedocs.io/en/latest/examples.html#advanced-usage


def walkTree(tree, rootNode, path):

    d = list(map(lambda x: x.tag, tree.children(rootNode)))
    d.sort()

    for z in d:
        path = walkTree(tree, z, path + z)

    return path


input = list(map(lambda x: x.strip(), open("test_input.txt").readlines()))
tree = Tree()
tree.create_node("root", "root")

# first figure out how many steps there are and then sort them
# by their name
for lines in input:
    (l1, l2) = (lines[5], lines[36])
    print(lines)
    if tree.contains(l1) and tree.contains(l2):
        tree.move_node(l2, l1)
    elif tree.contains(l1) and not tree.contains(l2):
        tree.create_node(l2, l2, parent=l1)
    elif not tree.contains(l1) and tree.contains(l2):
        # get the root for l2 and make that the root for l1
        # then move l2 under l1
        tree.create_node(l1, l1, parent=tree.parent(l2))
Ejemplo n.º 32
0
    # with urllib.request.urlopen('http://www.image-net.org/api/xml/structure_released.xml') as response:
    #     html = response.read()
    # f11 = open('treetext.txt','wb')
    # f11.write(html)
    # f11.close()
    f11 = open(
        '/usr/local/apps/profiles-rest-api/android-model403/treetext.txt',
        'rb')
    html = f11.read()
    f11.close()

    tree = ElementTree(fromstring(html))
    root = tree.getroot()

    synsetTree = Tree()

    synsetTree.create_node('Entity', 'fall11', data=Confidence(0, 0))
    for synset in root.iter('synset'):
        for child in synset:
            if child.get('wnid') in synsetTree._nodes:
                continue
            synsetTree.create_node(child.get('words'),
                                   child.get('wnid'),
                                   parent=synset.get('wnid'),
                                   data=Confidence(0, 0))

# synsetTree.show()

#treeDog = synsetTree.subtree('n02087122')
    treeDog = synsetTree
Ejemplo n.º 33
0
    def init_root_list(self, examples, mode='train'):
        """
        init batch tree list
        :param sen: training data
        :param label: ground truth label
        :param id_list: id list is used to map the raw feature extracted by raw_sen
        """
        keep_prob = self.config['keep_prob'] if mode == 'train' else 1.0
        self.batch_size = len(examples)
        self.trees = [Tree() for _ in range(self.batch_size)]

        self.sen_list = [e.sen for e in examples]
        self.label_list = [e.label for e in examples]
        self.char_list = [e.char for e in examples]
        self.pos_list = [e.pos for e in examples]

        # in tf version, no sorting by length
        data = batchify_with_label(self.sen_list, self.char_list,
                                   self.label_list)
        self.fw_seq_tensor, self.bw_seq_tensor, self.word_seq_lengths = data[
            0], data[1], data[2]
        self.char_seq_tensor, self.char_seq_lengths = data[3], data[4]
        self.label_seq_tensor = data[5]
        self.max_seq_len = self.word_seq_lengths.max().item()

        self.feature_seq_tensor = batchify_feature(
            char_seq_tensor=self.char_seq_tensor,
            word_seq_lengths=self.word_seq_lengths,
            pos_list=self.pos_list,
            feature_dim=self.config['raw_feature_dim'])

        # print('********** check ***********')
        # print(self.fw_seq_tensor.shape, self.bw_seq_tensor.shape)
        # print(self.word_seq_lengths)
        # print(self.max_seq_len)
        # print(self.feature_seq_tensor.shape)

        # reshape tensors to [b*t, d]
        # self.fw_seq_tensor = np.reshape(
        #     self.fw_seq_tensor, [self.batch_size*self.max_seq_len, -1])
        # self.bw_seq_tensor = np.reshape(
        #     self.bw_seq_tensor, [self.batch_size*self.max_seq_len, -1])
        # self.feature_seq_tensor = np.reshape(
        #     self.feature_seq_tensor, [self.batch_size*self.max_seq_len, -1])

        # self.fc_output_data = self.pv_model.get_fc_output(
        #     fw_word=self.fw_seq_tensor, bw_word=self.bw_seq_tensor,
        #     extra_fea=self.feature_seq_tensor, keep_prob=keep_prob)

        # print('obtain fc_output, type', self.fc_output_data.shape, type(self.fc_output_data))

        # self.fc_output_data = np.reshape(
        #     self.fc_output_data, [self.batch_size, self.max_seq_len, -1])

        # print('reshape fc_output',  self.fc_output_data.shape)

        root_node_list = []
        root_init = [[] for _ in range(4)]

        for i in range(len(self.trees)):
            root_node = self.trees[i].create_node(identifier='root',
                                                  data=Node())
            # TODO: check visit_num of init node is 0 or 1 ?????
            root_node.data.num = 0
            root_node.data.index = i
            root_node_list.append(root_node)
            root_init[0].append(root_node)
            root_init[1].append(self.fw_seq_tensor[i][0])
            root_init[2].append(self.bw_seq_tensor[i][0])
            root_init[3].append(self.feature_seq_tensor[i][0])

        probs = self.pv_model.get_policy(fw_word=np.array(root_init[1]),
                                         bw_word=np.array(root_init[2]),
                                         extra_fea=np.array(root_init[3]),
                                         keep_prob=keep_prob)

        for i in range(len(probs)):
            self.expand(root_init[0][i], probs[i])

        del root_init
        gc.collect()

        return root_node_list
Ejemplo n.º 34
0
def analyze(token):
    def getNext():
        i = 0
        while True:
            yield i
            i += 1

    import pandas as pd
    from collections import deque

    from treelib import Tree, Node
    import re

    tokens = []
    for l in token:
        s = re.split(r' ', l)
        tokens.append(s[0])
    tokens.append('$')
    test = deque(tokens)
    rr = {
        'r1': 'E=E+T',
        'r2': 'E=T',
        'r3': 'T=T*F',
        'r4': 'T=F',
        'r5': 'F=(E)',
        'r6': 'F=i'
    }

    class CompiledError(StandardError):
        def __init__(self, arg):
            self.arg = arg

        def __str__(self):
            return self.arg

    state = [0]
    symbolic = ['$']
    ast = []
    table = pd.read_csv('table.csv', index_col=0, na_filter=False)
    ltest = len(test)
    while (len(test) != 0):
        if test[0] not in table.columns:
            raise CompiledError('%s, unrecognized token at %d' %
                                (test[0], ltest - len(test)))
        ins = table.loc[state[-1], test[0]]
        if len(ins) == 0:
            raise CompiledError('%s complie failed at %d, unexpected token' %
                                (test[0], ltest - len(test)))
        if ins[0] == 's':
            state.append(int(ins[1:]))
            tree = Tree()
            tree.create_node(test[0], getNext())
            ast.append(tree)
            symbolic.append(test.popleft())
        elif ins[0] == 'r':
            rule = rr[ins]
            print rule
            li = list(rule)
            li.reverse()
            temptree = []
            for i in li:
                if i != '=':
                    symbolic.pop()
                    state.pop()
                    temptree.append(ast.pop())
                else:
                    break
            symb = rule[:rule.find('=')]
            symbolic.append(symb)
            state.append(int(table.loc[state[-1], symbolic[-1]]))
            tree = Tree()
            ii = getNext()
            tree.create_node(symb, ii)
            for tri in temptree:
                tree.paste(ii, tri)
            ast.append(tree)
        elif ins == 'AC':
            print 'succeed'
            tree = ast.pop()
            tree.show()
            return
    raise CompiledError('%s compiled failed at %d, unexpected token' %
                        (test[0], ltest - len(test)))
Ejemplo n.º 35
0
def ID3(data):
    tree = Tree()
    return __ID3(tree, data, None, None, None, {}, 0)
Ejemplo n.º 36
0
def help_func_block(grammar, tokens, root_name="block", function=None):

    #go line by line
    #if }
    #return tree
    #if {
    #recursive help_func_block
    #grab up to till first ;
    #call expression handeler on that sub list
    #returns a tree which is appended

    tree = Tree()
    root_node = Node(tag=root_name)
    tree.add_node(root_node, parent=None)

    func_flag_no_init = 0
    func_flag_init = 0
    func_flag = 0
    front_index = 0
    num_tokens_to_skip = 0

    i = 0
    while (i < len(tokens)):
        if (tokens[i][0] == "}"):
            return [tree, num_tokens_to_skip + 1]

        elif (tokens[i][0] == "{"):
            result = help_func_block(grammar,
                                     tokens[i + 1:],
                                     function=function)

            front_index += 1 + result[1]
            i += 1 + result[1]
            num_tokens_to_skip += 1 + result[1]

            tree.paste(root_node.identifier, result[0])

        elif (tokens[i][0] in ["if", "while"]):
            if_node = Node(tag=tokens[i][0])
            tree.add_node(if_node, parent=root_node)

            if_cond = Node(tag="condition")
            tree.add_node(if_cond, parent=if_node)

            first_bracket = -1
            for token in tokens[i:]:
                if (token[0] == '{'):
                    first_bracket = tokens.index(token)
                    break
                elif (token[0] == '}'):
                    # Break to throw error if unmatched
                    break
            if (first_bracket < 0):
                raise Exception(tokens[i][0] + " without body '{' on line " +
                                str(tokens[i][2]))

            cond_result = help_func_expression(grammar,
                                               tokens[i + 2:first_bracket - 1],
                                               function=function)
            body_result = help_func_block(grammar,
                                          tokens[first_bracket + 1:],
                                          root_name="condition_body",
                                          function=function)

            # Increment i, num_tokens_to_skip, and front_index
            if_skip = 1  # if/while
            if_skip += 1  # opening bracket
            if_skip += 2  # parens
            if_skip += cond_result[1]
            if_skip += body_result[1]

            num_tokens_to_skip += if_skip
            front_index += if_skip
            i += if_skip
            tree.paste(if_cond.identifier, cond_result[0])
            tree.paste(if_node.identifier, body_result[0])

        elif (tokens[i][0] == "return"):
            result = help_func_return(grammar, tokens[i:], function=function)
            front_index += result[1]
            i += result[1]
            num_tokens_to_skip += result[1]

            tree.paste(root_node.identifier, result[0])

        elif (tokens[i][0] == ";"):
            back_index = i

            expr_tokens = tokens[front_index:back_index]

            # Remove leading and trailing ( and )
            while (len(expr_tokens) > 0
                   and (expr_tokens[0][0] == '(' or expr_tokens[0][0] == ')')):
                expr_tokens.pop(0)
                num_tokens_to_skip += 1
            while (len(expr_tokens) > 0 and
                   (expr_tokens[-1][0] == '(' or expr_tokens[-1][0] == ')')):
                expr_tokens.pop(-1)
                num_tokens_to_skip += 1

            if (len(expr_tokens) > 0):
                if (len(expr_tokens) == 2
                        and expr_tokens[0][1] == 'typeSpecifier'
                        and expr_tokens[1][1] == 'ID'):
                    func_flag = 1
                    func_flag_no_init = 1
                    # print("This is a variable declaration with no intilization")
                    var_type = expr_tokens[0][0]
                    var_name = expr_tokens[1][0]
                    __symbol_tables[function][var_name] = var_type
                    expr_tokens.pop(0)
                elif (len(expr_tokens) > 2
                      and expr_tokens[0][1] == 'typeSpecifier'
                      and expr_tokens[1][1] == 'ID'
                      and expr_tokens[2][1] == '='):
                    func_flag = 1
                    # print("This is a variable declaration with intilization")
                    var_type = expr_tokens[0][0]
                    var_name = expr_tokens[1][0]
                    __symbol_tables[function][var_name] = var_type
                    expr_tokens.pop(0)
                if (func_flag == 1):
                    tmp_tree = Tree()
                    tmp_tree_root = Node(tag=var_type)
                    tmp_tree.add_node(tmp_tree_root, parent=None)
                    tmp_tree.add_node(Node(tag=var_name), parent=tmp_tree_root)

                result = help_func_expression(grammar,
                                              expr_tokens,
                                              function=function)

                if (func_flag == 1):
                    result[1] += 1
                front_index = back_index + 1
                i += 1
                num_tokens_to_skip += 1 + result[1]
                if (func_flag_no_init != 1):
                    tree.paste(root_node.identifier, result[0])
                if (func_flag == 1):
                    # pass
                    tree.paste(root_node.identifier, tmp_tree)
                func_flag_no_init = 0
                func_flag_init = 0
                func_flag = 0
                tmp_tree = None

        else:
            i += 1

    # Iterated through tokens without closing '}'
    raise Exception(errors.ERR_NO_BLOCK_END + " on line " +
                    str(tokens[i - 1][2]))
Ejemplo n.º 37
0
def help_func_funDeclaration(grammar, tokens):
    #first token is return type
    #the next token is the name
    #the third token is the (
    #sometime after that should be a
    #)
    assert (len(tokens) >= 4)

    tree = Tree()

    # organization nodes
    return_node = Node(tag="return_type")
    params_node = Node(tag="params")
    body_node = Node(tag="func_body")

    # create root node
    if (tokens[1][0] == "_start"):
        raise Exception("Function name _start is reserved for assembly")
    elif (tokens[1][0] == "main"):
        tokens[1][0] = "_start"
    func_name = tokens[1][0]
    func_root = Node(tag="func:" + func_name)
    return_type = Node(tag=tokens[0][0])
    # Create symbol subtable
    __symbol_tables[func_name] = {}

    # Assemble basic subtree
    tree.add_node(func_root, parent=None)
    tree.add_node(return_node, parent=func_root)
    tree.add_node(params_node, parent=func_root)

    tree.add_node(return_type, parent=return_node)

    # Create and add params nodes
    params = []
    var_case = 0  # 0 = empty, 1 = void, 2 = variables
    for i in range(3, len(tokens), 3):
        if (i == 3 and tokens[i][0] == 'void'):
            var_case = 1
            break
        elif (tokens[i][0] == ")"):
            break
        else:
            try:
                params.append((tokens[i][0], tokens[i + 1][0]))
                # i+0 = type
                # i+1 = name
                # i+3 = comma if it exists
                var_case = 2
            except:
                raise Exception(errors.ERR_BAD_FUNC_PAR + " '" + tokens[i][0] +
                                "' on line " + str(tokens[i][2]))
            if (tokens[i + 2][0] != ','):
                break

    for param in params:
        type_node = Node(tag=param[0])
        name_node = Node(tag=param[1])

        tree.add_node(type_node, parent=params_node)
        tree.add_node(name_node, parent=type_node)
        #check grammar rules

    # Create and add body
    body_tokens = []
    skip_tokens = 0
    if (var_case % 3 == 0):
        # Empty parameters
        body_tokens = tokens[5:]
        skip_tokens = 5
        pass
    elif (var_case % 3 == 1):
        # Void parameter
        body_tokens = tokens[6:]
        skip_tokens = 6
        pass
    elif (var_case % 3 == 2):
        # Has paremeters
        body_tokens = tokens[4 + (3 * (len(params))):]
        skip_tokens = 4 + (3 * (len(params)))
        pass

    #call help_func_block
    #parser_out = run_parser(body_tokens, grammar, look_for_brace=True, root_name="func_body") #may be off by one
    block_out = help_func_block(grammar,
                                body_tokens,
                                root_name="func_body",
                                function=func_name)
    body_tree = block_out[0]
    skip_tokens += block_out[1]
    tree.paste(func_root.identifier, body_tree)

    return [tree, skip_tokens]
Ejemplo n.º 38
0
def help_func_expression(grammar, tokens, function=None):

    tokens_skip = 0
    # Remove leading and trailing ( and )
    while (len(tokens) > 0 and (tokens[0][0] == '(' or tokens[0][0] == ')')):
        tokens.pop(0)
        tokens_skip += 1
    while (len(tokens) > 0 and (tokens[-1][0] == '(' or tokens[-1][0] == ')')):
        tokens.pop()
        tokens_skip += 1

    # Check for subexpression denoted by parentheses
    op_depth = []
    depth = 0
    paren_open = -1
    paren_close = -1
    for i in range(len(tokens)):
        if (tokens[i][0] == ';'):
            break  # End of expression
        elif (tokens[i][0] == '('):
            depth += 1
            paren_open = i
        elif (tokens[i][0] == ')'):
            paren_close = i
            depth -= 1
        op_depth.append(depth)

    # Find the lowest precedence operator
    lowest_prec_op = []
    op_precedence = {
        "&&": 50,
        "||": 40,
        "shiftop": 35,
        "mulop": 30,
        "sumop": 20,
        "relop": 10,
        "=": 0,
        "+=": 0,
        "-=": 0,
        "*=": 0,
        "\=": 0,
        "%=": 0
    }

    for token in tokens:
        if (token[0] == ';'):
            break
        elif (token[1] in op_precedence):

            if (len(lowest_prec_op) == 0):
                lowest_prec_op = token
            else:
                cur_token_depth = op_depth[tokens.index(token)]
                lowest_prec_depth = op_depth[tokens.index(lowest_prec_op)]

                if (cur_token_depth < lowest_prec_depth):
                    # Higher depth guarantees replacement
                    lowest_prec_op = token
                elif (cur_token_depth == lowest_prec_depth
                      and op_precedence[token[1]] <=
                      op_precedence[lowest_prec_op[1]]):
                    # To replace, must be on same depth and lower precedence
                    lowest_prec_op = token

    if (len(lowest_prec_op) == 0):
        # Check if "expression" is just a single constant
        if (len(tokens) > 1 and tokens[0][1] == "ID" and tokens[1][1] == "("):
            # Create node with function name
            tree = Tree()
            if (tokens[0][0] == "_start"):
                raise Exception(
                    "Function name _start is reserved for assembly")
            elif (tokens[0][0] == "main"):
                tokens[0][0] = "_start"
            call_node = Node(tag="func:" + tokens[0][0])
            tree.add_node(call_node, parent=None)
            tokens_skip += 2

            # Children of node are function parameters
            # Iterate through tokens to find each parameter
            # Parameters split on ',' with depth=0
            depth = 0
            token_depth = []
            end_point = -1

            for i in range(2, len(tokens)):
                tokens_skip += 1
                if (tokens[i][0] == "("):
                    depth += 1
                elif (tokens[i][0] == ")"):
                    depth -= 1

                token_depth.append(depth)

                if (depth < 0):
                    # End ) found
                    end_point = i
                    break

            if (end_point < 0):
                end_point = len(tokens)
                #raise Exception("No ending ')' for function call '" +
                #    tokens[0][0] + "'")

            # Find split points for the expressions
            split_points = [2]
            for i in range(len(token_depth)):
                if (token_depth[i] == 0 and tokens[i + 2][0] == ','):
                    split_points.append(i + 3)

            split_points.append(end_point)

            func_params = []
            for i in range(len(split_points) - 1):
                # print([split_points[i], split_points[i+1]])
                func_params.append(tokens[split_points[i]:split_points[i + 1]])

            # Add parameters to tree
            for p in func_params:
                # Needs to call expression handler to evaluate parameters
                # - Currently, operators in function calls are lower prec than the function for some reason
                # - Exceptions caused by nesting function calls
                param_node = Node(tag=p[0][0])
                tree.add_node(param_node, parent=call_node)

            return [tree, tokens_skip]
        elif ((tokens[0][1] == "NUMCONST" or tokens[0][1] == "FLOATCONST"
               or tokens[0][1] == "CHARCONST" or tokens[0][1] == "STRINGCONST"
               or tokens[0][1] == "true" or tokens[0][1] == "false"
               or tokens[0][1] == "ID")):
            # Check for no-parameter function
            if (tokens[0][0] in __symbol_tables.keys()):
                # Found a function call without parameters
                tokens_skip += 1
                tree = Tree()
                if (tokens[0][0] == "_start"):
                    raise Exception(
                        "Function name _start is reserved for assembly")
                elif (tokens[0][0] == "main"):
                    tokens[0][0] = "_start"
                value_node = Node(tag="func:" + tokens[0][0])
                tree.add_node(value_node, parent=None)
                return [tree, tokens_skip]
            else:
                # Expression is a constant or named variable
                tokens_skip += 1
                tree = Tree()
                value_node = Node(tag=tokens[0][0])
                tree.add_node(value_node, parent=None)
                return [tree, tokens_skip]
        else:
            raise Exception("Unknown token sequence: " + str(tokens))

    # Lowest precedence operator found
    # Lowest precedence operator is root.
    tree = Tree()
    op_node = Node(tag=lowest_prec_op[0])
    tree.add_node(op_node, parent=None)

    # Recursive calls to make left and right subtrees
    tokens_skip += 1

    tokens_l = tokens[:tokens.index(lowest_prec_op)]
    tokens_r = tokens[tokens.index(lowest_prec_op) + 1:]

    has_tokens_l = False
    for token in tokens_l:
        if (token[0] != '(' and token[0] != ')'):
            has_tokens_l = True
            break

    has_tokens_r = False
    for token in tokens_r:
        if (token[0] != '(' and token[0] != ')'):
            has_tokens_r = True
            break

    if (len(tokens_l) > 0 and has_tokens_l):
        expr_l = help_func_expression(grammar, tokens_l, function=function)
        tree.paste(op_node.identifier, expr_l[0])
        tokens_skip += expr_l[1]
    else:
        tokens_skip += len(tokens_l)

    if (len(tokens_r) > 0 and has_tokens_r):
        expr_r = help_func_expression(grammar, tokens_r, function=function)
        tree.paste(op_node.identifier, expr_r[0])
        tokens_skip += expr_r[1]
    else:
        tokens_skip += len(tokens_r)

    return [tree, tokens_skip]
Ejemplo n.º 39
0
def help_func_selectionStmt(grammar, tokens):
    # Called only by the help_func_manager
    # PLACEHOLDER RETURN STATEMENT
    tree = Tree()
    tree.add_node(Node(tag="Placeholder Selection"), parent=None)
    return [tree, 0]
Ejemplo n.º 40
0
 def __init__(self, max_id=0):
     self.tree = Tree()
Ejemplo n.º 41
0
    def create(self,words_list,postags_list,arcs_list):
        # 输入三个list
        # 第一个是words_list 词语序列,词序
        # 第二个词性
        # 第三个是依存关系,这个也是用于构建树的关键

        tree = Tree()

        # 使用一层层的搭建技术
        # 我们设定五个层
        layer1 = []
        layer2 = []
        layer3 = []
        layer4 = []
        # layer5 = []
        # print('words_list' + str(words_list))
        # print('arcs_list'+str(arcs_list))

        # 首节点
        for i in range(len(arcs_list)):
            arc_head = arcs_list[i].split(':')[0]

            # 首节点
            if int(arc_head) == 0:
                HED_id = i

        # layer1层
        for i in range(len(arcs_list)):
            arc_head = arcs_list[i].split(':')[0]

            if int(arc_head) - 1 == int(HED_id):
                node = {'node' + str(i) : 'HED'}
                layer1.append(node)

        # layer2层
        for i in range(len(arcs_list)):
            arc_head = arcs_list[i].split(':')[0]

            # 说明有arc_head在layer1中,那就是这个点在layer2中
            for lay in layer1:
                if int(list(lay.keys())[0].lstrip('node')) == int(arc_head) - 1:
                    node = {'node' + str(i) : list(lay.keys())[0]}
                    layer2.append(node)

        # layer3层
        for i in range(len(arcs_list)):
            arc_head = arcs_list[i].split(':')[0]

            # 说明有arc_head在layer2中,那就是这个点在layer3中
            for lay in layer2:
                if int(list(lay.keys())[0].lstrip('node')) == int(arc_head) - 1:
                    node = {'node' + str(i): list(lay.keys())[0]}
                    layer3.append(node)

        # layer4层
        for i in range(len(arcs_list)):
            arc_head = arcs_list[i].split(':')[0]

            # 说明有arc_head在layer3中,那就是这个点在layer4中
            for lay in layer3:
                if int(list(lay.keys())[0].lstrip('node')) == int(arc_head) - 1:
                    node = {'node' + str(i): list(lay.keys())[0]}
                    layer4.append(node)


        # print(layer1)
        # print(layer2)
        # print(layer3)
        # print(layer4)


        # 四层都构建完毕
        # 下面就根据一层层的搭建树
        # 首先创建根节点
        if not tree.contains('HED'):
            tree.create_node(str(HED_id) + ' ' + words_list[int(HED_id)],
                             'HED',
                             data=postags_list[int(HED_id)] + ' ' + arcs_list[int(HED_id)].split(':')[1])

        # layer1
        for lay in layer1:
            nodename = list(lay.keys())[0]
            parent = list(lay.values())[0]
            tree.create_node(
                nodename.lstrip('node') + ' ' + words_list[int(nodename.lstrip('node'))],
                nodename,
                parent=parent,
                data=postags_list[int(nodename.lstrip('node'))] + ' ' + arcs_list[int(nodename.lstrip('node'))].split(':')[1])

        # layer2
        for lay in layer2:
            nodename = list(lay.keys())[0]
            parent = list(lay.values())[0]
            tree.create_node(
                nodename.lstrip('node') + ' ' + words_list[int(nodename.lstrip('node'))],
                nodename,
                parent=parent,
                data=postags_list[int(nodename.lstrip('node'))] + ' ' + arcs_list[int(nodename.lstrip('node'))].split(':')[1])

        # layer3
        for lay in layer3:
            nodename = list(lay.keys())[0]
            parent = list(lay.values())[0]
            tree.create_node(
                nodename.lstrip('node') + ' ' + words_list[int(nodename.lstrip('node'))],
                nodename,
                parent=parent,
                data=postags_list[int(nodename.lstrip('node'))] + ' ' + arcs_list[int(nodename.lstrip('node'))].split(':')[1])

        # layer4
        for lay in layer4:
            nodename = list(lay.keys())[0]
            parent = list(lay.values())[0]
            tree.create_node(
                nodename.lstrip('node') + ' ' + words_list[int(nodename.lstrip('node'))],
                nodename,
                parent=parent,
                data=postags_list[int(nodename.lstrip('node'))] + ' ' + arcs_list[int(nodename.lstrip('node'))].split(':')[1])

        return tree
Ejemplo n.º 42
0
    def test_modify_node_identifier_recursively(self):
        tree = Tree()
        tree.create_node("Harry", "harry")
        tree.create_node("Jane", "jane", parent="harry")
        n = tree.get_node("jane")
        self.assertTrue(n.identifier == 'jane')

        # Success to modify
        tree.update_node(n.identifier, identifier='xyz')
        self.assertTrue(tree.get_node("jane") is None)
        self.assertTrue(tree.get_node("xyz").identifier == 'xyz')
Ejemplo n.º 43
0
def load_tree(target_file):
    with open(target_file, 'r') as infile:
        dictionary = json.load(infile)
    return __dictionary_to_tree(dictionary, Tree())
Ejemplo n.º 44
0
def help_func_varDeclaration(grammar, tokens):
    # Called only by the help_func_manager
    # PLACEHOLDER RETURN STATEMENT
    tree = Tree()
    tree.add_node(Node(tag="Placeholder Var Dec"), parent=None)
    return [tree, 0]
Ejemplo n.º 45
0
    def configure_tree_topology(self, root, degree=2, remove=False):
        """Configures the cluster's network topology as a tree.

        The tree consists of the specified root node and the nodes,
        which build the subtrees. The childrens are incrementally chosen,
        in other words, sequentially as specified in the config file.

        Arguments:
            root {integer} -- The tree's root node.

        Keyword Arguments:
            degree {integer} -- The maximum number of children (default: {2})
            remove {boolean} -- Remove the configuration (default: {False})
        """

        self.logger.info("Configuring tree topology...")
        tree = Tree()
        root_node = self.topology.get_node(root)
        tree.create_node(root_node.name, root_node.node_id)
        parent_node = root
        for nodex in self.topology.nodes:
            if nodex.node_id == root_node.node_id:
                continue
            if len(tree.children(parent_node)) >= degree:
                if parent_node == root and root != 0:
                    parent_node = 0
                elif parent_node + 1 == root:
                    parent_node += 2
                else:
                    parent_node += 1
            tree.create_node(nodex.name, nodex.node_id, parent_node)

        self.logger.info("The following tree will be configured:")
        tree.show()

        for nodex in self.topology.nodes:
            self.logger.debug("%s:", nodex.name)
            subtree = tree.subtree(nodex.node_id)
            for nodey in self.topology.nodes:
                if nodex.node_id == nodey.node_id:
                    continue
                if subtree.contains(nodey.node_id):
                    children = tree.children(nodex.node_id)
                    for child in children:
                        if (child.identifier == nodey.node_id
                                or tree.is_ancestor(child.identifier,
                                                    nodey.node_id)):
                            nodex.add_forwarding(
                                nodey,
                                self.topology.get_node(child.identifier))
                            break
                elif tree.parent(nodex.node_id) != None:
                    nodex.add_forwarding(
                        nodey,
                        self.topology.get_node(
                            tree.parent(nodex.node_id).identifier))

        if not self.testing:
            self.topology.send_forwarding_tables(remove)
Ejemplo n.º 46
0
class RAMSTKDataModel(object):  # pragma: no cover
    """
    This is the meta-class for all RAMSTK Data Models.

    :ivar tree: the :class:`treelib.Tree` that will contain the
                structure of the RAMSTK module being modeled..
    :ivar dao: the :class:`ramstk.dao.DAO` object used to communicate
               with the RAMSTK Program database.
    """

    def __init__(self, dao):
        """
        Initialize an RAMSTK data model instance.

        :param dao: the data access object for communicating with the
                    RAMSTK Program database.
        :type dao: :class:`ramstk.dao.DAO.DAO`
        """
        # Initialize private dictionary attributes.

        # Initialize private list attributes.

        # Initialize private scalar attributes.
        self._last_id = None

        # Initialize public dictionary attributes.

        # Initialize public list attributes.

        # Initialize public scalar attributes.
        self.dao = dao
        self.tree = Tree()
        self.last_id = None

        # Add the root to the Tree().  This is neccessary to allow multiple
        # entries at the top level as there can only be one root in a treelib
        # Tree().  Manipulation and viewing of a RAMSTK module tree needs to
        # ignore the root of the tree.
        try:
            self.tree.create_node(tag=self._tag, identifier=0, parent=None)
        except (tree.MultipleRootError, tree.NodeIDAbsentError,
                tree.DuplicatedNodeIdError):
            pass

    def do_select(self, node_id, **kwargs):  # pylint: disable=unused-argument
        """
        Retrieve the instance of the RAMSTK<MODULE> model for the Node ID passed.

        :param int node_id: the Node ID of the data package to retrieve.
        :return: the instance of the RAMSTK<MODULE> class that was requested
                 or None if the requested Node ID does not exist.
        """
        try:
            _entity = self.tree.get_node(node_id).data
        except AttributeError:
            _entity = None
        except tree.NodeIDAbsentError:
            _entity = None

        return _entity

    def do_select_all(self, **kwargs):  # pylint: disable=unused-argument
        """
        Retrieve and build the RAMSTK Module tree.

        :return: an SQLAlchemy session instance.
        :rtype:
        """
        _root = self.tree.root
        for _node in self.tree.children(_root):
            self.tree.remove_node(_node.identifier)

        return self.dao.RAMSTK_SESSION(
            bind=self.dao.engine, autoflush=False, expire_on_commit=False)

    def do_insert(self, **kwargs):
        """
        Add the list of RAMSTK<MODULE> instance to the RAMSTK Program database.

        :param list entities: the list of RAMSTK<MODULE> entities to add to the
                              RAMSTK Program database.
        :return: (_error_code, _msg); the error code and associated message.
        :rtype: (int, str)
        """
        _entities = kwargs['entities']
        _session = self.dao.RAMSTK_SESSION(
            bind=self.dao.engine, autoflush=False, expire_on_commit=False)

        _error_code, _msg = self.dao.db_add(_entities, _session)

        _session.close()

        return _error_code, _msg

    def do_delete(self, node_id):
        """
        Delete the instance of RAMSTK<MODULE> from the RAMSTK Program database.

        :param int node_id entity: the ID of the RAMSTK<MODULE> record to be
                                   removed from the RAMSTK Program database.
        :return: (_error_code, _msg); the error code and associated message.
        :rtype: (int, str)
        """
        _msg = ''

        _session = self.dao.RAMSTK_SESSION(
            bind=self.dao.engine, autoflush=False, expire_on_commit=False)

        try:
            _entity = self.tree.get_node(node_id).data
            _error_code, _msg = self.dao.db_delete(_entity, _session)

            if _error_code == 0:
                self.tree.remove_node(node_id)

        except AttributeError:
            _error_code = 2005

        _session.close()

        return _error_code, _msg

    def do_update(self, node_id):
        """
        Update the RAMSTK<MODULE> instance in the RAMSTK Program database.

        :param entity: the RAMSTK<MODULE> instance to update in the RAMSTK Program
                       database.
        :return: (_error_code, _msg); the error code and associated message.
        :rtype: (int, str)
        """
        _error_code = 0
        _msg = ''

        _session = self.dao.RAMSTK_SESSION(
            bind=self.dao.engine,
            autoflush=True,
            autocommit=False,
            expire_on_commit=False)

        try:
            _entity = self.tree.get_node(node_id).data
            if _entity is not None:
                _session.add(_entity)
                _error_code, _msg = self.dao.db_update(_session)
        except AttributeError:
            _error_code = 1
            _msg = ('RAMSTK ERROR: Attempted to save non-existent '
                    'entity with Node ID {0:s}.').format(str(node_id))

        _session.close()

        return _error_code, _msg
Ejemplo n.º 47
0
# Anjie Wang
# node.py

import zmq
import time
import sys
import threading
import config
from treelib import Node, Tree

# lieutenant don't start sending msg until they receive general's cmd (flag=True)
flag = False
# tree for storing received msg
tree = Tree()
cur_sum = 0
cur_round = 0
list = []
# identity of actor
identity = ""
lock = threading.Lock()


# change the msg of "attack/retreat" if the lieutenant if faulty
def process_msg(msg):
    global identity
    if identity == "lf":
        # faulty lieutenant
        return "retreat " + msg.split(" ", 1)[1]
    else:
        return msg
Ejemplo n.º 48
0
    def __init__(self):
        self.mfest = load_manifest("../chapters.yaml")
        self.books = {}
        self.chaps = {}

        for adef in self.mfest:
            for defheader, defs in adef.items():
                if not defheader.startswith("BOOK_"):
                    self.chaps[defheader] = defs
                else:
                    self.books[defheader] = defs
        #
        # nested dict approach, not working very well
        '''
        for title, bookchaps in self.books.items():
            print ("BOOK: {title}".format(title=title))
            print ("+" * 80)
            book = {title: bookchaps}
            pprint (book)
            print ("-" * 80)
            pprint (expand_def(book, self.chaps))
            print ("*" * 80)
        #pprint(books)
        '''

        #
        # tree approach, better
        self.treechap = {}
        for title, chap in self.chaps.items():
            self.treechap[title] = create_tree({title: chap})

        self.treebook = {}
        for title, book in self.books.items():
            self.treebook[title] = create_tree({title: book})

        for title, tree in self.treebook.items():
            # tree.show()
            for node in tree.expand_tree(mode=Tree.DEPTH):
                # print ("+", node)
                realtag = node
                if type(realtag) is Node:
                    realtag = node.tag
                if "|" in realtag:
                    realtag = realtag.split("|")[1]
                if realtag.startswith("$ref:"):
                    chapkey = realtag.split("$ref:")[1]
                    newtree = Tree(tree=self.treechap[chapkey], deep=True)
                    # move up its children to replace totally the root
                    subtree = newtree.subtree(
                        newtree.children(newtree.root)[0].tag)
                    newtree = subtree
                    for anode in tree.children(node):
                        origtag = anode.tag
                        if "|" in origtag:
                            origtag = anode.tag.split("|")[1]
                        # print (origtag)
                        newtree.create_node(timestamp_node(origtag),
                                            origtag,
                                            parent=newtree.root,
                                            data=time())
                    # find parent node of the node to be replaced
                    parent = tree.parent(node)
                    # use the old timestamp data to preserve insertion order
                    newtree.get_node(
                        newtree.root).data = tree.get_node(node).data
                    # remove old node
                    tree.remove_subtree(node)
                    # replace with new expanded node
                    tree.paste(parent.identifier, newtree)
Ejemplo n.º 49
0
 def __init__(self):
     Tree.__init__(self)
Ejemplo n.º 50
0
class StepParse:
    def __init__(self):
        pass

    def load_step(self, step_filename):
        self.nauo_lines = []
        self.prod_def_lines = []
        self.prod_def_form_lines = []
        self.prod_lines = []
        self.filename = os.path.splitext(step_filename)[0]

        line_hold = ''
        line_type = ''

        # Find all search lines
        with open(step_filename) as f:
            for line in f:
                # TH: read pointer of lines as they are read, so if the file has text wrap it will notice and add it to the following lines
                index = re.search("#(.*)=", line)
                if index:
                    # TH: if not none then it is the start of a line so read it
                    # want to hold line until it has checked next line
                    # if next line is a new indexed line then save previous line
                    if line_hold:
                        if line_type == 'nauo':
                            self.nauo_lines.append(line_hold)
                        elif line_type == 'prod_def':
                            self.prod_def_lines.append(line_hold)
                        elif line_type == 'prod_def_form':
                            self.prod_def_form_lines.append(line_hold)
                        elif line_type == 'prod':
                            self.prod_lines.append(line_hold)
                        line_hold = ''
                        line_type = ''

                    prev_index = True  # TH rememeber previous line had an index
                    if 'NEXT_ASSEMBLY_USAGE_OCCURRENCE' in line:
                        line_hold = line.rstrip()
                        line_type = 'nauo'
                    elif ('PRODUCT_DEFINITION ' in line
                          or 'PRODUCT_DEFINITION(' in line):
                        line_hold = line.rstrip()
                        line_type = 'prod_def'
                    elif 'PRODUCT_DEFINITION_FORMATION' in line:
                        line_hold = line.rstrip()
                        line_type = 'prod_def_form'
                    elif ('PRODUCT ' in line or 'PRODUCT(' in line):
                        line_hold = line.rstrip()
                        line_type = 'prod'
                else:
                    prev_index = False
                    #TH: if end of file and previous line was held
                    if 'ENDSEC;' in line:
                        if line_hold:
                            if line_type == 'nauo':
                                self.nauo_lines.append(line_hold)
                            elif line_type == 'prod_def':
                                self.prod_def_lines.append(line_hold)
                            elif line_type == 'prod_def_form':
                                self.prod_def_form_lines.append(line_hold)
                            elif line_type == 'prod':
                                self.prod_lines.append(line_hold)
                            line_hold = ''
                            line_type = ''
                    else:
                        #TH: if not end of file
                        line_hold = line_hold + line.rstrip()

        self.nauo_refs = []
        self.prod_def_refs = []
        self.prod_def_form_refs = []
        self.prod_refs = []

        # TH: added 'replace(","," ").' to replace ',' with a space to make the spilt easier if there are not spaces inbetween the words'

        # Find all (# hashed) line references and product names
        # TH: it might be worth finding a different way of extracting data we do want rather than fixes to get rid of the data we don't
        for j in range(len(self.nauo_lines)):
            self.nauo_refs.append([
                el.rstrip(',')
                for el in self.nauo_lines[j].replace(",", " ").replace(
                    "=", " ").split() if el.startswith('#')
            ])
        for j in range(len(self.prod_def_lines)):
            self.prod_def_refs.append([
                el.rstrip(',') for el in self.prod_def_lines[j].replace(
                    ",", " ").replace("=", " ").split() if el.startswith('#')
            ])
        for j in range(len(self.prod_def_form_lines)):
            self.prod_def_form_refs.append([
                el.rstrip(',') for el in self.prod_def_form_lines[j].replace(
                    ",", " ").replace("=", " ").split() if el.startswith('#')
            ])
        for j in range(len(self.prod_lines)):
            self.prod_refs.append([
                el.strip(',')
                for el in self.prod_lines[j].replace(",", " ").replace(
                    "(", " ").replace("=", " ").split() if el.startswith('#')
            ])
            self.prod_refs[j].append(self.prod_lines[j].split("'")[1])

        # Get first two items in each sublist (as third is shape ref)
        #
        # First item is 'PRODUCT_DEFINITION' ref
        # Second item is 'PRODUCT_DEFINITION_FORMATION <etc>' ref
        self.prod_all_refs = [el[:2] for el in self.prod_def_refs]

        # Match up all references down to level of product name
        for j in range(len(self.prod_all_refs)):

            # Add 'PRODUCT_DEFINITION' ref
            for i in range(len(self.prod_def_form_refs)):
                if self.prod_def_form_refs[i][0] == self.prod_all_refs[j][1]:
                    self.prod_all_refs[j].append(self.prod_def_form_refs[i][1])
                    break

            # Add names from 'PRODUCT_DEFINITION' lines
            for i in range(len(self.prod_refs)):
                if self.prod_refs[i][0] == self.prod_all_refs[j][2]:
                    self.prod_all_refs[j].append(self.prod_refs[i][2])
                    break

        # Find all parent and child relationships (3rd and 2nd item in each sublist)
        self.parent_refs = [el[1] for el in self.nauo_refs]
        self.child_refs = [el[2] for el in self.nauo_refs]

        # Find distinct parts and assemblies via set operations; returns list, so no repetition of items
        self.all_type_refs = set(self.child_refs) | set(self.parent_refs)
        self.ass_type_refs = set(self.parent_refs)
        self.part_type_refs = set(self.child_refs) - set(self.parent_refs)

        # Get first two items in each sublist (as third is shape ref)
        #
        # First item is 'PRODUCT_DEFINITION' ref
        # Second item is 'PRODUCT_DEFINITION_FORMATION <etc>' ref
        self.prod_all_refs = [el[:2] for el in self.prod_def_refs]

        # Match up all references down to level of product name
        for j in range(len(self.prod_all_refs)):

            # Add 'PRODUCT_DEFINITION' ref
            for i in range(len(self.prod_def_form_refs)):
                if self.prod_def_form_refs[i][0] == self.prod_all_refs[j][1]:
                    self.prod_all_refs[j].append(self.prod_def_form_refs[i][1])
                    break

            # Add names from 'PRODUCT_DEFINITION' lines
            for i in range(len(self.prod_refs)):
                if self.prod_refs[i][0] == self.prod_all_refs[j][2]:
                    self.prod_all_refs[j].append(self.prod_refs[i][2])
                    break

        # Find all parent and child relationships (3rd and 2nd item in each sublist)
        self.parent_refs = [el[1] for el in self.nauo_refs]
        self.child_refs = [el[2] for el in self.nauo_refs]

        # Find distinct parts and assemblies via set operations; returns list, so no repetition of items
        self.all_type_refs = set(self.child_refs) | set(self.parent_refs)
        self.ass_type_refs = set(self.parent_refs)
        self.part_type_refs = set(self.child_refs) - set(self.parent_refs)
        #TH: find root node
        self.root_type_refs = set(self.parent_refs) - set(self.child_refs)
        self.create_dict()

    def show_values(self):
        # TH: basic testing, if needed these could be spilt up
        print(self.nauo_lines)
        print(self.prod_def_lines)
        print(self.prod_def_form_lines)
        print(self.prod_lines)
        print(self.nauo_refs)
        print(self.prod_def_refs)
        print(self.prod_def_form_refs)
        print(self.prod_refs)

    def create_dict(self):
        # TH: links nauo number with a name and creates dict
        self.part_dict = {}
        for part in self.all_type_refs:
            for sublist in self.prod_def_refs:
                if sublist[0] == part:
                    prod_loc = '#' + re.findall('\d+', sublist[1])[0]
                    pass
            for sublist in self.prod_def_form_refs:
                if sublist[0] == prod_loc:
                    prod_loc = '#' + str(re.findall('\d+', sublist[1])[0])
                    pass
            for sublist in self.prod_refs:
                if sublist[0] == prod_loc:
                    part_name = sublist[2]

            self.part_dict[part] = part_name

    def create_tree(self):
        #TH: create tree diagram in newick format
        #TH: find root node

        self.tree = Tree()
        #TH: check if there are any parts to make a tree from, if not don't bother
        if self.part_dict == {}:
            return

        root_node_ref = list(self.root_type_refs)[0]
        self.tree.create_node(self.part_dict[root_node_ref], 0)

        #TH: created root node now fill in next layer
        #TH: create dict for tree, as each node needs a unique name
        i = [0]  # itirates through nodes
        self.tree_dict = {}
        self.tree_dict[i[0]] = root_node_ref

        def tree_next_layer(self, parent):
            root_node = self.tree_dict[i[0]]
            for line in self.nauo_refs:
                if line[1] == root_node:
                    i[0] += 1
                    self.tree_dict[i[0]] = str(line[2])
                    self.tree.create_node(self.part_dict[line[2]],
                                          i[0],
                                          parent=parent)
                    tree_next_layer(self, i[0])

        tree_next_layer(self, 0)

    def print_tree(self):
        try:
            self.tree.show()
        except:
            self.create_tree()
            self.tree.show()

    def tree_to_json(self, save_to_file=False, filename='file', path=''):
        #TH: return json format tree, can also save to file
        if self.tree.size() != 0:
            data = self.tree.to_json()
            j = json.loads(data)
            if save_to_file == True:
                if path:
                    file_path = os.path.join(path, filename)
                else:
                    file_path = filename

                with open(file_path + '.json', 'w') as outfile:
                    json.dump(j, outfile)

            return data
        else:
            print("no tree to print")
            return
from Helper.Source import connect_to_db
Conn_Odin = connect_to_db()

# Submission of interest
AllSubmissions = pd.read_sql_query("select * from Submission_Info", Conn_Odin)
AllSubmissions[["ID_Submission", "Title"]].tail(60)
SubmissionOfInterest = "kboh0h"

Temp_Query = "select CI.ID_Comment, CI.ID_ParentID, CI.created_utc from Comment_Information CI " +\
             "where ID_Submission= '{}'".format(SubmissionOfInterest)

AllComments = pd.read_sql_query(Temp_Query, Conn_Odin)
AllComments2 = AllComments.copy()
AllComments2["created_utc"] = pd.to_datetime(AllComments2.created_utc)
AllComments2 = AllComments2.sort_values("created_utc").reset_index(drop=True)

tree1 = Tree()
tree1.create_node(identifier=SubmissionOfInterest)  # root node
for CommentIndex in range(len(AllComments2)):
    # CommentIndex=0
    ChildID = AllComments2.iloc[CommentIndex]["ID_Comment"]
    ParentID = AllComments2.iloc[CommentIndex]["ID_ParentID"]

    try:
        tree1.create_node(identifier=ChildID, parent=ParentID)
    except:
        print("An exception occurred")

tree1.show()
tree1.paths_to_leaves()
len(tree1.paths_to_leaves())
Ejemplo n.º 52
0
class AcquisitionChainIter(object):
    def __init__(self, acquisition_chain, parallel_prepare=True):
        self.__sequence_index = -1
        self._parallel_prepare = parallel_prepare
        self.__acquisition_chain_ref = weakref.ref(acquisition_chain)

        # set all slaves into master
        for master in (x for x in acquisition_chain._tree.expand_tree()
                       if isinstance(x, AcquisitionMaster)):
            del master.slaves[:]
            master.slaves.extend(
                acquisition_chain._tree.get_node(master).fpointer)

        # create iterators tree
        self._tree = Tree()
        self._root_node = self._tree.create_node("acquisition chain", "root")
        device2iter = dict()
        for dev in acquisition_chain._tree.expand_tree():
            if not isinstance(dev, (AcquisitionDevice, AcquisitionMaster)):
                continue
            dev_node = acquisition_chain._tree.get_node(dev)
            parent = device2iter.get(dev_node.bpointer, "root")
            try:
                it = iter(dev)
            except TypeError:
                one_shot = self.acquisition_chain._device2one_shot_flag.get(
                    dev, True)
                dev_iter = DeviceIterator(dev, one_shot)
            else:
                dev_iter = DeviceIteratorWrapper(it)
            device2iter[dev] = dev_iter
            self._tree.create_node(tag=dev.name,
                                   identifier=dev_iter,
                                   parent=parent)

    @property
    def acquisition_chain(self):
        return self.__acquisition_chain_ref()

    def prepare(self, scan, scan_info):
        preset_tasks = list()
        if self.__sequence_index == 0:
            preset_tasks.extend([
                gevent.spawn(preset.prepare)
                for preset in self.acquisition_chain._presets_list
            ])
            scan.prepare(scan_info, self.acquisition_chain._tree)

        self._execute("_prepare",
                      wait_between_levels=not self._parallel_prepare)

        if self.__sequence_index == 0:
            gevent.joinall(preset_tasks, raise_error=True)

    def start(self):
        if self.__sequence_index == 0:
            preset_tasks = [
                gevent.spawn(preset.start)
                for preset in self.acquisition_chain._presets_list
            ]
            gevent.joinall(preset_tasks, raise_error=True)

        self._execute("_start")

    def stop(self):
        self._execute("stop", master_to_slave=True, wait_all_tasks=True)

        preset_tasks = [
            gevent.spawn(preset.stop)
            for preset in self.acquisition_chain._presets_list
        ]

        gevent.joinall(preset_tasks)  # wait to call all stop on preset
        gevent.joinall(preset_tasks, raise_error=True)

    def next(self):
        self.__sequence_index += 1
        gevent.joinall([
            gevent.spawn(dev_iter.wait_ready)
            for dev_iter in self._tree.expand_tree() if dev_iter is not 'root'
        ],
                       raise_error=True)
        try:
            if self.__sequence_index:
                for dev_iter in self._tree.expand_tree():
                    if dev_iter is 'root':
                        continue
                    dev_iter.next()
        except StopIteration:  # should we stop all devices?
            for acq_dev_iter in (
                    x for x in self._tree.expand_tree()
                    if x is not 'root' and isinstance(x.device, (
                        AcquisitionDevice, AcquisitionMaster))):
                if hasattr(acq_dev_iter, 'wait_reading'):
                    acq_dev_iter.wait_reading()
                dispatcher.send("end", acq_dev_iter.device)
            raise
        return self

    def _execute(self,
                 func_name,
                 master_to_slave=False,
                 wait_between_levels=True,
                 wait_all_tasks=False):
        tasks = list()

        prev_level = None

        if master_to_slave:
            devs = list(self._tree.expand_tree(mode=Tree.WIDTH))[1:]
        else:
            devs = reversed(list(self._tree.expand_tree(mode=Tree.WIDTH))[1:])

        for dev in devs:
            node = self._tree.get_node(dev)
            level = self._tree.depth(node)
            if wait_between_levels and prev_level != level:
                gevent.joinall(tasks, raise_error=True)
                tasks = list()
                prev_level = level
            func = getattr(dev, func_name)
            tasks.append(gevent.spawn(func))
        # ensure that all tasks are executed
        # (i.e: don't raise the first exception on stop)
        if wait_all_tasks:
            gevent.joinall(tasks)

        gevent.joinall(tasks, raise_error=True)
def depth_cost(G: nx.Graph, T: tl.Tree):
    return T.depth()
Ejemplo n.º 54
0
def tree_build_from_dict(containers=None):
    """
    Build a tree based on a unsorted dictConfig(config).

    Build a tree of containers based on an unsorted dict of containers.

    Example:
    --------
        >>> containers = {'Fabric': {'parent_container': 'Tenant'},
            'Leaves': {'configlets': ['container_configlet'],
                        'devices': ['veos01'],
                        'images': ['4.22.0F'],
                        'parent_container': 'Fabric'},
            'MLAG01': {'configlets': ['container_configlet'],
                        'devices': ['veos01'],
                        'images': ['4.22.0F'],
                        'parent_container': 'Leaves'},
            'MLAG02': {'configlets': ['container_configlet'],
                        'devices': ['veos01'],
                        'images': ['4.22.0F'],
                        'parent_container': 'Leaves'},
            'Spines': {'configlets': ['container_configlet'],
                        'devices': ['veos01'],
                        'images': ['4.22.0F'],
                        'parent_container': 'Fabric'}}
        >>> print(tree_build_from_dict(containers=containers))
            {"Tenant": {"children": [{"Fabric": {"children": [{"Leaves": {"children": ["MLAG01", "MLAG02"]}}, "Spines"]}}]}}
    Parameters
    ----------
    containers : dict, optional
        Container topology to create on CVP, by default None
    
    Returns
    -------
    json
        tree topology
    """
    # Create tree object
    tree = Tree()  # Create the base node
    previously_created = list()
    # Create root node to mimic CVP behavior
    tree.create_node("Tenant", "Tenant")
    # Iterate for first level of containers directly attached under root.
    for container_name, container_info in containers.items():
        if container_info['parent_container'] in ['Tenant']:
            previously_created.append(container_name)
            tree.create_node(container_name,
                             container_name,
                             parent=container_info['parent_container'])
    # Loop since expected tree is not equal to number of entries in container topology
    while len(tree.all_nodes()) < len(containers) + 1:
        for container_name, container_info in containers.items():
            if tree.contains(
                    container_info['parent_container']
            ) and container_info['parent_container'] not in ['Tenant']:
                try:
                    tree.create_node(container_name,
                                     container_name,
                                     parent=container_info['parent_container'])
                except:
                    continue
    return tree.to_json()
Ejemplo n.º 55
0
#!/usr/bin/env python
import click
import hashlib
from treelib import Node, Tree

merkle_tree = Tree()
merkle_tree.create_node('root', 'root')


def merkle(hashList):
    """
    Calculate the Merkle Root recursively
    """

    # If a single name then return the hash
    if len(hashList) == 1:
        return hashList[0]

    newHashList = []
    # Process pairs. For odd length, the last is skipped
    for i in range(0, len(hashList) - 1, 2):
        new_node = hash2(hashList[i], hashList[i + 1])
        newHashList.append(new_node)

    # If odd, hash last item twice
    if len(hashList) % 2 == 1:
        odd_node = hash2(hashList[-1], hashList[-1])
        newHashList.append(odd_node)

    return merkle(newHashList)
Ejemplo n.º 56
0
 def setUp(self):
     tree = Tree()
     tree.create_node("Hárry", "hárry")
     tree.create_node("Jane", "jane", parent="hárry")
     tree.create_node("Bill", "bill", parent="hárry")
     tree.create_node("Diane", "diane", parent="jane")
     tree.create_node("George", "george", parent="bill")
     # Hárry
     #   |-- Jane
     #       |-- Diane
     #   |-- Bill
     #       |-- George
     self.tree = tree
     self.copytree = Tree(self.tree, True)
Ejemplo n.º 57
0
class TreeT(object):
    def __init__(self, max_id=0):
        self.tree = Tree()

    def from_ptb_to_tree(self, line, max_id=0, leaf_id=1, parent_id=None):
        # starts by ['(', 'pos']
        pos_tag = line[1]
        if parent_id is None:
            pos_id = 0
        else:
            pos_id = max_id
            max_id += 1

        self.tree.create_node(pos_tag, pos_id, parent_id, TreeData())

        parent_id = pos_id
        total_offset = 2

        if line[2] != '(':
            # sub-tree is leaf
            # line[0:3] = ['(', 'pos', 'word', ')']
            word_tag = line[2]
            self.tree.create_node(word_tag, leaf_id, parent_id, TreeData())
            return 4, max_id, leaf_id + 1

        line = line[2:]

        while line[0] != ')':
            offset, max_id, leaf_id = self.from_ptb_to_tree(
                line, max_id, leaf_id, parent_id)
            total_offset += offset
            line = line[offset:]

        return total_offset + 1, max_id, leaf_id

    def add_height(self, tree_dep):

        for n in self.tree.all_nodes():
            n.data.leaves = []

        for leaf in self.tree.leaves():
            lid = leaf.identifier
            hid = tree_dep[lid]
            if hid == self.tree.root:
                self.tree[lid].data.height = self.tree.depth(self.tree[lid])
                for cid in [
                        p for p in self.tree.paths_to_leaves() if lid in p
                ][0]:
                    self.tree[cid].data.leaves += [lid]
            else:
                height = -1
                cid = lid
                cond = True
                while cond:
                    self.tree[cid].data.leaves += [lid]
                    height += 1
                    cid = self.tree.parent(cid).identifier
                    cid_leaves = [l.identifier for l in self.tree.leaves(cid)]
                    cid_l_dep = [tree_dep[l] for l in cid_leaves if l != lid]
                    cond = set(cid_l_dep).issubset(set(cid_leaves))
                self.tree[lid].data.height = height

        x_nodes = [
            n.identifier for n in self.tree.all_nodes() if n.data.leaves == []
        ]
        for x_node in x_nodes[::-1]:
            min_id = min(self.tree.children(x_node),
                         key=lambda c: c.data.height)
            _lid = min_id.data.leaves[0]
            self.tree[_lid].data.height += 1
            self.tree[x_node].data.leaves += [_lid]

        return True

    def _from_tree_to_ptb(self, nid):
        nid = self.tree.subtree(nid).root
        if self.tree[nid].is_leaf():
            return ' (' + self.tree[nid].tag + ' ' + self.tree[
                nid].data.word + ')'

        res = ' (' + self.tree[nid].tag

        for c_nid in sorted(self.tree.children(nid),
                            key=lambda x: x.identifier):
            res += self._from_tree_to_ptb(c_nid.identifier)

        return res + ')'

    def from_tree_to_ptb(self):
        return self._from_tree_to_ptb(self.tree.root)

    def from_tag_to_tree(self, tag, word, pos_id=0):
        parent_id = None
        for tag_nodes in tag:
            if tag_nodes[0] in [CL, CR]:
                c_side = tag_nodes[0]
                _tag_nodes = tag_nodes[1:] if len(tag_nodes) > 1 else ['']
            else:
                c_side = ''
                _tag_nodes = tag_nodes
            self.tree.create_node(_tag_nodes[0],
                                  pos_id,
                                  parent=parent_id,
                                  data=TreeData(comb_side=c_side))

            parent_id = pos_id
            pos_id += 1
            for tag_node in _tag_nodes[1:]:
                self.tree.create_node(tag_node[1:],
                                      pos_id,
                                      parent=parent_id,
                                      data=TreeData(miss_side=tag_node[0]))
                pos_id += 1
        for l in self.tree.leaves():
            if l.data.miss_side == '':
                l.data.word = word
                break
        return pos_id

    @memoize
    def is_combine_to(self, side):
        return self.tree[self.tree.root].data.comb_side == side

    @memoize
    def is_combine_right(self):
        return self.is_combine_to(CR)

    @memoize
    def is_combine_left(self):
        return self.is_combine_to(CL)

    @memoize
    def is_complete_tree(self):
        return all([n.data.miss_side == '' for n in self.tree.all_nodes()])

    @memoize
    def get_missing_leaves_to(self, miss_val, side):
        return [
            l.identifier for l in self.tree.leaves(self.tree.root)
            if l.data.miss_side == side and l.tag == miss_val
        ]

    @memoize
    def get_missing_leaves_left(self, miss_val):
        return self.get_missing_leaves_to(miss_val, L)

    @memoize
    def get_missing_leaves_right(self, miss_val):
        return self.get_missing_leaves_to(miss_val, R)

    @memoize
    def root_tag(self):
        return self.tree[self.tree.root].tag

    @memoize
    def is_no_missing_leaves(self):
        return all(
            [l.data.miss_side == '' for l in self.tree.leaves(self.tree.root)])

    @memoize
    def combine_tree(self, _tree, comb_leaf):
        self.tree.paste(comb_leaf, _tree.tree)
        self.tree.link_past_node(comb_leaf)
        return self

    def tree_to_path(self, nid, path):

        # Stop condition
        if self.tree[nid].is_leaf():
            path[nid] = []
            return nid, self.tree[nid].data.height

        # Recursion
        flag = CR
        for child in self.tree.children(nid):
            cid = child.identifier
            leaf_id, height = self.tree_to_path(cid, path)

            if (height == 0):
                # Reached end of path can add flag
                path[leaf_id].insert(0, flag)
                # path[leaf_id].append(flag)

            if height > 0:
                path[leaf_id].insert(0, nid)
                # only single child will have height>0
                # and its value will be the one that is returned
                # to the parent
                ret_leaf_id, ret_height = leaf_id, height - 1

                # once we reached a height>0, it means that
                # this path includes the parent, and thus flag
                # direction should flip
                flag = CL

        return ret_leaf_id, ret_height

    def path_to_tags(self, path):
        tags = []
        for p in path:
            _res = []
            _p = copy.copy(p)
            if _p[0] in [CL, CR]:
                _res.append(_p[0])
                _p = _p[1:]
            while _p[:-1]:
                el_p = _p.pop(0)
                _res.append(self.tree[el_p].tag)
                for c in self.tree.children(el_p):
                    if c.identifier != _p[0]:
                        _res.append(R + c.tag if c.identifier > _p[0] else L +
                                    c.tag)
            _res.append(self.tree[_p[0]].tag)
            tags.append(_res)
        return tags

    def path_to_words(self, path):
        return [self.tree[k].tag for k in path]

    def from_tree_to_tag(self):
        path = {}
        self.tree_to_path(self.tree.root, path)
        return {
            'tags': self.path_to_tags(path.values()),
            'words': self.path_to_words(path.keys())
        }

    def from_ptb_to_tag(self, line, max_id, depend):
        self.from_ptb_to_tree(line, max_id)
        self.add_height(depend)
        path = {}
        self.tree_to_path(self.tree.root, path)
        return self.path_to_tags(path.values())
Ejemplo n.º 58
0
def testUnjsonify():
    tree = Tree()
    tree.create_node('home', 'home')
    tree.create_node('phone', 'phone', parent='home')
    tree.create_node('laptop', 'laptop', parent='home')
    tree.create_node('screen', 'screen', parent='laptop')
    tree.create_node(19, 19, parent='home')
    tree.create_node((1, 2), (1, 2), parent='screen')
    j = tree.to_json()
    unjsonify(j).show()
Ejemplo n.º 59
-1
 def test_subtree(self):
     subtree_copy = Tree(self.tree.subtree("jane"), deep=True)
     self.assertEqual(subtree_copy.parent("jane") is None, True)
     subtree_copy["jane"].tag = "Sweeti"
     self.assertEqual(self.tree["jane"].tag == "Jane", True)
     self.assertEqual(subtree_copy.level("diane"), 1)
     self.assertEqual(subtree_copy.level("jane"), 0)
     self.assertEqual(self.tree.level("jane"), 1)