Skip to content

Instantly share code, notes, and snippets.

@IamAdiSri
Last active July 15, 2020 01:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IamAdiSri/483f6f9b56c395a9e3e61ce51c026343 to your computer and use it in GitHub Desktop.
Save IamAdiSri/483f6f9b56c395a9e3e61ce51c026343 to your computer and use it in GitHub Desktop.
Pickling objects in Python3
class Pickler():
"""
Conveniently pickle Python objects and load pickled objects.
"""
def save(self, obj, loc):
"""
INPUT:
obj = Python object to be pickled.
loc = Location where pickled object will be saved.
OUTPUT:
Returns True if file was successfully saved, else returns False.
"""
try:
with open(loc, 'wb') as out:
pickle.dump(obj, out, pickle.HIGHEST_PROTOCOL)
return(True)
except:
return(False)
def load(self, loc):
"""
Input:
loc = Location where pickled object will be loaded from.
OUTPUT:
Returns object if successfully loaded, else returns False.
"""
try:
with open(loc, 'rb') as inp:
return(pickle.load(inp))
except:
return(False)
pkl = Pickler()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment