Guest User

Untitled

a guest
Dec 12th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. >>> import shelve
  2. >>> from contextlib import contextmanager
  3. >>> class Database:
  4. ... def __init__(self, path):
  5. ... self.path = path
  6. ...
  7. ... @contextmanager
  8. ... def open(self):
  9. ... s = shelve.open(self.path, writeback=True)
  10. ... try:
  11. ... yield s
  12. ... finally:
  13. ... s.close()
  14. ...
  15. >>>
  16. >>> d = Database('/home/zach/.tweet_poster/cache')
  17. >>>
  18. >>> with d.open() as db:
  19. ... print(db['108710608601493504'])
  20. ...
  21. Traceback (most recent call last):
  22. File "/usr/lib/python3.2/shelve.py", line 111, in __getitem__
  23. value = self.cache[key]
  24. KeyError: '108710608601493504'
  25.  
  26. During handling of the above exception, another exception occurred:
  27.  
  28. Traceback (most recent call last):
  29. File "<stdin>", line 2, in <module>
  30. File "/usr/lib/python3.2/shelve.py", line 113, in __getitem__
  31. f = BytesIO(self.dict[key.encode(self.keyencoding)])
  32. KeyError: b'108710608601493504'
  33. >>>
Add Comment
Please, Sign In to add comment