Git Product home page Git Product logo

python-bsonjs's Introduction

python-bsonjs

View build status

Info

See github for the latest source.

Author

Shane Harvey <[email protected]>

About

A fast BSON to MongoDB Extended JSON converter for Python that uses libbson.

Installation

python-bsonjs can be installed with pip:

$ python -m pip install python-bsonjs

Examples

>>> import bsonjs
>>> 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" }'

Using bsonjs with pymongo to insert a RawBSONDocument.

>>> import bsonjs
>>> from pymongo import MongoClient
>>> from bson.raw_bson import RawBSONDocument
>>> 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.acknowledged
True
>>> raw_doc = db.test.find_one({'x': 2})
>>> raw_doc.raw == bson_bytes
True
>>> bsonjs.dumps(raw_doc.raw)
'{ "_id" : 1, "x" : 2 }'

Speed

bsonjs is roughly 10-15x faster than PyMongo's json_util at decoding BSON to JSON and encoding JSON to BSON. See `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

Limitations

Top Level Arrays

Because libbson does not distinguish between top level arrays and top level documents, neither does python-bsonjs. This means that if you give dumps or dump a top level array it will give you back a dictionary. Below are two examples of this behavior

>>> import bson
>>> from bson import json_util
>>> import bsonjs
>>> bson.decode(bsonjs.loads(json_util.dumps(["a", "b", "c"])))
{'0': 'a', '1': 'b', '2': 'c'}
>>> bson.decode(bsonjs.loads(json_util.dumps([])))
{}

One potential solution to this problem is to wrap your list in a dictionary, like so

>>> list = ["a", "b", "c"]
>>> dict = {"data": list}
>>> wrapped = bson.decode(bsonjs.loads(json_util.dumps(dict)))
{'data': ['a', 'b', 'c']}
>>> wrapped["data"]
['a', 'b', 'c']

Installing From Source

python-bsonjs supports CPython 3.7+.

Compiler

You must build python-bsonjs separately for each version of Python. On Windows this means you must use the same C compiler your Python version was built with.

  • Windows build requires Microsoft Visual Studio 2015

Source

You can download the source using git:

$ git clone https://github.com/mongodb-labs/python-bsonjs.git

Install

Once you have the source properly downloaded, build and install the package:

$ pip install -v .

Test

To run the test suite:

$ python -m pytest

python-bsonjs's People

Contributors

agolin95 avatar behackett avatar blink1073 avatar juliusgeo avatar shaneharvey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-bsonjs's Issues

Help using aggregate_raw_results

I've been going around in circles a bit trying to understand if this library can be used to speed up decoding an aggregation query.

Or whether after recent pymongo updates its needed at all.

documents = []

cursor = collection.aggregate_raw_batches(
                              pipeline=aggregation_query,
)
while True:
    try:
        documents.extend([x for x in decode_all(cursor.next())])
    except StopIteration:
        break

How would I use bsonjs.dumps instead?

Wheels for Python 3.7, 3.8, 3.9

The latest wheels available on PyPI are for Python 3.6. It would be helpful to have prebuilt wheels available for more recent Python versions.

Build fails with Python 3.8.5 on Windows 10

Failed to build python-bsonjs
DEPRECATION: Could not build wheels for python-bsonjs which do not use PEP 517. pip will fall back to legacy 'setup.py install' for these. pip 21.0 will remove support for this functionality. A possible replacement is to fix the wheel build issue reported above. You can find discussion regarding this at https://github.com/pypa/pip/issues/8368.
Installing collected packages: python-bsonjs
    Running setup.py install for python-bsonjs ... error
    ERROR: Command errored out with exit status 1:
     command: 'c:\python\toolusagelogger\_env\scripts\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\bam5wi\\AppData\\Local\\Temp\\pip-install-dutuekdi\\python-bsonjs\\setup.py'"'"'; __file__='"'"'C:\\Users\\bam5wi\\AppData\\Local\\Temp\\pip-install-dutuekdi\\python-bsonjs\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\bam5wi\AppData\Local\Temp\pip-record-dbaa8bjo\install-record.txt' --single-version-externally-managed --compile --install-headers 'c:\python\toolusagelogger\_env\include\site\python3.8\python-bsonjs'
         cwd: C:\Users\bam5wi\AppData\Local\Temp\pip-install-dutuekdi\python-bsonjs\
    Complete output (75 lines):
    running install
    running build
    running build_ext
    building 'bsonjs' extension
    creating build
    creating build\temp.win-amd64-3.8
    creating build\temp.win-amd64-3.8\Release
    creating build\temp.win-amd64-3.8\Release\src
    creating build\temp.win-amd64-3.8\Release\libbson
    creating build\temp.win-amd64-3.8\Release\libbson\src
    creating build\temp.win-amd64-3.8\Release\libbson\src\bson
    creating build\temp.win-amd64-3.8\Release\libbson\src\jsonsl
    C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\Bin\x86_amd64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -DBSON_COMPILATION=1 -Isrc -Isrc/bson -Ilibbson/src -Ilibbson/src/jsonsl -Ilibbson/src/bson -Ic:\python\toolusagelogger\_env\include -IC:\Python\_envs\Python38\include -IC:\Python\_envs\Python38\include "-IC:\Program Files\Microsoft SDKs\Windows\v7.1\Include" "-IC:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include" -IC:\Python\ToolUsageLogger\_env\Include -IC:\Python\ToolUsageLogger\_env\Include\cairo "-IC:\Program Files (x86)\Windows Kits\10\Include\10.0.10150.0\ucrt" /Tcsrc/bsonjs.c /Fobuild\temp.win-amd64-3.8\Release\src/bsonjs.obj
    bsonjs.c
    c:\python\_envs\python38\include\object.h(436) : error C2054: expected '(' to follow 'inline'
    c:\python\_envs\python38\include\object.h(437) : error C2085: '_Py_NewReference' : not in formal parameter list
    c:\python\_envs\python38\include\object.h(437) : error C2143: syntax error : missing ';' before '{'
    c:\python\_envs\python38\include\object.h(446) : error C2054: expected '(' to follow 'inline'
    c:\python\_envs\python38\include\object.h(447) : error C2085: '_Py_ForgetReference' : not in formal parameter list
    c:\python\_envs\python38\include\object.h(447) : error C2143: syntax error : missing ';' before '{'
    c:\python\_envs\python38\include\object.h(456) : error C2054: expected '(' to follow 'inline'
    c:\python\_envs\python38\include\object.h(457) : error C2085: '_Py_INCREF' : not in formal parameter list
    c:\python\_envs\python38\include\object.h(457) : error C2143: syntax error : missing ';' before '{'
    c:\python\_envs\python38\include\object.h(464) : error C2054: expected '(' to follow 'inline'
    c:\python\_envs\python38\include\object.h(466) : error C2085: '_Py_DECREF' : not in formal parameter list
    c:\python\_envs\python38\include\object.h(466) : error C2143: syntax error : missing ';' before '{'
    c:\python\_envs\python38\include\object.h(529) : error C2054: expected '(' to follow 'inline'
    c:\python\_envs\python38\include\object.h(530) : error C2085: '_Py_XINCREF' : not in formal parameter list
    c:\python\_envs\python38\include\object.h(530) : error C2143: syntax error : missing ';' before '{'
    c:\python\_envs\python38\include\object.h(538) : error C2054: expected '(' to follow 'inline'
    c:\python\_envs\python38\include\object.h(539) : error C2085: '_Py_XDECREF' : not in formal parameter list
    c:\python\_envs\python38\include\object.h(539) : error C2143: syntax error : missing ';' before '{'
    c:\python\_envs\python38\include\cpython/object.h(339) : error C2054: expected '(' to follow 'inline'
    c:\python\_envs\python38\include\cpython/object.h(340) : error C2085: '_Py_Dealloc_inline' : not in formal parameter list
    c:\python\_envs\python38\include\cpython/object.h(340) : error C2143: syntax error : missing ';' before '{'
    c:\python\_envs\python38\include\objimpl.h(136) : error C2054: expected '(' to follow 'inline'
    c:\python\_envs\python38\include\objimpl.h(138) : error C2085: '_PyObject_INIT' : not in formal parameter list
    c:\python\_envs\python38\include\objimpl.h(138) : error C2143: syntax error : missing ';' before '{'
    c:\python\_envs\python38\include\objimpl.h(151) : error C2054: expected '(' to follow 'inline'
    c:\python\_envs\python38\include\objimpl.h(153) : error C2085: '_PyObject_INIT_VAR' : not in formal parameter list
    c:\python\_envs\python38\include\objimpl.h(153) : error C2143: syntax error : missing ';' before '{'
    c:\python\_envs\python38\include\cpython/abstract.h(75) : error C2054: expected '(' to follow 'inline'
    c:\python\_envs\python38\include\cpython/abstract.h(76) : error C2085: 'PyVectorcall_NARGS' : not in formal parameter list
    c:\python\_envs\python38\include\cpython/abstract.h(76) : error C2143: syntax error : missing ';' before '{'
    c:\python\_envs\python38\include\cpython/abstract.h(81) : error C2054: expected '(' to follow 'inline'
    c:\python\_envs\python38\include\cpython/abstract.h(82) : error C2085: '_PyVectorcall_Function' : not in formal parameter list
    c:\python\_envs\python38\include\cpython/abstract.h(82) : error C2143: syntax error : missing ';' before '{'
    c:\python\_envs\python38\include\cpython/abstract.h(114) : error C2054: expected '(' to follow 'inline'
    c:\python\_envs\python38\include\cpython/abstract.h(117) : error C2085: '_PyObject_Vectorcall' : not in formal parameter list
    c:\python\_envs\python38\include\cpython/abstract.h(117) : error C2143: syntax error : missing ';' before '{'
    c:\python\_envs\python38\include\cpython/abstract.h(144) : error C2054: expected '(' to follow 'inline'
    c:\python\_envs\python38\include\cpython/abstract.h(146) : error C2085: '_PyObject_FastCall' : not in formal parameter list
    c:\python\_envs\python38\include\cpython/abstract.h(146) : error C2143: syntax error : missing ';' before '{'
    c:\python\_envs\python38\include\cpython/abstract.h(151) : error C2054: expected '(' to follow 'inline'
    c:\python\_envs\python38\include\cpython/abstract.h(152) : error C2085: '_PyObject_CallNoArg' : not in formal parameter list
    c:\python\_envs\python38\include\cpython/abstract.h(152) : error C2143: syntax error : missing ';' before '{'
    c:\program files\microsoft sdks\windows\v7.1\include\sal_supp.h(57) : warning C4005: '__useHeader' : macro redefinition
            C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\sal.h(2886) : see previous definition of '__useHeader'
    c:\program files\microsoft sdks\windows\v7.1\include\specstrings_supp.h(77) : warning C4005: '__on_failure' : macro redefinition
            C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\sal.h(2896) : see previous definition of '__on_failure'
    c:\users\bam5wi\appdata\local\temp\pip-install-dutuekdi\python-bsonjs\libbson\src\bson\bson-compat.h(110) : warning C4005: 'PRIi32' : macro redefinition
            C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\inttypes.h(78) : see previous definition of 'PRIi32'
    c:\users\bam5wi\appdata\local\temp\pip-install-dutuekdi\python-bsonjs\libbson\src\bson\bson-compat.h(111) : warning C4005: 'PRId32' : macro redefinition
            C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\inttypes.h(68) : see previous definition of 'PRId32'
    c:\users\bam5wi\appdata\local\temp\pip-install-dutuekdi\python-bsonjs\libbson\src\bson\bson-compat.h(112) : warning C4005: 'PRIu32' : macro redefinition
            C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\inttypes.h(98) : see previous definition of 'PRIu32'
    c:\users\bam5wi\appdata\local\temp\pip-install-dutuekdi\python-bsonjs\libbson\src\bson\bson-compat.h(113) : warning C4005: 'PRIi64' : macro redefinition
            C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\inttypes.h(132) : see previous definition of 'PRIi64'
    c:\users\bam5wi\appdata\local\temp\pip-install-dutuekdi\python-bsonjs\libbson\src\bson\bson-compat.h(114) : warning C4005: 'PRId64' : macro redefinition
            C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\inttypes.h(126) : see previous definition of 'PRId64'
    c:\users\bam5wi\appdata\local\temp\pip-install-dutuekdi\python-bsonjs\libbson\src\bson\bson-compat.h(115) : warning C4005: 'PRIu64' : macro redefinition
            C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\inttypes.h(144) : see previous definition of 'PRIu64'
    src/bsonjs.c(102) : warning C4013: '_Py_DECREF' undefined; assuming extern returning int
    src/bsonjs.c(107) : warning C4013: '_Py_INCREF' undefined; assuming extern returning int
    error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\Bin\\x86_amd64\\cl.exe' failed with exit status 2

