Last active
July 15, 2020 01:56
-
-
Save IamAdiSri/483f6f9b56c395a9e3e61ce51c026343 to your computer and use it in GitHub Desktop.
Pickling objects in Python3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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