code: def pickle(self, f, protocol=-1):
          
          try: #Py2
              import cPickle as pickle
              if isinstance(f, basestring):
                  with open(f, 'wb') as f:
                      pickle.dump(self, f, protocol)
              else:
                  pickle.dump(self, f, protocol)
          except ImportError: #python3
              import pickle
              if isinstance(f, str):
                  with open(f, 'wb') as f:
                      pickle.dump(self, f, protocol)
              else:
                  pickle.dump(self, f, protocol)
docs: :param f: either filename or open file object to write to.
                    if it is an open buffer, you have to make sure to close
                    it properly.
          :param protocol: pickling protocol to use, python-pickle for details.