当前位置: 首页>>代码示例>>Python>>正文


Python bson.loads方法代码示例

本文整理汇总了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) 
开发者ID:orsinium-archive,项目名称:djburger,代码行数:27,代码来源:renderers.py

示例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 
开发者ID:seprich,项目名称:py-bson-rpc,代码行数:17,代码来源:socket_queue.py

示例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) 
开发者ID:seprich,项目名称:py-bson-rpc,代码行数:7,代码来源:socket_queue.py

示例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 
开发者ID:seprich,项目名称:py-bson-rpc,代码行数:8,代码来源:socket_queue.py

示例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 
开发者ID:seprich,项目名称:py-bson-rpc,代码行数:12,代码来源:socket_queue.py

示例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.") 
开发者ID:unchained-capital,项目名称:hermit,代码行数:33,代码来源:shard_set.py

示例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 
开发者ID:unchained-capital,项目名称:hermit,代码行数:16,代码来源:shard_set.py

示例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 :/" 
开发者ID:InsecurityAsso,项目名称:inshack-2018,代码行数:28,代码来源:server.py

示例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) 
开发者ID:orsinium-archive,项目名称:djburger,代码行数:11,代码来源:renderers.py


注:本文中的bson.loads方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。