intro-to-python icon indicating copy to clipboard operation
intro-to-python copied to clipboard

Pickle Module Notes

Open s2t2 opened this issue 4 years ago • 1 comments

  • https://docs.python.org/3/library/pickle.html
import os
import pickle

class Team()
  # ...

FILEPATH = os.path.join(os.path.dirname(__file__), "..", "my", "my-team.pkl")

def save_it():
   team = Team()

    print("SAVING...")
    with open(FILEPATH, "wb") as f:
        pickle.dump(team, f)

def load_it():
    print("LOADING...")
    with open(FILEPATH, "rb") as f:
        team = pickle.load(f)
    return team

FYI: the Team class definition is required for pickle load step, so if defined in separate file, need to import class

s2t2 avatar Jul 10 '20 15:07 s2t2

import pickle

def save_model(model, local_filepath):
    print("SAVING MODEL TO LOCAL FILE...")
    with open(local_filepath, "wb") as f:
        pickle.dump(model, f)

def load_model(local_filepath):
    print("LOADING MODEL FROM LOCAL FILE...")
    with open(local_filepath, "rb") as f:
        model = pickle.load(f)
    return model

s2t2 avatar Sep 05 '20 19:09 s2t2