本文整理汇总了Python中shelve.Shelf方法的典型用法代码示例。如果您正苦于以下问题:Python shelve.Shelf方法的具体用法?Python shelve.Shelf怎么用?Python shelve.Shelf使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类shelve
的用法示例。
在下文中一共展示了shelve.Shelf方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_mutable_entry
# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
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)
示例2: test_mutable_entry
# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
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)
示例3: __init__
# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
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 = {}
示例4: create_shelf_multi_csv
# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
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
示例5: create_shelf_csv
# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
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
示例6: populate_molecules_dict
# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [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
示例7: test_close
# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
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')
示例8: test_in_memory_shelf
# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
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)
示例9: test_writeback_also_writes_immediately
# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
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
示例10: _empty_mapping
# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
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
示例11: __enter__
# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
def __enter__(self):
assert(not self.is_open)
self.is_open = True
self._ui.debug('ENTER CALLED ON RUNCONTEXT')
raw_db = dumb_dbm.open(self.file_context.file_name)
self.db = shelve.Shelf(raw_db, writeback=True)
if not hasattr(self, 'partitions'):
self.partitions = []
return self
示例12: test_in_memory_shelf
# 需要导入模块: import shelve [as 别名]
# 或者: from shelve import Shelf [as 别名]
def test_in_memory_shelf(self):
d1 = byteskeydict()
s = shelve.Shelf(d1, protocol=0)
s['key1'] = (1,2,3,4)
self.assertEqual(s['key1'], (1,2,3,4))
s.close()
d2 = byteskeydict()
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.assertEqual(len(d2), 1)
self.assertNotEqual(d1.items(), d2.items())