From 18011ec69643b5771f7b1558c501ab1fd0e49ab6 Mon Sep 17 00:00:00 2001 From: hmowen Date: Sun, 13 Mar 2016 23:30:46 -0400 Subject: [PATCH 1/2] implemented pickle code --- counter.py | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/counter.py b/counter.py index 1e2fb56..edb5be3 100644 --- a/counter.py +++ b/counter.py @@ -2,7 +2,10 @@ from os.path import exists import sys +import os from pickle import dump, load +import pickle +#from pattern.web import * def update_counter(file_name, reset=False): """ Updates a counter stored in the file 'file_name' @@ -29,7 +32,47 @@ def update_counter(file_name, reset=False): >>> update_counter('blah2.txt') 2 """ - pass +# ts_eliot_texts_FULL = URL('http://www.gutenberg.org/cache/epub/1567/pg1567.txt').download() +# f = open('ts_eliot_full.pickle','w') +# pickle.dump(ts_eliot_texts_FULL,f) +# f.close() +# input_file = open('ts_eliot_full.pickle','r') +# reloaded_copy_of_eliot_texts = pickle.load(input_file) + if os.path.exists(file_name) == False: + f = open(file_name, 'w') + counter = 1 + pickle.dump(counter, f) + f.close + return counter + + elif os.path.exists(file_name) == True: + if reset == True: + f = open(file_name, 'w') + counter = 1 + pickle.dump(counter, f) + f.close + return counter + + else: + f = open(file_name, 'r+') + counter = pickle.load(f) + counter += 1 + f.seek(0,0) + pickle.dump(counter, f) + f.close + return counter + + print counter + + + + +# # Save data to a file (will be part of your data fetching script) +# lincoln_speeches_FULL = URL('http://www.gutenberg.org/cache/epub/14721/pg14721.txt').download() +# f = open('lincoln_speeches.pickle','w') +# pickle.dump(lincoln_speeches_FULL,f) +# f.close() + if __name__ == '__main__': if len(sys.argv) < 2: From 99fe3855ada9a6f44c379ab0701e45442dda8db0 Mon Sep 17 00:00:00 2001 From: hmowen Date: Sun, 13 Mar 2016 23:34:54 -0400 Subject: [PATCH 2/2] implemented pickle code --- counter.py | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/counter.py b/counter.py index edb5be3..42e554c 100644 --- a/counter.py +++ b/counter.py @@ -1,11 +1,8 @@ """ A program that stores and updates a counter using a Python pickle file""" -from os.path import exists import sys import os -from pickle import dump, load import pickle -#from pattern.web import * def update_counter(file_name, reset=False): """ Updates a counter stored in the file 'file_name' @@ -32,12 +29,6 @@ def update_counter(file_name, reset=False): >>> update_counter('blah2.txt') 2 """ -# ts_eliot_texts_FULL = URL('http://www.gutenberg.org/cache/epub/1567/pg1567.txt').download() -# f = open('ts_eliot_full.pickle','w') -# pickle.dump(ts_eliot_texts_FULL,f) -# f.close() -# input_file = open('ts_eliot_full.pickle','r') -# reloaded_copy_of_eliot_texts = pickle.load(input_file) if os.path.exists(file_name) == False: f = open(file_name, 'w') counter = 1 @@ -64,16 +55,6 @@ def update_counter(file_name, reset=False): print counter - - - -# # Save data to a file (will be part of your data fetching script) -# lincoln_speeches_FULL = URL('http://www.gutenberg.org/cache/epub/14721/pg14721.txt').download() -# f = open('lincoln_speeches.pickle','w') -# pickle.dump(lincoln_speeches_FULL,f) -# f.close() - - if __name__ == '__main__': if len(sys.argv) < 2: import doctest