Test on Windows

We should test on Evergreen which already has all the pythons and compilers we need to test on Windows. Alternatively we could try out Travis' experimental Windows support, Azure Pipelines, AppVeyor, or GitHub Actions.

Fix initialization from incompatible pointer type compile warnings

We should fix these compile warnings:

/bsonjs.c -o build/temp.linux-x86_64-3.10/src/bsonjs.o
src/bsonjs.c:234:14: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
     {"dump", dump, METH_VARARGS | METH_KEYWORDS, dump__doc__},
              ^~~~
src/bsonjs.c:234:14: note: (near initialization for ‘BsonjsClientMethods[0].ml_meth’)
src/bsonjs.c:235:15: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
     {"dumps", dumps, METH_VARARGS | METH_KEYWORDS, dumps__doc__},
               ^~~~~

Publishing wheel packages to PyPI

Dear Shane,

thanks a stack for conceiving and maintaining the excellent python-bsonjs package.

On a CI run of ours 1, we have been able to spot this error:

ERROR: Could not build wheels for python-bsonjs, which is required to install pyproject.toml-based projects

This is probably to be expected, because 2 shows there are no wheel packages available for more recent versions of Python, and the environment where python-bsonjs is being installed lacks the gcc compiler package.

Do you see any chance to publish wheel packages to PyPI, to improve this situation?

