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


Python BSON.from_dict方法代码示例

本文整理汇总了Python中pymongo.bson.BSON.from_dict方法的典型用法代码示例。如果您正苦于以下问题:Python BSON.from_dict方法的具体用法?Python BSON.from_dict怎么用?Python BSON.from_dict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pymongo.bson.BSON的用法示例。


在下文中一共展示了BSON.from_dict方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_reject_timezone

# 需要导入模块: from pymongo.bson import BSON [as 别名]
# 或者: from pymongo.bson.BSON import from_dict [as 别名]
 def test_reject_timezone(self):
     try:
         BSON.from_dict({u'test zone dst' :
                         datetime.datetime(1993, 4, 4, 2, tzinfo=SomeZone())})
     except NotImplementedError:
         return
     raise ValueError("datetime with zone didn't error")
开发者ID:drewp,项目名称:mongo-python-driver,代码行数:9,代码来源:test_bson.py

示例2: test_custom_class

# 需要导入模块: from pymongo.bson import BSON [as 别名]
# 或者: from pymongo.bson.BSON import from_dict [as 别名]
    def test_custom_class(self):
        self.assert_(isinstance(BSON.from_dict({}).to_dict(), dict))
        self.assertFalse(isinstance(BSON.from_dict({}).to_dict(), SON))
        self.assert_(isinstance(BSON.from_dict({}).to_dict(SON), SON))

        self.assertEqual(1, BSON.from_dict({"x": 1}).to_dict(SON)["x"])

        x = BSON.from_dict({"x": [{"y": 1}]})
        self.assert_(isinstance(x.to_dict(SON)["x"][0], SON))
开发者ID:kurzweil,项目名称:mongo-python-driver,代码行数:11,代码来源:test_bson.py

示例3: update

# 需要导入模块: from pymongo.bson import BSON [as 别名]
# 或者: from pymongo.bson.BSON import from_dict [as 别名]
 def update(col, spec, doc, upsert, multi):
     colname = _make_c_string(col)
     flags = 0
     if upsert:
         flags |= 1 << 0
     if multi:
         flags |= 1 << 1
     fmt = '<i%dsi' % len(colname)
     part = struct.pack(fmt, 0, colname, flags)
     return "%s%s%s" % (part, BSON.from_dict(spec), BSON.from_dict(doc))
开发者ID:viraptor,项目名称:diesel,代码行数:12,代码来源:mongodb.py

示例4: query

# 需要导入模块: from pymongo.bson import BSON [as 别名]
# 或者: from pymongo.bson.BSON import from_dict [as 别名]
 def query(col, spec, fields, skip, limit):
     data = [
         _ZERO, 
         _make_c_string(col), 
         struct.pack('<ii', skip, limit),
         BSON.from_dict(spec or {}),
     ]
     if fields:
         data.append(BSON.from_dict(dict.fromkeys(fields, 1)))
     return "".join(data)
开发者ID:viraptor,项目名称:diesel,代码行数:12,代码来源:mongodb.py

示例5: get_size

# 需要导入模块: from pymongo.bson import BSON [as 别名]
# 或者: from pymongo.bson.BSON import from_dict [as 别名]
 def get_size(self):
     """
     return the size of the underlying bson object
     """
     try:
         size = len(BSON.from_dict(self))
     except:
         self._process_custom_type('bson', self, self.structure)
         size = len(BSON.from_dict(self))
         self._process_custom_type('python', self, self.structure)
     return size
开发者ID:boban-dj,项目名称:mongokit,代码行数:13,代码来源:document.py

示例6: test_utf8

# 需要导入模块: from pymongo.bson import BSON [as 别名]
# 或者: from pymongo.bson.BSON import from_dict [as 别名]
    def test_utf8(self):
        w = {u"aéあ": u"aéあ"}
        self.assertEqual(w, BSON.from_dict(w).to_dict())

        x = {u"aéあ".encode("utf-8"): u"aéあ".encode("utf-8")}
        self.assertEqual(w, BSON.from_dict(x).to_dict())

        y = {"hello": u"aé".encode("iso-8859-1")}
        self.assertRaises(InvalidStringData, BSON.from_dict, y)

        z = {u"aé".encode("iso-8859-1"): "hello"}
        self.assertRaises(InvalidStringData, BSON.from_dict, z)
开发者ID:kurzweil,项目名称:mongo-python-driver,代码行数:14,代码来源:test_bson.py

