本文整理汇总了Python中bson.dumps方法的典型用法代码示例。如果您正苦于以下问题:Python bson.dumps方法的具体用法?Python bson.dumps怎么用?Python bson.dumps使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bson
的用法示例。
在下文中一共展示了bson.dumps方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_json_parser
# 需要导入模块: import bson [as 别名]
# 或者: from bson import dumps [as 别名]
def test_json_parser(self):
factory = RequestFactory()
with self.subTest(src_text='mixed'):
data = {
'name': 'John Doe',
'mail': 'example.gmail.com',
'themes': ['1', '2', '4'],
}
request = factory.post(
'/some/url/',
data=json.dumps(data),
content_type='application/json',
)
p = djburger.parsers.JSON()
parsed_data = p(request)
self.assertEqual(parsed_data, data)
示例2: test_bson_parser
# 需要导入模块: import bson [as 别名]
# 或者: from bson import dumps [as 别名]
def test_bson_parser(self):
factory = RequestFactory()
with self.subTest(src_text='mixed'):
data = {
'name': 'John Doe',
'mail': 'example.gmail.com',
'themes': ['1', '2', '4'],
}
request = factory.post(
'/some/url/',
data=bson.dumps(data),
content_type='application/json',
)
p = djburger.parsers.BSON()
parsed_data = p(request)
self.assertEqual(parsed_data, data)
示例3: load_bson
# 需要导入模块: import bson [as 别名]
# 或者: from bson import dumps [as 别名]
def load_bson():
global BSON
if BSON is None:
try:
from pymongo.bson import BSON
except ImportError:
from bson import dumps
class BSON(object):
@staticmethod
def encode(obj, *args, **kwargs):
return dumps(obj)
#--------------------------------------------------------------------------
示例4: __init__
# 需要导入模块: import bson [as 别名]
# 或者: from bson import dumps [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
示例5: dumps
# 需要导入模块: import bson [as 别名]
# 或者: from bson import dumps [as 别名]
def dumps(self, msg):
try:
return self._dumps(msg)
except Exception as e:
raise EncodingError(e)
示例6: put
# 需要导入模块: import bson [as 别名]
# 或者: from bson import dumps [as 别名]
def put(self, item):
'''
Put item to queue -> codec -> socket.
:param item: Message object.
:type item: dict, list or None
'''
if self._closed:
raise BsonRpcError('Attempt to put items to closed queue.')
msg_bytes = self.codec.into_frame(self.codec.dumps(item))
with self._lock:
self.socket.sendall(msg_bytes)
示例7: __init__
# 需要导入模块: import bson [as 别名]
# 或者: from bson import dumps [as 别名]
def __init__(self, extractor, framer, custom_codec_implementation=None):
self._extractor = extractor
self._framer = framer
if custom_codec_implementation is not None:
self._loads = custom_codec_implementation.loads
self._dumps = custom_codec_implementation.dumps
else:
import json
self._loads = json.loads
self._dumps = json.dumps
示例8: test_initialize_file
# 需要导入模块: import bson [as 别名]
# 或者: from bson import dumps [as 别名]
def test_initialize_file(self):
shard_set = ShardSet(self.interface)
shard_set.config.shards_file = 'shards file'
with patch("builtins.open", mock_open()) as mock_file:
shard_set.initialize_file()
mock_file.assert_called_with('shards file', 'wb')
mock_file().write.called_with(bson.dumps({}))
示例9: _ensure_shards
# 需要导入模块: import bson [as 别名]
# 或者: from bson import dumps [as 别名]
def _ensure_shards(self, shards_expected: bool = True) -> None:
if not self._shards_loaded:
bdata = None
# If the shards dont exist at the place where they
# are expected to by, try to restore them with the externally
# configured getPersistedShards command.
if not os.path.exists(self.config.shards_file):
try:
os.system(self.config.commands['getPersistedShards'])
except TypeError:
pass
# If for some reason the persistence layer failed to create the
# the shards file, we assume that we just need to initialize
# it as an empty bson object.
if not os.path.exists(self.config.shards_file):
with open(self.config.shards_file, 'wb') as f:
f.write(bson.dumps({}))
with open(self.config.shards_file, 'rb') as f:
bdata = bson.loads(f.read())
for name, shard_bytes in bdata.items():
self.shards[name] = Shard(
name, shamir_share.mnemonic_from_bytes(shard_bytes))
self._shards_loaded = True
if len(self.shards) == 0 and shards_expected:
raise HermitError("No shards found. Create some by entering 'shards' mode.")
示例10: initialize_file
# 需要导入模块: import bson [as 别名]
# 或者: from bson import dumps [as 别名]
def initialize_file(self) -> None:
if self.interface.confirm_initialize_file():
with open(self.config.shards_file, 'wb') as f:
f.write(bson.dumps({}))
示例11: to_bytes
# 需要导入模块: import bson [as 别名]
# 或者: from bson import dumps [as 别名]
def to_bytes(self):
data = {name: shard.to_bytes()
for (name, shard) in self.shards.items()}
return bson.dumps(data)
示例12: to_qr_bson
# 需要导入模块: import bson [as 别名]
# 或者: from bson import dumps [as 别名]
def to_qr_bson(self) -> bytes:
"""Serialize this shard as BSON, suitable for a QR code"""
return bson.dumps({self.name: self.to_bytes()})
示例13: fetch
# 需要导入模块: import bson [as 别名]
# 或者: from bson import dumps [as 别名]
def fetch():
# Hit local flask server
conn = HTTPConnection(fetcher_service, 8888)
options = []
for conf in fetch_options.values():
options.append(conf["key"] + "=" + str(conf["value"]).lower())
params = bson.dumps({
"options": options
})
conn.request("POST", "/?url=" + url_to_fetch, params)
response = conn.getresponse()
print("Stats:")
print(response.read().decode())
print()
示例14: __init__
# 需要导入模块: import bson [as 别名]
# 或者: from bson import dumps [as 别名]
def __init__(self, flat=True, **kwargs):
if not _yaml:
raise ImportError('BSON is not installed yet')
self.http_kwargs = {}
super(BSON, self).__init__(
renderer=_bson.dumps,
content_name='obj',
flat=flat,
**kwargs)