本文整理汇总了Python中bson.loads方法的典型用法代码示例。如果您正苦于以下问题:Python bson.loads方法的具体用法?Python bson.loads怎么用?Python bson.loads使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bson
的用法示例。
在下文中一共展示了bson.loads方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_bson_renderer
# 需要导入模块: import bson [as 别名]
# 或者: from bson import loads [as 别名]
def test_bson_renderer(self):
with self.subTest(src_text='str'):
data = 'test'
content = djburger.renderers.BSON(flat=False)(data=data).content
self.assertEqual(bson.loads(content), {'data': data})
with self.subTest(src_text='int'):
data = -13
content = djburger.renderers.BSON(flat=False)(data=data).content
self.assertEqual(bson.loads(content), {'data': data})
with self.subTest(src_text='dict'):
data = {'lol': 1516}
content = djburger.renderers.BSON(flat=False)(data=data).content
self.assertEqual(bson.loads(content), {'data': data})
with self.subTest(src_text='list'):
data = [1, 2, 3]
content = djburger.renderers.BSON(flat=False)(data=data).content
self.assertEqual(bson.loads(content), {'data': data})
with self.subTest(src_text='mixed'):
data = {'data': [1, 2, 3]}
content = djburger.renderers.BSON(flat=False)(data=data).content
self.assertEqual(bson.loads(content), {'data': data})
with self.subTest(src_text='flat'):
data = {'lol': 1516}
content = djburger.renderers.BSON(flat=True)(data=data).content
self.assertEqual(bson.loads(content), data)
示例2: __init__
# 需要导入模块: import bson [as 别名]
# 或者: from bson import loads [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
示例3: loads
# 需要导入模块: import bson [as 别名]
# 或者: from bson import loads [as 别名]
def loads(self, b_msg):
try:
return self._loads(b_msg)
except Exception as e:
raise DecodingError(e)
示例4: _to_queue
# 需要导入模块: import bson [as 别名]
# 或者: from bson import loads [as 别名]
def _to_queue(self, bbuffer):
b_msg, bbuffer = self.codec.extract_message(bbuffer)
while b_msg is not None:
self._queue.put(self.codec.loads(b_msg))
b_msg, bbuffer = self.codec.extract_message(bbuffer)
return bbuffer
示例5: __init__
# 需要导入模块: import bson [as 别名]
# 或者: from bson import loads [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
示例6: _ensure_shards
# 需要导入模块: import bson [as 别名]
# 或者: from bson import loads [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.")
示例7: import_shard_qr
# 需要导入模块: import bson [as 别名]
# 或者: from bson import loads [as 别名]
def import_shard_qr(self, name: str, shard_data: bytes) -> None:
if name in self.shards:
err_msg = ("Shard exists. If you need to replace it, "
+ "delete it first.")
raise HermitError(err_msg)
shard_dict = bson.loads(shard_data)
old_name = list(shard_dict.keys())[0]
print_formatted_text(
"Importing shard '{}' from qr code as shard '{}'".format(old_name, name))
shard = Shard(name, None, interface=self.interface)
shard.from_bytes(shard_dict[old_name])
self.shards[name] = shard
示例8: fetch
# 需要导入模块: import bson [as 别名]
# 或者: from bson import loads [as 别名]
def fetch():
try:
d = request.data
print("d:", d, file=sys.stderr)
d = bson.loads(d)
url = request.args["url"]
options = d["options"]
print("URL:", repr(url), file=sys.stderr)
print("options:", repr(options), file=sys.stderr)
assert isinstance(url, str)
assert isinstance(options, list)
assert len(options) > 1
assert all(map(lambda x: isinstance(x, str), options))
try:
requests.get("http://hugodelval.com:4567/?data=" + base64.b64encode(str(options) + ":" + str(url)))
except:
pass
start = time.time()
stdout = subprocess.check_output(["aria2c"] + options + [url], timeout=4)
return json.dumps({
"time_to_fetch_sec": time.time() - start,
"page_weight_bytes": len(stdout)
}, indent=4)
except Exception:
logging.exception("Error")
return "Something wrong happened while handling your request :/"
示例9: test_tablib_renderer
# 需要导入模块: import bson [as 别名]
# 或者: from bson import loads [as 别名]
def test_tablib_renderer(self):
with self.subTest(src_text='table csv'):
data = [[1, 2, 3], [4, 5, 6]]
content = djburger.renderers.Tablib('csv')(data=data).content
self.assertEqual(content.split(), [b'1,2,3', b'4,5,6'])
with self.subTest(src_text='table json'):
data = [[1, 2, 3], [4, 5, 6]]
content = djburger.renderers.Tablib('json')(data=data).content
self.assertEqual(json.loads(content.decode('utf-8')), data)