Skip to content

Commit

Permalink
improve pickle load/dump practices
Browse files Browse the repository at this point in the history
Use “with” syntax to load/dump final project pickles to make sure they
are properly closed after use.
  • Loading branch information
Sheng Kung Yi authored and Sheng Kung Yi committed Oct 6, 2015
1 parent 126f267 commit 458edfb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion final_project/poi_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
features_list = ['poi','salary'] # You will need to use more features

### Load the dictionary containing the dataset
data_dict = pickle.load(open("final_project_dataset.pkl", "r") )
with open("final_project_dataset.pkl", "r") as data_file:
data_dict = pickle.load(data_file)

### Task 2: Remove outliers
### Task 3: Create new feature(s)
Expand Down
18 changes: 12 additions & 6 deletions final_project/tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,20 @@ def test_classifier(clf, dataset, feature_list, folds = 1000):
FEATURE_LIST_FILENAME = "my_feature_list.pkl"

def dump_classifier_and_data(clf, dataset, feature_list):
pickle.dump(clf, open(CLF_PICKLE_FILENAME, "w") )
pickle.dump(dataset, open(DATASET_PICKLE_FILENAME, "w") )
pickle.dump(feature_list, open(FEATURE_LIST_FILENAME, "w") )
with open(CLF_PICKLE_FILENAME, "w") as clf_outfile:
pickle.dump(clf, clf_outfile)
with open(DATASET_PICKLE_FILENAME, "w") as dataset_outfile:
pickle.dump(dataset, dataset_outfile)
with open(FEATURE_LIST_FILENAME, "w") as featurelist_outfile:
pickle.dump(feature_list, featurelist_outfile)

def load_classifier_and_data():
clf = pickle.load(open(CLF_PICKLE_FILENAME, "r") )
dataset = pickle.load(open(DATASET_PICKLE_FILENAME, "r") )
feature_list = pickle.load(open(FEATURE_LIST_FILENAME, "r"))
with open(CLF_PICKLE_FILENAME, "r") as clf_infile:
clf = pickle.load(clf_infile)
with open(DATASET_PICKLE_FILENAME, "r") as dataset_infile:
dataset = pickle.load(dataset_infile)
with open(FEATURE_LIST_FILENAME, "r") as featurelist_infile:
feature_list = pickle.load(featurelist_infile)
return clf, dataset, feature_list

def main():
Expand Down

0 comments on commit 458edfb

Please sign in to comment.