12345678910111213141516171819202122232425262728293031 |
- import json
- class DB(object):
- '''
- db.add({timestamp: time.time(),
- agent: 'tizio',
- action: 'search',
- target: 'tasse'
- })
- db.add({timestamp: time.time(),
- agent: 'tizio',
- action: 'wall',
- seen: [
- {user: 'zerogreggi', msg: 'ho le pecore'},
- {user: 'caio', msg: 'ci faccio il formaggio'},
- ]
- })
- searches = [s for s in db.all() if s['action'] == 'wall']
- '''
- def __init__(self, fname):
- self.fname = fname
- def add(self, doc):
- with open(self.fname, 'a') as buf:
- buf.write(json.dumps(doc))
- buf.write('\n')
- def all(self):
- with open(self.fname) as buf:
- for line in buf:
- yield json.loads(line)
|