本文整理汇总了Python中dbm.error方法的典型用法代码示例。如果您正苦于以下问题:Python dbm.error方法的具体用法?Python dbm.error怎么用?Python dbm.error使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dbm
的用法示例。
在下文中一共展示了dbm.error方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: populate_molecules_dict
# 需要导入模块: import dbm [as 别名]
# 或者: from dbm import error [as 别名]
def populate_molecules_dict(self):
# Shelve creates a file with specific database. Using a temp file requires a workaround to open it.
t_filename = tempfile.NamedTemporaryFile(delete=True).name
# dbm could not work: Eg. dbm.error: cannot add item.
# Use dumbdbm for the local execution. Python 3 should fix this issue.
dumb_dict = dbm.open(t_filename, 'n')
shelve_out = shelve.Shelf(dict=dumb_dict)
for uri in self.molecule_uri:
self._logger.debug('ChEMBL getting Molecule from %s', uri)
with URLZSource(uri).open() as f_obj:
for line in f_obj:
#TODO handle malformed JSON lines better
mol = json.loads(line)
shelve_out[str(mol["molecule_chembl_id"])] = mol
self._logger.debug('ChEMBL Molecule loading done.')
return shelve_out
示例2: _load
# 需要导入模块: import dbm [as 别名]
# 或者: from dbm import error [as 别名]
def _load(self):
"""load db content from file"""
db_file = open(self.name, 'r')
try:
try:
return self.codec.decode(db_file.read())
except ValueError as error:
# file contains corrupted json data
msg = (error.args[0] +
"\nInvalid JSON data in %s\n" %
os.path.abspath(self.name) +
"To fix this problem, you can just remove the " +
"corrupted file, a new one will be generated.\n")
error.args = (msg,)
raise DatabaseException(msg)
finally:
db_file.close()
示例3: __init__
# 需要导入模块: import dbm [as 别名]
# 或者: from dbm import error [as 别名]
def __init__(self, name, codec):
"""Open/create a DB file"""
self.name = name
self.codec = codec
try:
self._dbm = ddbm.open(self.name, 'c')
except ddbm.error as exception:
message = str(exception)
if message == self.DBM_CONTENT_ERROR_MSG:
# When a corrupted/old format database is found
# suggest the user to just remove the file
new_message = (
'Dependencies file in %(filename)s seems to use '
'an old format or is corrupted.\n'
'To fix the issue you can just remove the database file(s) '
'and a new one will be generated.'
% {'filename': repr(self.name)})
raise DatabaseException(new_message)
else:
# Re-raise any other exceptions
raise DatabaseException(message)
self._db = {}
self.dirty = set()
示例4: db_opened
# 需要导入模块: import dbm [as 别名]
# 或者: from dbm import error [as 别名]
def db_opened(self, lock=False):
if DISABLE_CACHING_PERSISTENCE:
yield {}
return
from .resilience import retrying
with ExitStack() as stack:
with self.lock:
try:
db = stack.enter_context(
retrying(3, acceptable=GDBMException, sleep=5)(shelve.open)(self.path))
except Exception:
try:
os.unlink(self.path)
except FileNotFoundError:
pass
try:
db = stack.enter_context(shelve.open(self.path))
except Exception:
_logger.warning("Could not open PersistentCache: %s", self.path)
db = {}
if lock:
stack.enter_context(self.lock)
yield db
示例5: truncate_to_max
# 需要导入模块: import dbm [as 别名]
# 或者: from dbm import error [as 别名]
def truncate_to_max(errors, max_errors):
"""If max_errors was specified, truncate the list of errors.
Give the total number of times that the error was found elsewhere.
"""
if len(errors) > max_errors:
start1, end1, err1, msg1, replacements = errors[0]
if len(errors) == (max_errors + 1):
msg1 += " Found once elsewhere."
else:
msg1 += " Found {} times elsewhere.".format(len(errors))
errors = errors[1:max_errors]
errors = [(start1, end1, err1, msg1, replacements)] + errors
return errors
示例6: filter
# 需要导入模块: import dbm [as 别名]
# 或者: from dbm import error [as 别名]
def filter(self, scanlines, header):
"""Filter out any scanlines that existed in the previous granule.
Only works on datasets implementing get_dataname from the header.
"""
dataname = self.ds.get_dataname(header, robust=True)
if self._firstline_db is None:
try:
self._firstline_db = dbm.open(
str(self.granules_firstline_file), "r")
except dbm.error as e: # presumably a lock
tmpdir = tempfile.TemporaryDirectory()
self._tmpdir = tmpdir # should be deleted only when object is
tmp_gfl = str(pathlib.Path(tmpdir.name,
self.granules_firstline_file.name))
logger.warning("Cannot read GFL DB at {!s}: {!s}, "
"presumably in use, copying to {!s}".format(
self.granules_firstline_file, e.args, tmp_gfl))
shutil.copyfile(str(self.granules_firstline_file),
tmp_gfl)
self.granules_firstline_file = tmp_gfl
self._firstline_db = dbm.open(tmp_gfl)
try:
firstline = int(self._firstline_db[dataname])
except KeyError as e:
raise FilterError("Unable to filter firstline: {:s}".format(
e.args[0])) from e
if firstline > scanlines.shape[0]:
logger.warning("Full granule {:s} appears contained in previous one. "
"Refusing to return any lines.".format(dataname))
return scanlines[0:0]
return scanlines[scanlines["hrs_scnlin"] > firstline]
示例7: create
# 需要导入模块: import dbm [as 别名]
# 或者: from dbm import error [as 别名]
def create(self):
"""
Create a new on-disk database.
:raises anydbm.error: If there's a problem creating the database.
"""
if self.filename:
self.db = anydbm.open(self.filename, "n") #raises anydbm.error
self.db["--Reserved--type"] = self.type
self.db.sync()
else:
self.db = {}
示例8: open
# 需要导入模块: import dbm [as 别名]
# 或者: from dbm import error [as 别名]
def open(self):
"""
Open a pre-existing on-disk database.
:raises anydbm.error: If there's a problem opening the database.
:raises ValueError: If the database is not of the right type.
"""
if not self.filename:
raise ValueError("Can only open on-disk databases")
self.db = anydbm.open(self.filename, "w") #raises anydbm.error
try:
if self.db["--Reserved--type"] != self.type:
raise ValueError("Not a %s database" % self.type)
except KeyError:
raise ValueError("Not a recognized database")
示例9: populate_synonyms_for_molecule
# 需要导入模块: import dbm [as 别名]
# 或者: from dbm import error [as 别名]
def populate_synonyms_for_molecule(self, molecule_set, molecules_syn_dict):
def _append_to_mol2syn(m2s_dict, molecule):
"""if molecule has synonyms create a clean entry in m2s_dict with all synms for that chembl_id.
Returns either None if goes ok or the molecule chembl id if something wrong"""
if 'molecule_synonyms' in molecule and molecule['molecule_synonyms']:
synonyms = []
for syn in molecule['molecule_synonyms']:
synonyms.append(syn['synonyms'])
synonyms.append(syn['molecule_synonym'])
synonyms = list(set(synonyms))
m2s_dict[molecule['molecule_chembl_id']] = synonyms
return None
else:
return molecule['molecule_chembl_id']
if not molecule_set or not len(molecule_set):
self._logger.warn("No molecules in set")
return
data = {'molecules':[]}
for mol_k in molecule_set:
if mol_k in self.molecules_dict:
data['molecules'].append(self.molecules_dict[mol_k])
else:
raise ValueError('problem retrieving the molecule info from the local db', str(mol_k))
#if the data is what we expected, process it
if 'molecules' in data:
map_f = functools.partial(_append_to_mol2syn, molecules_syn_dict)
mols_without_syn = \
list(itertools.filterfalse(lambda mol: mol is None, map(map_f, data['molecules'])))
if mols_without_syn:
self._logger.debug('molecule list with no synonyms %s', str(mols_without_syn))
else:
self._logger.error("there is no 'molecules' key in the structure")
raise RuntimeError("unexpected chembl API response")
示例10: _sqlite3
# 需要导入模块: import dbm [as 别名]
# 或者: from dbm import error [as 别名]
def _sqlite3(self, name):
"""Open/create a sqlite3 DB file"""
# Import sqlite here so it's only imported when required
import sqlite3
def dict_factory(cursor, row):
"""convert row to dict"""
data = {}
for idx, col in enumerate(cursor.description):
data[col[0]] = row[idx]
return data
def converter(data):
return self.codec.decode(data.decode('utf-8'))
sqlite3.register_adapter(list, self.codec.encode)
sqlite3.register_adapter(dict, self.codec.encode)
sqlite3.register_converter("json", converter)
conn = sqlite3.connect(
name,
detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES,
isolation_level='DEFERRED')
conn.row_factory = dict_factory
sqlscript = """
create table if not exists doit (
task_id text not null primary key,
task_data json
);"""
try:
conn.execute(sqlscript)
except sqlite3.DatabaseError as exception:
new_message = (
'Dependencies file in %(filename)s seems to use '
'an bad format or is corrupted.\n'
'To fix the issue you can just remove the database file(s) '
'and a new one will be generated.'
'Original error: %(msg)s'
% {'filename': repr(name), 'msg': str(exception)})
raise DatabaseException(new_message)
return conn
示例11: get_error_message
# 需要导入模块: import dbm [as 别名]
# 或者: from dbm import error [as 别名]
def get_error_message(self):
'''return str with error message'''
return self.error_reason
示例12: _get_cache
# 需要导入模块: import dbm [as 别名]
# 或者: from dbm import error [as 别名]
def _get_cache(cachepath):
if cachepath in _cache_shelves:
return _cache_shelves[cachepath]
try:
cache = shelve.open(cachepath, protocol=2)
except dbm.error:
# dbm error on open - delete and retry
print('Error (%s) opening %s - will attempt to delete and re-open.' %
(sys.exc_info()[1], cachepath))
try:
os.remove(cachepath)
cache = shelve.open(cachepath, protocol=2)
except Exception:
print('Error on re-open: %s' % sys.exc_info()[1])
cache = None
except Exception:
# unknown error
print('Could not open cache file %s, maybe name collision. '
'Error: %s' % (cachepath, traceback.format_exc()))
cache = None
# Don't fail on bad caches
if cache is None:
print('Using in-memory shelf for cache file %s' % cachepath)
cache = shelve.Shelf(dict())
_cache_shelves[cachepath] = cache
return cache
示例13: lint
# 需要导入模块: import dbm [as 别名]
# 或者: from dbm import error [as 别名]
def lint(input_file, debug=False):
"""Run the linter on the input file."""
options = load_options()
if isinstance(input_file, string_types):
text = input_file
else:
text = input_file.read()
# Get the checks.
checks = get_checks(options)
# Apply all the checks.
errors = []
for check in checks:
result = check(text)
for error in result:
(start, end, check, message, replacements) = error
(line, column) = line_and_column(text, start)
if not is_quoted(start, text):
errors += [(check, message, line, column, start, end,
end - start, "warning", replacements)]
if len(errors) > options["max_errors"]:
break
# Sort the errors by line and column number.
errors = sorted(errors[:options["max_errors"]], key=lambda e: (e[2], e[3]))
return errors
示例14: assert_error
# 需要导入模块: import dbm [as 别名]
# 或者: from dbm import error [as 别名]
def assert_error(text, check, n=1):
"""Assert that text has n errors of type check."""
assert_error.description = "No {} error for '{}'".format(check, text)
assert(check in [error[0] for error in lint(text)])