Ejemplo n.º 1
0
    def test_eval(self):
        db = self.connection.pymongo_test
        db.test.remove({})

        self.assertRaises(TypeError, db.eval, None)
        self.assertRaises(TypeError, db.eval, 5)
        self.assertRaises(TypeError, db.eval, [])

        self.assertEqual(3, db.eval("function (x) {return x;}", 3))
        self.assertEqual(3, db.eval(u"function (x) {return x;}", 3))

        self.assertEqual(None, db.eval("function (x) {db.test.save({y:x});}",
                                       5))
        self.assertEqual(db.test.find_one()["y"], 5)

        self.assertEqual(5, db.eval("function (x, y) {return x + y;}", 2, 3))
        self.assertEqual(5, db.eval("function () {return 5;}"))
        self.assertEqual(5, db.eval("2 + 3;"))

        self.assertEqual(5, db.eval(Code("2 + 3;")))
        self.assertRaises(OperationFailure, db.eval, Code("return i;"))
        self.assertEqual(2, db.eval(Code("return i;", {"i": 2})))
        self.assertEqual(5, db.eval(Code("i + 3;", {"i": 2})))

        self.assertRaises(OperationFailure, db.eval, "5 ++ 5;")
Ejemplo n.º 2
0
 def GET(self):
     map = Code("""
   function() {
     emit(this.project, 1);
   }
 """)
     reduce = Code("""
   function(key, values) {
     var total = 0;
     values.forEach(function(v) {
       total += v
     });
     return total;
   }
 """)
     pattern = re.compile('^' + web.input().term + '.*')
     result = db.commits.map_reduce(map, reduce, query={'project': pattern})
     results = []
     for item in sorted(result.find(),
                        key=lambda item: -item['value'])[:10]:
         results.append({
             'label': item['_id'],
             'value': item['_id'],
             'count': item['value']
         })
     return json.dumps(results)
Ejemplo n.º 3
0
def list_candidates(support_oppose=None):
    code = """
            function () {
                %s
                emit(this['crp_id'], this['EXPENDITURE AMOUNT']);
                %s
            };
            """
    if support_oppose:
        code = code % ("if (this['SUPPORT/OPPOSE CODE'] === '%s') {" % support_oppose, 
                       "}")
    else:
        code = code % ("", "")
    map = Code(code)

    reduce = Code("""
                    function (key, values) { 
                        var sum = 0;
                        for (var i in values) {
                            sum += values[i];
                        }
                        return sum;
                    };
                    """)
    result = expenditures.map_reduce(map, reduce)

    candidates = []

    for x in result.find():
        candidate = expenditures.find_one({'crp_id': x['_id']})
        candidate.update(x)
        candidates.append(candidate)

    return candidates
Ejemplo n.º 4
0
def committees_in_support_opposition(candidate_slug, support_oppose):
    """A list of committees that have supported/opposed the
    given candidate, and how much they've spent doing so.
    """
    map = Code("""function () {
                    if (this['SUPPORT/OPPOSE CODE'] === '%s' && this['candidate_slug'] === '%s') {
                        emit(this['FILER COMMITTEE ID NUMBER'], this['EXPENDITURE AMOUNT']);
                    }
                  };""" % (support_oppose, candidate_slug))
    reduce = Code("""function (key, values) {
                        var sum = 0;
                        for (var i in values) {
                            sum += values[i];
                        }
                        return sum;
                    };""")
    committees = []

    result = expenditures.map_reduce(map, reduce)
    for x in result.find():
        committee = filers.find_one({'FILER FEC ID NUMBER': x['_id']})
        committee['name'] = committee['ORGANIZATION NAME']
        committee.update(x)
        committees.append(committee)

    committees.sort(key=itemgetter('value'), reverse=True)
    return committees
Ejemplo n.º 5
0
 def test_types(self):
     self.assertRaises(TypeError, Code, 5)
     self.assertRaises(TypeError, Code, None)
     self.assertRaises(TypeError, Code, "aoeu", 5)
     self.assertRaises(TypeError, Code, u"aoeu", 5)
     self.assert_(Code("aoeu"))
     self.assert_(Code(u"aoeu"))
     self.assert_(Code("aoeu", {}))
     self.assert_(Code(u"aoeu", {}))
Ejemplo n.º 6
0
 def basic_map_reduce_test(self):
     map = Code(open('./map.js', 'r').read())
     reduce = Code(open('./reduce.js', 'r').read())
     result = self.collection.map_reduce(map, reduce, {"query": {}})
     # result = self.collection.map_reduce(map, reduce);
     print result
     collection = self.db[result["result"]]
     for item in collection.find():
         print item
