本文整理汇总了Python中treelib.Tree.children方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.children方法的具体用法?Python Tree.children怎么用?Python Tree.children使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类treelib.Tree
的用法示例。
在下文中一共展示了Tree.children方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compare_actual_folder_with_tree
# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import children [as 别名]
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)
示例2: create_dummy_download_folder
# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import children [as 别名]
def create_dummy_download_folder(root: path, tree: Tree) -> path:
root_name = tree.root
root_path = root.joinpath(root_name)
if not root_path.exists():
print("Creating {}".format(root_path))
if root_name.endswith(".mp3"):
root_path.touch()
else:
root_path.mkdir()
time.sleep(0.01) # sleep to ensure that the created folders don't have the same ctime
children = tree.children(root_name)
for children in children:
subtree = tree.subtree(children.identifier)
create_dummy_download_folder(root_path, subtree)
return root_path
示例3: __init__
# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import children [as 别名]
class MonteCarlo:
N_THREADS = 1
PERCENTILE = 100
def __init__(self, engine=None, hero=None):
# self.last_ev = 0
# self.rolling_10 = deque(maxlen=10)
# self.rolling_40 = deque(maxlen=40)
self.ev_history = {}
self.time_start = None
self.duration = None
self.queue = None
self.leaf_path = None
if not engine:
# logger.info('engine not given, loading from file...')
self.engine_checksum = None
self.load_engine(hero)
else:
# logger.info('engine given')
self.init(engine, hero)
@property
def current_actions(self):
return [(c.data['action'], c.data['ev'], c.data['traversed'])
for c in self.tree.children(self.tree.root)]
def is_time_left(self):
return time.time() - self.time_start < self.duration
@retrace.retry(on_exception=(EOFError, KeyError), interval=0.1, limit=None)
def load_engine(self, hero):
with shelve.open(Engine.FILE) as shlv:
if shlv['hash'] != self.engine_checksum:
# logger.info('loading engine from file...')
self.engine_checksum = shlv['hash']
self.init(shlv['engine'], hero)
def init(self, engine, hero):
# logger.info('init state')
self.engine = engine
self.hero = hero or self.engine.q[0][0]
self.hero_pocket = self.engine.data[self.hero]['hand']
for s in self.engine.data:
self.ev_history[s] = deque(maxlen=50)
# logger.info('HERO is at seat {} with {}'.format(self.hero, self.hero_pocket))
self.watched = False
self.init_tree()
def init_tree(self):
"""create the tree. Add a root; available action will add the first level of children"""
# self.traversed_ceiling = 1
self.tree = Tree()
root = self.tree.create_node('root', identifier='root', data={'traversed': 0, 'ev': 0, 'stats': 1, 'cum_stats': 1})
# # logger.info('tree:\n{}'.format(self.tree.show()))
# input('new tree')
def watch(self):
"""Runs when engine file changes. Just kicks off run for 3s sprints"""
# logger.info('Monte Carlo watching every {}s...'.format(self.timeout))
while True:
# loads new engine file if checksum changed
self.load_engine()
# do not analyze if game finished
if self.engine.phase in [self.engine.PHASE_SHOWDOWN, self.engine.PHASE_GG]:
if not self.watched:
# logger.error('game is finished')
self.watched = True
time.sleep(3)
continue
# do not analyze if hero does not have pocket
if self.hero_pocket in [['__', '__'], [' ', ' ']]:
if not self.watched:
# logger.error('hero does not have a pocket')
self.watched = True
time.sleep(0.5)
continue
# do not analyze if hero is not to play
if self.hero != self.engine.q[0][0]:
if not self.watched:
# logger.error('hero is not to act')
self.watched = True
time.sleep(0.5)
continue
if self.is_complete:
if not self.watched:
# logger.error('mc is complete')
self.watched = True
time.sleep(2)
continue
# run a few sims
# logger.debug('running now with timeout {}'.format(self.timeout))
self.run()
#.........这里部分代码省略.........
示例4: verifier
# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import children [as 别名]
#.........这里部分代码省略.........
def assignAtom(self, i):
for atomEquivClass in self.SameAtomList.get_sets():
if str(i) in atomEquivClass:
return self.SameAtomList.get_leader(str(i))
self.SameAtomList.insert(str(i))
return self.SameAtomList.get_leader(str(i))
def setUpSameAtomList(self):
'''
Using the Union Find datastructure, I will keep track of the equivalence
classes of SameAtoms and then supply a label based on the index of the
subset in which a subformula corresponding with an atom is contained.
'''
startIndexSameAtoms = findInFile(self.instanceFileLines, lambda x: "PREDICATE SameAtom" in x) + 1
sameAtomPairs = self.instanceFileLines[startIndexSameAtoms:]
for pair in sameAtomPairs:
tmp = pair.split(",")
label1 = tmp[0].split("(")[1]
label2 = tmp[1].split(")")[0]
self.SameAtomList.insert(label1, label2)
def determineConnective(self, i):
'''
Each subformula label is guaranteed to be the first argument of some
tuple; either it will correspond with an atom, the only argument,
or it will correspond with the main connective of a unary
(i.e. negation, box, or diamond) or binary (i.e. conjunction or
disjunction) subformula.
'''
SiThing=findInFile(self.instanceFileLines, lambda x: "("+str(i)+")" in x)
if SiThing:
for k in range(SiThing, 0, -1): # go back in the file until you can find out what predicate we're dealing with
if self.instanceFileLines[k].split(" ")[0] == "PREDICATE":
if self.instanceFileLines[k].split(" ")[1] == "Falsum":
return "false"
return self.assignAtom(i)
if findInFile(self.instanceFileLines, lambda x: "("+str(i)+"," in x):
SiAsMainConnective = findInFile(self.instanceFileLines, lambda x: "("+str(i)+"," in x)
for j in range(SiAsMainConnective, 0, -1): # go back in the file until you can find out what predicate we're dealing with
if self.instanceFileLines[j].split(" ")[0] == "PREDICATE":
if self.instanceFileLines[j].split(" ")[1] == "SameAtom": # if there are no tuples under SameAtom, then this isn't reached due to SiAsMainConnective
return self.assignAtom(i)
else:
return self.assignSymbol(self.instanceFileLines[j].split(" ")[1]) # if the predicate refers to an operator, then we need to find out which one!
def nodeCreation(self, predicate, SiConnective, i):
SiAsOperand = findInFile(self.instanceFileLines, predicate)
ParentOfSi = str(self.instanceFileLines[SiAsOperand].split(",")[0].split("(")[1])
self.syntaxTree.create_node(SiConnective, str(i), parent=ParentOfSi)
def makeSyntaxTreeNode(self, SiConnective, i):
if findInFile(self.instanceFileLines, lambda x: ","+str(i)+"," in x): #find where subformula i appears as second operand, and
self.nodeCreation(lambda x: ","+str(i)+"," in x, SiConnective, i)
elif findInFile(self.instanceFileLines, lambda x: ","+str(i)+")" in x):
self.nodeCreation(lambda x: ","+str(i)+")" in x, SiConnective, i)
else:
self.syntaxTree.create_node(SiConnective,str(i))
def buildTree(self):
'''
To build the syntax tree for the formula as laid out in the instance
file, we need to delve into the formula by means of stripping off the
main connective of each subformula (starting with the main connective
of the formula itself) and labeling a tree node with the symbol
corresponding with that connective. Note that each subformula appears
exactly once as the first argument of a tuple, and can appear at most
once as a second (or third, for binary operators) argument in a tuple.
'''
self.syntaxTree = Tree()
for i in range(1, self.numTreeNodes+1):
SiConnective = self.determineConnective(i)
self.makeSyntaxTreeNode(SiConnective, i)
def myShowTree(self, tree, root):
'''
In-order depth-first traversal of syntax tree using deep recursion; first
layer of recursion receives root of tree, where each sub-layer receives
respectively the left child then the right child as roots of those subtrees
with visitation of the root node occurring in the middle.
'''
rootNID = root.identifier
x=self.syntaxTree.children(rootNID)
if len(self.syntaxTree.children(rootNID)) == 2:
print("(", end=" ")
self.myShowTree(self.syntaxTree, self.syntaxTree.children(rootNID)[0])
print(self.syntaxTree.get_node(rootNID).tag, end=" ")
if len(self.syntaxTree.children(rootNID)) >= 1:
if len(self.syntaxTree.children(rootNID)) == 1:
print("(", end=" ")
self.myShowTree(self.syntaxTree, self.syntaxTree.children(rootNID)[0])
else:
self.myShowTree(self.syntaxTree, self.syntaxTree.children(rootNID)[1])
print(")", end=" ")