With kind regards,
Andreas.

Footnotes

  1. https://github.com/crate-workbench/cratedb-toolkit/actions/runs/6829892087/job/18576893058?pr=76#step:6:572

  2. https://pypi.org/project/python-bsonjs/0.3.0/#files

Vendor libbson

python-bsonjs currently relies on the libbson library being installed on the system. Vendoring libbson will avoid this requirement and make installing as simple as

$ pip install python-bsonjs

Converting Audit bson file to JSON

Hi Shane,

We have a requirment to process all audit bson file of Mongo database and store it at a centralized location for reporting. In our current process, we scan all the audit bson file convert it to json and send the json file to be persisted at centralized location via REST API call. Following is a snippet of code...

**from bson.json_util import loads, dumps, DEFAULT_JSON_OPTIONS
from bson import decode_all

if not self.util.isFileExists(auditFile):
return self.util.buildResponse(self.Globals.unsuccess, f"file {auditFile} is missing ")

myAuditFileSize = self.util.getFileSizeBytes(auditFile) / (1024*1024)

if myAuditFileSize > self.BSON_FILE_SIZE_LIMIT_MB:
print(f"Audit bson file '{auditFile}' size is larger than {self.BSON_FILE_SIZE_LIMIT_MB}")
return self.util.buildResponse(self.Globals.unsuccess, f"Audit bson file '{auditFile}' size is larger than {self.BSON_FILE_SIZE_LIMIT_MB}MB ")

3. processing - converting bson to json

try:
if self.util.getFileExtn(auditFile).lower() == "json":
myMongoAuditData = self.util.readJsonFile(auditFile)
else:
with open(auditFile, 'rb') as file:
myMongoAuditData = decode_all(file.read())

return myMongoAuditData**

We are facing issue on processing larger bson file thus restricting the size of audit bson file which will be processed. I need your help to use "bsonjs" module to process the audit bson file to generate the json file (will be better to generate smaller json file).

Pls assist.

Thanks,

Anil Kumar

Saving a list object loads as a dictionary

if you do the following:
bsonjs.loads(json.dumps([]))

The result is {}

This caused some minor issues in a program I'm working on. Since json is able to load lists I think there is no reason bson shouldn't experience similar behavior.

Libbson no longer builds on Windows with Python 2.7

Libbson no longer builds on Windows with Python 2.7:

$ /cygdrive/c/python/Python27/python setup.py test
running test
running egg_info
creating python_bsonjs.egg-info
writing python_bsonjs.egg-info\PKG-INFO
writing top-level names to python_bsonjs.egg-info\top_level.txt
writing dependency_links to python_bsonjs.egg-info\dependency_links.txt
writing manifest file 'python_bsonjs.egg-info\SOURCES.txt'
reading manifest file 'python_bsonjs.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching '*.h' under directory 'libbson'
writing manifest file 'python_bsonjs.egg-info\SOURCES.txt'
running build_ext
building 'bsonjs' extension
creating build
creating build\temp.win-amd64-2.7
creating build\temp.win-amd64-2.7\Release
creating build\temp.win-amd64-2.7\Release\src
creating build\temp.win-amd64-2.7\Release\src\bson
creating build\temp.win-amd64-2.7\Release\src\common
creating build\temp.win-amd64-2.7\Release\src\jsonsl
C:\Program Files (x86)\Common Files\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -DBS
ON_COMPILATION=1 -Isrc -Isrc/bson -Isrc/jsonsl -Isrc/common -IC:\python\Python27\include -IC:\python\Python27\PC /Tcsrc/bsonjs.c /Fob
uild\temp.win-amd64-2.7\Release\src/bsonjs.obj
bsonjs.c
c:\cygwin\home\administrator\python-bsonjs\src\bson\bson-compat.h(80) : fatal error C1083: Cannot open include file: 'stdint.h': No s
uch file or directory
error: command 'C:\\Program Files (x86)\\Common Files\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\amd64\\cl.exe' failed with exi
t status 2

This was changed in libbson 1.10: https://jira.mongodb.org/browse/CDRIVER-2473 when the minimum version of Visual Studio was raised to VS2010. The Microsoft Visual C++ Compiler for Python 2.7 uses VS2008 and does not include stdint.h.

Our options are to:

  • drop support for Python 2.7 altogether
  • drop support for Python 2.7 on Windows only
  • hack up libbson to add back support for VS2008

Add windows support.

  • Test bsonjs on windows platforms.
  • Add detailed installation instructions in the README.

Building from source does not work on Windows with Python 2.6 and 2.7

Libbson 1.6.2 directly includes stdint.h which is not provided by the version of visual studio used by Python 2.6 and 2.7 on Windows.

PS D:\Git\python-bsonjs2> D:\Python26\python.exe .\setup.py build_ext
running build_ext
building 'bsonjs' extension
creating build
creating build\temp.win-amd64-2.6
creating build\temp.win-amd64-2.6\Release
creating build\temp.win-amd64-2.6\Release\src
creating build\temp.win-amd64-2.6\Release\libbson
creating build\temp.win-amd64-2.6\Release\libbson\src
creating build\temp.win-amd64-2.6\Release\libbson\src\bson
creating build\temp.win-amd64-2.6\Release\libbson\src\jsonsl
C:\Users\admin\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c /nologo /Ox /MD
/W3 /GS- /DNDEBUG -DBSON_COMPILATION=1 -Isrc -Isrc/bson -Ilibbson/src -Ilibbson/src/jsonsl -Ilibbson/src/bson -ID:\Pytho
n26\include -ID:\Python26\PC /Tcsrc/bsonjs.c /Fobuild\temp.win-amd64-2.6\Release\src/bsonjs.obj
bsonjs.c
C:\Users\admin\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c /nologo /Ox /MD
/W3 /GS- /DNDEBUG -DBSON_COMPILATION=1 -Isrc -Isrc/bson -Ilibbson/src -Ilibbson/src/jsonsl -Ilibbson/src/bson -ID:\Pytho
n26\include -ID:\Python26\PC /Tclibbson/src\bson\bcon.c /Fobuild\temp.win-amd64-2.6\Release\libbson/src\bson\bcon.obj
bcon.c
C:\Users\admin\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c /nologo /Ox /MD
/W3 /GS- /DNDEBUG -DBSON_COMPILATION=1 -Isrc -Isrc/bson -Ilibbson/src -Ilibbson/src/jsonsl -Ilibbson/src/bson -ID:\Pytho
n26\include -ID:\Python26\PC /Tclibbson/src\bson\bson-atomic.c /Fobuild\temp.win-amd64-2.6\Release\libbson/src\bson\bson
-atomic.obj
bson-atomic.c
C:\Users\admin\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c /nologo /Ox /MD
/W3 /GS- /DNDEBUG -DBSON_COMPILATION=1 -Isrc -Isrc/bson -Ilibbson/src -Ilibbson/src/jsonsl -Ilibbson/src/bson -ID:\Pytho
n26\include -ID:\Python26\PC /Tclibbson/src\bson\bson-clock.c /Fobuild\temp.win-amd64-2.6\Release\libbson/src\bson\bson-
clock.obj
bson-clock.c
C:\Users\admin\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c /nologo /Ox /MD
/W3 /GS- /DNDEBUG -DBSON_COMPILATION=1 -Isrc -Isrc/bson -Ilibbson/src -Ilibbson/src/jsonsl -Ilibbson/src/bson -ID:\Pytho
n26\include -ID:\Python26\PC /Tclibbson/src\bson\bson-context.c /Fobuild\temp.win-amd64-2.6\Release\libbson/src\bson\bso
n-context.obj
bson-context.c
C:\Users\admin\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c /nologo /Ox /MD
/W3 /GS- /DNDEBUG -DBSON_COMPILATION=1 -Isrc -Isrc/bson -Ilibbson/src -Ilibbson/src/jsonsl -Ilibbson/src/bson -ID:\Pytho
n26\include -ID:\Python26\PC /Tclibbson/src\bson\bson-decimal128.c /Fobuild\temp.win-amd64-2.6\Release\libbson/src\bson\
bson-decimal128.obj
bson-decimal128.c
libbson/src\bson\bson-decimal128.c(19) : fatal error C1083: Cannot open include file: 'stdint.h': No such file or direct
ory
error: command '"C:\Users\admin\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe"'
failed with exit status 2

