def test_binary(self):
     a_string = "hello world"
     a_binary = Binary("hello world")
     self.assert_(a_binary.startswith("hello"))
     self.assert_(a_binary.endswith("world"))
     self.assert_(isinstance(a_binary, Binary))
     self.failIf(isinstance(a_string, Binary))
Example #2
0
def _element_to_bson(key, value, check_keys):
    if not isinstance(key, basestring):
        raise InvalidDocument("documents must have only string keys, "
                              "key was %r" % key)

    if check_keys:
        if key.startswith("$"):
            raise InvalidName("key %r must not start with '$'" % key)
        if "." in key:
            raise InvalidName("key %r must not contain '.'" % key)

    name = _make_c_string(key, True)
    if isinstance(value, float):
        return "\x01" + name + struct.pack("<d", value)

    # Use Binary w/ subtype 3 for UUID instances
    try:
        import uuid

        if isinstance(value, uuid.UUID):
            value = Binary(value.bytes, subtype=3)
    except ImportError:
        pass

    if isinstance(value, Binary):
        subtype = value.subtype
        if subtype == 2:
            value = struct.pack("<i", len(value)) + value
        return "\x05%s%s%s%s" % (name, struct.pack("<i", len(value)),
                                 chr(subtype), value)
    if isinstance(value, Code):
        cstring = _make_c_string(value)
        scope = _dict_to_bson(value.scope, False, False)
        full_length = struct.pack("<i", 8 + len(cstring) + len(scope))
        length = struct.pack("<i", len(cstring))
        return "\x0F" + name + full_length + length + cstring + scope
    if isinstance(value, str):
        cstring = _make_c_string(value)
        length = struct.pack("<i", len(cstring))
        return "\x02" + name + length + cstring
    if isinstance(value, unicode):
        cstring = _make_c_string(value)
        length = struct.pack("<i", len(cstring))
        return "\x02" + name + length + cstring
    if isinstance(value, dict):
        return "\x03" + name + _dict_to_bson(value, check_keys, False)
    if isinstance(value, (list, tuple)):
        as_dict = SON(zip([str(i) for i in range(len(value))], value))
        return "\x04" + name + _dict_to_bson(as_dict, check_keys, False)
    if isinstance(value, ObjectId):
        return "\x07" + name + value.binary
    if value is True:
        return "\x08" + name + "\x01"
    if value is False:
        return "\x08" + name + "\x00"
    if isinstance(value, (int, long)):
        # TODO this is a really ugly way to check for this...
        if value > 2**64 / 2 - 1 or value < -2**64 / 2:
            raise OverflowError("MongoDB can only handle up to 8-byte ints")
        if value > 2**32 / 2 - 1 or value < -2**32 / 2:
            return "\x12" + name + struct.pack("<q", value)
        return "\x10" + name + struct.pack("<i", value)
    if isinstance(value, datetime.datetime):
        millis = int(calendar.timegm(value.timetuple()) * 1000 +
                     value.microsecond / 1000)
        return "\x09" + name + struct.pack("<q", millis)
    if isinstance(value, Timestamp):
        time = struct.pack("<i", value.time)
        inc = struct.pack("<i", value.inc)
        return "\x11" + name + inc + time
    if value is None:
        return "\x0A" + name
    if isinstance(value, RE_TYPE):
        pattern = value.pattern
        flags = ""
        if value.flags & re.IGNORECASE:
            flags += "i"
        if value.flags & re.LOCALE:
            flags += "l"
        if value.flags & re.MULTILINE:
            flags += "m"
        if value.flags & re.DOTALL:
            flags += "s"
        if value.flags & re.UNICODE:
            flags += "u"
        if value.flags & re.VERBOSE:
            flags += "x"
        return "\x0B" + name + _make_c_string(pattern, True) + \
            _make_c_string(flags)
    if isinstance(value, DBRef):
        return _element_to_bson(key, value.as_doc(), False)

    raise InvalidDocument("cannot convert value of type %s to bson" %
                          type(value))
