本文整理汇总了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")
示例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))
示例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))
示例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)
示例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
示例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)
示例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")})
示例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")])))
示例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"))
示例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)
示例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))
示例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()
示例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))
示例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)
示例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)