See https://jira.mongodb.org/browse/CDRIVER-2138.

BSONJS_VERSION macro causes compile error on Windows

Error:

PS D:\Git\python-bsonjs> python setup.py build_ext
running build_ext
building 'bsonjs' extension
C:\Users\admin\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c /nologo /Ox /MD
/W3 /GS- /DNDEBUG -DBSONJS_VERSION="0.1.0" -DBSON_COMPILATION=1 -Isrc -Isrc/bson -Ilibbson/src -Ilibbson/src/yajl -Ilibb
son/src/bson -IC:\Python27\include -IC:\Python27\PC /Tcsrc/bsonjs.c /Fobuild\temp.win-amd64-2.7\Release\src/bsonjs.obj
bsonjs.c
src/bsonjs.c(258) : error C2440: 'function' : cannot convert from 'double' to 'const char *'
src/bsonjs.c(258) : warning C4024: 'PyString_FromString' : different types for formal and actual parameter 1
src/bsonjs.c(258) : error C2143: syntax error : missing ')' before 'constant'
src/bsonjs.c(259) : error C2181: illegal else without matching if
src/bsonjs.c(259) : error C2059: syntax error : 'while'
src/bsonjs.c(260) : error C2059: syntax error : 'return'
src/bsonjs.c(261) : error C2059: syntax error : '}'
error: command 'C:\\Users\\admin\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\amd6
4\\cl.exe' failed with exit status 2

Cause:
The double quotes around BSONJS_VERSION in setup.py are lost resulting in the macro:

#define BSONJS_VERSION 0.1.0

instead of:

#define BSONJS_VERSION "0.1.0"

Update libbson to 1.4.0

Libbson 1.4.0 fixes a couple issues regarding BSON to MongoDB extended JSON conversion including:

  • bson_json_reader_read aborted on some invalid Extended JSON documents.
  • Error messages sometimes truncated at 63 chars.

Using bsonjs for GridFS

Hi,

First of all, it looks like an extremely interesting library that could really improve some of the performance issues in pymongo with larger documents! So this might be maybe more of a question maybe than an issue, but this might help other people too.

So normally if I put something into gridfs, i use bson.json_util.dumps to do a conversion:
dict -> json string -> bson binary.
From what I read on pymongo, bsonjs is a stand-in for it (with less options but much better performance). However as far as i can see, bsonjs doesn't do exactly the same as json_util but only the
json string -> bson binary
So I need to construct the json string first myself, for which i can use bson.json_util or another library like ujson:

%timeit a = bson.json_util.dumps(nested_dict)
1 loop, best of 3: 255 ms per loop

%timeit a = ujson.dumps(nested_dict)
10 loops, best of 3: 24.4 ms per loop

So this makes it about 10x faster, which is great. But I cannot always use ujson (because of ObjectIds) and bsonjs itself falls over encodings in strings which i cannot control with ujson.

Question 1: Unless I have a quick way to generate the json string, there is no real advantage to it? So i need to work with these constraints right?

Question 2: Is there any way to also write raw documents to GridFS like you documented for the document_class? Or does that happen automatically if the used db is in Raw BSON mode?

Question 3: How does it work for (non-GridFS, normal collections) updates? do i need to encode the {'_id': ObjectId('123456789')}, {'$set': {'foo': 'bar'}} as two raw bson documents?

