database.py 732 B

12345678910111213141516171819202122232425262728293031
  1. import json
  2. class DB(object):
  3. '''
  4. db.add({timestamp: time.time(),
  5. agent: 'tizio',
  6. action: 'search',
  7. target: 'tasse'
  8. })
  9. db.add({timestamp: time.time(),
  10. agent: 'tizio',
  11. action: 'wall',
  12. seen: [
  13. {user: 'zerogreggi', msg: 'ho le pecore'},
  14. {user: 'caio', msg: 'ci faccio il formaggio'},
  15. ]
  16. })
  17. searches = [s for s in db.all() if s['action'] == 'wall']
  18. '''
  19. def __init__(self, fname):
  20. self.fname = fname
  21. def add(self, doc):
  22. with open(self.fname, 'a') as buf:
  23. buf.write(json.dumps(doc))
  24. buf.write('\n')
  25. def all(self):
  26. with open(self.fname) as buf:
  27. for line in buf:
  28. yield json.loads(line)