From f952f38c6833ec7e1aeb9923cd4d0da1176b0a0c Mon Sep 17 00:00:00 2001 From: Lauren Gulland Date: Sun, 13 Mar 2016 20:54:20 -0400 Subject: [PATCH 1/2] Finished project 3 toolbox, all docstrings correct. --- blah.txt | 2 ++ blah2.txt | 2 ++ counter.py | 18 ++++++++++++++++-- 3 files changed, 20 insertions(+), 2 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..ba8c4c4 --- /dev/null +++ b/blah.txt @@ -0,0 +1,2 @@ +I3 +. \ No newline at end of file diff --git a/blah2.txt b/blah2.txt new file mode 100644 index 0000000..1cc9408 --- /dev/null +++ b/blah2.txt @@ -0,0 +1,2 @@ +I2 +. \ No newline at end of file diff --git a/counter.py b/counter.py index 1e2fb56..32e2df3 100644 --- a/counter.py +++ b/counter.py @@ -2,7 +2,7 @@ from os.path import exists import sys -from pickle import dump, load +import pickle def update_counter(file_name, reset=False): """ Updates a counter stored in the file 'file_name' @@ -29,7 +29,21 @@ def update_counter(file_name, reset=False): >>> update_counter('blah2.txt') 2 """ - pass + if exists(file_name) and reset==False: + f = open(file_name, 'r+') + f_read = f.read() + current = int(pickle.loads(f_read)) + current +=1 + f.seek(0,0) + pickle.dump(current, f) + f.close() + else: + f = open(file_name, 'w') + current = 1 + pickle.dump(current, f) + f.close() + return current + if __name__ == '__main__': if len(sys.argv) < 2: From e7e99304eff78773ed08450241f99099c78cf756 Mon Sep 17 00:00:00 2001 From: Lauren Gulland Date: Sun, 13 Mar 2016 20:58:17 -0400 Subject: [PATCH 2/2] Added test case of two digits (incrementing counter to 10). still works. --- blah.txt | 2 +- counter.py | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/blah.txt b/blah.txt index ba8c4c4..7f0de02 100644 --- a/blah.txt +++ b/blah.txt @@ -1,2 +1,2 @@ -I3 +I10 . \ No newline at end of file diff --git a/counter.py b/counter.py index 32e2df3..14b9f28 100644 --- a/counter.py +++ b/counter.py @@ -26,7 +26,21 @@ def update_counter(file_name, reset=False): 1 >>> update_counter('blah.txt') 3 - >>> update_counter('blah2.txt') + >>> update_counter('blah.txt') + 4 + >>> update_counter('blah.txt') + 5 + >>> update_counter('blah.txt') + 6 + >>> update_counter('blah.txt') + 7 + >>> update_counter('blah.txt') + 8 + >>> update_counter('blah.txt') + 9 + >>> update_counter('blah.txt') #included to test two digit case + 10 + >>> update_counter('blah2.txt') 2 """ if exists(file_name) and reset==False: