def collectPathInfo(ingore_list, clean, data): name, versions = data n_name = Node(name) if name in ingore_list and clean: n_name.name = f"{n_name.name} (ignored)" return n_name all_size = 0 for vid, paths in versions.items(): n_vid = Node(vid, parent=n_name) vid_size = 0 for p in paths: if os.path.isfile(p) or os.path.isdir(p): file_size = getPathSize(p) if FLAGS.show_stat else 0 vid_size += file_size file_size = f"{file_size}Mb" else: file_size = "--- not found" n_path = Node(f"{file_size} [{p}]", parent=n_vid) n_vid.name = f"{n_vid.name} ({vid_size}Mb)" all_size += vid_size n_name.name = f"{n_name.name} ({all_size}Mb)" return n_name
def generate_tree(node, n, inc, dec, prob_inc, prob_dec): nod1 = Node(root.name, node, prob=1) # generate leaves nod2 = Node(root.name, node, prob=1) nod1.name = nod1.parent.name + variation(nod1.parent.name, inc) # variation on the price nod2.name = nod2.parent.name - variation(nod1.parent.name, dec) nod1.__setattr__("prob", (nod1.parent.__getattribute__("prob") * prob_inc)) # chance of reaching the leaf nod2.__setattr__("prob", (nod2.parent.__getattribute__("prob") * prob_dec)) if n != 0: generate_tree(nod1, n - 1, inc, dec, prob_inc, prob_dec) # recursive branches generation generate_tree(nod2, n - 1, inc, dec, prob_inc, prob_dec)
def Con_to_Dep(tree): ROOT = Node("ROOT") VERB = Node("Not filled", parent=ROOT) for subtree in tree[0]: if subtree.label() == 'VP': VERB_temp = VP_subtree(subtree) VERB.name = VERB_temp.children[0].name while len(VERB_temp.children[0].children) > 0: VERB_temp.children[0].children[0].parent = VERB elif subtree.label()[:2] == 'NP' or subtree.label() == 'WHNP': NSUBJ = Node("NSUBJ", parent=VERB) NSUBJ_value = NP_subtree(subtree)[1] NSUBJ_value.parent = NSUBJ elif subtree.label() == '.': PUNCT = Node("PUNCT", parent=VERB) PUNCT_value = Node(subtree.label() + " " + subtree[0]) PUNCT_value.parent = PUNCT elif subtree.label() == 'NNP': AGENT = Node("AGENT", parent=ROOT) AGENT_value = Node(subtree.label() + " " + subtree[0]) AGENT_value.parent = AGENT return ROOT
def parseStructureText(self, parentElement): currentDepth = 0 rootNode = Node('root', parent=None) currentNode = rootNode for element in parentElement.iter(): tag = element.tag if tag == 'TM': depth = int(element.attrib['niv']) if depth == currentDepth + 1: currentNode = Node('', parent=currentNode) else: for _ in range(abs(depth - currentDepth) + 1): currentNode = currentNode.parent newNode = Node('', parent=currentNode) currentNode = newNode currentDepth = depth elif tag == 'TITRE_TM': currentNode.name = element.text elif tag == 'LIEN_TXT': Node('', parent=currentNode, idtxt=element.get('idtxt'), titre=element.get('titretxt')) self.structTree = rootNode self.parsedTree = self.recursiveParseNode(self.structTree)
def _distribute(node: Node) -> None: """Distribute sums and products until no further distribution possible. Works inplace on provided node. This method will create a structure that might contain a `product_1` of a `product_2` and a `kernel`. This if the same as a product of the kernels contained in `product_2` and the `kernel`. `distribute` merges these in the end. Parameters ---------- node: Node Node of the AST of a kernel that potentially contains distributable products or sums. """ if node.is_leaf: return if node.name is gpflow.kernels.Product: # Search on own level (only `node`) and on children, frist result will be distributed. sum_to_distribute = [ child for child in node.children if child.name is gpflow.kernels.Sum ] if sum_to_distribute: sum_to_distr = sum_to_distribute[0] children_to_distribute_to = [ child for child in node.children if child is not sum_to_distr ] node.name = gpflow.kernels.Sum node.full_name = 'Sum' node.children = [] for child in sum_to_distr.children: new_prod = Node(gpflow.kernels.Product, full_name='Product', parent=node) new_kids = [ deepcopy(child) for child in children_to_distribute_to ] if child.name is gpflow.kernels.Product: # Child to distribute to is a `Product`, doing nothing would lead to two nested products. new_kids.extend( [deepcopy(child) for child in child.children]) else: new_kids += [child] for kid in new_kids: kid.parent = new_prod for child in node.children: _distribute(child)
def type_decl(Line, n): temp_str = "Variable type: " #built-in or typedef-defined type if "IdentifierType" in str(Line.type): for type in Line.type.names: temp_str += str(type) definition = Node(temp_str, parent=n) name = Node("Name: " + str(Line.declname), parent=n) return #enum elif "Enum" in str(Line.type): temp_str += "enum " + str(Line.type.name) # AST for values of ENUM definition = Node(temp_str, parent=n) name = Node("Name: " + str(Line.declname), parent=n) values = Node("Values", parent=n) # for each value (since it's practically an operand) an AST is created if "EnumeratorList" in str(Line.type.values): for value in Line.type.values.enumerators: ni = Node(value.name, parent=values) if not "None" in str(value.value): ast_value(value.value, ni) elif ("Struct" in str(Line.type)) or ("Union" in str(Line.type)): definition = Node(temp_str + "struct " + str(Line.type.name), parent=n) if "Union" in str(Line.type): definition.name = temp_str + "union " + str(Line.type.name) name = Node("Name: " + str(Line.declname), parent=n) # create ast for fields n_decls = Node("Fields", parent=n) if not "None" in str(Line.type.decls): for decl in Line.type.decls: if not "None" in str(decl): if_decl(decl, n_decls) elif "Decl" in str(Line.type): n_decl_name = "" if "PtrDecl" in str(Line.type): n_decl_name = "PtrDecl" elif "TypeDecl" in str(Line.type): n_decl_name = "TypeDecl" elif "ArrayDecl" in str(Line.type): n_decl_name = "ArrayDecl" n_decl = Node(n_decl_name + " ", parent=n) type_decl(Line.type, n_decl) return
def _replace_white_products(node: Node) -> None: """Substitute all product parts in a kernel that include stationary and `white` kernels by a `white` kernel. Only replaces product parts that are `white` or stationary: ``` replace_white_products('white * white * rbf * linear') -> 'white * linear' ``` Works inplace on provided node. Parameters ---------- node: Node Node of the AST of a kernel that could contain `white` products. """ if node.is_leaf: return if node.name is gpflow.kernels.Product: white_children = [ child for child in node.children if child.name is gpflow.kernels.White ] if white_children: non_stationary_children = [ child for child in node.children if child.name in [gpflow.kernels.Linear, gpflow.kernels.Polynomial] ] new_kids = [white_children[0]] + non_stationary_children if len(new_kids) == 1: if node.is_root: node.name = new_kids[0].name node.full_name = new_kids[0].full_name else: new_kids[0].parent = node.parent node.parent = None node.children = [] else: node.children = new_kids for child in node.children: _replace_white_products(child)
def _merge_rbfs(node: Node) -> None: """Merge RBFs that are part of one product. Works inplace on provided node. Parameters ---------- node: Node Node of the AST of a kernel that potentially contains non-merged RBFs. """ if node.is_leaf: return if node.name is gpflow.kernels.Product: rbf_children = [ child for child in node.children if child.name is gpflow.kernels.RBF ] other_children = [ child for child in node.children if child.name is not gpflow.kernels.RBF ] new_kids = other_children + rbf_children[:1] if len(new_kids) == 1: if node.is_root: node.name = new_kids[0].name try: node.full_name = new_kids[0].full_name except AttributeError: pass else: new_kids[0].parent = node.parent node.parent = None node.children = [] else: node.children = new_kids for child in node.children: _merge_rbfs(child)
def _tree_to_kernel(node: Node): if node.is_leaf: return node.name(1) return node.name([_tree_to_kernel(child) for child in node.children])
def visit_Call(self, node: ast.Call) -> Any: function_name = None if isinstance(node.func, ast.Attribute): function_name = node.func.value.id elif isinstance(node.func, ast.Name): function_name = node.func.id is_rank = False # 랭크함수가 있는지 확인 if function_name == "rank": is_rank = True parent_dependency_node = Node(function_name, self.function_dependency_stack[0]) self.function_dependency_stack.appendleft(parent_dependency_node) if function_name in built_in_function_list: fun = built_in_function_list[built_in_function_list.index( function_name)] args = list() callable_function = None if fun.module == "language": callable_function = getattr(language, function_name) elif fun.module == "indicator": callable_function = getattr(indicator, function_name) elif fun.module == "function": callable_function = getattr(function, function_name) else: raise Exception() function_spec = inspect.getfullargspec(callable_function) function_parameter_name_list = function_spec[0] function_annotation_list = function_spec.annotations for index, argument in enumerate(node.args): parameter_name = function_parameter_name_list[index] parameter_type = function_annotation_list[parameter_name] is_enum = isinstance(parameter_type, enum.EnumMeta) if is_enum: enum_node = ast.copy_location( Attribute( Attribute(Name("parameter"), get_enum_name(parameter_type)), argument.id), argument) args.append(enum_node) elif isinstance(argument, ast.Name): if argument.id in Field.__members__: self.fields.add(Field.__members__[argument.id]) arg = ast.Constant(argument.id, argument.id) else: raise UnDefinedFieldException(node.end_lineno, argument.id) # Series 참조로 변환 # 예) close -> df['close'] args.append(to_field(arg)) elif isinstance(argument, ast.Constant) and isinstance( argument.value, int): # 정수형 파라미터는 그대로 전달 args.append(argument) elif isinstance(argument, ast.Call): args.append(self.visit_Call(argument)) if fun.en_name == "ts_delay" and self.max_ts_delay < node.args[ 1].value: self.max_ts_delay = node.args[1].value node = make_function_call(fun.module, fun.en_name, args, node) else: pass self.function_dependency_stack.popleft() inner_function = InnerFunction(node, is_rank) parent_dependency_node.name = inner_function.name self.functions[inner_function.name] = inner_function return to_field(make_constant(inner_function.name))
print(participants) f = Node("f") b = Node("b", parent=f) c = Node("c", parent=b) d = Node("d", parent=b) e = Node("e", parent=f) h = Node("h", parent=e) g = Node("g", parent=e) #print(f.height) res = findall_by_attr(f, "b") res1 = res[0] b.name = "z" f.leaves #print (b.name) participant4 = Participant("12345", "004") participant5 = Participant("12345", "004") part_perm = dict() part_perm[participant4] = PermissionTypesEnum.PUBLISH.value part_perm[participant5] = PermissionTypesEnum.PUBLISH_AND_SUBSCRIBE.value del part_perm[participant5] for part_perm_key, part_perm_value in part_perm.items(): print(part_perm_key.pairwise_key) print(part_perm_key.participant_id)
def NP_subtree(tree): NP = Node("NP") # Head_NP = Node(root_node.text+" "+root_node.tag_,parent=NP) if tree.n_lefts > 0: for token in tree.lefts: if token.dep_ == 'det': det_Value = Node(token.text + " " + token.tag_, parent=NP) if token.dep_ == 'poss': det_Value = Node(token.text + " " + token.tag_, parent=NP) if token.dep_ == 'amod': ADJP_value = amod_subtree(token) if len(ADJP_value.children) > 1: ADJP_value.parent = NP else: ADJP_value.children[0].parent = NP if token.dep_ == 'compound': Compound_Value = Node(token.text + " " + token.tag_, parent=NP) if tree.text == 'who' or tree.text == 'where' or tree.text == 'what': NP.name = "WHNP" Head_NP = Node(tree.text + " " + tree.tag_, parent=NP) if tree.n_rights > 0: for token in tree.rights: if token.dep_ == 'cc': CC_value = Node(token.text + " " + token.tag_, parent=NP) if token.dep_ == 'conj': Conj_value = NP_subtree(token) if len(Conj_value.children) <= 1: Conj_value.children[0].parent = NP ####Check if subtrees joined by CONJ else: Conj_joined = 0 for child in Conj_value.children: if child.name.split()[1] == 'CC': Conj_joined = 1 if Conj_joined == 1: while len(Conj_value.children) > 0: Conj_value.children[0].parent = NP else: Conj_value.parent = NP if token.dep_ == 'punct': Punct_value = punct_subtree(token) Punct_value.parent = NP if token.dep_ == 'appos': Appos_value = appos_subtree(token) Appos_value.parent = NP if token.dep_ == 'relcl': S_bar = Dep_to_Con(token) S_bar.parent = NP S_bar.name = "SBAR" return NP
def VP_subtree(tree): VERB = Node("VERB") Sub_VP_exists = 0 for subtree in tree: if subtree.label() == 'VP': Sub_VP_exists = 1 return VP_subtree(subtree) if Sub_VP_exists == 0: ##Find main verb verb_index = -1 for subtree in range(0, len(tree)): if tree[subtree].label()[:2] == 'VB': verb_index = subtree Main_Verb = Node(tree[verb_index].label() + " " + tree[verb_index][0], parent=VERB) for subtree in range(0, len(tree)): if subtree == verb_index: pass elif tree[subtree].label() == 'PP': PREP = Node("PREP", parent=VERB.children[0]) PREP_value = PP_subtree(tree[subtree]) PREP_value.parent = PREP elif tree[subtree].label()[-3:] == 'TMP': NPADVMOD = Node("NPADVMOD", parent=VERB.children[0]) NPADVMOD_value = NP_subtree(tree[subtree])[1] NPADVMOD_value.parent = NPADVMOD elif tree[subtree].label()[:2] == 'NP': DOBJ = Node("DOBJ", parent=VERB.children[0]) DOBJ_value = NP_subtree(tree[subtree])[1] DOBJ_value.parent = DOBJ elif tree[subtree].label() == 'ADVP': ADVMOD = Node("ADVMOD", parent=VERB.children[0]) ADVMOD_value = ADVP_subtree(tree[subtree]) ADVMOD_value.parent = ADVMOD elif tree[subtree].label() == 'SBAR': New_Tree = Tree('S', tree[subtree][1]) CCOMP_value = Con_to_Dep(Tree('ROOT', [New_Tree])).children[0] CCOMP = Node("CCOMP") VERB_temp = Node("", parent=CCOMP) VERB_temp.name = CCOMP_value.name if tree[subtree][0].label()[:2] == 'WH': NSUBJ_value = NP_subtree(tree[subtree][0])[1] NSUBJ = Node("NSUBJ", parent=VERB_temp) NSUBJ_value.parent = NSUBJ CCOMP.name = 'ADVCL' else: MARK_value = Node(tree[subtree][0].label() + " " + tree[subtree][0][0]) MARK = Node("MARK", parent=VERB_temp) MARK_value.parent = MARK for child in CCOMP_value.children: child.parent = VERB_temp CCOMP.parent = VERB.children[0] else: pass return VERB
def NP_subtree(tree): Max_value = -1 Sub_NP_NX_exist = 0 NP_NX_list = [] for subtree in tree: if subtree.label() == 'NP' or subtree.label() == 'NX' or subtree.label( ) == 'WHNP': Sub_NP_NX_exist = 1 output = NP_subtree(subtree) NP_NX_list.append((output[0], output[1])) elif subtree.label() == 'ADJP': pass elif subtree.label() == 'SBAR': NSUBJ_value = NP_subtree(subtree[0])[1] New_Tree = Tree('S', subtree[1]) RELCL_value = Con_to_Dep(Tree('ROOT', [New_Tree])).children[0] RELCL = Node("RELCL") VERB_temp = Node("", parent=RELCL) VERB_temp.name = RELCL_value.name NSUBJ = Node("NSUBJ", parent=VERB_temp) NSUBJ_value.parent = NSUBJ for child in RELCL_value.children: child.parent = VERB_temp NP_NX_list.append(RELCL) else: New_Node = Node(subtree.label() + " " + subtree[0]) NP_NX_list.append(New_Node) if Sub_NP_NX_exist == 1: Max_index = 0 Final_List = [] for item in range(0, len(NP_NX_list)): if isinstance(NP_NX_list[item], tuple): if NP_NX_list[item][0] > Max_value: Max_value = NP_NX_list[item][0] Max_index = item Final_List.append(NP_NX_list[item][1]) else: Final_List.append(NP_NX_list[item]) NSUBJ_value = Include_Dependency(Final_List, Max_index) else: Max_index = 0 for item in range(0, len(NP_NX_list)): if NP_NX_list[item].name.split()[0][:2] == 'NN': Max_index = item NSUBJ_value = Include_Dependency(NP_NX_list, Max_index) return (Max_value + 1, NSUBJ_value)
def ast_compound(Compound, parent_node, prev_node): value_list = [ "BinaryOp", "UnaryOp", "ID", "Constant", "Typename", "FuncCall", "Cast", "Assignment", "ArrayRef" ] v_l_temp = [ "StructRef", "NamedInitializer", "InitList", "Case", "Default", "CompoundLiteral", "Break", "Continue", "Return", "TernaryOp" ] value_list.extend(v_l_temp) for Line in Compound: if any(word in str(Line) for word in value_list): ast_value(Line, parent_node) #something is being declared in this line elif "Decl" in str(Line): if_decl(Line, parent_node) elif ("Compound" in str(Line)) and not ("CompoundLiteral" in str(Line)): n = Node("Compound", parent=parent_node) if not "None" in str(Line.block_items): ast_compound(Line.block_items, n, n) elif "Switch" in str(Line): n = Node("Switch", parent=parent_node) n_cond = Node("Condition", parent=n) ast_value(Line.cond, n_cond) if ("Case" in str(Line.stmt)) or ("Default" in str(Line.stmt)): ast_value(Line.stmt, n) elif ("Compound" in str( Line.stmt)) and not ("CompoundLiteral" in str(Line.stmt)): for case in Line.stmt.block_items: ast_value(case, n) elif "If" in str(Line): n = Node("If", parent=parent_node) n_cond = Node("Condition", parent=n) ast_value(Line.cond, n_cond) n_true = Node("IfTrue", parent=n) n_false = Node("IfFalse", parent=n) if not "None" in str(Line.iftrue): ast_compound([Line.iftrue], n_true, n_true) else: n_true_none = Node("None", parent=n_true) if not "None" in str(Line.iffalse): ast_compound([Line.iffalse], n_false, n_false) else: n_false_none = Node("None", parent=n_false) elif "Label" in str(Line): n = Node("Label") label_name_node = Node("Name: " + str(Line.name), parent=n) if "Label" in str(Line.stmt): if_label(Line.stmt, parent_node, n, n) elif not "None" in str(Line.stmt): ast_compound([Line.stmt], parent_node, n) none_node = Node("Last label in row", parent=n) n.parent = parent_node.children[len(parent_node.children) - 1] elif "Goto" in str(Line): n = Node("Goto", parent=parent_node) name = Node("Name: " + str(Line.name), n) elif ("DoWhile" in str(Line)) or (("While" in str(Line)) and not ("Do" in str(Line))): n = Node("While", parent=parent_node) if "DoWhile" in str(Line): n.name = "DoWhile" n_cond = Node("Condition", parent=n) ast_value(Line.cond, n_cond) if ("Compound" in str( Line.stmt)) and not ("CompoundLiteral" in str(Line.stmt)): ast_value(Line.stmt, n) elif not "None" in str(Line.stmt): ast_compound([Line.stmt], n, n) elif "For" in str(Line): n = Node("For", parent=parent_node) n_init = Node("Init", parent=n) if "DeclList" in str(Line.init): ast_compound(Line.init.decls, n_init, n_init) elif "ExprList" in str(Line.init): for expr in Line.init.exprs: ast_value(expr, n_init) elif not "None" in str(Line.init): ast_value(Line.init, n_init) # n_cond = Node("Condition", parent=n) if "ExprList" in str(Line.cond): for expr in Line.cond.exprs: ast_value(expr, n_cond) elif not "None" in str(Line.cond): ast_value(Line.cond, n_cond) # n_next = Node("Next", parent=n) if "ExprList" in str(Line.next): for expr in Line.next.exprs: ast_value(expr, n_next) elif not "None" in str(Line.next): ast_value(Line.next, n_next) # if ("Compound" in str( Line.stmt)) and not ("CompoundLiteral" in str(Line.stmt)): ast_value(Line.stmt, n) elif not "None" in str(Line.stmt): ast_compound([Line.stmt], n, n) elif "EmptyStatement" in str(Line): n = Node("Empty Statement;", parent=parent_node) #this is needed so that if instruction is different from compared one only in having label before it #distance would increase only by 1 if (prev_node.name == ";") or (prev_node.name == "Compound"): for child in parent_node.children: if not "Label" in str(child.children): placeholder_label_node = Node("Label", parent=child) label_name_node = Node("None", parent=placeholder_label_node) none_node = Node("Last label in row", parent=placeholder_label_node) return
def if_decl(Line, parent_node): # array is being declared if "ArrayDecl" in str(Line.type): n = Node("ArrayDecl", parent=parent_node) # create subtree of quals quals(Line, n) # calling a function that will build a tree with type qualifiers and typename if "Decl" in str(Line.type.type): type_decl(Line.type, n) # name = Node(str(Line.name), parent=n) # this fragment creates a tree for dimension of array dimension = Node("Dimension: ", parent=n) if not "None" in str(Line.type.dim): ast_value(Line.type.dim, dimension) # A node that contains dimension qualifiers dim_qualfs = Node("Dimension qualifiers: ", parent=n) if "const" in Line.type.dim_quals: dim_qualfs.name += "const " if "volatile" in Line.type.dim_quals: dim_qualfs.name += "volatile " if "restrict" in Line.type.dim_quals: dim_qualfs.name += "restrict " if "static" in Line.type.dim_quals: dim_qualfs.name += "static " # An AST for initialization list of array if ".Decl" in str(Line): n_initlist = Node("InitList", parent=n) if "InitList" in str(Line.init): for initializer in Line.init.exprs: if not "None" in str(initializer): ast_value(initializer, n_initlist) elif not "None" in str(Line.init): n_initlist.name = "Init" ast_value(Line.init, n_initlist) # variable is being declared elif "TypeDecl" in str(Line.type): n = Node("TypeDecl", parent=parent_node) # create subtree of quals quals(Line, n) # calling a function that will build an AST with type, type qualifiers and variable name type_decl(Line.type, n) # an AST for initializier of a declared variable if ".Decl" in str(Line): n_init = Node("Init", parent=n) if "InitList" in str(Line.init): n_init.name = "InitList" for initializer in Line.init.exprs: if not "None" in str(initializer): ast_value(initializer, n_init) elif not "None" in str(Line.init): ast_value(Line.init, n_init) # enum declaration elif "Enum" in str(Line.type): n = Node("Enum", parent=parent_node) # create subtree of quals quals(Line, n) # Node - name of enum type being declared name = Node("Name: " + str(Line.type.name), parent=n) # AST for values of ENUM values = Node("Values", parent=n) # for each value (since it's practically an operand) an AST is created if not ("None" in str(Line.type.values)) and not ("None" in str( Line.type.values.enumerators)): for value in Line.type.values.enumerators: ni = Node(value.name, parent=values) if not "None" in str(value.value): ast_value(value.value, ni) # pointer declaration elif "PtrDecl" in str(Line.type): n = Node("PtrDecl", parent=parent_node) # create subtree of quals quals(Line, n) # calling a function that will build an AST with type, type qualifiers and variable name if "Decl" in str(Line.type.type): type_decl(Line.type, n) # an AST for initializier of a declared variable if ".Decl" in str(Line): n_init = Node("Init", parent=n) if "InitList" in str(Line.init): n_init.name = "InitList" for initializer in Line.init.exprs: if not "None" in str(initializer): ast_value(initializer, n_init) elif not "None" in str(Line.init): ast_value(Line.init, n_init) #struct or union declaration elif ("Struct" in str(Line.type)) or ("Union" in str(Line.type)): n = Node("Struct", parent=parent_node) if "Union" in str(Line.type): n.name = "Union" # create subtree of quals quals(Line, n) #create ast for fields n_decls = Node("Fields", parent=n) if not "None" in str(Line.type.decls): for decl in Line.type.decls: if not "None" in str(decl): if_decl(decl, n_decls) return