示例7: test_null_character

# 需要导入模块: from pymongo.bson import BSON [as 别名]
# 或者: from pymongo.bson.BSON import from_dict [as 别名]
    def test_null_character(self):
        doc = {"a": "\x00"}
        self.assertEqual(doc, BSON.from_dict(doc).to_dict())

        doc = {"a": u"\x00"}
        self.assertEqual(doc, BSON.from_dict(doc).to_dict())

        self.assertRaises(InvalidDocument, BSON.from_dict, {"\x00": "a"})
        self.assertRaises(InvalidDocument, BSON.from_dict, {u"\x00": "a"})

        self.assertRaises(InvalidDocument, BSON.from_dict, {"a": re.compile("ab\x00c")})
        self.assertRaises(InvalidDocument, BSON.from_dict, {"a": re.compile(u"ab\x00c")})
开发者ID:kurzweil,项目名称:mongo-python-driver,代码行数:14,代码来源:test_bson.py

示例8: test_move_id

# 需要导入模块: from pymongo.bson import BSON [as 别名]
# 或者: from pymongo.bson.BSON import from_dict [as 别名]
    def test_move_id(self):
        self.assertEqual("\x19\x00\x00\x00\x02_id\x00\x02\x00\x00\x00a\x00"
                         "\x02a\x00\x02\x00\x00\x00a\x00\x00",
                         BSON.from_dict(SON([("a", "a"), ("_id", "a")])))

        self.assertEqual("\x2c\x00\x00\x00"
                         "\x02_id\x00\x02\x00\x00\x00b\x00"
                         "\x03b\x00"
                         "\x19\x00\x00\x00\x02a\x00\x02\x00\x00\x00a\x00"
                         "\x02_id\x00\x02\x00\x00\x00a\x00\x00\x00",
                         BSON.from_dict(SON([("b", SON([("a", "a"), ("_id", "a")])),
                                             ("_id", "b")])))
开发者ID:kurzweil,项目名称:mongo-python-driver,代码行数:14,代码来源:test_bson.py

示例9: main

# 需要导入模块: from pymongo.bson import BSON [as 别名]
# 或者: from pymongo.bson.BSON import from_dict [as 别名]
def main():
    connection = Connection()
    runner = BenchmarkRunner(10000, connection.server_info()["gitVersion"])

    runner.run_benchmark(Encode(small, "small"))
    runner.run_benchmark(Encode(medium, "medium"))
    runner.run_benchmark(Encode(large, "large"))

    runner.run_benchmark(Decode(BSON.from_dict(small), "small"))
    runner.run_benchmark(Decode(BSON.from_dict(medium), "medium"))
    runner.run_benchmark(Decode(BSON.from_dict(large), "large"))

    runner.run_benchmark(Insert(connection.benchmark, medium, "medium"))

    runner.run_benchmark(FindOne(connection.benchmark.medium_no_index, {"x": 5000}, "medium"))
开发者ID:drg,项目名称:mongo-python-driver,代码行数:17,代码来源:benchmark.py

示例10: test_aware_datetime

# 需要导入模块: from pymongo.bson import BSON [as 别名]
# 或者: from pymongo.bson.BSON import from_dict [as 别名]
 def test_aware_datetime(self):
     aware = datetime.datetime(1993, 4, 4, 2, tzinfo=FixedOffset(555, "SomeZone"))
     as_utc = (aware - aware.utcoffset()).replace(tzinfo=utc)
     self.assertEqual(datetime.datetime(1993, 4, 3, 16, 45, tzinfo=utc), as_utc)
     after = BSON.from_dict({"date": aware}).to_dict(tz_aware=True)["date"]
     self.assertEqual(utc, after.tzinfo)
     self.assertEqual(as_utc, after)
开发者ID:kurzweil,项目名称:mongo-python-driver,代码行数:9,代码来源:test_bson.py

示例11: test_ordered_dict

# 需要导入模块: from pymongo.bson import BSON [as 别名]
# 或者: from pymongo.bson.BSON import from_dict [as 别名]
 def test_ordered_dict(self):
     try:
         from collections import OrderedDict
     except ImportError:
         raise SkipTest()
     d = OrderedDict([("one", 1), ("two", 2), ("three", 3), ("four", 4)])
     self.assertEqual(d, BSON.from_dict(d).to_dict(as_class=OrderedDict))
