用于在bson和json之间转换的库。
python-bsonjs的Python项目详细描述
Info: | See github for the latest source. |
---|---|
Author: | Shane Harvey <shane.harvey@mongodb.com> |
关于
用于python的快速bson到mongodb扩展json转换器 libbson。
示例
>>>importbsonjs>>>bson_bytes=bsonjs.loads('{"hello": "world"}')>>>bson_bytes'\x16\x00\x00\x00\x02hello\x00\x06\x00\x00\x00world\x00\x00'>>>bsonjs.dumps(bson_bytes)'{ "hello" : "world" }'
使用bsonjs和pymongo插入rawbsondocument。
>>>importbsonjs>>>frompymongoimportMongoClient>>>frombson.raw_bsonimportRawBSONDocument>>>client=MongoClient("localhost",27017,document_class=RawBSONDocument)>>>db=client.test>>>bson_bytes=bsonjs.loads('{"_id": 1, "x": 2}')>>>bson_bytes'\x15\x00\x00\x00\x10_id\x00\x01\x00\x00\x00\x10x\x00\x02\x00\x00\x00\x00'>>>result=db.test.insert_one(RawBSONDocument(bson_bytes))>>>result.inserted_id# NOTE: inserted_id is None>>>result.acknowledgedTrue>>>raw_doc=db.test.find_one({'x':2})>>>raw_doc.raw==bson_bytesTrue>>>bsonjs.dumps(raw_doc.raw)'{ "_id" : 1, "x" : 2 }'
速度
在将bson解码为
json和将json编码为bson。见benchmark.py
:
$ python benchmark.py Timing: bsonjs.dumps(b) 10000 loops, best of 3: 0.110911846161 Timing: json_util.dumps(bson.BSON(b).decode()) 10000 loops, best of 3: 1.46571397781 bsonjs is 13.22x faster than json_util Timing: bsonjs.loads(j) 10000 loops, best of 3: 0.0628039836884 Timing: bson.BSON().encode(json_util.loads(j)) 10000 loops, best of 3: 0.683200120926 bsonjs is 11.72x faster than json_util
从源安装
python bsonjs支持cpython 2.6、2.7和3.3+。
编译器
必须为每个版本的python分别构建python bsonjs。打开 这意味着您必须使用与python版本相同的c编译器 用建造的。
- python 2.6和2.7需要Microsoft Visual C++ Compiler for Python 2.7
- Python3.3和3.4需要Microsoft Visual Studio 2010 Professional
- Python3.5及以上版本需要Microsoft Visual Studio 2015
来源
注意,这个存储库包含一个git submodule。必须初始化 子模块使用:
$ git clone --recursive https://github.com/mongodb-labs/python-bsonjs.git
或者,如果您的git版本没有clone --recursive:
$ git clone https://github.com/mongodb-labs/python-bsonjs.git $ cd python-bsonjs $ git submodule update --init --recursive
安装
正确下载源代码后,生成并安装软件包:
$ python setup.py install
测试
运行测试套件:
$ python setup.py test