A question from the documentation

82 views
Skip to first unread message

clueless_rosh

unread,
Jun 29, 2011, 2:26:19 PM6/29/11
to BSON
i was reading the bson documentation and came across this part :
class bson.binary.Binary(data[, subtype=OLD_BINARY_SUBTYPE])¶

Bases: str

Representation of BSON binary data.

This is necessary because we want to represent Python strings as
the BSON string type. We need to wrap binary data so we can tell the
difference between what should be considered binary data and what
should be considered a string when we encode to BSON.

reference : http://api.mongodb.org/python/current/api/bson/binary.html#module-bson.binary

my question is what does Bases represent here

Also what kind of an object wud this be.. will this be a
bson.binary.Binary object or bson.BSON object
Can i check my data representation with
bson.is_valid(bson)¶

Check that the given string represents valid BSON data.

Raises TypeError if bson is not an instance of str. Returns True
if bson is valid BSON, False otherwise.

I tried it on my own and i get a false for binary.Binary(data)
but a true for
bson_string = BSON.from_dict({ 'data': binary.Binary( data ) })

the entire program is as follows:
from bson import is_valid,binary,BSON

f = open("/home/roshini/Downloads/bsoneg.pdf", "rb")
data = f.read();
bson_string = BSON.from_dict({ 'data': binary.Binary( data ) })
bson_string1 = binary.Binary( data )
stringdata= is_valid(bson_string)
bindata = is_valid(bson_string1)
print boo
print path
new_f = open('decoded.pdf','w')
new_f.write(binary.Binary( data ))
new_f.close()

Kyle Banker

unread,
Jun 29, 2011, 2:35:50 PM6/29/11
to bs...@googlegroups.com
'Bases' means that Binary is a subclass of str.

The resulting object is bson.binary.Binary. You can use isinstance to verify.

You can only check validity on the result of BSON.from_dict.

roshini johri

unread,
Jun 29, 2011, 2:56:36 PM6/29/11
to bs...@googlegroups.com
I see , so what would be the resulting object from  BSON.from_dict.?  as in frm my program what would bson_string be? bson_string1 would be bson.binary.Binary object I guess. Thanks a lot for your help!

Kyle Banker

unread,
Jun 29, 2011, 3:06:37 PM6/29/11
to bs...@googlegroups.com
bson_string is an instance of BSON, which is a subclass of str.
Reply all
Reply to author
Forward
0 new messages