Example #3
0
    def test_basic_from_dict(self):
        self.assertRaises(TypeError, BSON.from_dict, 100)
        self.assertRaises(TypeError, BSON.from_dict, "hello")
        self.assertRaises(TypeError, BSON.from_dict, None)
        self.assertRaises(TypeError, BSON.from_dict, [])

        self.assertEqual(BSON.from_dict({}), BSON("\x05\x00\x00\x00\x00"))
        self.assertEqual(
            BSON.from_dict({"test": u"hello world"}),
            "\x1B\x00\x00\x00\x02\x74\x65\x73\x74\x00\x0C\x00\x00"
            "\x00\x68\x65\x6C\x6C\x6F\x20\x77\x6F\x72\x6C\x64\x00"
            "\x00")
        self.assertEqual(
            BSON.from_dict({u"mike": 100}),
            "\x0F\x00\x00\x00\x10\x6D\x69\x6B\x65\x00\x64\x00\x00"
            "\x00\x00")
        self.assertEqual(
            BSON.from_dict({"hello": 1.5}),
            "\x14\x00\x00\x00\x01\x68\x65\x6C\x6C\x6F\x00\x00\x00"
            "\x00\x00\x00\x00\xF8\x3F\x00")
        self.assertEqual(BSON.from_dict({"true": True}),
                         "\x0C\x00\x00\x00\x08\x74\x72\x75\x65\x00\x01\x00")
        self.assertEqual(
            BSON.from_dict({"false": False}),
            "\x0D\x00\x00\x00\x08\x66\x61\x6C\x73\x65\x00\x00"
            "\x00")
        self.assertEqual(
            BSON.from_dict({"empty": []}),
            "\x11\x00\x00\x00\x04\x65\x6D\x70\x74\x79\x00\x05\x00"
            "\x00\x00\x00\x00")
        self.assertEqual(
            BSON.from_dict({"none": {}}),
            "\x10\x00\x00\x00\x03\x6E\x6F\x6E\x65\x00\x05\x00\x00"
            "\x00\x00\x00")
        self.assertEqual(
            BSON.from_dict({"test": Binary("test")}),
            "\x18\x00\x00\x00\x05\x74\x65\x73\x74\x00\x08\x00\x00"
            "\x00\x02\x04\x00\x00\x00\x74\x65\x73\x74\x00")
        self.assertEqual(
            BSON.from_dict({"test": Binary("test", 128)}),
            "\x14\x00\x00\x00\x05\x74\x65\x73\x74\x00\x04\x00\x00"
            "\x00\x80\x74\x65\x73\x74\x00")
        self.assertEqual(BSON.from_dict({"test": None}),
                         "\x0B\x00\x00\x00\x0A\x74\x65\x73\x74\x00\x00")
        self.assertEqual(
            BSON.from_dict({"date": datetime.datetime(2007, 1, 8, 0, 30, 11)}),
            "\x13\x00\x00\x00\x09\x64\x61\x74\x65\x00\x38\xBE\x1C"
            "\xFF\x0F\x01\x00\x00\x00")
        self.assertEqual(
            BSON.from_dict({"regex": re.compile("a*b", re.IGNORECASE)}),
            "\x12\x00\x00\x00\x0B\x72\x65\x67\x65\x78\x00\x61\x2A"
            "\x62\x00\x69\x00\x00")
        self.assertEqual(
            BSON.from_dict({"$where": Code("test")}),
            "\x1F\x00\x00\x00\x0F\x24\x77\x68\x65\x72\x65\x00\x12"
            "\x00\x00\x00\x05\x00\x00\x00\x74\x65\x73\x74\x00\x05"
            "\x00\x00\x00\x00\x00")
        a = ObjectId("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B")
        self.assertEqual(
            BSON.from_dict({"oid": a}),
            "\x16\x00\x00\x00\x07\x6F\x69\x64\x00\x00\x01\x02\x03"
            "\x04\x05\x06\x07\x08\x09\x0A\x0B\x00")
        self.assertEqual(
            BSON.from_dict({"ref": DBRef("coll", a)}),
            "\x2F\x00\x00\x00\x03ref\x00\x25\x00\x00\x00\x02$ref"
            "\x00\x05\x00\x00\x00coll\x00\x07$id\x00\x00\x01\x02"
            "\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x00\x00")