开发者ID:statico,项目名称:mongo-python-driver,代码行数:9,代码来源:test_bson.py

示例12: test_data_files

# 需要导入模块: from pymongo.bson import BSON [as 别名]
# 或者: from pymongo.bson.BSON import from_dict [as 别名]
    def test_data_files(self):
        # TODO don't hardcode this, actually clone the repo
        data_files = "../mongo-qa/modules/bson_tests/tests/*/*.xson"
        generate = True

        for file_name in glob.glob(data_files):
            f = open(file_name, "r")
            xml = f.read()
            f.close()

            try:
                doc = SON.from_xml(xml)
                bson = BSON.from_dict(doc)
            except UnsupportedTag:
                print "skipped file %s: %s" % (file_name, sys.exc_info()[1])
                continue

            try:
                f = open(file_name.replace(".xson", ".bson"), "rb")
                expected = f.read()
                f.close()

                self.assertEqual(bson, expected, file_name)
                self.assertEqual(doc, bson.to_dict(), file_name)

            except IOError:
                if generate:
                    print "generating .bson for %s" % file_name

                    f = open(file_name.replace(".xson", ".bson"), "w")
                    f.write(bson)
                    f.close()
开发者ID:mikejs,项目名称:mongo-python-driver,代码行数:34,代码来源:test_bson.py

示例13: test_from_then_to_dict

# 需要导入模块: from pymongo.bson import BSON [as 别名]
# 或者: from pymongo.bson.BSON import from_dict [as 别名]
    def test_from_then_to_dict(self):

        def helper(dict):
            self.assertEqual(dict, (BSON.from_dict(dict)).to_dict())
        helper({})
        helper({"test": u"hello"})
        self.assert_(isinstance(BSON.from_dict({"hello": "world"})
                                .to_dict()["hello"],
                                   types.UnicodeType))
        helper({"mike": -10120})
        helper({"long": long(10)})
        helper({"really big long": 2147483648})
        helper({u"hello": 0.0013109})
        helper({"something": True})
        helper({"false": False})
        helper({"an array": [1, True, 3.8, u"world"]})
        helper({"an object": {"test": u"something"}})
        helper({"a binary": Binary("test", 100)})
        helper({"a binary": Binary("test", 128)})
        helper({"a binary": Binary("test", 254)})
        helper({"another binary": Binary("test")})
        helper(SON([(u'test dst', datetime.datetime(1993, 4, 4, 2))]))
        helper({"big float": float(10000000000)})

        def from_then_to_dict(dict):
            return dict == (BSON.from_dict(dict)).to_dict()

        qcheck.check_unittest(self, from_then_to_dict,
                              qcheck.gen_mongo_dict(3))
开发者ID:mikejs,项目名称:mongo-python-driver,代码行数:31,代码来源:test_bson.py

示例14: test_aware_datetime

# 需要导入模块: from pymongo.bson import BSON [as 别名]
# 或者: from pymongo.bson.BSON import from_dict [as 别名]
 def test_aware_datetime(self):
     aware = datetime.datetime(1993, 4, 4, 2, tzinfo=SomeZone())
     utc_naive = (aware - aware.utcoffset()).replace(tzinfo=None)
     self.assertEqual(datetime.datetime(1993, 4, 3, 16, 45), utc_naive)
     after = BSON.from_dict({"date": aware}).to_dict()["date"]
     self.assertEqual(None, after.tzinfo)
     self.assertEqual(utc_naive, after)
开发者ID:benaranguren,项目名称:mongo-python-driver,代码行数:9,代码来源:test_bson.py

示例15: test_naive_decode

# 需要导入模块: from pymongo.bson import BSON [as 别名]
# 或者: from pymongo.bson.BSON import from_dict [as 别名]
 def test_naive_decode(self):
     aware = datetime.datetime(1993, 4, 4, 2, tzinfo=FixedOffset(555, "SomeZone"))
     naive_utc = (aware - aware.utcoffset()).replace(tzinfo=None)
     self.assertEqual(datetime.datetime(1993, 4, 3, 16, 45), naive_utc)
     after = BSON.from_dict({"date": aware}).to_dict()["date"]
     self.assertEqual(None, after.tzinfo)
     self.assertEqual(naive_utc, after)
开发者ID:kurzweil,项目名称:mongo-python-driver,代码行数:9,代码来源:test_bson.py


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