-
Notifications
You must be signed in to change notification settings - Fork 0
/
063.py
executable file
·36 lines (30 loc) · 880 Bytes
/
063.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import json
import cPickle
import plyvel
def main():
if len(sys.argv) == 1:
db = plyvel.DB('./db/', create_if_missing=True)
with db.write_batch() as b:
for line in sys.stdin:
j = json.loads(line)
name = j.get('name')
tags = j.get('tags')
if name is not None and tags is not None:
b.put(name.encode('utf-8'), cPickle.dumps(tags))
else:
db = plyvel.DB('./db/')
tags = db.get(sys.argv[1])
if tags is not None:
tags = cPickle.loads(tags)
for tag in tags:
print '{}\t{}'.format(tag['value'], tag['count'])
# % zcat data/artist.json.gz | src/063.py
#
# % src/063.py "Peter P."
# punk 1
# germany 1
if __name__ == '__main__':
main()