my code stock.com

Snippet options

Download: Download snippet as ggts_map_reducepy.py.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

#! c:/Python26/python.exe
# coding: utf-8 

import sys
import codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)

import pymongo
from pymongo import Connection [email protected]
from pymongo.code import Code [email protected]

def activities_m_r():
    con = Connection()
    coll = con.ggts.activities
    m = pymongo.code.Code("""function(){ emit({published:this.published_jst_d,name :this.actor.displayName}, {count: 1, replies: this.object.replies.totalItems}); }""")
    r = pymongo.code.Code("""
        function(key, values) {
            var result = {count: 0, replies: 0};

            values.forEach(function(value) {
            result.count += value.count;
            result.replies += value.replies;
            });

            return result;
            }
        """)    
    results = coll.map_reduce(m, r,"myresult")
    return results

def comments_m_r():
    con = Connection()
    coll = con.ggts.comments
    m = pymongo.code.Code("""function(){ emit({published:this.published_jst_d,name :this.actor.displayName}, {count: 1, replies: this.object.replies.totalItems}); }""")
    r = pymongo.code.Code("""
        function(key, values) {
            var result = {count: 0, replies: 0};

            values.forEach(function(value) {
            result.count += value.count;
            result.replies += value.replies;
            });

            return result;
            }
        """)    
    results = coll.map_reduce(m, r,"myresult")
    return results

if __name__ == '__main__':
    print 'activities'
    results = activities_m_r()
    for result in results.find():
        print str(result['_id']['published']) + ',' + str(result['_id']['name']) + ',' + str(result['value']['count']) + ',' + str(result['value']['replies'])
    print '--------------------'

    print 'comments'
    results = comments_m_r()
    for result in results.find():
        print str(result['_id']['published']) + ',' + str(result['_id']['name']) + ',' + str(result['value']['count']) + ',' + str(result['value']['replies'])
    print '--------------------'

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.