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
2 changes: 2 additions & 0 deletions blah.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
I10
.
2 changes: 2 additions & 0 deletions blah2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
I2
.
34 changes: 31 additions & 3 deletions counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -26,10 +26,38 @@ 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
"""
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:
Expand Down