Advertisement
Guest User

Untitled

a guest
Jun 6th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.95 KB | None | 0 0
  1. from __future__ import print_function
  2.  
  3. import os
  4.  
  5. try:
  6.     import cPickle as pickle
  7. except ImportError:
  8.     import pickle
  9.  
  10.  
  11. class Thing:
  12.     def __init__(self, s):
  13.         self.s = s
  14.  
  15.     def __repr__(self):
  16.         return 'Thing: %s' % self.s
  17.  
  18.     def save(self, filename):
  19.         '''Save thing to a file.'''
  20.         with open(filename, 'wb') as f:
  21.             pickle.dump(self, f)
  22.  
  23.     @staticmethod
  24.     def load(filename):
  25.         ''' Return a thing loaded from file.'''
  26.         with open(filename, 'rb') as f:
  27.             obj = pickle.load(f)
  28.         return obj
  29.  
  30.     def split_into_tokens(message):
  31.         pass
  32.  
  33.     def split_into_lemmas(message):
  34.         pass
  35.  
  36. SAVE_FILE = 'thing.pkl'
  37.  
  38. if __name__ == '__main__':
  39.     if os.path.isfile(SAVE_FILE):
  40.         t = Thing.load(SAVE_FILE)
  41.         print('Loaded thing from disk:', SAVE_FILE)
  42.  
  43.     else:
  44.         t = Thing('foo')
  45.         t.save('thing.pkl')
  46.         print('Saved thing to disk:', SAVE_FILE)
  47.  
  48.     print(t)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement