本文整理汇总了Python中bson.BSON属性的典型用法代码示例。如果您正苦于以下问题:Python bson.BSON属性的具体用法?Python bson.BSON怎么用?Python bson.BSON使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类bson
的用法示例。
在下文中一共展示了bson.BSON属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: unpack_response
# 需要导入模块: import bson [as 别名]
# 或者: from bson import BSON [as 别名]
def unpack_response(self, cursor_id=None,
codec_options=_UNICODE_REPLACE_CODEC_OPTIONS):
"""Unpack a response from the database and decode the BSON document(s).
Check the response for errors and unpack, returning a dictionary
containing the response data.
Can raise CursorNotFound, NotMasterError, ExecutionTimeout, or
OperationFailure.
:Parameters:
- `cursor_id` (optional): cursor_id we sent to get this response -
used for raising an informative exception when we get cursor id not
valid at server response
- `codec_options` (optional): an instance of
:class:`~bson.codec_options.CodecOptions`
"""
self.raw_response(cursor_id)
return bson.decode_all(self.documents, codec_options)
示例2: unpack_response
# 需要导入模块: import bson [as 别名]
# 或者: from bson import BSON [as 别名]
def unpack_response(self, cursor_id=None,
codec_options=_UNICODE_REPLACE_CODEC_OPTIONS,
user_fields=None, legacy_response=False):
"""Unpack a response from the database and decode the BSON document(s).
Check the response for errors and unpack, returning a dictionary
containing the response data.
Can raise CursorNotFound, NotMasterError, ExecutionTimeout, or
OperationFailure.
:Parameters:
- `cursor_id` (optional): cursor_id we sent to get this response -
used for raising an informative exception when we get cursor id not
valid at server response
- `codec_options` (optional): an instance of
:class:`~bson.codec_options.CodecOptions`
"""
self.raw_response(cursor_id)
if legacy_response:
return bson.decode_all(self.documents, codec_options)
return bson._decode_all_selective(
self.documents, codec_options, user_fields)
示例3: expand_format
# 需要导入模块: import bson [as 别名]
# 或者: from bson import BSON [as 别名]
def expand_format(fs):
out = ""
i = 0
while i < len(fs):
x = fs[i]
if x in string.digits:
out += fs[i+1] * int(x)
i += 1
else:
out += x
i += 1
return out
###############################################################################
# Custom Cuckoomon "Netlog" protocol - by skier and rep.
# Kind of deprecated, more generic BSON protocol below.
###############################################################################
示例4: _raise_document_too_large
# 需要导入模块: import bson [as 别名]
# 或者: from bson import BSON [as 别名]
def _raise_document_too_large(operation, doc_size, max_size):
"""Internal helper for raising DocumentTooLarge."""
if operation == "insert":
raise DocumentTooLarge("BSON document too large (%d bytes)"
" - the connected server supports"
" BSON document sizes up to %d"
" bytes." % (doc_size, max_size))
else:
# There's nothing intelligent we can say
# about size for update and delete
raise DocumentTooLarge("%r command document too large" % (operation,))
示例5: raw_response
# 需要导入模块: import bson [as 别名]
# 或者: from bson import BSON [as 别名]
def raw_response(self, cursor_id=None):
"""Check the response header from the database, without decoding BSON.
Check the response for errors and unpack.
Can raise CursorNotFound, NotMasterError, ExecutionTimeout, or
OperationFailure.
:Parameters:
- `cursor_id` (optional): cursor_id we sent to get this response -
used for raising an informative exception when we get cursor id not
valid at server response.
"""
if self.flags & 1:
# Shouldn't get this response if we aren't doing a getMore
if cursor_id is None:
raise ProtocolError("No cursor id for getMore operation")
# Fake a getMore command response. OP_GET_MORE provides no
# document.
msg = "Cursor not found, cursor id: %d" % (cursor_id,)
errobj = {"ok": 0, "errmsg": msg, "code": 43}
raise CursorNotFound(msg, 43, errobj)
elif self.flags & 2:
error_object = bson.BSON(self.documents).decode()
# Fake the ok field if it doesn't exist.
error_object.setdefault("ok", 0)
if error_object["$err"].startswith("not master"):
raise NotMasterError(error_object["$err"], error_object)
elif error_object.get("code") == 50:
raise ExecutionTimeout(error_object.get("$err"),
error_object.get("code"),
error_object)
raise OperationFailure("database error: %s" %
error_object.get("$err"),
error_object.get("code"),
error_object)
return [self.documents]
示例6: execute_unack
# 需要导入模块: import bson [as 别名]
# 或者: from bson import BSON [as 别名]
def execute_unack(self, docs, client):
request_id, msg, to_send = self._batch_command(docs)
# Though this isn't strictly a "legacy" write, the helper
# handles publishing commands and sending our message
# without receiving a result. Send 0 for max_doc_size
# to disable size checking. Size checking is handled while
# the documents are encoded to BSON.
self.legacy_write(request_id, msg, 0, False, to_send)
return to_send
示例7: to_object
# 需要导入模块: import bson [as 别名]
# 或者: from bson import BSON [as 别名]
def to_object(bson_bytes):
"""Return deserialized object from BSON bytes"""
return bson.BSON(bson_bytes).decode(CodecOptions(document_class=SON,
tz_aware=True))
示例8: to_bson
# 需要导入模块: import bson [as 别名]
# 或者: from bson import BSON [as 别名]
def to_bson(obj):
"""Return serialized BSON string from object"""
return bson.BSON.encode(obj)
示例9: read_argv
# 需要导入模块: import bson [as 别名]
# 或者: from bson import BSON [as 别名]
def read_argv(self):
return self.read_list(self.read_string)
###############################################################################
# Generic BSON based protocol - by rep
# Allows all kinds of languages / sources to generate input for Cuckoo,
# thus we can reuse report generation / signatures for other API trace sources.
###############################################################################
示例10: __init__
# 需要导入模块: import bson [as 别名]
# 或者: from bson import BSON [as 别名]
def __init__(self, custom_codec_implementation=None):
if custom_codec_implementation is not None:
self._loads = custom_codec_implementation.loads
self._dumps = custom_codec_implementation.dumps
else:
# Use implementation from pymongo or from pybson
import bson
if hasattr(bson, 'BSON'):
# pymongo
self._loads = lambda raw: bson.BSON.decode(bson.BSON(raw))
self._dumps = lambda msg: bytes(bson.BSON.encode(msg))
else:
# pybson
self._loads = bson.loads
self._dumps = bson.dumps
示例11: process_response
# 需要导入模块: import bson [as 别名]
# 或者: from bson import BSON [as 别名]
def process_response(response):
"""All responses should be httplib.OK.
The response should contain a BSON document (content-type
application/bson) or a JSON document (content-type application/json). If
so, the document will be decoded and the result returned, otherwise the
raw binary content will be returned.
:param response: The result of MAASClient.get/post/etc.
:type response: urllib.request.addinfourl (a file-like object that has a
.code attribute.)
"""
if response.code != http.client.OK:
text_status = http.client.responses.get(response.code, "<unknown>")
message = "%s, expected 200 OK" % text_status
raise urllib.error.HTTPError(
response.url, response.code, message, response.headers, response.fp
)
content = response.read()
content_type = response.headers.get_content_type()
if content_type == "application/bson":
return bson.BSON(content).decode()
elif content_type == "application/json":
content_charset = response.headers.get_content_charset()
return json.loads(
content.decode(
"utf-8" if content_charset is None else content_charset
)
)
else:
return content
示例12: get_details
# 需要导入模块: import bson [as 别名]
# 或者: from bson import BSON [as 别名]
def get_details(self, node):
url = reverse("node_handler", args=[node.system_id])
response = self.client.get(url, {"op": "details"})
self.assertEqual(http.client.OK, response.status_code)
self.assertEqual("application/bson", response["content-type"])
return bson.BSON(response.content).decode()
示例13: _unpack_response
# 需要导入模块: import bson [as 别名]
# 或者: from bson import BSON [as 别名]
def _unpack_response(response, cursor_id=None, as_class=dict,
tz_aware=False, uuid_subtype=OLD_UUID_SUBTYPE,
compile_re=True):
"""Unpack a response from the database.
Check the response for errors and unpack, returning a dictionary
containing the response data.
:Parameters:
- `response`: byte string as returned from the database
- `cursor_id` (optional): cursor_id we sent to get this response -
used for raising an informative exception when we get cursor id not
valid at server response
- `as_class` (optional): class to use for resulting documents
"""
response_flag = struct.unpack("<i", response[:4])[0]
if response_flag & 1:
# Shouldn't get this response if we aren't doing a getMore
assert cursor_id is not None
raise CursorNotFound("cursor id '%s' not valid at server" %
cursor_id)
elif response_flag & 2:
error_object = bson.BSON(response[20:]).decode()
if error_object["$err"].startswith("not master"):
raise AutoReconnect(error_object["$err"])
elif error_object.get("code") == 50:
raise ExecutionTimeout(error_object.get("$err"),
error_object.get("code"),
error_object)
raise OperationFailure("database error: %s" %
error_object.get("$err"),
error_object.get("code"),
error_object)
result = {}
result["cursor_id"] = struct.unpack("<q", response[4:12])[0]
result["starting_from"] = struct.unpack("<i", response[12:16])[0]
result["number_returned"] = struct.unpack("<i", response[16:20])[0]
result["data"] = bson.decode_all(response[20:],
as_class, tz_aware, uuid_subtype,
compile_re)
assert len(result["data"]) == result["number_returned"]
return result
示例14: _unpack_response
# 需要导入模块: import bson [as 别名]
# 或者: from bson import BSON [as 别名]
def _unpack_response(response, cursor_id=None, codec_options=CodecOptions()):
"""Unpack a response from the database.
Check the response for errors and unpack, returning a dictionary
containing the response data.
Can raise CursorNotFound, NotMasterError, ExecutionTimeout, or
OperationFailure.
:Parameters:
- `response`: byte string as returned from the database
- `cursor_id` (optional): cursor_id we sent to get this response -
used for raising an informative exception when we get cursor id not
valid at server response
- `codec_options` (optional): an instance of
:class:`~bson.codec_options.CodecOptions`
"""
response_flag = struct.unpack("<i", response[:4])[0]
if response_flag & 1:
# Shouldn't get this response if we aren't doing a getMore
assert cursor_id is not None
# Fake a getMore command response. OP_GET_MORE provides no document.
msg = "Cursor not found, cursor id: %d" % (cursor_id,)
errobj = {"ok": 0, "errmsg": msg, "code": 43}
raise CursorNotFound(msg, 43, errobj)
elif response_flag & 2:
error_object = bson.BSON(response[20:]).decode()
# Fake the ok field if it doesn't exist.
error_object.setdefault("ok", 0)
if error_object["$err"].startswith("not master"):
raise NotMasterError(error_object["$err"], error_object)
elif error_object.get("code") == 50:
raise ExecutionTimeout(error_object.get("$err"),
error_object.get("code"),
error_object)
raise OperationFailure("database error: %s" %
error_object.get("$err"),
error_object.get("code"),
error_object)
result = {"cursor_id": struct.unpack("<q", response[4:12])[0],
"starting_from": struct.unpack("<i", response[12:16])[0],
"number_returned": struct.unpack("<i", response[16:20])[0],
"data": bson.decode_all(response[20:], codec_options)}
assert len(result["data"]) == result["number_returned"]
return result
示例15: _unpack_response
# 需要导入模块: import bson [as 别名]
# 或者: from bson import BSON [as 别名]
def _unpack_response(response,
cursor_id=None,
codec_options=_UNICODE_REPLACE_CODEC_OPTIONS):
"""Unpack a response from the database.
Check the response for errors and unpack, returning a dictionary
containing the response data.
Can raise CursorNotFound, NotMasterError, ExecutionTimeout, or
OperationFailure.
:Parameters:
- `response`: byte string as returned from the database
- `cursor_id` (optional): cursor_id we sent to get this response -
used for raising an informative exception when we get cursor id not
valid at server response
- `codec_options` (optional): an instance of
:class:`~bson.codec_options.CodecOptions`
"""
response_flag = struct.unpack("<i", response[:4])[0]
if response_flag & 1:
# Shouldn't get this response if we aren't doing a getMore
if cursor_id is None:
raise ProtocolError("No cursor id for getMore operation")
# Fake a getMore command response. OP_GET_MORE provides no document.
msg = "Cursor not found, cursor id: %d" % (cursor_id,)
errobj = {"ok": 0, "errmsg": msg, "code": 43}
raise CursorNotFound(msg, 43, errobj)
elif response_flag & 2:
error_object = bson.BSON(response[20:]).decode()
# Fake the ok field if it doesn't exist.
error_object.setdefault("ok", 0)
if error_object["$err"].startswith("not master"):
raise NotMasterError(error_object["$err"], error_object)
elif error_object.get("code") == 50:
raise ExecutionTimeout(error_object.get("$err"),
error_object.get("code"),
error_object)
raise OperationFailure("database error: %s" %
error_object.get("$err"),
error_object.get("code"),
error_object)
result = {"cursor_id": struct.unpack("<q", response[4:12])[0],
"starting_from": struct.unpack("<i", response[12:16])[0],
"number_returned": struct.unpack("<i", response[16:20])[0],
"data": bson.decode_all(response[20:], codec_options)}
assert len(result["data"]) == result["number_returned"]
return result