本文整理汇总了Python中PyDbLite.Base.create_index方法的典型用法代码示例。如果您正苦于以下问题:Python Base.create_index方法的具体用法?Python Base.create_index怎么用?Python Base.create_index使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyDbLite.Base
的用法示例。
在下文中一共展示了Base.create_index方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Queue
# 需要导入模块: from PyDbLite import Base [as 别名]
# 或者: from PyDbLite.Base import create_index [as 别名]
class Queue(object):
""" Simple queue with PyDbLite backend. """
def __init__(self, queue_type=0, config=None):
if config is not None:
# copy config
pass
else:
self.commit = False
self.db_filename = "/tmp/queue.pydb"
self.mode = "override"
self.queue = Base(self.db_filename)
def create_queue(self):
self.queue.create('id', 'item', mode = self.mode)
self.queue.create_index('id')
return None
def push(self, item):
self.queue.insert(self.length(), item)
return None
def pop(self):
if not self.is_empty():
id = self.length() - 1
r = self.queue._id[id]
self.queue.delete(r)
return r
else:
return None
def list(self):
return self.queue.records
def length(self):
return len(self.queue)
def is_empty(self):
return self.length() == 0
def commit(self):
if self.commit is True:
self.queue.commit()
return None
示例2: __init__
# 需要导入模块: from PyDbLite import Base [as 别名]
# 或者: from PyDbLite.Base import create_index [as 别名]
class ArticleDB:
"""class for persistent storage of articles.
what is stored from each Article object is defined in Article.TO_SAVE
"""
def __init__(self, dbfile, mode = 'open', autocommit = False):
self.db = Base(dbfile)
self.db.create(*Article.TO_SAVE, **{'mode': mode})
self.db.create_index(*INDEX_ON)
self.autocommit = autocommit
def insertArticle(self, art):
"""insert article into database, with id consistency check"""
present = []
if art.id_int != None:
present.extend(self.db._id_int[art.id_int])
# if art.idZBL != None:
# present.extend(self.db._idZBL[art.idZBL])
# if art.idMR != None:
# present.extend(self.db._idMR[art.idMR])
ids = list(set([rec['__id__'] for rec in present])) # unique ids
present = [self.db[id] for id in ids] # remove duplicate identical entries (coming from matches on more than one id on the same article)
new = art
for old in present: # FIXME HACK turns off consistency checking
try:
new.mergeWith(Article.Article(record = old)) # article already present in database -- check if ids are consistent, update it with new info from art
except Exception, e:
# logging.error('inconsistent database contents (%i overlapping records); leaving database unchanged' % (len(present)))
#logging.info('inconsistency between \n%s\n%s' % (new, Article.Article(old)))
logging.warning('inconsistency between %s and %s' % (new, Article.Article(old)))
# return False
if len(present) == 0:
# logging.debug('inserting a new article')
pass
else:
# logging.debug('replacing %i old (consistent) record(s) for %s' % (len(present), new))
pass
self.db.delete(present)
id = self.db.insert(**new.__dict__)
if self.autocommit:
self.commit()
return True
示例3: range
# 需要导入模块: from PyDbLite import Base [as 别名]
# 或者: from PyDbLite.Base import create_index [as 别名]
for i in range(ln):
res += random.choice(string.letters)
return res
def sentence(n, m):
ln = random.randint(1, n)
res = []
for i in range(ln):
res.append(word(m))
return " ".join(res)
os.remove("blog")
db = Base("blog").create("parent", "title", "author", "text", "date")
db.create_index("parent")
nbthreads = 200
for i in range(nbthreads):
# generate thread
author = "pierre"
title = sentence(10, 10)
text = sentence(100, 10)
date = datetime.datetime(
random.randint(2004, 2006),
random.randint(1, 12),
random.randint(1, 28),
random.randint(0, 23),
random.randint(0, 59),
random.randint(0, 59),
)
示例4: TagOrganizer
# 需要导入模块: from PyDbLite import Base [as 别名]
# 或者: from PyDbLite.Base import create_index [as 别名]
class TagOrganizer(Organizer):
def __init__(self, cache, category=None):
self.tags = None
self.category = category
Organizer.__init__(self, cache, False)
def reset(self):
if not self.tags:
self.tags = Base(DB_FILE_TAGS)
self.tags.create('realpath', 'category', 'tag', mode = 'override')
self.tags.create_index('realpath')
self.tags.create_index('category')
Organizer.reset(self)
def updatecache(self):
self._generatetags()
Organizer.updatecache(self)
def _deletefromcache(self, path):
realpath = self.realpath(path)
logger.debug("_deletefromcache(%s)" % realpath)
for tag in self.tags.get_index('realpath')[realpath]:
self.tags.delete(tag)
def deletefromcache(self, path):
self._deletefromcache(path)
Organizer.deletefromcache(self, path)
def addtocache(self, path):
self._deletefromcache(path)
self.generatetags(self.realpath(path))
Organizer.addtocache(self, path)
def generatepaths(self, realpath):
for record in self.tags.get_index('realpath')[realpath]:
yield os.path.join(os.sep, record['tag'],
os.path.basename(realpath))
def dirlist(self, path):
if path == '/':
return self.taglist(self.category)
else:
return []
############################################
# Tag functions
def _generatetags(self):
for filename in filter(util.ignoretag, #IGNORE:W0141
self.cache.filelist()):
self.generatetags(filename)
def generatetags(self, filename):
pass
def tag(self, realpath, category, tag):
logger.debug('tag(%s, %s, %s)' % (realpath, category, tag))
if not tag == None and not tag == '':
self.tags.insert(realpath, category, tag)
def filelistbytags(self, category, tags):
self.refreshcache()
for record in self.tags.get_index('category')[category]:
if record['tag'] in tags:
yield os.path.basename(record['realpath'])
def taglist(self, category):
self.refreshcache()
return util.unique([record['tag'] for record in
self.tags.get_index('category')[category]])
示例5: Organizer
# 需要导入模块: from PyDbLite import Base [as 别名]
# 或者: from PyDbLite.Base import create_index [as 别名]
class Organizer(Cacheable):
"""
This is the base class for organizers
"""
def __init__(self, cache, recursive=True):
Cacheable.__init__(self)
self.cache = cache
self.recursive = recursive
self.transformed = None
# Do not call reset here, it is called from fs.py when the fs is
# already started
def reset(self):
if not self.transformed:
self.transformed = Base(DB_TRANSFORMED)
self.transformed.create('realpath', 'path', 'dirname', mode='override')
self.transformed.create_index('realpath')
self.transformed.create_index('path')
self.transformed.create_index('dirname')
self.cache.reset()
Cacheable.reset(self)
def updatecache(self):
self.generateallpaths()
def deletefromcache(self, path):
realpath = self.realpath(path)
logger.debug("deletefromcache(%s)" % realpath)
self.cache.deletefromcache(realpath)
for item in self.transformed.get_index('realpath')[realpath]:
self.transformed.delete(item)
def addtocache(self, path):
if not self.transformed.get_index('path')[path]:
realpath = self.realpath(path)
self.cache.addtocache(realpath)
self.addfile(realpath)
############################################
# Overwritable functions
def dirlist(self, path): #IGNORE:W0613
"""
Returns a list of (non-existent, generated, virtual) directories for a
given path. Default implementation.
"""
return []
def generatepaths(self, realpath):
"""
Generates paths for a given real path. A file can have more than one
transformed path. Default implementation.
"""
yield util.addtrailingslash(util.removeroot(realpath,
self.cache.filter.root))
def generaterealpath(self, path):
"""
Generates a real path for a inexistent path. Default implementation.
"""
return os.path.join(self.cache.filter.root, path[1:])
############################################
# General functions
def generateallpaths(self):
"""
Generates paths for all the files given by the cache and stores them
in self.transformed
"""
for realpath in self.cache.filelist():
if self.recursive:
# Add all sub-directories first
currentpath = self.cache.filter.root
for pathpart in util.pathparts(util.removeroot(realpath,
self.cache.filter.root)):
currentpath = os.path.join(currentpath, pathpart)
self.addfile(currentpath)
else:
self.addfile(realpath)
def addfile(self, realpath):
"""
Stores a file in self.transformed if not there already and returns the
paths for that file in the proxy file system
"""
logger.debug('addfile(%s)' % realpath)
if not util.ignoretag(util.removeroot(realpath,
self.cache.filter.root)):
return []
self.refreshcache()
transformed = self.transformed.get_index('realpath')[realpath]
if transformed:
return (record['path'] for record in transformed)
else:
paths = []
#.........这里部分代码省略.........