Python dbm.error() Examples
The following are 14
code examples of dbm.error().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
dbm
, or try the search function
.
Example #1
Source File: chembl_lookup.py From data_pipeline with Apache License 2.0 | 6 votes |
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
Example #2
Source File: dependency.py From doit with MIT License | 6 votes |
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()
Example #3
Source File: dependency.py From doit with MIT License | 6 votes |
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()
Example #4
Source File: caching.py From easypy with BSD 3-Clause "New" or "Revised" License | 6 votes |
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
Example #5
Source File: tools.py From proselint with BSD 3-Clause "New" or "Revised" License | 6 votes |
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
Example #6
Source File: filters.py From typhon with MIT License | 5 votes |
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]
Example #7
Source File: basedb.py From scalyr-agent-2 with Apache License 2.0 | 5 votes |
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 = {}
Example #8
Source File: basedb.py From scalyr-agent-2 with Apache License 2.0 | 5 votes |
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")
Example #9
Source File: chembl_lookup.py From data_pipeline with Apache License 2.0 | 5 votes |
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")
Example #10
Source File: dependency.py From doit with MIT License | 5 votes |
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
Example #11
Source File: dependency.py From doit with MIT License | 5 votes |
def get_error_message(self): '''return str with error message''' return self.error_reason
Example #12
Source File: tools.py From proselint with BSD 3-Clause "New" or "Revised" License | 5 votes |
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
Example #13
Source File: tools.py From proselint with BSD 3-Clause "New" or "Revised" License | 5 votes |
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
Example #14
Source File: tools.py From proselint with BSD 3-Clause "New" or "Revised" License | 5 votes |
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)])