当前位置: 首页>>代码示例>>Python>>正文


Python Tree.contains方法代码示例

本文整理汇总了Python中treelib.Tree.contains方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.contains方法的具体用法?Python Tree.contains怎么用?Python Tree.contains使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在treelib.Tree的用法示例。


在下文中一共展示了Tree.contains方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TreePipeline

# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import contains [as 别名]
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'))
开发者ID:vlasy,项目名称:skool,代码行数:32,代码来源:pipelines.py

示例2: DockerIf

# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import contains [as 别名]
class DockerIf(object):
    def __init__(self, base_url='unix://var/run/docker.sock'):
        self.client = Client(base_url=base_url)

    def remove_image(self, *args, **kwargs):
        self.client.remove_image(*args, **kwargs)

    def add_image_node(self, image, image_id, parent=''):
        is_dangling = image_id in self.dangling

        node = "%s (%s)" % (image[u'RepoTags'], image[u'Id'])

        if parent == '':
            self.image_tree.create_node(node, image_id, parent='/',
                                        data={u'image': image, u'Dangling': is_dangling})
        else:
            self.image_tree.create_node(node, image_id, parent=parent,
                                        data={u'image': image, u'Dangling': is_dangling})

        if image[u'Id'] in self.pending:
            for node in self.pending[image[u'Id']]:
                self.add_image_node(node[0], node[1], parent=image[u'Id'])

    def prepare_image_tree(self):
        self.image_tree = Tree()
        self.image_tree.create_node('', '/')
        self.pending = dict()
        dangling_images = self.client.images(filters={u'dangling': True})
        self.dangling = []
        for image in dangling_images:
            self.dangling.append(image[u'Id'])
        for image in self.client.images(all=True):
            if u'ParentId' in image and image[u'ParentId'] != '':
                if self.image_tree.contains(image[u'ParentId']) or image[u'ParentId'] == '':
                    self.add_image_node(image, image[u'Id'], parent=image[u'ParentId'])
                else:
                    if image[u'ParentId'] not in self.pending:
                        self.pending[image[u'ParentId']] = []
                    self.pending[image[u'ParentId']].append((image, image[u'Id']))
            else:
                self.add_image_node(image, image[u'Id'])

    def get_image_tree(self):
        self.prepare_image_tree()
        return self.image_tree
开发者ID:DoomHammer,项目名称:layerpeeler,代码行数:47,代码来源:__init__.py

示例3: FpGrowth

# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import contains [as 别名]
def FpGrowth(fName):
    
    readFile(fName)
    Cone = getSizeOneItemSet(globOriginalList)
    priorityDict = priorityDic(Cone)
    #print(priorityDict)
    tree = Tree()   
    tree.create_node("{}", "root")
    #reconstruct the whole transction database based on the priority
    counter = 0
    for set in globOriginalList:
        temp = dict()
        for element in set:
            priority = priorityDict.get(element)
            temp.update({element:priority})
            sorted_temp = sorted(temp.items(), key=operator.itemgetter(1))
            sorted_temp.reverse()
        #print(sorted_temp)
        # construct Fp tree
        root = "root"
        for tuple in sorted_temp:
            if(not tree.contains(tuple[0])):
                tree.create_node(tuple[0], tuple[0], root, 0)
                root = tuple[0]
            else: 
                if tuple[0] in tree.is_branch(root):
                    #print("node already in this branch, don't know what to do")
                    #print("going down")
                    root = tuple[0]
                    #print(root)
                else:
                    #print("should create a duplicate node")
                    tree.create_node(tuple[0], counter, root, 0)
                    root = counter
                    counter += 1
                # I need to decide whether to create a new node or not
                # the condition is under this branch if this node exist
                # so I should check the root
    tree.show()
开发者ID:MohanL,项目名称:apriori-Fpgrowth,代码行数:41,代码来源:FpGrowth.py

示例4: WarcFileSystem

# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import contains [as 别名]
class WarcFileSystem( LoggingMixIn, Operations ):
	"""Filesystem built on a WARC's URI paths."""
	def __init__( self, warc ):
		self.warc = warc
		logger.debug( "Mounting %s" % self.warc )
		self.fh = WarcRecord.open_archive( warc, gzip="auto", mode="rb" )
		self.tree = Tree()
		self._get_records()

	def _get_records( self ):
		"""Parses a WARC, building a hierarchical tree."""
		statinfo = os.stat( self.warc )
		self.gid = statinfo.st_gid
		self.uid = statinfo.st_uid
		self.tree.create_node( self.warc, "/" )
		self.records = {}
		bar = progressbar.ProgressBar( maxval=statinfo.st_size, widgets=[ progressbar.Bar( "=", "[", "]"), " ", progressbar.Percentage() ] )
		bar.start()
		for( offset, record, errors ) in self.fh.read_records( limit=None ):
			if record is not None and record.type != WarcRecord.WARCINFO:
				parent = "/"
				segments = [ record.type ] + re.split( "/+", record.url )
				for e in segments:
					identifier = "/".join( [ parent, e ] )
					if not self.tree.contains( identifier ):
						node = WarcRecordNode( record, offset, tag=e, identifier=identifier )
						self.tree.add_node( node, parent=parent )
					parent = identifier
				self.records[ record.url ] = ( offset, record )
				bar.update( offset )
		bar.finish()
		logger.debug( self.tree.show() )

