Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions blah.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
S'counter = 3'
p0
.
3 changes: 3 additions & 0 deletions blah2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
S'counter = 2'
p0
.
22 changes: 15 additions & 7 deletions counter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
""" A program that stores and updates a counter using a Python pickle file"""
"""
Author = Cedric Kim
A program that stores and updates a counter using a Python pickle file"""

from os.path import exists
import sys
Expand Down Expand Up @@ -29,11 +31,17 @@ def update_counter(file_name, reset=False):
>>> update_counter('blah2.txt')
2
"""
pass
if not exists(file_name) or reset: ##if the file does not exist, or reset is true
counter_string = 'counter = 0' ##make a string with counter 0
pickle.dump(counter_string, open(file_name, 'w')) ##dump the data into a the file
counter_string = pickle.load(open(file_name, 'r+')) ##open the file in r+
counter_number = int(counter_string.lstrip('counter =')) ##find the value of the counter
updated_counter_string = 'counter = ' + str(counter_number+1) ##update the counter
pickle.dump(updated_counter_string, open(file_name, 'r+')) ##dump the new value in the file
return counter_number + 1 ##returns the value

if __name__ == '__main__':
if len(sys.argv) < 2:
import doctest
doctest.testmod()
else:
print "new value is " + str(update_counter(sys.argv[1]))
import pickle
import doctest
doctest.testmod()
print "new value is " + str(update_counter('test.txt'))
3 changes: 3 additions & 0 deletions test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
S'counter = 34'
p0
.
Empty file added test.txt~
Empty file.