From b081bc4d0d0dfb6ea99f1e60e56b9d83f41e3814 Mon Sep 17 00:00:00 2001 From: Mikhaela Dietch Date: Sat, 19 Mar 2016 17:31:21 -0400 Subject: [PATCH 1/2] in process --- blah.txt | 1 + blah2.txt | 0 counter.py | 45 ++++++++++++++++++++++++++++++++++++--------- 3 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 blah.txt create mode 100644 blah2.txt diff --git a/blah.txt b/blah.txt new file mode 100644 index 0000000..56a6051 --- /dev/null +++ b/blah.txt @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/blah2.txt b/blah2.txt new file mode 100644 index 0000000..e69de29 diff --git a/counter.py b/counter.py index 1e2fb56..9094763 100644 --- a/counter.py +++ b/counter.py @@ -1,7 +1,10 @@ """ A program that stores and updates a counter using a Python pickle file""" -from os.path import exists +from os.path import exists +#Return True if path refers to an existing path. +#Returns False for broken symbolic links. import sys +import pickle from pickle import dump, load def update_counter(file_name, reset=False): @@ -29,11 +32,35 @@ def update_counter(file_name, reset=False): >>> update_counter('blah2.txt') 2 """ - pass - -if __name__ == '__main__': - if len(sys.argv) < 2: - import doctest - doctest.testmod() - else: - print "new value is " + str(update_counter(sys.argv[1])) \ No newline at end of file + + + if exists(file_name) == False or reset == True: # if file dne or reset = True + f = open(file_name,'w') # f is now a file object ready to be written + counter = 1 + pickle.load(f) + f.write(str(counter)) # writes counter to file + f.close() + pickle.dump() + print + + elif exists(file_name) and reset == False: # if file exist and reset = False add one to counter + counter += 1 # adds 1 to pre-existing counter + f = open(file_name,'r+') # f is now a file object ready to be read + content = pickle.load(f) # load instead of dump as f already exists + f.seek(0,0) # moves pointer to start of file to write over previous content + f.write(str(counter)) # writes counter to file + f.close() + + + + +update_counter("blah.txt") + +# if __name__ == '__main__': +# if len(sys.argv) < 2: +# import doctest +# doctest.testmod() +# else: +# print "new value is " + str(update_counter(sys.argv[1])) # catches name variable + + From 3d911368d7ccc7ff03ec35b23cfcbe8021537da3 Mon Sep 17 00:00:00 2001 From: Mikhaela Dietch Date: Sun, 20 Mar 2016 12:47:46 -0400 Subject: [PATCH 2/2] final commit of tool box with comments --- blah.txt | 3 ++- blah2.txt | 2 ++ counter.py | 50 +++++++++++++++++++++++++++----------------------- dogs | 2 ++ pigs | 2 ++ 5 files changed, 35 insertions(+), 24 deletions(-) create mode 100644 dogs create mode 100644 pigs diff --git a/blah.txt b/blah.txt index 56a6051..ba8c4c4 100644 --- a/blah.txt +++ b/blah.txt @@ -1 +1,2 @@ -1 \ No newline at end of file +I3 +. \ No newline at end of file diff --git a/blah2.txt b/blah2.txt index e69de29..1cc9408 100644 --- a/blah2.txt +++ b/blah2.txt @@ -0,0 +1,2 @@ +I2 +. \ No newline at end of file diff --git a/counter.py b/counter.py index 9094763..3315bd4 100644 --- a/counter.py +++ b/counter.py @@ -33,34 +33,38 @@ def update_counter(file_name, reset=False): 2 """ + counter = 0 - if exists(file_name) == False or reset == True: # if file dne or reset = True - f = open(file_name,'w') # f is now a file object ready to be written - counter = 1 - pickle.load(f) - f.write(str(counter)) # writes counter to file + # If file dne or reset = True we will create counter starting at 1 + if exists(file_name) == False or reset == True: + f = open(file_name,'w') # f is now a file object ready to be written + counter = 1 # set counter ready to write to file + pickle.dump(counter,f) + return counter f.close() - pickle.dump() - print - elif exists(file_name) and reset == False: # if file exist and reset = False add one to counter - counter += 1 # adds 1 to pre-existing counter - f = open(file_name,'r+') # f is now a file object ready to be read - content = pickle.load(f) # load instead of dump as f already exists + # If file exist and reset = False we will add one to counter + elif exists(file_name) and reset == False: + f = open(file_name,'r+') # f is now a file object ready to be read and then written + counter = pickle.load(f) # load f as file already exists f.seek(0,0) # moves pointer to start of file to write over previous content - f.write(str(counter)) # writes counter to file - f.close() - + counter += 1 # adds 1 to pre-existing counter + pickle.dump(counter,f) # update counter in f + return counter + f.close() - +if __name__ == '__main__': + if len(sys.argv) < 2: + import doctest + doctest.testmod() + else: + print "new value is " + str(update_counter(sys.argv[1])) # catches name variable -update_counter("blah.txt") - -# if __name__ == '__main__': -# if len(sys.argv) < 2: -# import doctest -# doctest.testmod() -# else: -# print "new value is " + str(update_counter(sys.argv[1])) # catches name variable + + # I had a hard time with this, my main difficulty was in sorting out the order of operations to call + # 1) open/create file + # 2) if file contains content: load content with pickle + # 3) dump new content into file + # 4) return content & close file diff --git a/dogs b/dogs new file mode 100644 index 0000000..569369f --- /dev/null +++ b/dogs @@ -0,0 +1,2 @@ +I1 +. \ No newline at end of file diff --git a/pigs b/pigs new file mode 100644 index 0000000..569369f --- /dev/null +++ b/pigs @@ -0,0 +1,2 @@ +I1 +. \ No newline at end of file