本文整理汇总了Python中CodernityDB.database.Database.close方法的典型用法代码示例。如果您正苦于以下问题:Python Database.close方法的具体用法?Python Database.close怎么用?Python Database.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CodernityDB.database.Database
的用法示例。
在下文中一共展示了Database.close方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BenchCodernityDB
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import close [as 别名]
class BenchCodernityDB(BenchBase):
ID_FIELD = "_id"
def __init__(self, *args, **kwargs):
super(BenchCodernityDB, self).__init__(*args, **kwargs)
def create_database(self):
self.db = Database(self.db_name)
self.db.create()
self.db.add_index(WithSmallNumberIndex(self.db.path, "small_number"))
def delete_database(self):
self.db.close()
shutil.rmtree(self.db_name)
def create(self, record):
self.db.insert(record)
def get(self, key):
return self.db.get("id", key, with_doc=True)
def query(self, **kwargs):
key, val = kwargs.items()[0]
return list(self.db.get_many(key, val, limit=-1, with_doc=True))
示例2: __init__
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import close [as 别名]
class cache :
"""
cache for word morphological analysis
"""
def __init__(self,):
"""
Create Analex Cache
"""
self.cache={'checkedWords':{},
'FreqWords':{'noun':{}, 'verb':{},'stopword':{}},
};
self.db = Database('/tmp/qalsadiCache')
if not self.db.exists():
self.db.create();
x_ind = WithAIndex(self.db.path, 'a')
self.db.add_index(x_ind)
else:
self.db.open();
def __del__(self):
"""
Delete instance and clear cache
"""
self.cache=None;
self.db.close();
def isAlreadyChecked(self, word):
try:
return bool(self.db.get('a', word))
except: return False
#~ except: return False;
def getChecked(self, word):
x = self.db.get('a', word, with_doc=True)
y= x.get('doc',False);
if y: return y.get('d',[])
else: return []
def addChecked(self, word, data):
idata = {"a":word,'d':data}
self.db.insert(idata)
def existsCacheFreq(self, word, wordtype):
return word in self.cache['FreqWords'];
def getFreq(self, originalword, wordtype):
return self.cache['FreqWords'][wordtype].get(originalword,0);
def addFreq(self, original, wordtype, freq):
self.cache['FreqWords'][wordtype][original]=freq;
示例3: __init__
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import close [as 别名]
class DBImport:
'''
import scan: scans existing self.db and rebuilds config file
create self.db: creates self.db file, master index, question index and table index
'''
def __init__(self,passkey,xtraDB):
self.key = passkey
self.dbName = xtraDB
self.db=Database(self.dbName)
self.importScan()
def __del__(self):
if (self.db.opened):
self.db.close()
# ADD REBUILD OPTION
def importScan(self):
#read from config, as a check
self.db=Database(self.dbName)
if(self.db.exists()):
self.db.open()
self.db.id_ind.enc_key = self.key
for curr in self.db.all('id'): #since first passkey in self.db should be only one there, function only perfomed once
if curr['t'] == 'master':
masterKey=''.join(curr['_id'])
self.DBConfig = AppConfig()
self.DBConfig.putmap('databaseinfo','indexkey',masterKey)#masterkey=value
self.DBConfig.putmap('databaseinfo','databasename',self.dbName)
break
#add else statement for errors if couldnt be written for found
self.db.close()
return True
示例4: migrate
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import close [as 别名]
def migrate(source, destination):
"""
Very basic for now
"""
dbs = Database(source)
dbt = Database(destination)
dbs.open()
dbt.create()
dbt.close()
for curr in os.listdir(os.path.join(dbs.path, "_indexes")):
if curr != "00id.py":
shutil.copyfile(os.path.join(dbs.path, "_indexes", curr), os.path.join(dbt.path, "_indexes", curr))
dbt.open()
for c in dbs.all("id"):
del c["_rev"]
dbt.insert(c)
return True
示例5: main
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import close [as 别名]
def main():
db = Database('/tmp/demo_secure')
key = 'abcdefgh'
id_ind = EncUniqueHashIndex(db.path, 'id', storage_class='Salsa20Storage')
db.set_indexes([id_ind])
db.create()
db.id_ind.enc_key = key
for x in xrange(100):
db.insert(dict(x=x, data='testing'))
db.close()
dbr = Database('/tmp/demo_secure')
dbr.open()
dbr.id_ind.enc_key = key
for curr in dbr.all('id', limit=5):
print curr
示例6: main
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import close [as 别名]
def main():
db = Database("/tmp/demo_secure")
key = "abcdefgh"
id_ind = EncUniqueHashIndex(db.path, "id")
db.set_indexes([id_ind])
db.create()
db.id_ind.enc_key = key
print db.id_ind.storage
for x in xrange(100):
db.insert(dict(x=x, data="testing"))
db.close()
dbr = Database("/tmp/demo_secure")
dbr.open()
dbr.id_ind.enc_key = key
for curr in dbr.all("id", limit=5):
print curr
示例7: CodernityDB
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import close [as 别名]
class CodernityDB(BaseService):
"""A service providing a codernity db interface."""
name = 'db'
default_config = dict(db=dict(path=''), app=dict(dir=''))
def __init__(self, app):
super(CodernityDB, self).__init__(app)
self.dbfile = os.path.join(self.app.config['app']['dir'],
self.app.config['db']['path'])
self.db = None
self.uncommitted = dict()
self.stop_event = Event()
self.db = Database(self.dbfile)
try:
log.info('opening db', path=self.dbfile)
self.db.open()
except DatabasePathException:
log.info('db does not exist, creating it', path=self.dbfile)
self.db.create()
self.db.add_index(MD5Index(self.dbfile, 'key'))
def _run(self):
self.stop_event.wait()
def stop(self):
# commit?
log.info('closing db')
if self.started:
self.db.close()
self.stop_event.set()
def get(self, key):
log.debug('getting entry', key=key)
if key in self.uncommitted:
if self.uncommitted[key] is None:
raise KeyError("key not in db")
return self.uncommitted[key]
try:
value = self.db.get('key', key, with_doc=True)['doc']['value']
except RecordNotFound:
raise KeyError("key not in db")
return compress.decompress(value)
def put(self, key, value):
log.debug('putting entry', key=key, value=value)
self.uncommitted[key] = value
def commit(self):
log.debug('committing', db=self)
for k, v in self.uncommitted.items():
if v is None:
doc = self.db.get('key', k, with_doc=True)['doc']
self.db.delete(doc)
else:
self.db.insert({'key': k, 'value': compress.compress(v)})
self.uncommitted.clear()
def delete(self, key):
log.debug('deleting entry', key=key)
self.uncommitted[key] = None
def __contains__(self, key):
try:
self.get(key)
except KeyError:
return False
return True
def __eq__(self, other):
return isinstance(other, self.__class__) and self.db == other.db
def __repr__(self):
return '<DB at %d uncommitted=%d>' % (id(self.db), len(self.uncommitted))
def inc_refcount(self, key, value):
self.put(key, value)
def dec_refcount(self, key):
pass
def revert_refcount_changes(self, epoch):
pass
def commit_refcount_changes(self, epoch):
pass
def cleanup(self, epoch):
pass
def put_temporarily(self, key, value):
self.inc_refcount(key, value)
self.dec_refcount(key)
示例8: __init__
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import close [as 别名]
class SecuIn:
'''
Handles all data input into the database
'''
def __init__(self,passkey):
self.key = passkey
self.initQuestions = SecuQ(self.key)
self.DBConfig = AppConfig()
self.dbName = self.DBConfig.mapget('databaseinfo')['databasename']
self.db = Database(self.dbName)
initDay = DayEntry(self.key) # checks day hash or creates a new one
self.dayKey = initDay.dayKey
def questionDataIN(self,data):
'''
Data IN:
{'a' : 2, 'b': 14 , 'c': 11, 'd': 43, 'note' : 'hello'}
or
{ 'b': 14 , 'c': 11, 'd': 43, 'note' : 'hello'}
some entries may be missing
Data OUT: (NEVER DELETE ANYTIHNG :) )
{'date' : xx , _id: ###date2### , 'a':{'xxdate3xx':2},
'b':{'xxdate3xx':14},
'c':{'xxdate3xx':11},
'note':{'xxdate3xx':'you'}}
{'date' : xx , _id: ###date1### , 'a':{'xxdate1xx':1,'xxdate2xx':2},
'b':{'xxdate1xx':14,'xxdate2xx':14},
'c':{'xxdate1xx':11,'xxdate2xx':11},
'note':{'xxdate2xx':'hello','xxdate3xx':'you'}
'''
timeIN = getTimeStamp() #get now time
#initialize new questions
# get data, as doc {'date':'xx/xx/xxTxx:xx:xxxx','question1':'x','question2':'x'}, same as dic format
if(self.db.exists()):
self.db.open()
self.db.id_ind.enc_key = self.key
dayrow = self.db.get('id', self.dayKey, with_doc=True)
#this function assumes database already opened
# this is gonna be a tuple that is inserted directly
#convert data from javasript to python dict/json
# if (type(data) is str):
dataIN=eval(data) #{ 'b': 14 , 'c': 11, 'd': 43, 'note' : 'hello'}
datachanged = dataIN.keys()
for question in datachanged:
try:
dayrow[question][timeIN] = dataIN[question]
except KeyError: #first write to key, initiate
dayrow[question] = {}
dayrow[question][timeIN] = dataIN[question]
self.db.update(dayrow)
self.db.close()
self.initQuestions.questionsValidate(datachanged) #insert questions whos data had changed
#if all ok!
return True
示例9: __init__
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import close [as 别名]
class cache :
"""
cache for word morphological analysis
"""
DB_PATH = os.path.join(os.path.expanduser('~'), '.thaalabCache')
def __init__(self, cache_path=False):
"""
Create Analex Cache
"""
# use this dictionary as a local cache,
# The global db will be updated on destructing object
# get the database path
if hasattr(sys, 'frozen'): # only when running in py2exe this exists
base = sys.prefix
else: # otherwise this is a regular python script
base = os.path.dirname(os.path.realpath(__file__))
if not cache_path:
file_path = self.DB_PATH
else:
file_path = os.path.join(os.path.dirname(cache_path), '.thaalabCache')
self.cache={};
self.db = Database(file_path)
if not self.db.exists():
self.db.create();
x_ind = WithAIndex(self.db.path, 'a')
self.db.add_index(x_ind)
else:
self.db.open();
def __del__(self):
"""
Delete instance and clear cache
"""
self.cache=None;
self.db.close();
def update(self):
"""update data base """
#~ pass
for word in self.cache:
self.add_checked(word, self.cache[word])
def is_already_checked(self, word):
try:
return bool(self.db.get('a', word))
except:
return False
#~ except: return False;
def get_checked(self, word):
try:
x = self.db.get('a', word, with_doc=True)
y = x.get('doc',False);
if y:
return y.get('d',[])
else: return []
except:
return []
def add_checked(self, word, data):
idata = {"a":word,'d':data}
try:
saved = self.db.get('a', word, with_doc=True)
except:
saved = False
if saved:
saved['doc']['d'] = data
doc = saved['doc']
doc['update'] = True
self.db.update(doc)
else:
self.db.insert(idata)
def exists_cache_word(self, word):
""" test if word exists in cache"""
#if exists in cache dictionary
if word in self.cache:
return True
else: # test in database
if self.is_already_checked(word):
stored_data = self.get_checked(word)
self.cache[word] = stored_data
return bool(self.cache[word])
else:
# add null dict to the word index to avoid multiple database check
self.cache[word] = {}
return {}
def get_relation_freq(self, word_prev, word_cur, relation):
self.exists_cache_word(word_prev)
return self.cache.get(word_prev, {}).get(word_cur, {}).get(relation, 0);
def is_related(self, word_prev, word_cur):
""" test if two words are related"""
#serach in cache
self.exists_cache_word(word_prev)
#.........这里部分代码省略.........
示例10: __init__
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import close [as 别名]
class SecuFrame: #in producion, key must be specified
def __init__(self,passkey,date_range='all'):
self.key = passkey
self.Qeng = SecuQ(self.key)
self.indexdb = DBIndexSystem(self.key)
#self.indexdb.masterIndex
#self.indexdb.Qindex
#self.indexdb.Tindex
#self.indexdb.IndexedTable
#self.indexdb.dbName
self.dayindex = DayEntry(self.key)
#self.dayindex.dayKey
self.DBConfig = AppConfig()
self.dbName = self.DBConfig.mapget('databaseinfo')['databasename']
self.db = Database(self.dbName)
self.dbparseable = self.db2json(daterange=date_range,clean=True)
def __del__(self):
if (self.db.opened):
self.db.close()
def db2json(self,daterange,clean=True):
'''
> daterange
- tuple datetime objects to specify range
(dateObj,dateObj)
'''
dfJSON = []
if(self.db.exists()):
self.db.open()
self.db.id_ind.enc_key = self.key
if daterange == "all":
if clean == True:
for currHash in self.indexdb.IndexedTable: #get row
curr = self.db.get('id', currHash, with_doc=True)
curr.pop('_id')
curr.pop('_rev')
dfJSON.append(curr)
self.db.close()
return dfJSON
if clean == False:
for currHash in self.indexdb.IndexedTable: #get row
curr = self.db.get('id', currHash, with_doc=True)
dfJSON.append(curr)
self.db.close()
return dfJSON
if daterange == "today":
if clean == True:
curr = self.db.get('id', self.dayindex.dayKey, with_doc=True)
curr.pop('_id')
curr.pop('_rev')
dfJSON.append(curr)
self.db.close()
return dfJSON
'''
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
if ((type(daterange) == tuple) & (len(daterange)<=2) & (daterange[0]<daterange[1]) &(type(daterange[0])==datetime.datetime) & (type(daterange[1])==datetime.datetime): #if it's a valid daterange
if clean == True
for curr in db.all('id'): #get row
currdto=dt.datetime.strptime(curr['date'],"%Y-%m-%d %H:%M:%S.%f")
if ( daterange[0] <= currdto <= daterange[1]):
curr.pop('_id')
curr.pop('_rev')
dfJSON.append(curr)
db.close()
return dfJSON
if clean == False:
#.........这里部分代码省略.........
示例11: CodernityDataStore
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import close [as 别名]
class CodernityDataStore(object):
PATH_TYPE = 'path'
def __init__(self, db_path):
self.db = Database(db_path)
if self.db.exists():
self.db.open()
else:
self.db.create()
path_index = PathIndex(self.db.path, 'path')
self.db.add_index(path_index)
path_added_index = PathAddedIndex(self.db.path, 'path_added')
self.db.add_index(path_added_index)
@classmethod
def dt_str(cls, datetime):
return datetime.isoformat()[0:19]
def add_video(self, path, video, added=None):
logger.debug("add_video(%s, %s, %s)", path, video, added)
added = added or datetime.utcnow()
existing = list(self.db.get_many('path', path, with_doc=True))
video_data, video_type = Serializer.serialize_video(video)
data = dict(_t=self.PATH_TYPE, path=path, video_data=video_data, video_type=video_type,
downloads=dict(), added=self.dt_str(added))
self.db.insert(data)
for existing_path in existing:
self.db.delete(existing_path['doc'])
def add_download(self, path, provider, sub_id, language, score):
logger.debug("add_download(%s, %s, %s, %s, %d)", path, provider, sub_id, language, score)
data = self.db.get('path', path, with_doc=True)
path = data['doc']
download = dict(provider=provider, sub_id=sub_id, lang=str(language), score=score)
if str(language) in path['downloads']:
path['downloads'][str(language)].append(download)
else:
path['downloads'][str(language)] = [download]
self.db.update(path)
def get_downloads_for_video(self, path):
logger.debug("get_downloads_for_video(%s)", path)
data = self.db.get('path', path, with_doc=True)
return data['doc']['downloads']
@staticmethod
def exceeds_desired_score(video, score, desired_movie_score, desired_episode_score):
if isinstance(video, Episode):
return score >= desired_episode_score
elif isinstance(video, Movie):
return score >= desired_movie_score
def get_incomplete_videos(self, languages, desired_movie_score, desired_episode_score, ignore_older_than):
logger.debug("get_incomplete_videos(%s, %d, %d, %s)", languages, desired_movie_score, desired_episode_score, ignore_older_than)
within_date = self.db.get_many('path_added', start=self.dt_str(ignore_older_than), with_doc=True)
results = []
for path in (data['doc'] for data in within_date):
video = Serializer.deserialize_video(path['video_type'], path['video_data'])
needs = []
for lang in languages:
if str(lang) in path['downloads']:
current_score = max(download['score'] for download in path['downloads'][str(lang)])
if not self.exceeds_desired_score(video, current_score, desired_movie_score, desired_episode_score):
needs.append(dict(lang=lang, current_score=current_score))
else:
needs.append(dict(lang=lang, current_score=0))
if needs:
results.append(dict(path=path['path'], video=video, needs=needs))
logger.debug("found %d incomplete videos: %s", len(results), results)
return results
def close(self):
self.db.close()
示例12: questionGet
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import close [as 别名]
#.........这里部分代码省略.........
if oQ[question]['active'] == 'unInit':
self.unInit[question] = oQ[question]
if (oQ[question]['active'] == 'unInit') | (oQ[question]['active'] == 'True'):
self.valid[question] = oQ[question]
if oQ[question]['active'] == 'False':
self.notactive[question] = oQ[question]
if oQ[question]['typ'] == 'note':
self.typ[question] = oQ[question]
try:
if oQ[question]['aggregate'] == 'True':
self.aggregate[question] = oQ[question]
except KeyError:
pass
try:
if oQ[question]['multipoint'] == 'True':
self.multipoint[question] = oQ[question]
except KeyError:
pass
self.db.close()
return True
'''
Qinfo=
{
'a':{'active':'True','typ':'slider','range':'0-100','aggregate':True, 'multipoint':True},
'b':{'active':'True','typ':'slider','range':'0-100','aggregate':True, 'multipoint':False},
'c':{'active':'True','typ':'slider','range':'0-100','aggregate':False, 'multipoint':True},
'd':{'active':'True','typ':'slider','range':'0-100','aggregate':False, 'multipoint':False},
'note':{'active':'True','typ':'note', 'multipoint':"False"}
}
'''
def questionInsert(self,data,descriptor='inclusive'):# this will be a class later for ... infinite data
if(self.db.exists()):
self.db.open()
self.db.id_ind.enc_key = self.key
#select Qindex
Qindex = self.db.get('id', self.indexdb.Qindex, with_doc=True)