Python shelve.Shelf() Examples
The following are 30
code examples of shelve.Shelf().
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
shelve
, or try the search function
.
Example #1
Source File: test_shelve.py From ironpython2 with Apache License 2.0 | 7 votes |
def test_mutable_entry(self): d1 = {} s = shelve.Shelf(d1, protocol=2, writeback=False) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) s['key1'].append(5) self.assertEqual(s['key1'], [1,2,3,4]) s.close() d2 = {} s = shelve.Shelf(d2, protocol=2, writeback=True) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) s['key1'].append(5) self.assertEqual(s['key1'], [1,2,3,4,5]) s.close() self.assertEqual(len(d1), 1) self.assertEqual(len(d2), 1)
Example #2
Source File: test_shelve.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 6 votes |
def test_mutable_entry(self): d1 = {} s = shelve.Shelf(d1, protocol=2, writeback=False) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) s['key1'].append(5) self.assertEqual(s['key1'], [1,2,3,4]) s.close() d2 = {} s = shelve.Shelf(d2, protocol=2, writeback=True) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) s['key1'].append(5) self.assertEqual(s['key1'], [1,2,3,4,5]) s.close() self.assertEqual(len(d1), 1) self.assertEqual(len(d2), 1)
Example #3
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 #4
Source File: test_shelve.py From CTFCrackTools with GNU General Public License v3.0 | 6 votes |
def test_mutable_entry(self): d1 = {} s = shelve.Shelf(d1, protocol=2, writeback=False) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) s['key1'].append(5) self.assertEqual(s['key1'], [1,2,3,4]) s.close() d2 = {} s = shelve.Shelf(d2, protocol=2, writeback=True) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) s['key1'].append(5) self.assertEqual(s['key1'], [1,2,3,4,5]) s.close() self.assertEqual(len(d1), 1) self.assertEqual(len(d2), 1)
Example #5
Source File: test_shelve.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def test_mutable_entry(self): d1 = byteskeydict() s = shelve.Shelf(d1, protocol=2, writeback=False) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) s['key1'].append(5) self.assertEqual(s['key1'], [1,2,3,4]) s.close() d2 = byteskeydict() s = shelve.Shelf(d2, protocol=2, writeback=True) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) s['key1'].append(5) self.assertEqual(s['key1'], [1,2,3,4,5]) s.close() self.assertEqual(len(d1), 1) self.assertEqual(len(d2), 1)
Example #6
Source File: test_shelve.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_mutable_entry(self): d1 = byteskeydict() s = shelve.Shelf(d1, protocol=2, writeback=False) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) s['key1'].append(5) self.assertEqual(s['key1'], [1,2,3,4]) s.close() d2 = byteskeydict() s = shelve.Shelf(d2, protocol=2, writeback=True) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) s['key1'].append(5) self.assertEqual(s['key1'], [1,2,3,4,5]) s.close() self.assertEqual(len(d1), 1) self.assertEqual(len(d2), 1)
Example #7
Source File: test_shelve.py From BinderFilter with MIT License | 6 votes |
def test_mutable_entry(self): d1 = {} s = shelve.Shelf(d1, protocol=2, writeback=False) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) s['key1'].append(5) self.assertEqual(s['key1'], [1,2,3,4]) s.close() d2 = {} s = shelve.Shelf(d2, protocol=2, writeback=True) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) s['key1'].append(5) self.assertEqual(s['key1'], [1,2,3,4,5]) s.close() self.assertEqual(len(d1), 1) self.assertEqual(len(d2), 1)
Example #8
Source File: test_shelve.py From medicare-demo with Apache License 2.0 | 6 votes |
def test_mutable_entry(self): d1 = {} s = shelve.Shelf(d1, protocol=2, writeback=False) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) s['key1'].append(5) self.assertEqual(s['key1'], [1,2,3,4]) s.close() d2 = {} s = shelve.Shelf(d2, protocol=2, writeback=True) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) s['key1'].append(5) self.assertEqual(s['key1'], [1,2,3,4,5]) s.close() self.assertEqual(len(d1), 1) self.assertEqual(len(d2), 1)
Example #9
Source File: repository.py From bugbug with Mozilla Public License 2.0 | 6 votes |
def __init__(self, save): self.save = save try: self.db_experiences = shelve.Shelf( LMDBDict("data/commit_experiences.lmdb", readonly=not save), protocol=pickle.DEFAULT_PROTOCOL, writeback=save, ) except lmdb.Error as e: if not save and "No such file or directory" in str(e): self.db_experiences = {} else: raise if not save: self.mem_experiences = {}
Example #10
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 #11
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 #12
Source File: test_shelve.py From oss-ftp with MIT License | 6 votes |
def test_mutable_entry(self): d1 = {} s = shelve.Shelf(d1, protocol=2, writeback=False) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) s['key1'].append(5) self.assertEqual(s['key1'], [1,2,3,4]) s.close() d2 = {} s = shelve.Shelf(d2, protocol=2, writeback=True) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) s['key1'].append(5) self.assertEqual(s['key1'], [1,2,3,4,5]) s.close() self.assertEqual(len(d1), 1) self.assertEqual(len(d2), 1)
Example #13
Source File: test_shelve.py From android_universal with MIT License | 6 votes |
def test_mutable_entry(self): d1 = byteskeydict() s = shelve.Shelf(d1, protocol=2, writeback=False) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) s['key1'].append(5) self.assertEqual(s['key1'], [1,2,3,4]) s.close() d2 = byteskeydict() s = shelve.Shelf(d2, protocol=2, writeback=True) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) s['key1'].append(5) self.assertEqual(s['key1'], [1,2,3,4,5]) s.close() self.assertEqual(len(d1), 1) self.assertEqual(len(d2), 1)
Example #14
Source File: test_shelve.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_mutable_entry(self): d1 = byteskeydict() s = shelve.Shelf(d1, protocol=2, writeback=False) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) s['key1'].append(5) self.assertEqual(s['key1'], [1,2,3,4]) s.close() d2 = byteskeydict() s = shelve.Shelf(d2, protocol=2, writeback=True) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) s['key1'].append(5) self.assertEqual(s['key1'], [1,2,3,4,5]) s.close() self.assertEqual(len(d1), 1) self.assertEqual(len(d2), 1)
Example #15
Source File: test_shelve.py From gcblue with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_mutable_entry(self): d1 = {} s = shelve.Shelf(d1, protocol=2, writeback=False) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) s['key1'].append(5) self.assertEqual(s['key1'], [1,2,3,4]) s.close() d2 = {} s = shelve.Shelf(d2, protocol=2, writeback=True) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) s['key1'].append(5) self.assertEqual(s['key1'], [1,2,3,4,5]) s.close() self.assertEqual(len(d1), 1) self.assertEqual(len(d2), 1)
Example #16
Source File: test_shelve.py From android_universal with MIT License | 5 votes |
def test_writeback_also_writes_immediately(self): # Issue 5754 d = {} key = 'key' encodedkey = key.encode('utf-8') s = shelve.Shelf(d, writeback=True) s[key] = [1] p1 = d[encodedkey] # Will give a KeyError if backing store not updated s['key'].append(2) s.close() p2 = d[encodedkey] self.assertNotEqual(p1, p2) # Write creates new object in store
Example #17
Source File: test_shelve.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def test_close(self): d1 = {} s = shelve.Shelf(d1, protocol=2, writeback=False) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) self.assertEqual(len(s), 1) s.close() self.assertRaises(ValueError, len, s) try: s['key1'] except ValueError: pass else: self.fail('Closed shelf should not find a key')
Example #18
Source File: test_shelve.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def test_writeback_also_writes_immediately(self): # Issue 5754 d = {} s = shelve.Shelf(d, writeback=True) s['key'] = [1] p1 = d['key'] # Will give a KeyError if backing store not updated s['key'].append(2) s.close() p2 = d['key'] self.assertNotEqual(p1, p2) # Write creates new object in store
Example #19
Source File: test_shelve.py From android_universal with MIT License | 5 votes |
def test_default_protocol(self): with shelve.Shelf({}) as s: self.assertEqual(s._protocol, 3)
Example #20
Source File: test_shelve.py From android_universal with MIT License | 5 votes |
def _empty_mapping(self): if self._in_mem: x= shelve.Shelf(byteskeydict(), **self._args) else: self.counter+=1 x= shelve.open(self.fn+str(self.counter), **self._args) self._db.append(x) return x
Example #21
Source File: test_shelve.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_default_protocol(self): with shelve.Shelf({}) as s: self.assertEqual(s._protocol, 3)
Example #22
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 #23
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 #24
Source File: ch11_ex2.py From Mastering-Object-Oriented-Python-Second-Edition with MIT License | 5 votes |
def close(self) -> None: if self.database: self.database["_DB:max"] = self.max self.database.close() self.database = cast(shelve.Shelf, None)
Example #25
Source File: ch11_ex2.py From Mastering-Object-Oriented-Python-Second-Edition with MIT License | 5 votes |
def new(self, path: Path) -> None: self.database: shelve.Shelf = shelve.open(str(path), "n") self.max: Dict[str, int] = {"Post": 0, "Blog": 0} self.sync()
Example #26
Source File: ch11_ex2.py From Mastering-Object-Oriented-Python-Second-Edition with MIT License | 5 votes |
def __init__(self) -> None: self.database: shelve.Shelf = cast(shelve.Shelf, None) self.max: Dict[str, int] = {"Post": 0, "Blog": 0}
Example #27
Source File: test_shelve.py From medicare-demo with Apache License 2.0 | 5 votes |
def _empty_mapping(self): if self._in_mem: x= shelve.Shelf({}, **self._args) else: self.counter+=1 x= shelve.open(self.fn+str(self.counter), **self._args) self._db.append(x) return x
Example #28
Source File: test_shelve.py From medicare-demo with Apache License 2.0 | 5 votes |
def test_in_memory_shelf(self): d1 = {} s = shelve.Shelf(d1, protocol=0) s['key1'] = (1,2,3,4) self.assertEqual(s['key1'], (1,2,3,4)) s.close() d2 = {} s = shelve.Shelf(d2, protocol=1) s['key1'] = (1,2,3,4) self.assertEqual(s['key1'], (1,2,3,4)) s.close() self.assertEqual(len(d1), 1) self.assertNotEqual(d1, d2)
Example #29
Source File: test_shelve.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def _empty_mapping(self): if self._in_mem: x= shelve.Shelf(byteskeydict(), **self._args) else: self.counter+=1 x= shelve.open(self.fn+str(self.counter), **self._args) self._db.append(x) return x
Example #30
Source File: test_shelve.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_close(self): d1 = {} s = shelve.Shelf(d1, protocol=2, writeback=False) s['key1'] = [1,2,3,4] self.assertEqual(s['key1'], [1,2,3,4]) self.assertEqual(len(s), 1) s.close() self.assertRaises(ValueError, len, s) try: s['key1'] except ValueError: pass else: self.fail('Closed shelf should not find a key')