From 325ae816221b4959487ce6cb23b7e101824ae8f8 Mon Sep 17 00:00:00 2001 From: Serena Chen Date: Mon, 14 Mar 2016 02:10:38 -0400 Subject: [PATCH 1/2] dont know if this code wooorks --- counter.py | 78 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 30 deletions(-) diff --git a/counter.py b/counter.py index 1e2fb56..7cfc4a4 100644 --- a/counter.py +++ b/counter.py @@ -5,35 +5,53 @@ from pickle import 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') - 2 - >>> update_counter('blah2.txt',True) - 1 - >>> update_counter('blah.txt') - 3 - >>> update_counter('blah2.txt') - 2 - """ - pass + """ 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') + 2 + >>> update_counter('blah2.txt',True) + 1 + >>> update_counter('blah.txt') + 3 + >>> update_counter('blah2.txt') + 2 + """ + + if exists(file_name) and not reset: + f = open(file_name, 'r+') + num = load(f) + try: + newNum = int(num)+1 + except ValueError: + f.close() + break #create a new file + f.seek(0,0) + load(f,str(newNum)) + return newNum + + + f = open(file_name. 'w') + dump(f, '1') + return '1' + + 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 len(sys.argv) < 2: + import doctest + doctest.testmod() + else: + print "new value is " + str(update_counter(sys.argv[1])) From eb64f63680b00e8e8455c5124c11e68f8f433ad2 Mon Sep 17 00:00:00 2001 From: jaredbriskman Date: Mon, 14 Mar 2016 02:18:14 -0400 Subject: [PATCH 2/2] finished after windows struggles --- blah.txt | 3 +++ blah2.txt | 3 +++ counter.py | 16 +++++++++------- 3 files changed, 15 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..ce1fb1d --- /dev/null +++ b/blah.txt @@ -0,0 +1,3 @@ +S'3' +p0 +. \ No newline at end of file diff --git a/blah2.txt b/blah2.txt new file mode 100644 index 0000000..f295a9a --- /dev/null +++ b/blah2.txt @@ -0,0 +1,3 @@ +S'2' +p0 +. \ No newline at end of file diff --git a/counter.py b/counter.py index 7cfc4a4..a6eb44e 100644 --- a/counter.py +++ b/counter.py @@ -35,17 +35,19 @@ def update_counter(file_name, reset=False): num = load(f) try: newNum = int(num)+1 + f.seek(0,0) + dump(str(newNum),f) + return int(newNum) + except ValueError: + #if num is not a number f.close() - break #create a new file - f.seek(0,0) - load(f,str(newNum)) - return newNum - f = open(file_name. 'w') - dump(f, '1') - return '1' + + f = open(file_name, 'w') + dump('1',f) + return 1