Python anydbm.open() Examples
The following are 30
code examples of anydbm.open().
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
anydbm
, or try the search function
.
Example #1
Source File: BaseDB.py From python-for-android with Apache License 2.0 | 6 votes |
def __contains__(self, username): """Check if the database contains the specified username. @type username: str @param username: The username to check for. @rtype: bool @return: True if the database contains the username, False otherwise. """ if self.db == None: raise AssertionError("DB not open") self.lock.acquire() try: return self.db.has_key(username) finally: self.lock.release()
Example #2
Source File: anydbm.py From medicare-demo with Apache License 2.0 | 6 votes |
def open(file, flag = 'r', mode = 0666): # guess the type of an existing database from whichdb import whichdb result=whichdb(file) if result is None: # db doesn't exist if 'c' in flag or 'n' in flag: # file doesn't exist and the new # flag was used so use default type mod = _defaultmod else: raise error, "need 'c' or 'n' flag to open new db" elif result == "": # db type cannot be determined raise error, "db type could not be determined" else: mod = __import__(result) return mod.open(file, flag, mode)
Example #3
Source File: Drug.py From data_pipeline with Apache License 2.0 | 6 votes |
def create_shelf_csv(self, uris, key_col, dialect): # sanity check inputs assert uris is not None assert len(uris) > 0 # Shelve creates a file with specific database. Using a temp file requires a workaround to open it. # dumbdbm creates an empty database file. In this way shelve can open it properly. #note: this file is never deleted! filename = tempfile.NamedTemporaryFile(delete=True).name shelf = shelve.Shelf(dict=dbm.open(filename, 'n')) for uri in uris: with URLZSource(uri).open() as f_obj: f_obj = codecs.getreader("utf-8")(f_obj) for row in csv.DictReader(f_obj, dialect=dialect): key_value = row[key_col] key = self.str_hook(key_value) if key is not None: if key in shelf: raise ValueError("Duplicate key %s in uri %s" % (key,uri)) row_dict = dict(row) del row_dict[key_col] shelf[key] = row_dict return shelf
Example #4
Source File: Drug.py From data_pipeline with Apache License 2.0 | 6 votes |
def create_shelf_multi_csv(self, uris, key_col, dialect): # sanity check inputs assert uris is not None assert len(uris) > 0 # Shelve creates a file with specific database. Using a temp file requires a workaround to open it. # dumbdbm creates an empty database file. In this way shelve can open it properly. #note: this file is never deleted! filename = tempfile.NamedTemporaryFile(delete=True).name shelf = shelve.Shelf(dict=dbm.open(filename, 'n')) for uri in uris: with URLZSource(uri).open() as f_obj: f_obj = codecs.getreader("utf-8")(f_obj) for row in csv.DictReader(f_obj, dialect=dialect): key_value = row[key_col] key = self.str_hook(key_value) if key is not None: row_dict = dict(row) del row_dict[key_col] existing = shelf.get(key,[]) existing.append(row_dict) shelf[key] = existing return shelf
Example #5
Source File: bottle.py From annotated-py-projects with MIT License | 6 votes |
def close(self): # 关闭 for db in self.open.values(): db.close() self.open.clear() ############################################################################### # 全局变量定义 - 配置参数 ############################################################################### # Modul initialization 模块的初始化参数.
Example #6
Source File: bottle.py From annotated-py-bottle with MIT License | 6 votes |
def close(self): # 关闭 for db in self.open.values(): db.close() self.open.clear() ############################################################################### # 全局变量定义 - 配置参数 ############################################################################### # Modul initialization 模块的初始化参数.
Example #7
Source File: subunit_trace.py From os-testr with Apache License 2.0 | 6 votes |
def find_test_run_time_diff(test_id, run_time): times_db_path = os.path.join(os.path.join(os.getcwd(), '.testrepository'), 'times.dbm') if os.path.isfile(times_db_path): try: test_times = dbm.open(times_db_path) except Exception: return False try: avg_runtime = float(test_times.get(str(test_id), False)) except Exception: try: avg_runtime = float(test_times[str(test_id)]) except Exception: avg_runtime = False if avg_runtime and avg_runtime > 0: run_time = float(run_time.rstrip('s')) perc_diff = ((run_time - avg_runtime) / avg_runtime) * 100 return perc_diff return False
Example #8
Source File: basedb.py From scalyr-agent-2 with Apache License 2.0 | 6 votes |
def keys(self): """ Return a list of usernames in the database. :rtype: list :returns: The usernames in the database. """ if self.db == None: raise AssertionError("DB not open") self.lock.acquire() try: usernames = self.db.keys() finally: self.lock.release() usernames = [u for u in usernames if not u.startswith("--Reserved--")] return usernames
Example #9
Source File: BaseDB.py From python-for-android with Apache License 2.0 | 6 votes |
def keys(self): """Return a list of usernames in the database. @rtype: list @return: The usernames in the database. """ if self.db == None: raise AssertionError("DB not open") self.lock.acquire() try: usernames = self.db.keys() finally: self.lock.release() usernames = [u for u in usernames if not u.startswith("--Reserved--")] return usernames
Example #10
Source File: BaseDB.py From python-for-android with Apache License 2.0 | 6 votes |
def __contains__(self, username): """Check if the database contains the specified username. @type username: str @param username: The username to check for. @rtype: bool @return: True if the database contains the username, False otherwise. """ if self.db == None: raise AssertionError("DB not open") self.lock.acquire() try: return self.db.has_key(username) finally: self.lock.release()
Example #11
Source File: test.py From python-for-android with Apache License 2.0 | 6 votes |
def test_071_anydbm(): # issue #71 {{{1 import os if sys.version_info[0] == 2: import anydbm else: import dbm as anydbm # FIXME: determine path to sdcard. like: path = os.environ[""] del os.chmod for fname in ( # failed: this is not SL4A application folder... # os.path.join("/data/data/com.googlecode.pythonforandroid", # "files", "test_anydbm.dbm"), # OK: _chmod work well. # os.path.join("/data/local/abc", "test_anydbm.dbm"), # failed: _chmod not worked in FAT (SD card) os.path.join("/sdcard", "sl4a", "test_anydbm.dbm"), ): try: os.remove(fname + ".dat") except: pass anydbm.open(fname, "n") os.remove(fname + ".dat") return True
Example #12
Source File: anydbm.py From Computable with MIT License | 6 votes |
def open(file, flag = 'r', mode = 0666): # guess the type of an existing database from whichdb import whichdb result=whichdb(file) if result is None: # db doesn't exist if 'c' in flag or 'n' in flag: # file doesn't exist and the new # flag was used so use default type mod = _defaultmod else: raise error, "need 'c' or 'n' flag to open new db" elif result == "": # db type cannot be determined raise error, "db type could not be determined" else: mod = __import__(result) return mod.open(file, flag, mode)
Example #13
Source File: test.py From python-for-android with Apache License 2.0 | 6 votes |
def test_107_large_file_report(): # issue #107 {{{1 import os errors = [] fname = "sample.bin" for n in (4294967294, 4294967297): fp = open(fname, "wb") fp.seek(n) fp.write("1".encode("utf-8")) fp.close() ans = os.path.getsize(fname) if ans != (n + 1): errors.append("%s(answer) vs %s(expected)" % (ans, n + 1)) os.remove(fname) if not errors: return True print("can't get size collectly with %s" % str(errors)) return False
Example #14
Source File: BaseDB.py From python-for-android with Apache License 2.0 | 5 votes |
def __setitem__(self, username, value): if self.db == None: raise AssertionError("DB not open") valueStr = self._setItem(username, value) self.lock.acquire() try: self.db[username] = valueStr if self.filename: self.db.sync() finally: self.lock.release()
Example #15
Source File: test_anydbm.py From medicare-demo with Apache License 2.0 | 5 votes |
def test_anydbm_read(self): self.init_db() f = anydbm.open(_fname, 'r') self.read_helper(f) f.close()
Example #16
Source File: BaseDB.py From python-for-android with Apache License 2.0 | 5 votes |
def __delitem__(self, username): if self.db == None: raise AssertionError("DB not open") self.lock.acquire() try: del(self.db[username]) if self.filename: self.db.sync() finally: self.lock.release()
Example #17
Source File: test_anydbm.py From medicare-demo with Apache License 2.0 | 5 votes |
def test_anydbm_keys(self): self.init_db() f = anydbm.open(_fname, 'r') keys = self.keys_helper(f) f.close()
Example #18
Source File: Drug.py From data_pipeline with Apache License 2.0 | 5 votes |
def create_shelf(self, uris, key_f): #sanity check inputs assert uris is not None assert len(uris) > 0 # Shelve creates a file with specific database. Using a temp file requires a workaround to open it. # dumbdbm creates an empty database file. In this way shelve can open it properly. #note: this file is never deleted! filename = tempfile.NamedTemporaryFile(delete=True).name shelf = shelve.Shelf(dict=dbm.open(filename, 'n')) for uri in uris: with URLZSource(uri).open() as f_obj: #for python2 we need to decode utf-8 if sys.version_info < (3, 0): f_obj = codecs.getreader("utf-8")(f_obj) for line_no, line in enumerate(f_obj): try: obj = json.loads(line) except json.JSONDecodeError as e: self.logger.error("Unable to read line %d %s %s", line_no, uri, e) raise e key_value = key_f(obj) key = self.str_hook(key_value) if key is not None: if key in shelf: raise ValueError("Duplicate key %s in uri %s" % (key,uri)) shelf[key] = obj return shelf
Example #19
Source File: test_anydbm.py From medicare-demo with Apache License 2.0 | 5 votes |
def init_db(self): f = anydbm.open(_fname, 'n') for k in self._dict: f[k] = self._dict[k] f.close()
Example #20
Source File: Drug.py From data_pipeline with Apache License 2.0 | 5 votes |
def create_shelf_multi(self, uris, key_f): #sanity check inputs assert uris is not None assert len(uris) > 0 # Shelve creates a file with specific database. Using a temp file requires a workaround to open it. # dumbdbm creates an empty database file. In this way shelve can open it properly. #note: this file is never deleted! filename = tempfile.NamedTemporaryFile(delete=True).name shelf = shelve.Shelf(dict=dbm.open(filename, 'n')) for uri in uris: with URLZSource(uri).open() as f_obj: #for python2 we need to decode utf-8 if sys.version_info < (3, 0): f_obj = codecs.getreader("utf-8")(f_obj) for line_no, line in enumerate(f_obj): try: obj = json.loads(line) except json.JSONDecodeError as e: self.logger.error("Unable to read line %d %s", line_no, uri) raise e key_value = key_f(obj) key = self.str_hook(key_value) if key is not None: existing = shelf.get(key,[]) existing.append(obj) shelf[key] = existing return shelf
Example #21
Source File: BaseDB.py From python-for-android with Apache License 2.0 | 5 votes |
def __setitem__(self, username, value): if self.db == None: raise AssertionError("DB not open") valueStr = self._setItem(username, value) self.lock.acquire() try: self.db[username] = valueStr if self.filename: self.db.sync() finally: self.lock.release()
Example #22
Source File: BaseDB.py From python-for-android with Apache License 2.0 | 5 votes |
def __getitem__(self, username): if self.db == None: raise AssertionError("DB not open") self.lock.acquire() try: valueStr = self.db[username] finally: self.lock.release() return self._getItem(username, valueStr)
Example #23
Source File: BaseDB.py From python-for-android with Apache License 2.0 | 5 votes |
def create(self): """Create a new on-disk database. @raise 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 #24
Source File: test.py From python-for-android with Apache License 2.0 | 5 votes |
def test_029_isfile(): # issue #29 {{{1 import os # FIXME: determine path to sdcard. like: path = os.environ[""] path = os.path.dirname(__file__) fname = os.path.abspath(os.path.join(path, "test_isfile")) open(fname, "w").write("this is test") os.path.isfile(fname) os.remove(fname) try: assert os.path.isfile(fname) is False except Exception as e: print(e) return False return True
Example #25
Source File: shelve.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def open(filename, flag='c', protocol=None, writeback=False): """Open a persistent dictionary for reading and writing. The filename parameter is the base filename for the underlying database. As a side-effect, an extension may be added to the filename and more than one file may be created. The optional flag parameter has the same interpretation as the flag parameter of anydbm.open(). The optional protocol parameter specifies the version of the pickle protocol (0, 1, or 2). See the module's __doc__ string for an overview of the interface. """ return DbfilenameShelf(filename, flag, protocol, writeback)
Example #26
Source File: shelve.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def __init__(self, filename, flag='c', protocol=None, writeback=False): import anydbm Shelf.__init__(self, anydbm.open(filename, flag), protocol, writeback)
Example #27
Source File: anydbm.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def open(file, flag='r', mode=0666): """Open or create database at path given by *file*. Optional argument *flag* can be 'r' (default) for read-only access, 'w' for read-write access of an existing database, 'c' for read-write access to a new or existing database, and 'n' for read-write access to a new database. Note: 'r' and 'w' fail if the database doesn't exist; 'c' creates it only if it doesn't exist; and 'n' always creates a new database. """ # guess the type of an existing database from whichdb import whichdb result=whichdb(file) if result is None: # db doesn't exist if 'c' in flag or 'n' in flag: # file doesn't exist and the new # flag was used so use default type mod = _defaultmod else: raise error, "need 'c' or 'n' flag to open new db" elif result == "": # db type cannot be determined raise error, "db type could not be determined" else: mod = __import__(result) return mod.open(file, flag, mode)
Example #28
Source File: bottle.py From annotated-py-projects with MIT License | 5 votes |
def __delitem__(self, key): if key not in self.open: self.open[key].clear() self.open[key].save() del self.open[key]
Example #29
Source File: bottle.py From annotated-py-projects with MIT License | 5 votes |
def __getitem__(self, key): if key not in self.open and not key.startswith('_'): self.open[key] = BottleBucket(key) # 调用 return self.open[key]
Example #30
Source File: bottle.py From annotated-py-projects with MIT License | 5 votes |
def __init__(self): self.__dict__['open'] = {}