-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
45 lines (42 loc) · 1007 Bytes
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import cherrypy
from pymongo import MongoClient
from bson.son import SON
from collections import OrderedDict
db = MongoClient()['pubmed']
def counts(terms):
a = 1965
b = 2015
res = db.article.aggregate(
[
{
'$match': {
'year': {'$gt': a - 1, '$lt': b + 1 },
'mesh': {'$in': terms}
}
},
{
'$group' : {
'_id' : "$year",
"count": {"$sum": 1}
}
},
{'$sort': {'_id': 1}}
]
)
d = {}; wres = {}
for r in res:
d[r['_id']] = r['count']
for i in range(a, b + 1):
if i in d:
wres[i] = d[i]
else:
wres[i] = 0
return [{'x':k, 'y':wres[k]} for k in wres].__repr__()
class HelloWorld(object):
def index(self):
return "Hello World!"
def freqs(self, terms):
return counts([terms,])
index.exposed = True
freqs.exposed = True
cherrypy.quickstart(HelloWorld())