本文整理汇总了Python中CodernityDB.database.Database.open方法的典型用法代码示例。如果您正苦于以下问题:Python Database.open方法的具体用法?Python Database.open怎么用?Python Database.open使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CodernityDB.database.Database
的用法示例。
在下文中一共展示了Database.open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: init_db
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import open [as 别名]
def init_db():
db = Database(OUTPUT_DB)
try:
db.create()
except IndexConflict:
db.open()
return db
示例2: __init__
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import open [as 别名]
def __init__(self):
db = Database('db')
if db.exists():
db.open()
else:
db.create()
index = UrlIndex(db.path, 'urlidx')
db.add_index(index)
self._db = db
示例3: __init__
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import open [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;
示例4: __init__
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import open [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
示例5: main
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import open [as 别名]
def main():
db = Database('db/tut2')
if db.exists():
db.open()
else:
db.create()
x_ind = WithXIndex(db.path, 'y')
db.add_index(x_ind)
for x in xrange(100):
db.insert(dict(x=x))
for y in xrange(100):
db.insert(dict(y=y))
print db.get('x', 10, with_doc=True)
示例6: migrate
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import open [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
示例7: main
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import open [as 别名]
def main():
db = Database('/tmp/tut4')
db.open()
#db.create()
#x_ind = WithXIndex(db.path, 'x')
#db.add_index(x_ind)
'''
for x in xrange(100):
db.insert(dict(x=x))
for y in xrange(100):
db.insert(dict(y=y))
'''
print db.get('x', 10, with_doc=True)
for curr in db.get_many('x', start=15, end=25, limit=-1, with_doc=False):
print curr
示例8: main
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import open [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
示例9: CDBBase
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import open [as 别名]
class CDBBase(object):
def __init__(self, path, logger = None ):
self.logger = logger or logging.getLogger( __name__ )
self._db = Database(path)
if self._db.exists():
self._db.open()
self.logger.debug( "Abierta con exito la BD '%s'", path )
else:
self.logger.debug( "Creando la BD '%s'", path )
self._db.create()
self._initialitate( self._db )
def _initialitate(self, db):
pass
def get_db(self):
return self._db
示例10: main
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import open [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
示例11: __init__
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import open [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
示例12: CodernityDB
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import open [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)
示例13: Store
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import open [as 别名]
class Store():
def __init__(self, pathname):
self.store_path = os.path.join(pathname, "store")
self.objects_counter = {}
self.init_store_db()
if not os.path.exists(self.store_path):
os.mkdir(self.store_path)
def init_store_dir(self):
if not os.path.exists(self.store_path):
os.mkdir(self.store_path)
objects_path = os.path.join(self.store_path, "objects")
if not os.path.exists(objects_path):
os.mkdir(objects_path)
backups_path = os.path.join(self.store_path, "backups")
if not os.path.exists(backups_path):
os.mkdir(backups_path)
journal_path = os.path.join(self.store_path, "journal")
if not os.path.exists(journal_path):
os.mkdir(journal_path)
journal_objects_path = os.path.join(self.store_path, "journal/objects")
if not os.path.exists(journal_objects_path):
os.mkdir(journal_objects_path)
journal_backups_path = os.path.join(self.store_path, "journal/backups")
if not os.path.exists(journal_backups_path):
os.mkdir(journal_backups_path)
def init_store_db(self):
self.db = Database(os.path.join(self.store_path, "store.db"))
if not self.db.exists():
self.db.create()
self.db.add_index(WithHashIndex(self.db.path, "hash"))
self.db.add_index(WithPointerIndex(self.db.path, "pointer"))
else:
self.db.open()
def get_path(self):
return self.store_path #volania napr. BackupObject.new...(... , target.get_path())
def get_backup_path(self, backup_name):
backup_path = os.path.join(self.store_path, "backups")
return os.path.join(backup_path, backup_name)
def get_journal_backup_path(self, backup_name):
backup_path = os.path.join(self.get_journal_path(), "backups")
return os.path.join(backup_path, backup_name)
def get_journal_backup_path(self, backup_name):
backup_path = os.path.join(self.get_journal_path(), "backups")
return os.path.join(backup_path, backup_name)
def get_objet_dir_path(self, hash):
return os.path.join(self.store_path, "objects", hash[:2])
def get_object_path(self, hash):
object_path = os.path.join(self.store_path, "objects", hash[:2])
return os.path.join(object_path, hash + ".data")
def get_journal_object_path(self, hash):
object_path = os.path.join(self.get_journal_path(), "objects", hash[:2])
if not os.path.exists(object_path):
os.mkdir(object_path)
return os.path.join(object_path, hash + ".data")
def get_journal_tmp_object_path(self, hash):
object_path = os.path.join(self.get_journal_path(), "objects")
return os.path.join(object_path, hash + ".data")
def get_object_header_path(self, hash):
object_header_path = os.path.join(self.store_path, "objects", hash[:2])
return os.path.join(object_header_path, hash + ".meta")
def get_journal_object_header_path(self, hash):
object_header_path = os.path.join(self.get_journal_path(), "objects", hash[:2])
if not os.path.exists(object_header_path):
os.mkdir(object_header_path)
return os.path.join(object_header_path, hash + ".meta")
def get_journal_tmp_object_header_path(self, hash):
object_header_path = os.path.join(self.get_journal_path(), "objects")
return os.path.join(object_header_path, hash + ".meta")
def get_backups_path(self):
return os.path.join(self.store_path, "backups")
def get_latest_path(self):
latest_tmp_path = os.path.join(self.store_path, "backups")
return os.path.join(latest_tmp_path, "latest")
def get_journal_latest_path(self):
latest_tmp_path = os.path.join(self.get_journal_path(), "backups")
return os.path.join(latest_tmp_path, "latest")
def get_journal_path(self):
return os.path.join(self.store_path, "journal")
def get_all_backups(self):
backups_path = os.path.join(self.store_path, "backups")
backups = os.listdir(backups_path)
#.........这里部分代码省略.........
示例14: SQLAlchemy
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import open [as 别名]
# -*- coding: utf-8 -*-
from lite_mms.basemain import app
app.config["SQLALCHEMY_DATABASE_URI"] = app.config["DBSTR"]
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)
from CodernityDB.database import Database
codernity_db = Database(app.config['CODERNITY_DATABASE_PATH'])
if codernity_db.exists():
codernity_db.open()
codernity_db.reindex()
else:
codernity_db.create()
app.config["MONGODB_DB"] = "localhost"
def init_db():
# 必须要import models, 否则不会建立表
from lite_mms import models
db.create_all()
示例15: questionGet
# 需要导入模块: from CodernityDB.database import Database [as 别名]
# 或者: from CodernityDB.database.Database import open [as 别名]
class SecuQ:
'''
Abstract:
Handles all Questions, input and output input into the database
def questionGet(self):
- reloads all question database attributes
def questionInsert(self,data,descriptor='inclusive'):
-> data = input question dict
ex:
'a':{'active':'True','typ':'slider','range':'0-100','aggregate':True, 'multipoint':True}, ,,,,.................
- inclusive = Updates entried (overwrites) (safe)
- exclusive = deletes all entried not in input (dangerous)
def questionsValidate(self,data):
-> data = input list of questions [(keys)]
each question verified, if not initiated, will be initiated after
'''
def __init__(self,passkey):
self.key = passkey
self.indexdb = DBIndexSystem(self.key)
#self.indexdb.masterIndex
#self.indexdb.Qindex
#self.indexdb.Tindex
#self.indexdb.IndexedTable
#self.indexdb.dbName
self.db = Database(self.indexdb.dbName)
#init variables blank, avoid key errors
self.all = {}
self.valid = {}
self.active = {}
self.notactive = {}
self.unInit = {}
self.typ = {}
self.aggregate = {}
self.multipoint = {}
self.questionGet() #populate variables
#query: all , valid, true,unInit, false, inline
def questionGet(self): #the passkey in question should be loaded from config or index passkey
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)
oQ = Qindex.copy()
# delete unnessesary to modify
# if u use del <key> it deletes all instances in all variables using same reference to dict
oQ.pop('_rev', None)
oQ.pop('_id', None)
oQ.pop('t', None)
#oQ.pop('questions', None)
#returns list in string for of all keys aka the question and metadata
#step through and assign
questionsSet = oQ.keys()
for question in questionsSet:
self.all[question] = oQ[question]
if oQ[question]['active'] == 'True':
self.active[question] = oQ[question]
if oQ[question]['active'] == 'unInit':
self.unInit[question] = oQ[question]
if (oQ[question]['active'] == 'unInit') | (oQ[question]['active'] == 'True'):
self.valid[question] = oQ[question]
#.........这里部分代码省略.........