Ejemplo n.º 7
0
 def test_repr(self):
     c = Code("hello world")
     self.assertEqual(repr(c), "Code('hello world', {})")
     c.scope["foo"] = "bar"
     self.assertEqual(repr(c), "Code('hello world', {'foo': 'bar'})")
     c = Code("hello world", {"blah": 3})
     self.assertEqual(repr(c), "Code('hello world', {'blah': 3})")
     c = Code("\x08\xFF")
     self.assertEqual(repr(c), "Code('\\x08\\xff', {})")
Ejemplo n.º 8
0
def GetKeys(p):
    mr = Code("function() {for (var key in this) { emit(key, null);}}")
    r = Code("function(key, stuff) { return null;}")

    result = p.map_reduce(map=mr, reduce=r)

    keys = result.distinct("_id")

    return keys
Ejemplo n.º 9
0
 def test_code(self):
     a_string = "hello world"
     a_code = Code("hello world")
     self.assert_(a_code.startswith("hello"))
     self.assert_(a_code.endswith("world"))
     self.assert_(isinstance(a_code, Code))
     self.failIf(isinstance(a_string, Code))
     self.assertEqual(a_code.scope, {})
     a_code.scope["my_var"] = 5
     self.assertEqual(a_code.scope, {"my_var": 5})
Ejemplo n.º 10
0
 def test_code(self):
     a_string = "hello world"
     a_code = Code("hello world")
     self.assert_(a_code.startswith("hello"))
     self.assert_(a_code.endswith("world"))
     self.assert_(isinstance(a_code, Code))
     self.assertFalse(isinstance(a_string, Code))
     self.assertEqual(a_code.scope, {})
     a_code.scope["my_var"] = 5
     self.assertEqual(a_code.scope, {"my_var": 5})
Ejemplo n.º 11
0
    def get_top_mapreduce(self):
        '''
    使用MongoDB中的mapreduce功能自动生成关于所有用户关注对象的频度统计写入top_rank集合中
    '''
        map = Code(open('gettopMap.js', 'r').read())
        reduce = Code(open('gettopReduce.js', 'r').read())

        print "INFO\t: Begin to MapReduce ..."
        results = self.friend_collection.map_reduce(map, reduce, "top_rank")
        print "INFO\t: Has been written to ", results.full_name
Ejemplo n.º 12
0
    def group(self,
              key,
              condition,
              initial,
              reduce,
              finalize=None,
              command=True):
        """Perform a query similar to an SQL *group by* operation.

        Returns an array of grouped items.

        The `key` parameter can be:

          - ``None`` to use the entire document as a key.
          - A :class:`list` of keys (each a :class:`basestring`) to group by.
          - A :class:`basestring` or :class:`~pymongo.code.Code` instance
            containing a JavaScript function to be applied to each document,
            returning the key to group by.

        :Parameters:
          - `key`: fields to group by (see above description)
          - `condition`: specification of rows to be
            considered (as a :meth:`find` query specification)
          - `initial`: initial value of the aggregation counter object
          - `reduce`: aggregation function as a JavaScript string
          - `finalize`: function to be called on each object in output list.
          - `command` (optional): DEPRECATED if ``True``, run the group as a
            command instead of in an eval - this option is deprecated and
            will be removed in favor of running all groups as commands

        .. versionchanged:: 1.4
           The `key` argument can now be ``None`` or a JavaScript function,
           in addition to a :class:`list` of keys.
        .. versionchanged:: 1.3
           The `command` argument now defaults to ``True`` and is deprecated.
        """
        if not command:
            warnings.warn(
                "eval-based groups are deprecated, and the "
                "command option will be removed.", DeprecationWarning)

        group = {}
        if isinstance(key, basestring):
            group["$keyf"] = Code(key)
        elif key is not None:
            group = {"key": self._fields_list_to_dict(key)}
        group["ns"] = self.__name
        group["$reduce"] = Code(reduce)
        group["cond"] = condition
        group["initial"] = initial
        if finalize is not None:
            group["finalize"] = Code(finalize)

        return self.__database.command({"group": group})["retval"]
Ejemplo n.º 13
0
    def get_follower_mapreduce(self):
        '''
    不使用,意义不大
    '''
        map = Code(open('getfollowerMap.js', 'r').read())
        reduce = Code(open('getfollowerReduce.js', 'r').read())

        print "INFO\t: Begin to MapReduce ..."
        results = self.friend_collection.map_reduce(
            map,
            reduce,
            "top_follower",
            scope={'idList': self.top_hundred.keys()})
        print "INFO\t: Has been written to ", results.full_name
Ejemplo n.º 14
0
def payee_list(request):
    map = Code("""function () {
                    emit({'slug': this['payee_slug'], 'name': this['payee_name']}, this['EXPENDITURE AMOUNT']);
                };""")
    reduce = Code("""function (key, values) {
                        var sum = 0;
                        for (var i in values) {
                            sum += values[i];
                        }
                        return sum;
                };""")
    result = expenditures.map_reduce(map, reduce)

    return render_to_response('ie/payee_list.html',
                              {'payees': [{'payee': x['_id'], 'amount': x['value']} for x in result.find().sort('_id')], }
                              )
Ejemplo n.º 15
0
    def where(self, code):
        """Adds a $where clause to this query.

        The `code` argument must be an instance of :class:`basestring`
        or :class:`~pymongo.code.Code` containing a JavaScript
        expression. This expression will be evaluated for each
        document scanned. Only those documents for which the
        expression evaluates to *true* will be returned as
        results. The keyword *this* refers to the object currently
        being scanned.

        Raises :class:`TypeError` if `code` is not an instance of
        :class:`basestring`. Raises
        :class:`~pymongo.errors.InvalidOperation` if this
        :class:`Cursor` has already been used. Only the last call to
        :meth:`where` applied to a :class:`Cursor` has any effect.

        :Parameters:
          - `code`: JavaScript expression to use as a filter
        """
        self.__check_okay_to_chain()
        if not isinstance(code, Code):
            code = Code(code)

        self.__spec["$where"] = code
        return self
Ejemplo n.º 16
0
    def __init__(self,
                 measures,
                 groupBy,
                 condition,
                 dbName,
                 table,
                 name="",
                 maxSD=3,
                 subject="s_id",
                 count=False,
                 level="subject"):
        if groupBy:
            if type(groupBy) == str:
                self.groupBy = [groupBy]
            else:
                self.groupBy = groupBy
        else:
            self.groupBy = []

        if level == "trial":
            self.groupBy += ["trial"]

        self.level = level

        dbA = MongoAdmin(dbName)
        my_table = dbA.getTable(table)

        self.posts = my_table.posts

        self.condition = condition
        self.maxSD = maxSD

        self.name = "%s_%s" % (dbName, table)
        for g in self.groupBy:
            self.name = self.name + "_" + g

        for m in measures:
            self.name = self.name + "_" + m

        if name:
            self.name += "_%s" % name

        if type(measures) == str:
            self.measures = [measures]
        else:
            self.measures = measures
        self.subject = subject
        self.count = count

        #initialization and finalization for the groupBy query - note that the reduce functions are constructed at run time to correspond with the necessary subject SD screen
        self.initial = {"csum": 0, "ccount": 0, "ss": 0, "avg": 0, "std": 0}
        self.finalize = Code(
            "function(prev){ prev.avg = prev.csum / prev.ccount; prev.std = Math.sqrt(Math.abs(prev.ss - prev.avg * prev.csum) / prev.ccount);} "
        )

        self.s_ids = self.posts.find(condition).distinct(subject)

        self.Compute()
Ejemplo n.º 17
0
    def Trim(self):

        reduceFunc = Code(
            "function(obj,prev) { meas = obj.%s; prev.csum += meas; prev.ccount++; prev.ss += meas * meas;}"
            % self.measure)

        initial = {"csum": 0, "ccount": 0, "ss": 0, "avg": 0, "std": 0}
        finalize = Code(
            "function(prev){ prev.avg = prev.csum / prev.ccount; prev.std = Math.sqrt((prev.ss - (prev.csum * prev.csum/ prev.ccount)) / prev.ccount);}"
        )

        mapFunc = Code("function () {emit(this.%s, 1);}" % self.measure)

        summary = self.posts.group({}, {}, initial, reduceFunc, finalize)

        if summary:

            self.avg = summary[0]['avg']
            self.std = summary[0]['std']
            self.count = summary[0]['ccount']
            if str(self.count) == "NA" or str(self.count) == "nan":
                self.count = 0

            if self.maxSD:

                mapFunc = Code("function() {"
                               "emit(this.%s, 1);"
                               "}" % (self.measure))

                reduceFunc = Code("function(obj, prev) {"
                                  "zscore = Math.abs(obj - %s) / %s;"
                                  "return zscore; }" % (self.avg, self.std))

                result = self.posts.map_reduce(map=mapFunc, reduce=reduceFunc)

                item = result.find().sort('value', -1)[0]

                if item['value'] > self.maxSD:
                    self.posts.remove({self.measure: item['_id']})
                    self.Trim()
        else:
            self.avg = 0
            self.std = 0
            self.count = 0
Ejemplo n.º 18
0
def candidate_support_oppose(candidate_slug, support_oppose):
    map = Code("""function () {
                       if (this['SUPPORT/OPPOSE CODE'] === '%s' && this['candidate_slug'] === '%s') {
                           emit(this['CANDIDATE ID NUMBER'], this['EXPENDITURE AMOUNT']);
                       }
                  };""" % (support_oppose, candidate_slug))
    reduce = Code("""function (key, values) {
                        var sum = 0;
                        for (var i in values) {
                            sum += values[i];
                        }
                        return sum;
                      };""")
    result = expenditures.map_reduce(map, reduce)
    data = result.find_one()
    if data:
        return result.find_one()['value']

    return 0
Ejemplo n.º 19
0
def committee_support_oppose(committee_slug, support_oppose):
    fec_id = filers.find_one({'committee_slug': committee_slug})['FILER FEC ID NUMBER']
    map = Code("""function () {
                    if (this['SUPPORT/OPPOSE CODE'] === '%s' && this['FILER COMMITTEE ID NUMBER'] === '%s') {
                        emit(this['FILER COMMITTEE ID NUMBER'], this['EXPENDITURE AMOUNT']);
                    }
                };""" % (support_oppose, fec_id))
    reduce = Code("""function (key, values) {
                        var sum = 0;
                        for (var i in values) {
                            sum += values[i];
                        }
                        return sum;
                    };""")
    candidates = []
    result = expenditures.map_reduce(map, reduce).find_one()
    if result:
        return result['value']
    return 0
Ejemplo n.º 20
0
 def test_repr(self):
     c = Code("hello world")
     self.assertEqual(repr(c), "Code('hello world', {})")
     c.scope["foo"] = "bar"
     self.assertEqual(repr(c), "Code('hello world', {'foo': 'bar'})")
     c = Code("hello world", {"blah": 3})
     self.assertEqual(repr(c), "Code('hello world', {'blah': 3})")
     c = Code("\x08\xFF")
     self.assertEqual(repr(c), "Code('\\x08\\xff', {})")
Ejemplo n.º 21
0
    def test_where(self):
        db = self.db
        db.test.remove({})

        a = db.test.find()
        self.assertRaises(TypeError, a.where, 5)
        self.assertRaises(TypeError, a.where, None)
        self.assertRaises(TypeError, a.where, {})

        for i in range(10):
            db.test.save({"x": i})

        self.assertEqual(3, len(list(db.test.find().where('this.x < 3'))))
        self.assertEqual(3,
                         len(list(db.test.find().where(Code('this.x < 3')))))
        self.assertEqual(
            3, len(list(db.test.find().where(Code('this.x < i', {"i": 3})))))
        self.assertEqual(10, len(list(db.test.find())))

        self.assertEqual(3, db.test.find().where('this.x < 3').count())
        self.assertEqual(10, db.test.find().count())
        self.assertEqual(3, db.test.find().where(u'this.x < 3').count())
        self.assertEqual([0, 1, 2],
                         [a["x"] for a in db.test.find().where('this.x < 3')])
        self.assertEqual(
            [], [a["x"] for a in db.test.find({
                "x": 5
            }).where('this.x < 3')])
        self.assertEqual(
            [5], [a["x"] for a in db.test.find({
                "x": 5
            }).where('this.x > 3')])

        cursor = db.test.find().where('this.x < 3').where('this.x > 7')
        self.assertEqual([8, 9], [a["x"] for a in cursor])

        a = db.test.find()
        b = a.where('this.x > 3')
        for _ in a:
            break
        self.assertRaises(InvalidOperation, a.where, 'this.x < 3')
Ejemplo n.º 22
0
def map_reduce_tags():
    map = Code("""
            function () {
                this.tags.forEach(function(z) {
                    emit(z, 1);
                });
            }
        """)

    reduce = Code("""
            function (key, values) {
                 var total = 0;
                 for (var i = 0; i < values.length; i++) {
                   total += values[i];
                 }
                 return total;
               }
            """)
    db = Connection()['flask-test']
    result = db.question.map_reduce(map, reduce, out='tags')
    return result.find()
