From f34b8d38e2096b48ad6e488bdf43af283f30d79b Mon Sep 17 00:00:00 2001 From: CBrooke47 Date: Thu, 13 Oct 2016 14:17:22 -0400 Subject: [PATCH] Toolbox Submission. Maybe? Possibly? --- blah.txt | 2 ++ blah2.txt | 2 ++ counter.py | 29 ++++++++++++++++++++++------- 3 files changed, 26 insertions(+), 7 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..569369f --- /dev/null +++ b/blah.txt @@ -0,0 +1,2 @@ +I1 +. \ 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..6479f09 100755 --- a/counter.py +++ b/counter.py @@ -1,23 +1,25 @@ -""" A program that stores and updates a counter using a Python pickle file""" +""" +Chloe Grubb +October 2016 +Software Design + +ToolBox-Pickling: A program that stores and updates a counter using a Python pickle file +""" from os.path import exists import sys -from pickle import dump, load +from pickle import dumps, dump, load def update_counter(file_name, reset=False): """ Updates a counter stored in the file 'file_name' - A new counter will be created and initialized to 1 if none exists or if the reset flag is True. - If the counter already exists and reset is False, the counter's value will be incremented. - file_name: the file that stores the counter to be incremented. If the file doesn't exist, a counter is created and initialized to 1. reset: True if the counter in the file should be rest. returns: the new counter value - >>> update_counter('blah.txt',True) 1 >>> update_counter('blah.txt') @@ -28,8 +30,21 @@ def update_counter(file_name, reset=False): 3 >>> update_counter('blah2.txt') 2 + >>> update_counter('blah.txt',True) + 1 """ - pass + if reset or not exists(file_name): + f = open(file_name,"w") + dump(1,f) + return 1 + else: + f = open(file_name,"r+") + counter = load(f) + counter += 1 + f.seek(0,0) + dump(counter,f) + return counter + f.close() if __name__ == '__main__': if len(sys.argv) < 2: