def build_tree(self): print("Building product tree for " + str(len(self.data)) + " products") if os.path.exists('trees/' + str(self.num_rows)): return self.load_tree(self.num_rows) """ Constructing the tree structure with unique data from each column, the hierarchy goes like : - Products |- Department |- Commodity-Desc |- SubCommodity-Desc |- Size of the product |- Brand name |- Product ID """ r = Resolver('name') for row in self.data.iterrows(): row = row[1] for index, node_name in enumerate(row): if index == 0: # The first index will be attached to the root node try: # If we find the child, we do nothing r.get(self.tree, node_name) except ChildResolverError: # If we don't (that raises an exception), we add the child to the root Node(node_name, self.tree) else: parent = r.get(self.tree, self.get_path_for_index_at_row(row, index)) Node(node_name, parent=parent) self.save_tree(self.num_rows) return self
def __init__(self, texture_info_list): self.walker = Walker() r = Resolver('name') id = 0 self.root = TreeNode('Root', id) id += 1 for info in texture_info_list: respath = info.respath cur_parent = self.root cur_parent.filesize.bytes += info.filesize.bytes cur_node = None for chunk in respath.chunks: try: cur_node = r.get(cur_parent, chunk) cur_parent = cur_node except ChildResolverError: cur_node = TreeNode(chunk, id, cur_parent) cur_parent = cur_node id += 1 if chunk == respath.chunks[-1]: cur_node.horizontal_desc = str(info) finally: cur_node.filesize.bytes += info.filesize.bytes
def createModel(self): self.decisionTreeModel = np.empty([self.numBins, self.numBins]) for i in range(0, self.numBins): for j in range(0, self.numBins): r = Resolver() query = "Feature1/bin " + str(i + 1) + "/Feature2/bin " + str( j + 1) + "/*" try: prediction = r.glob(self.decisionTree, query)[0].name except: try: query = "Feature2/bin " + str( j + 1) + "/Feature1/bin " + str(i + 1) + "/*" prediction = r.glob(self.decisionTree, query)[0].name except: try: query = "Feature2/bin " + str(j + 1) + "/*" prediction = r.glob(self.decisionTree, query)[0].name except: query = "Feature1/bin " + str(i + 1) + "/*" prediction = r.glob(self.decisionTree, query)[0].name if (prediction == "Label = 1"): prediction = 1 elif (prediction == "Label = 0"): prediction = 0 self.decisionTreeModel[i][j] = prediction
def build_tree_from_files(files: List[Dict]) -> Node: """Builds a directory tree out of the results from the metadata-hub query. The tree is build upon a "Root" node which is returned at the end. Each dict contains the entire result but only the file paths are used. Args: files (List[Dict]): list containing the results from the mdh query Returns: Node: The root node (anytree.Node) of the resulting tree """ # '/home/dome_/test_tree' root_node = Node("Root") parent_finder = Resolver("name") file_paths = _extract_file_paths_parts(files) max_index = _length_of_longest_path(file_paths) for i in range(max_index): # In every iteration of the outer loop we only work on parts up to position i for file_path in file_paths: if i >= len(file_path): # After reaching the last part of a path it can be skipped continue last_path_node = file_path[i] path_without_last_node = _create_path_from_parts(file_path[:i]) parent_node = parent_finder.get(root_node, path_without_last_node) if not _parent_has_child(parent_node, last_path_node): Node(last_path_node, parent_node) return root_node
def getattr(self, path, fh=None): path_stat = SFS_Stat() print(f"getattr called with: {path}") os_path = os.stat(path) path_stat.st_size = os_path.st_size path_stat.st_blocks now = time.time() path_stat.st_atime = now path_stat.st_mtime = now path_stat.st_ctime = now if path in [".", "..", "/"]: path_stat.st_mode = stat.S_IFDIR | 0o755 return path_stat.__dict__ file_finder = Resolver("name") path = path[1:] # strip leading "/" path_node: Node = file_finder.get(self.directory_tree, path) if len(path_node.children) == 0: print("got regular file") path_stat.st_mode = stat.S_IFREG | 0o755 else: path_stat.st_mode = stat.S_IFDIR | 0o755 return path_stat.__dict__
def has_path(self, path): r = Resolver('id') try: r.get(self.root, path) return True except: return False
def node_search(tree, cible): s = cible if s == "": return [] if s[0] == '/': s = '/' + tree.root.name + s r = Resolver('name') return r.glob(tree, s + '*')
def resolve_rels(self, patt) -> List['AnalNode']: """ >>> f.resolve_rels("*/obj") :param patt: :return: """ r = Resolver('dependency_relation') return r.glob(self, patt)
def resolve_rel(self, path) -> 'AnalNode': """ >>> f.resolve_rel("./obl/amod") :param path: :return: """ r = Resolver('dependency_relation') return r.get(self, path)
def merge_private_into_main_tree(tree_root: VSSNode): r = Resolver() try: private = r.get(tree_root, "/Vehicle/Private") detect_and_merge(tree_root, private) private.parent = None except ChildResolverError: print("No private Attribute branch detected")
def node_path_length(tree): resolver = Resolver('name') start = '*' for i in range(tree.height): node = resolver.glob(tree, start + '{' + sys.argv[2] + '}') if node != []: return node[0].depth else: start += '/*'
def resolve(self, path: str) -> "TlvNode": """Return the node at *path*.""" path = path.lower() try: node = Resolver().get(self, path) except ResolverError as error: raise TlvError( f"Can not resolve path '{path}' from this node", tag=repr(self.tag) ) from error return node
def __init__(self, node=None, **kwargs): super().__init__(**kwargs) self._root = WellSegment(name='FIELD', is_group=True) if node is None else node self._resolver = Resolver() self.init_state(has_blocks=False, has_cf=False, spatial=True, all_tracks_complete=False, all_tracks_inside=False, full_perforation=False)
def get(self, path): """ >>> from sagas.nlu.warehouse import warehouse as wh >>> wh.get("/_/ent:Person") :param path: :return: """ r = Resolver('qualified') return r.get(self, path)
def read_putty_registry(self): aReg = ConnectRegistry(None, HKEY_CURRENT_USER) aKey = OpenKey(aReg, r"Software\SimonTatham\PuTTY\Sessions") res = Resolver('name') for i in range(0, QueryInfoKey(aKey)[0]): try: asubkey_name = EnumKey(aKey, i) if str(asubkey_name) == 'WinSSHTerm': continue asubkey = OpenKey(aKey, asubkey_name) try: sessionPath = str( QueryValueEx(asubkey, "PsmPath")[0].encode('utf-8')) except Exception as e: sessionPath = "Sessions" pass list = sessionPath.split('\\') tmp = self.root counter = 1 for i in list: pathB64 = base64.b64encode(i) try: if res.get(tmp, pathB64): tmp = res.get(tmp, pathB64) if counter >= len(list): self.saveSessionData( node=tmp, name=str(asubkey_name), username=str( QueryValueEx(asubkey, "UserName")[0]), privateKey=str( QueryValueEx(asubkey, "PublicKeyFile")[0]), hostname=str( QueryValueEx(asubkey, "HostName")[0]), port=str( QueryValueEx(asubkey, "PortNumber")[0])) except ChildResolverError as e: tmp = Node(pathB64, parent=tmp, type="Container") if counter >= len(list): self.saveSessionData( node=tmp, name=str(asubkey_name), username=str( QueryValueEx(asubkey, "UserName")[0]), privateKey=str( QueryValueEx(asubkey, "PublicKeyFile")[0]), hostname=str( QueryValueEx(asubkey, "HostName")[0]), port=str( QueryValueEx(asubkey, "PortNumber")[0])) counter = counter + 1 except EnvironmentError as e: break
def handle_message(self, message): #m = 0 if m == 0: self.value = message["value"] self.decide() #m > 0 else: path = message["path"] #Commander message, empty tree if len(message["path"]) == 1: self.root = AnyNode(id=path[0], value=message["value"], decide_value=message["value"], name=path[0]) #Message from a lieutenant else: #Get last known node on path and add new node as its child with open("./debug/messages_handled_lieutenant.txt", "a") as f: print(self.id, message["path"], path[-1], file=f) r = Resolver('id') last_node = r.get(self.root, '/'.join(path[1:-1])) AnyNode(id=path[-1], value=message["value"], decide_value=message["value"], parent=last_node, name=path[-1]) with open("./debug/all_tree_operations.txt", "a") as f: print(self.id, message["path"], last_node.id, path[-1], file=f) if self.id not in path and len(path) <= m: #If possible, apend its own id to the path and broadcast the message path.append(self.id) message = self.construct_message(path, message["value"]) self.multicast(self.process_list[1:], message) leafs = [ node for node in LevelOrderIter(self.root) if node.is_leaf ] if len(leafs) == n_leafs: #If already received all the messages, start phase 2 reverse_nodes = [node for node in LevelOrderIter(self.root)][::-1] for node in reverse_nodes: if not node.is_leaf: node.decide_value = majority( [n.decide_value for n in node.children]) self.value = self.root.decide_value print("Process", self.id, "has decided", self.value) #print("Process", self.id, "has decided", self.value) with open('./results/' + self.id + ".txt", "w") as f: print(self.id, RenderTree(self.root), file=f)
def _get_class(self, row): """get the class of a row in a dataframe""" node = self._tree r = Resolver('name') while not node.is_leaf: if node.feat != None: print "the " + node.feat + " is " + row[node.feat] n = r.get(node, row[node.feat]) node = n print "\tso we predict " + node.target return node.target
def __init__(self): self.root = SourceNode() self.display_overlay = None self.extra_widget = None self.selected_output = None self._output_type = None self.all_params = dict() self._param_finder = Resolver() self.node_dict = dict()
def node_exists(root, node_name) -> bool: """Checks if a node with the name provided to this method exists Args: root: root node of tree or root of search if search is applied to subtree node_name: name of the node that is searched for. """ try: r = Resolver() r.get(root, node_name) return True except ChildResolverError: return False
def read_mobaxterm_ini(self): style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST dialog = wx.FileDialog(self, message='Open MobaXterm.ini', wildcard='(*.ini)|*.ini', style=style) if dialog.ShowModal() == wx.ID_OK: file = dialog.GetPath() else: return False dialog.Destroy() try: config = configparser.RawConfigParser() config.optionxform = str config.read(file) res = Resolver('name') for s in config.sections(): if s.startswith('Bookmarks'): if config[s]['SubRep'] == 'PuTTY sessions': continue tmp = self.root for (key, val) in config.items(s): if key == 'ImgNum': continue if key == 'SubRep' and val: sessionPath = config[s]['SubRep'] list = sessionPath.encode('utf-8').split('\\') counter = 1 for i in list: pathB64 = base64.b64encode(i) try: if res.get(tmp, pathB64): tmp = res.get(tmp, pathB64) except ChildResolverError as e: node = Node(pathB64, parent=tmp, type='Container') tmp = node counter = counter + 1 break for (key, val) in config.items(s): if key == 'ImgNum' or key == 'SubRep': continue sessionData = val.encode('utf-8').split('%') if sessionData[0] == '#109#0': self.saveSessionData(tmp, key, sessionData[3], '', sessionData[1], sessionData[2]) return True except Exception as e: wx.MessageBox(str(e), "Error") return False
def readdir(self, path, fh): print(f"readdir called with {path}") children = [".", ".."] file_finder = Resolver("name") path = path[1:] # strip leading "/" path_node: Node = file_finder.get(self.directory_tree, path) child: Node for child in path_node.children: children.append(child.name) print(f"added {child.name}") return children
def read_superputty_xml(self): style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST dialog = wx.FileDialog(self, message='Open Sessions.XML', wildcard='(*.XML)|*.XML', style=style) if dialog.ShowModal() == wx.ID_OK: file = dialog.GetPath() else: return False dialog.Destroy() try: tree = ET.parse(file) for item in tree.iter(): if item.tag == "SessionData": sessionPath = item.attrib.get('SessionId') list = sessionPath.encode('utf-8').split('/') tmp = self.root res = Resolver('name') counter = 1 for i in list: pathB64 = base64.b64encode(i) try: if res.get(tmp, pathB64): tmp = res.get(tmp, pathB64) if counter >= len(list): print pathB64 self.saveSessionData( node=tmp, name=str(item.attrib.get('SessionName').encode('utf-8')), username=str(item.attrib.get('Username').encode('utf-8')), privateKey='', hostname=str(item.attrib.get('Host').encode('utf-8')), port=str(item.attrib.get('Port').encode('utf-8')) ) print pathB64 except ChildResolverError as e: if counter < len(list): tmp = Node(pathB64, parent=tmp, type="Container") if counter >= len(list): self.saveSessionData( node=tmp, name=str(item.attrib.get('SessionName').encode('utf-8')), username=str(item.attrib.get('Username').encode('utf-8')), privateKey='', hostname=str(item.attrib.get('Host').encode('utf-8')), port=str(item.attrib.get('Port').encode('utf-8')) ) counter = counter + 1 return True except Exception as e: wx.MessageBox(str(e), "Error") return False
def on_message_list_activated(self, index: QModelIndex): # Gets activated node. activated_node = index.internalPointer() # Recover node message message content r = Resolver('name') message = r.get(activated_node, './content') object_tree_index = self.object_tree.model().index_from_node(message) self.object_tree.setRootIndex(object_tree_index) # Object tree first element object_tree_first_item = self.object_tree.model().index(0, 0, object_tree_index) self.on_object_tree_clicked(object_tree_first_item)
def test_enum(): class Animals(IntEnum): Mammal = 1 Cat = 2 Dog = 3 root = Node("ANIMAL") mammal = Node(Animals.Mammal, parent=root) cat = Node(Animals.Cat, parent=mammal) dog = Node(Animals.Dog, parent=mammal) r = Resolver() eq_(r.glob(root, "/ANIMAL/*"), [mammal]) eq_(r.glob(root, "/ANIMAL/*/*"), [cat, dog])
def main(): # part 1 print("# part1:") f = open("input7.txt", "r") lines = f.readlines() f.close() root = Node("root") # generate first level for n_line, line in enumerate(lines): splited_line = line.replace("\n","").split(" contain ") Node(splited_line[0].strip().replace("bags", "bag").replace(".", ""), parent=root, bag_value=0) # generate n-th levels for pre, fill, node in RenderTree(root): for n_line, line in enumerate(lines): splited_line = line.replace("\n","").split(" contain ") if(node.name in splited_line[0]): desc = splited_line[1].split(",") for d in desc: d_value = ''.join([i for i in d if i.isdigit()]).strip() #if(len(d_value) == 0): d_value = 0 d_name = ''.join([i for i in d if not i.isdigit()]).strip() d_name = d_name.replace("bags", "bag").replace(".", "") if(d_name not in "no other bag"): Node(d_name, parent=node, bag_value=d_value) #print the tree # for pre, fill, node in RenderTree(root): # print("%s%s" % (pre, node.name)) results = [] for pre, fill, node in RenderTree(root, maxlevel=2): finds = findall(node, filter_=lambda n: "shiny gold" in n.name) if(len(finds) > 0): results.append(finds) print("total part1 :", len(results) - 2) #part 2 r = Resolver('name') shiny_gold_bag_node = r.get(root, "shiny gold bag") shiny_gold_bag_node.bag_value = 1 # print the tree # for pre, fill, node in RenderTree(shiny_gold_bag_node): # print("%s%s(%s)" % (pre, node.name, node.bag_value)) print("total part2 :", count_bags(shiny_gold_bag_node))
def distance_to_interface(tree, node_name): resolver = Resolver('name') start = '*' for i in range(tree.height): node = resolver.glob(tree, start + node_name) if node != []: depth = node[0].depth return depth else: if node_name == '{external node}': return 0 else: start += '/*' # When a node is not connected return 999
def on_network_event_list_activated(self, index: QModelIndex): # Gets activated node. activated_node = index.internalPointer() # Recover node packet list. r = Resolver('name') packets = r.get(activated_node, './packets') # Sets packet list as root index on object tree. object_tree_index = self.object_tree.model().index_from_node(packets) self.object_tree.setRootIndex(object_tree_index) # Object tree first element object_tree_first_item = self.object_tree.model().index(0, 0, object_tree_index) self.on_object_tree_clicked(object_tree_first_item)
def add_to_structure(base, resource): try: r = Resolver("name") try: if '?' in resource[0]: return existing_node = r.get(base, resource[0]) if len(resource) > 1: add_to_structure(existing_node, resource[1:]) except Exception as e: new_node = Node(resource[0], parent=base) if len(resource) > 1: add_to_structure(new_node, resource[1:]) except Exception as e: return {'web_structure': e}
def __floordiv__(self, patt): """ >>> from sagas.nlu.warehouse import warehouse as wh >>> wh//'find*' >>> wh//'*Person*' >>> [(el, el.words) for el in wh//'*Person*'] :param patt: :return: """ if isinstance(patt, str): r = Resolver('name') return r.glob(self, patt) elif isinstance(patt, ref_): val = self.resolve_entity(patt.val) return [val] else: return []
def node_path_length(tree, node_name): resolver = Resolver('name') total = 0 children_number = 0 start = '*' for i in range(tree.height): node = resolver.glob(tree, start + node_name) if node: node[0].parent = None result = get_all_children(node[0], total, children_number, True) total += result[0] children_number += result[1] else: start += '/*' if children_number != 0: total = total / children_number return total