Example #4
0
 def test_subtype(self):
     b = Binary("hello")
     self.assertEqual(b.subtype, 2)
     c = Binary("hello", 100)
     self.assertEqual(c.subtype, 100)
Example #5
0
 def _make_html_page(self, response, lru, lrulinks):
     p = self._make_raw_page(response, lru)
     if STORE_HTML:
         p['body'] = Binary(response.body.encode('zip'))
     p['lrulinks'] = lrulinks
     return p
 def __create_mongo_value_for_value(self, value):
     if isinstance(value, datastore_types.Rating):
         return {
             'class': 'rating',
             'rating': int(value),
         }
     if isinstance(value, datastore_types.Category):
         return {
             'class': 'category',
             'category': str(value),
         }
     if isinstance(value, datastore_types.Key):
         return {
             'class': 'key',
             'path': self.__id_for_key(value._ToPb()),
         }
     if isinstance(value, types.ListType):
         list_for_db = [
             self.__create_mongo_value_for_value(v) for v in value
         ]
         sorted_list = sorted(value)
         return {
             'class':
             'list',
             'list':
             list_for_db,
             'ascending_sort_key':
             self.__create_mongo_value_for_value(sorted_list[0]),
             'descending_sort_key':
             self.__create_mongo_value_for_value(sorted_list[-1]),
         }
     if isinstance(value, users.User):
         return {
             'class': 'user',
             'email': value.email(),
         }
     if isinstance(value, datastore_types.Text):
         return {
             'class': 'text',
             'string': unicode(value),
         }
     if isinstance(value, datastore_types.Blob):
         return Binary(value)
     if isinstance(value, datastore_types.ByteString):
         return {'class': 'bytes', 'value': Binary(value)}
     if isinstance(value, datastore_types.IM):
         return {
             'class': 'im',
             'protocol': value.protocol,
             'address': value.address,
         }
     if isinstance(value, datastore_types.GeoPt):
         return {
             'class': 'geopt',
             'lat': value.lat,
             'lon': value.lon,
         }
     if isinstance(value, datastore_types.Email):
         return {
             'class': 'email',
             'value': value,
         }
     if isinstance(value, datastore_types.BlobKey):
         return {
             'class': 'blobkey',
             'value': value,
         }
     return value
  # load svg data
  handle = rsvg.Handle(None, svg)

  # setup image
  image = cairo.ImageSurface(cairo.FORMAT_ARGB32, 250, 250)
  ctx = cairo.Context(image)
  ctx.fill()

  # scale to 250x250
  scale = 250.0 / max(handle.get_property("height"),
                      handle.get_property("width"))
  ctx.scale(scale, scale)

  # translate to center
  height = scale * handle.get_property("height")
  width = scale * handle.get_property("width")
  ctx.translate(125.0 - (0.5 * width),
                125.0 - (0.5 * height))

  # render image
  handle.render_cairo(ctx)

  # write png data
  png = StringIO()
  image.write_to_png(png)

  # insert diagram into document
  molecules_collection.update({"inchikey" : str(inchikey)},
                              {"$set" : {"diagram" : Binary(png.getvalue(), BINARY_SUBTYPE)}},
                              False)
Example #8
0
from pymongo import ASCENDING

from mongodb_benchmark_tools import post_data

small = {
    "integer": 5,
    "number": 5.05,
    "boolean": False,
    "array": ["test", "benchmark"]
}
medium = {
    "base_url": "http://www.example.com/test-me",
    "total_word_count": 6743,
    "access_time": datetime.datetime.utcnow(),
    "sub_object": small,
    "data": Binary("hello" * 40),
    "big_array": ["mongodb"] * 20
}
large = {"bigger_array": [medium] * 5, "data": Binary("hello" * 500)}


class Benchmark(object):
    name = "benchmark"
    description = "a benchmark"
    categories = []

    def setup(self):
        pass

    def run(self, iterations):
        pass