From 2d0dbe661f39999c294ac38b77fbcf77292e764e Mon Sep 17 00:00:00 2001 From: Jeff Pflueger Date: Wed, 26 Apr 2017 21:43:49 -0400 Subject: [PATCH] completed pickling toolbox --- blah.p | Bin 0 -> 11 bytes blah.txt | Bin 0 -> 11 bytes blah2.p | Bin 0 -> 11 bytes blah2.txt | Bin 0 -> 11 bytes counter.py | 27 ++++++++++++++++++++------- jeff.p | Bin 0 -> 11 bytes jeff.txt | 0 7 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 blah.p create mode 100644 blah.txt create mode 100644 blah2.p create mode 100644 blah2.txt create mode 100644 jeff.p create mode 100644 jeff.txt diff --git a/blah.p b/blah.p new file mode 100644 index 0000000000000000000000000000000000000000..7621841f65698867152fb563d17b7bfe0f02e45f GIT binary patch literal 11 ScmZo*j$mY9U@$IZ&;tMq76GpS literal 0 HcmV?d00001 diff --git a/blah.txt b/blah.txt new file mode 100644 index 0000000000000000000000000000000000000000..7621841f65698867152fb563d17b7bfe0f02e45f GIT binary patch literal 11 ScmZo*j$mY9U@$IZ&;tMq76GpS literal 0 HcmV?d00001 diff --git a/blah2.p b/blah2.p new file mode 100644 index 0000000000000000000000000000000000000000..04ffe6b311b07d70866373346ba65e75ec480acb GIT binary patch literal 11 ScmZo*j$mY9U@$6V&;tMq5&^CN literal 0 HcmV?d00001 diff --git a/blah2.txt b/blah2.txt new file mode 100644 index 0000000000000000000000000000000000000000..04ffe6b311b07d70866373346ba65e75ec480acb GIT binary patch literal 11 ScmZo*j$mY9U@$6V&;tMq5&^CN literal 0 HcmV?d00001 diff --git a/counter.py b/counter.py index 38ec816..98ba282 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): @@ -19,18 +19,31 @@ def update_counter(file_name, reset=False): reset: True if the counter in the file should be rest. returns: the new counter value - >>> update_counter('blah.txt',True) + >>> update_counter('blah.p',True) 1 - >>> update_counter('blah.txt') + >>> update_counter('blah.p') 2 - >>> update_counter('blah2.txt',True) + >>> update_counter('blah2.p',True) 1 - >>> update_counter('blah.txt') + >>> update_counter('blah.p') 3 - >>> update_counter('blah2.txt') + >>> update_counter('blah2.p') 2 """ - pass + if not reset: + try: + counter = int(pickle.load(open(file_name, "rb"))) + counter += 1 + print(counter) + pickle.dump(str(counter), open(file_name, "wb")) + except FileNotFoundError: + open(file_name, 'a').close() + pickle.dump(str(1), open(file_name, "wb")) + print(1) + else: + open(file_name, 'a').close() + pickle.dump(str(1), open(file_name, "wb")) + print(1) if __name__ == '__main__': if len(sys.argv) < 2: diff --git a/jeff.p b/jeff.p new file mode 100644 index 0000000000000000000000000000000000000000..9a29eb5e8721a44ed6502c839749ac663f259efb GIT binary patch literal 11 ScmZo*j$mY9U@$FY&;tMq9s#ic literal 0 HcmV?d00001 diff --git a/jeff.txt b/jeff.txt new file mode 100644 index 0000000..e69de29