本文整理匯總了Python中dbm.whichdb方法的典型用法代碼示例。如果您正苦於以下問題:Python dbm.whichdb方法的具體用法?Python dbm.whichdb怎麽用?Python dbm.whichdb使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類dbm
的用法示例。
在下文中一共展示了dbm.whichdb方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_compat_pickle
# 需要導入模塊: import dbm [as 別名]
# 或者: from dbm import whichdb [as 別名]
def test_compat_pickle(self):
tests = [
(range(1, 7), '__builtin__', 'xrange'),
(map(int, '123'), 'itertools', 'imap'),
(functools.reduce, '__builtin__', 'reduce'),
(dbm.whichdb, 'whichdb', 'whichdb'),
(Exception(), 'exceptions', 'Exception'),
(collections.UserDict(), 'UserDict', 'IterableUserDict'),
(collections.UserList(), 'UserList', 'UserList'),
(collections.defaultdict(), 'collections', 'defaultdict'),
]
for val, mod, name in tests:
for proto in range(3):
with self.subTest(type=type(val), proto=proto):
pickled = self.dumps(val, proto)
self.assertIn(('c%s\n%s' % (mod, name)).encode(), pickled)
self.assertIs(type(self.loads(pickled)), type(val))
示例2: test_compat_unpickle
# 需要導入模塊: import dbm [as 別名]
# 或者: from dbm import whichdb [as 別名]
def test_compat_unpickle(self):
# xrange(1, 7)
pickled = b'\x80\x02c__builtin__\nxrange\nK\x01K\x07K\x01\x87R.'
unpickled = self.loads(pickled)
self.assertIs(type(unpickled), range)
self.assertEqual(unpickled, range(1, 7))
self.assertEqual(list(unpickled), [1, 2, 3, 4, 5, 6])
# reduce
pickled = b'\x80\x02c__builtin__\nreduce\n.'
self.assertIs(self.loads(pickled), functools.reduce)
# whichdb.whichdb
pickled = b'\x80\x02cwhichdb\nwhichdb\n.'
self.assertIs(self.loads(pickled), dbm.whichdb)
# Exception(), StandardError()
for name in (b'Exception', b'StandardError'):
pickled = (b'\x80\x02cexceptions\n' + name + b'\nU\x03ugh\x85R.')
unpickled = self.loads(pickled)
self.assertIs(type(unpickled), Exception)
self.assertEqual(str(unpickled), 'ugh')
# UserDict.UserDict({1: 2}), UserDict.IterableUserDict({1: 2})
for name in (b'UserDict', b'IterableUserDict'):
pickled = (b'\x80\x02(cUserDict\n' + name +
b'\no}U\x04data}K\x01K\x02ssb.')
unpickled = self.loads(pickled)
self.assertIs(type(unpickled), collections.UserDict)
self.assertEqual(unpickled, collections.UserDict({1: 2}))
示例3: execute
# 需要導入模塊: import dbm [as 別名]
# 或者: from dbm import whichdb [as 別名]
def execute(self, opt_values, pos_args):
dep_file = opt_values['dep_file']
db_type = whichdb(dep_file)
print("DBM type is '%s'" % db_type)
if db_type in ('dbm', 'dbm.ndbm'): # pragma: no cover
raise InvalidCommand('ndbm does not support iteration of elements')
data = dbm.open(dep_file)
for key, value_str in dbm_iter(data):
value_dict = json.loads(value_str.decode('utf-8'))
value_fmt = pprint.pformat(value_dict, indent=4, width=100)
print("{key} -> {value}".format(key=key, value=value_fmt))
示例4: dep_manager_fixture
# 需要導入模塊: import dbm [as 別名]
# 或者: from dbm import whichdb [as 別名]
def dep_manager_fixture(request, dep_class, tmp_path_factory):
filename = str(tmp_path_factory.mktemp('x', True) / 'testdb')
dep_file = Dependency(dep_class, filename)
dep_file.whichdb = whichdb(dep_file.name) if dep_class is DbmDB else 'XXX'
dep_file.name_ext = db_ext.get(dep_file.whichdb, [''])
def remove_depfile():
if not dep_file._closed:
dep_file.close()
remove_db(dep_file.name)
request.addfinalizer(remove_depfile)
return dep_file
示例5: create_cache_with_backend
# 需要導入模塊: import dbm [as 別名]
# 或者: from dbm import whichdb [as 別名]
def create_cache_with_backend(name, pkgname='astropy'):
dldir, urlmapfn = _get_download_cache_locs(pkgname)
e = dbm.whichdb(urlmapfn)
if e is not None:
raise IOError(f"Cache already exists in format {e} ({name} requested)")
try:
m = import_module(name)
except ImportError:
pytest.skip(f"Module {name} not available")
with m.open(urlmapfn, "c"):
pass