Ejemplo n.º 23
0
def main():

    wordmap = """
function wordMap(){
	//find words in the document text
	var words = this.org_content.match(/\w+/g);	
	if (words == null){
		return;
	}	
	for (var i = 0; i < words.length; i++){
		//emit every word, with count of one
		emit(words[i], {count: 1});	
	}
}"""

    wordreduce = """
function wordReduce(key,values){
	var total = 0;
	for (var i = 0; i < values.length; i++){
		total += values[i].count;
	}
	return {count: total};
}"""

    #Load map and reduce functions
    map = Code(wordmap)
    reduce = Code(wordreduce)

    db = getDB()
    coll = db.drops

    #Run the map-reduce query
    result = "wordcount"
    results = coll.map_reduce(map, reduce, result, query={"name": "Integron"})

    #Print the results
    for result in results.find():
        print(result['_id'], result['value']['count'])
Ejemplo n.º 24
0
def candidates_by_committee(committee_slug, support_oppose):
    fec_id = filers.find_one({'committee_slug': committee_slug})['FILER FEC ID NUMBER']
    map = Code("""function () {
                        if (this['SUPPORT/OPPOSE CODE'] === '%s' && this['FILER COMMITTEE ID NUMBER'] === '%s') {
                            emit(this['crp_id'], this['EXPENDITURE AMOUNT']);
                        }
                    };""" % (support_oppose, fec_id))
    reduce = Code("""function (key, values) {
                        var sum = 0;
                        for (var i in values) {
                            sum += values[i];
                        }
                        return sum;
                    };""")
    candidates = []
    result = expenditures.map_reduce(map, reduce)
    for x in result.find():
        candidate = expenditures.find_one({'crp_id': x['_id']})
        candidate.update(x)
        candidates.append(candidate)

    candidates.sort(key=itemgetter('value'), reverse=True)
    return candidates
Ejemplo n.º 25
0
def list_committees():
    map = Code("""
                function () {
                    emit(this['FILER COMMITTEE ID NUMBER'], this['EXPENDITURE AMOUNT']);
                };
                """)
    reduce = Code("""
                    function (key, values) {
                        var sum = 0;
                        for (var i in values) {
                            sum += values[i];
                        }
                        return sum;
                    };
                    """)
    result = expenditures.map_reduce(map, reduce)
    committees = []
    for x in result.find():
        committee = filers.find_one({'FILER FEC ID NUMBER': x['_id']})
        committee['name'] = committee['ORGANIZATION NAME']
        committee.update(x)
        committees.append(committee)

    return committees
Ejemplo n.º 26
0
    def eval(self, code, *args):
        """Evaluate a JavaScript expression on the Mongo server.

        Useful if you need to touch a lot of data lightly; in such a scenario
        the network transfer of the data could be a bottleneck. The `code`
        argument must be a JavaScript function. Additional positional
        arguments will be passed to that function when it is run on the
        server.

        Raises TypeError if `code` is not an instance of (str, unicode,
        `Code`). Raises OperationFailure if the eval fails. Returns the result
        of the evaluation.

        :Parameters:
          - `code`: string representation of JavaScript code to be evaluated
          - `args` (optional): additional positional arguments are passed to
            the `code` being evaluated
        """
        if not isinstance(code, Code):
            code = Code(code)

        command = SON([("$eval", code), ("args", list(args))])
        result = self.command(command)
        return result.get("retval", None)
Ejemplo n.º 27
0
    def test_group_with_scope(self):
        db = self.db
        db.drop_collection("test")
        db.test.save({"a": 1})
        db.test.save({"b": 1})

        reduce_function = "function (obj, prev) { prev.count += inc_value; }"

        self.assertEqual(
            2,
            db.test.group([], {}, {"count": 0},
                          Code(reduce_function, {"inc_value": 1}))[0]['count'])
        self.assertEqual(
            4,
            db.test.group([], {}, {"count": 0},
                          Code(reduce_function, {"inc_value": 2}))[0]['count'])

        self.assertEqual(
            1,
            db.test.group([], {}, {"count": 0},
                          Code(reduce_function,
                               {"inc_value": 0.5}))[0]['count'])

        if version.at_least(db.connection(), (1, 1)):
            self.assertEqual(
                2,
                db.test.group([], {}, {"count": 0},
                              Code(reduce_function, {"inc_value": 1}),
                              command=True)[0]['count'])

            self.assertEqual(
                4,
                db.test.group([], {}, {"count": 0},
                              Code(reduce_function, {"inc_value": 2}),
                              command=True)[0]['count'])

            self.assertEqual(
                1,
                db.test.group([], {}, {"count": 0},
                              Code(reduce_function, {"inc_value": 0.5}),
                              command=True)[0]['count'])
Ejemplo n.º 28
0
def _get_code_w_scope(data):
    (_, data) = _get_int(data)
    (code, data) = _get_string(data)
    (scope, data) = _get_object(data)
    return (Code(code, scope), data)
Ejemplo n.º 29
0
 def _unique_insert(self, message_dict, unique_key):
     return self.queue_database.eval(
         Code(
             "function(obj) { if ( db.%s.count({%s: obj.%s}) ) { return false; } db.%s.insert(obj); return true;}"
             % (self.queue, unique_key, unique_key, self.queue)),
         message_dict)
Ejemplo n.º 30
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")
Ejemplo n.º 31
0
def generate_statistics():
    """
    Use mongo's map/reduce to output state-by-state (and total) counts of
    various Open State objects to the 'counts' collection.
    """
    m = Code("""
    function () {
        var val = {'bills': 1,
                   'introduced': 0,
                   'categorized': 0,
                   'voters': 0,
                   'idd_voters': 0,
                   'actions': this.actions.length,
                   'votes': this.votes.length,
                   'versions': this.versions.length};
        if(this.hasOwnProperty('subjects')) {
             val['subjects'] = 1;
        } else {
             val['subjects'] = 0;
        }

        // count how many actions are categorized & introduced
        for(var i=0; i < this.actions.length; ++i) {
            if(this.actions[i]["type"] != "other") {
                val['categorized']++;
            }
            for(var j=0; j < this.actions[i]["type"].length; ++j) {
                if(this.actions[i]["type"][j] == "bill:introduced") {
                    val['introduced'] = 1;
                }
            }
        }

        // check sponsor leg_ids
        val['sponsors'] = this.sponsors.length;
        val['idd_sponsors'] = 0;

        for(var i=0; i < this.sponsors.length; ++i) {
             if(this.sponsors[i]['leg_id']) {
                 val['idd_sponsors'] += 1;
             }
        }

        // go through yes/no/other votes and count voter leg_ids
        for(var i=0; i < this.votes.length; ++i) {
            for(var j=0; j < this.votes[i]["yes_votes"].length; ++j) {
                val['voters'] += 1;
                if(this.votes[i]["yes_votes"][j]["leg_id"]) {
                    val['idd_voters'] += 1;
                }
            }
        }
        for(var i=0; i < this.votes.length; ++i) {
            for(var j=0; j < this.votes[i]["no_votes"].length; ++j) {
                val['voters'] += 1;
                if(this.votes[i]["no_votes"][j]["leg_id"]) {
                    val['idd_voters'] += 1;
                }
            }
        }
        for(var i=0; i < this.votes.length; ++i) {
            for(var j=0; j < this.votes[i]["other_votes"].length; ++j) {
                val['voters'] += 1;
                if(this.votes[i]["other_votes"][j]["leg_id"]) {
                    val['idd_voters'] += 1;
                }
            }
        }

        emit(this['state'], val);
        emit('total', val);
    }""")

    r = Code("""
    function (key, values) {
        sums = {'bills': 0, 'actions': 0, 'votes': 0, 'versions': 0,
                'subjects': 0, 'introduced': 0, 'categorized': 0,
                'voters': 0, 'idd_voters': 0,
                'sponsors': 0, 'idd_sponsors': 0};
        for (var i in values) {
            value = values[i];

            for (var t in value) {
                sums[t] += value[t];
            }
        }

        return sums;
    }""")

    # a finalizer to convert all counts from JS numbers (floats) to longs
    f = Code("""
    function (key, value) {
        for (var t in value) {
            value[t] = new NumberLong(value[t]);
        }

        return value;
    }""")

    db.bills.map_reduce(m, r, finalize=f, out='counts')
Ejemplo n.º 32
0
 def __setattr__(self, name, code):
     self._database.system.js.save({"_id": name, "value": Code(code)},
                                    safe=True)
Ejemplo n.º 33
0
 def map_reduce( self, map_func, reduce_func ):
     
     map_func = Code(map_func.replace('\n',''))
     reduce_func = Code(reduce_func.replace('\n',''))
     
     return self._monga._db[self._doctype._collection_name].map_reduce(map_func, reduce_func)