本文整理汇总了Python中anydbm.open方法的典型用法代码示例。如果您正苦于以下问题:Python anydbm.open方法的具体用法?Python anydbm.open怎么用?Python anydbm.open使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类anydbm
的用法示例。
在下文中一共展示了anydbm.open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: find_test_run_time_diff
# 需要导入模块: import anydbm [as 别名]
# 或者: from anydbm import open [as 别名]
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
示例2: open
# 需要导入模块: import anydbm [as 别名]
# 或者: from anydbm import open [as 别名]
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)
示例3: close
# 需要导入模块: import anydbm [as 别名]
# 或者: from anydbm import open [as 别名]
def close(self): # 关闭
for db in self.open.values():
db.close()
self.open.clear()
###############################################################################
# 全局变量定义 - 配置参数
###############################################################################
# Modul initialization 模块的初始化参数.
示例4: keys
# 需要导入模块: import anydbm [as 别名]
# 或者: from anydbm import open [as 别名]
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
示例5: test_071_anydbm
# 需要导入模块: import anydbm [as 别名]
# 或者: from anydbm import open [as 别名]
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
示例6: test_107_large_file_report
# 需要导入模块: import anydbm [as 别名]
# 或者: from anydbm import open [as 别名]
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
示例7: __contains__
# 需要导入模块: import anydbm [as 别名]
# 或者: from anydbm import open [as 别名]
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()
示例8: keys
# 需要导入模块: import anydbm [as 别名]
# 或者: from anydbm import open [as 别名]
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
示例9: create_shelf_multi_csv
# 需要导入模块: import anydbm [as 别名]
# 或者: from anydbm import open [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
示例10: create_shelf_csv
# 需要导入模块: import anydbm [as 别名]
# 或者: from anydbm import open [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
示例11: open
# 需要导入模块: import anydbm [as 别名]
# 或者: from anydbm import open [as 别名]
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)
示例12: __init__
# 需要导入模块: import anydbm [as 别名]
# 或者: from anydbm import open [as 别名]
def __init__(self, filename, flag='c', protocol=None, writeback=False):
import anydbm
Shelf.__init__(self, anydbm.open(filename, flag), protocol, writeback)
示例13: open
# 需要导入模块: import anydbm [as 别名]
# 或者: from anydbm import open [as 别名]
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)
示例14: test_open_existing_hash
# 需要导入模块: import anydbm [as 别名]
# 或者: from anydbm import open [as 别名]
def test_open_existing_hash(self):
# Verify we can open a file known to be a hash v2 file
db = bsddb185.hashopen(findfile("185test.db"))
self.assertEqual(db["1"], "1")
db.close()
示例15: test_anydbm_create
# 需要导入模块: import anydbm [as 别名]
# 或者: from anydbm import open [as 别名]
def test_anydbm_create(self):
# Verify that anydbm.open does *not* create a bsddb185 file
tmpdir = tempfile.mkdtemp()
try:
dbfile = os.path.join(tmpdir, "foo.db")
anydbm.open(dbfile, "c").close()
ftype = whichdb.whichdb(dbfile)
self.assertNotEqual(ftype, "bsddb185")
finally:
shutil.rmtree(tmpdir)