#	def access( self, path, amode ):
#		logger.debug( path )
#		raise FuseOSError( EPERM )

	def chmod( self, path, mode ):
		raise FuseOSError( EPERM )

	def chown( self, path, uid, gid ):
		raise FuseOSError( EPERM )

	def create( self, path, mode ):
		raise FuseOSError( EPERM )

	def destroy( self, path ):
		self.fh.close()

#	def flush( self, path, fh ):
#		raise FuseOSError( EPERM )

	def fsync( self, path, datasync, fh ):
		raise FuseOSError( EPERM )
		
	def fsyncdir( self, path, datasync, fh ):
		raise FuseOSError( EPERM )

	def getattr( self, path, fh=None ):
		"""Returns stat info for a path in the tree."""
		logger.debug( path )
		if path == "/":
			stat = os.stat( self.warc )
			return dict( [
				( "st_mode", ( S_IFDIR | 0444 ) ),
				( "st_ino", stat.st_ino ),
				( "st_dev", stat.st_dev ),
				( "st_nlink", stat.st_nlink ),
				( "st_uid", stat.st_uid ),
				( "st_gid", stat.st_gid ),
				( "st_size", stat.st_size ),
				( "st_ctime", stat.st_ctime ),
				( "st_mtime", stat.st_mtime ),
				( "st_atime", stat.st_atime )
			] )
		else:
			return self.name_to_attrs( "/%s" % path )

	def getxattr( self, path, name, position=0 ):
		"""Returns the value for an extended attribute."""
		if path != "/":
			path = "/%s" % path

		node = self.tree.get_node( path )
		if node is None:
			raise FuseOSError( ENOENT )

		try:
			return node.xattrs[ name ]
		except KeyError:
			raise FuseOSError( ENODATA )

	def init( self, path ):
		pass

	def link( self, target, source ):
		raise FuseOSError( EPERM )

	def listxattr( self, path ):
		"""Returns a list of extended attribute names."""
#.........这里部分代码省略.........
开发者ID:PsypherPunk,项目名称:python-webarchive-fuse,代码行数:103,代码来源:warcf.py

示例5: Route

# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import contains [as 别名]
class Route(object):
    def __init__(self, universe):
        self.route    = Tree()
        self.universe = universe
        self.max_hops = 4

    def show(self):
        self.route.show()
    
    def asString(self):
        return (','.join([self.route[node].tag for node in self.route.expand_tree(mode=Tree.DEPTH)]))

    def getRoute(self):
        return self.route
        
    def byScore_key(self, s):
        return s.score
            
    def findRoute(self, start):
        parent = self.universe.findSystem(start)
        self.route.create_node(start, start, data=parent)
        systems = self.findNextSystems(start, start) 
        self.buildRoute(systems, start)
        
        return self.route

    def buildRoute(self, systems, parent):
        for s in systems:
            n = s.name
            h = 0
            
            if (self.route.contains(n) == False):  
                self.route.create_node(n, n, parent=parent, data=s)
        
                hop = h + self.route.depth(n)
                
                if (hop < self.max_hops):
                    sub_systems = self.findNextSystems(parent, n)                        
                    self.buildRoute(sub_systems, n)
            else:
                 n = parent + ' --> ' + n
                 self.route.create_node(n, n, parent=parent, data=s)

    def getSystemId(self, name, i=0):
        if (self.route.contains(name) == False):
            return name
        else:
            i += 1
            n = name + '(' + str(i) + ')'
            return self.getSystemId(n)

    def findNextSystems(self, parent, start):
        systems = []
        optimal = self.universe.distances.findOptimalSystems(start)
        
        for s in sorted(set(optimal)):
            if (s != parent):
                i = self.universe.findSystem(s)
                if (i.permit == False):
                    systems.append(i)
            
        s = sorted(systems, key = self.byScore_key)

        return s[:self.max_hops]

# http://xiaming.me/treelib/examples.html
#
# class SystemTree(object):
#     def __init__(self):
#         self.tree = Tree()
#         
#     def addNode(self, id, o):
#         self.tree.create_node(o, id)
#         
#     def addChildNode(self, p, id, o):
#         self.tree.create_node(o, id, parent=p)
#     
#     def getNode(self, id):
#         return self.tree.subtree(id)
#         
#     def __repr__(self):
#         return self.tree.to_json(with_data=True)
# 
# 
# t = SystemTree()
# t.addNode('Aerial', 'Aerial')
# t.addChildNode('Aerial', 'Jotun', 'Jotun')
# t.addChildNode('Jotun', 'Rusani', 'Rusani')
# n = t.getNode('Jotun')
# print(n)
# n = t.tree.contains('Invalid')
# print(n)
# t.tree.show()
开发者ID:andrewdjackson,项目名称:eliterares,代码行数:95,代码来源:route.py


注:本文中的treelib.Tree.contains方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。