Thanks!

New PyPi package release request

Any chance of getting a new release with the updated libbson that fixes known issues? I've found this library to be necessary for large ELT processes using pymongo and would love to see it continue.

Test_binary failing

Currently on master the test_binary test fails with the following error:

FAIL: test_binary (test.test_bsonjs.TestBsonjs)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/julius/python-bsonjs/test/test_bsonjs.py", line 195, in test_binary
    self.round_trip(bin_type_dict)
  File "/Users/julius/python-bsonjs/test/test_bsonjs.py", line 79, in round_trip
    self.assertEqual(doc, json_util.loads(
AssertionError: {'bin': Binary(b'\x00\x01\x02\x03\x04', 0)} != {'bin': b'\x00\x01\x02\x03\x04'}
- {'bin': Binary(b'\x00\x01\x02\x03\x04', 0)}
?         -------                       ----
+ {'bin': b'\x00\x01\x02\x03\x04'}

Update libbson to 1.6

Libbson 1.6 included some major improvements to it's bson to json encoding:

  • Use jsonsl instead of libyajl as our JSON parsing library, parse JSON more
    strictly, fix minor parsing bugs.
  • Extended JSON documents like '{"$code": "...", "$scope": {}}' are now parsed
    into BSON "code" elements.
  • ISO8601 dates now allow years from 0000 to 9999 inclusive. Before, years
    before 1970 were prohibited.
  • BSON floats and ints are now distinguished in JSON output.

Windows Python 3.8 compile warnings in libbson 1.17.0

Windows Python 3.8 compile warnings in libbson 1.17.0:

$ /cygdrive/c/python/Python38/python setup.py build_ext -i
running build_ext
building 'bsonjs' extension
creating build
creating build\temp.win-amd64-3.8
creating build\temp.win-amd64-3.8\Release
creating build\temp.win-amd64-3.8\Release\src
creating build\temp.win-amd64-3.8\Release\src\bson
creating build\temp.win-amd64-3.8\Release\src\common
creating build\temp.win-amd64-3.8\Release\src\jsonsl
...
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.25.28610\bin\HostX64\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -DBSON_COMPILATION=1 -Isrc -Isrc/bson -Isrc/jsonsl -Isrc/common -IC:\python\Python38\include -IC:\python\Python38\includ
e "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.25.28610\Include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.25.28610\ATLMFC\Include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.
18362.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\
4.7.2\include\um" /Tcsrc\bson\bson-atomic.c /Fobuild\temp.win-amd64-3.8\Release\src\bson\bson-atomic.obj
bson-atomic.c
src\bson\bson-atomic.c(93): warning C4133: 'function': incompatible types - from 'volatile int64_t *' to 'volatile LONG *'
src\bson\bson-atomic.c(93): warning C4244: 'function': conversion from 'int64_t' to 'LONG', possible loss of data

src\bson\bson-context.c(290): warning C4267: '=': conversion from 'size_t' to 'int', possible loss of data

src\bson\bson-decimal128.c(176): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
src\bson\bson-decimal128.c(180): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
src\bson\bson-decimal128.c(629): warning C4267: '-=': conversion from 'size_t' to 'int32_t', possible loss of data

src\bson\bson-iter.c(114): warning C4267: '=': conversion from 'size_t' to 'uint32_t', possible loss of data

src\bson\bson-json.c(617): warning C4146: unary minus operator applied to unsigned type, result still unsigned
src\bson\bson-json.c(1069): warning C4267: 'function': conversion from 'size_t' to 'int', possible loss of data
src\bson\bson-json.c(2090): warning C4018: '<': signed/unsigned mismatch
src\bson\bson-json.c(2091): warning C4018: '<': signed/unsigned mismatch

src\bson\bson-timegm.c(488): warning C4244: '+=': conversion from 'const int64_t' to 'int_fast32_t', possible loss of data

src\jsonsl\jsonsl.c(921): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
src\jsonsl\jsonsl.c(959): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

insert_many not working

i was experimenting with bsonjs. insert_one works fine, however when I tried insert_many, I got the following error msg: "File "C:\ProgramData\Anaconda2\lib\site-packages\pymongo\common.py", line 453, in validate_is_document_type
"collections.MutableMapping" % (option,))
TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping".

I casted "rawBS1 = RawBSONDocument(bson_bytes)" just the line before, and worked fine with insert_one.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.