本文整理汇总了Python中dbm.ndbm.open方法的典型用法代码示例。如果您正苦于以下问题:Python ndbm.open方法的具体用法?Python ndbm.open怎么用?Python ndbm.open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dbm.ndbm
的用法示例。
在下文中一共展示了ndbm.open方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_annotation
# 需要导入模块: from dbm import ndbm [as 别名]
# 或者: from dbm.ndbm import open [as 别名]
def test_annotation(server, dbm_db, tracker_ctx):
with client() as c:
with SampleTracker.annotate(ann="value"):
c.ping()
with SampleTracker.annotate() as ann:
ann.update({"sig": "c.hello()", "user_id": "125"})
c.hello('you')
time.sleep(0.2)
db = dbm.open(db_file, 'r')
headers = list(db.keys())
data = [pickle.loads(db[i]) for i in headers]
data.sort(key=lambda x: x["seq"])
assert data[0]["annotation"] == {"ann": "value"} and \
data[1]["annotation"] == {"sig": "c.hello()", "user_id": "125"}
示例2: test_counter
# 需要导入模块: from dbm import ndbm [as 别名]
# 或者: from dbm.ndbm import open [as 别名]
def test_counter(server, dbm_db, tracker_ctx):
with client() as c:
c.get_phonenumbers("hello", 1)
with SampleTracker.counter():
c.ping()
c.hello("counter")
c.sleep(8)
time.sleep(0.2)
db = dbm.open(db_file, 'r')
headers = list(db.keys())
data = [pickle.loads(db[i]) for i in headers]
data.sort(key=lambda x: x["api"])
get, hello, ping, sleep = data
assert get["api"] == "get_phonenumbers" and get["seq"] == '1'
assert ping["api"] == "ping" and ping["seq"] == '1'
assert hello["api"] == "hello" and hello["seq"] == '2'
assert sleep["api"] == "sleep" and sleep["seq"] == '2'
示例3: record
# 需要导入模块: from dbm import ndbm [as 别名]
# 或者: from dbm.ndbm import open [as 别名]
def record(self, header, exception):
db = dbm.open(db_file, 'w')
key = "%s:%s" % (header.request_id, header.seq)
db[key.encode("ascii")] = pickle.dumps(header.__dict__)
db.close()
示例4: client
# 需要导入模块: from dbm import ndbm [as 别名]
# 或者: from dbm.ndbm import open [as 别名]
def client(client_class=TTrackedClient, port=PORT):
socket = TSocket("localhost", port)
try:
trans = TBufferedTransportFactory().get_transport(socket)
proto = TBinaryProtocolFactory().get_protocol(trans)
trans.open()
args = [addressbook.AddressBookService, proto]
if client_class.__name__ == TTrackedClient.__name__:
args.insert(0, SampleTracker("test_client", "test_server"))
yield client_class(*args)
finally:
trans.close()
示例5: dbm_db
# 需要导入模块: from dbm import ndbm [as 别名]
# 或者: from dbm.ndbm import open [as 别名]
def dbm_db(request):
db = dbm.open(db_file, 'n')
db.close()
def fin():
try:
os.remove(db_file)
except OSError:
pass
request.addfinalizer(fin)
示例6: test_tracker
# 需要导入模块: from dbm import ndbm [as 别名]
# 或者: from dbm.ndbm import open [as 别名]
def test_tracker(server, dbm_db, tracker_ctx):
with client() as c:
c.hello('you')
assert c.tracker.response_header.meta == {'you': 'you'}
time.sleep(0.2)
db = dbm.open(db_file, 'r')
headers = list(db.keys())
assert len(headers) == 1
request_id = headers[0]
data = pickle.loads(db[request_id])
assert "start" in data and "end" in data
data.pop("start")
data.pop("end")
assert data == {
"request_id": request_id.decode("ascii").split(':')[0],
"seq": '1',
"client": "test_client",
"server": "test_server",
"api": "hello",
"status": True,
"annotation": {},
"meta": {},
}
示例7: test_exception
# 需要导入模块: from dbm import ndbm [as 别名]
# 或者: from dbm.ndbm import open [as 别名]
def test_exception(server, dbm_db, tracker_ctx):
with pytest.raises(addressbook.PersonNotExistsError):
with client() as c:
c.get("jane")
db = dbm.open(db_file, 'r')
headers = list(db.keys())
assert len(headers) == 1
header = pickle.loads(db[headers[0]])
assert header["status"] is False
示例8: test_tracked_client_v2_tracked_server_v2
# 需要导入模块: from dbm import ndbm [as 别名]
# 或者: from dbm.ndbm import open [as 别名]
def test_tracked_client_v2_tracked_server_v2(
tracked_server_v2, dbm_db, tracker_ctx):
with client(TTrackedClientV2, PORT + 4) as c:
assert c._upgraded is True
c.ping()
time.sleep(0.2)
db = dbm.open(db_file, 'r')
headers = list(db.keys())
assert len(headers) == 1
request_id = headers[0]
data = pickle.loads(db[request_id])
assert "start" in data and "end" in data
data.pop("start")
data.pop("end")
assert data == {
"request_id": request_id.decode("ascii").split(':')[0],
"seq": '1',
"client": "test_client",
"server": "test_server",
"api": "ping",
"status": True,
"annotation": {},
"meta": {},
}
示例9: test_tracked_client_v2_tracked_server_v3
# 需要导入模块: from dbm import ndbm [as 别名]
# 或者: from dbm.ndbm import open [as 别名]
def test_tracked_client_v2_tracked_server_v3(server, dbm_db, tracker_ctx):
with client(TTrackedClientV2) as c:
assert c._upgraded is True
c.ping()
time.sleep(0.2)
db = dbm.open(db_file, 'r')
headers = list(db.keys())
assert len(headers) == 1
request_id = headers[0]
data = pickle.loads(db[request_id])
assert "start" in data and "end" in data
data.pop("start")
data.pop("end")
assert data == {
"request_id": request_id.decode("ascii").split(':')[0],
"seq": '1',
"client": "test_client",
"server": "test_server",
"api": "ping",
"status": True,
"annotation": {},
"meta": {},
}
assert not hasattr(ctx, 'response_header')
示例10: open
# 需要导入模块: from dbm import ndbm [as 别名]
# 或者: from dbm.ndbm import open [as 别名]
def open(file, flag='r', mode=0o666):
"""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.
"""
global _defaultmod
if _defaultmod is None:
for name in _names:
try:
mod = __import__(name, fromlist=['open'])
except ImportError:
continue
if not _defaultmod:
_defaultmod = mod
_modules[name] = mod
if not _defaultmod:
raise ImportError("no dbm clone found; tried %s" % _names)
# guess the type of an existing database, if not creating a new one
result = whichdb(file) if 'n' not in flag else None
if result is None:
# db doesn't exist or 'n' flag was specified to create a new db
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[0]("need 'c' or 'n' flag to open new db")
elif result == "":
# db type cannot be determined
raise error[0]("db type could not be determined")
elif result not in _modules:
raise error[0]("db type is {0}, but the module is not "
"available".format(result))
else:
mod = _modules[result]
return mod.open(file, flag, mode)