From 2bd5ee350ced1af6c3a880016cfa53ad006e70ea Mon Sep 17 00:00:00 2001 From: hthomas60 Date: Thu, 2 Nov 2017 11:31:49 -0400 Subject: [PATCH] Pickle Toolbox --- .gitignore | 192 ++++++++++++++++++++++++++--------------------------- README.md | 8 +-- blah.txt | 1 + blah2.txt | 1 + counter.py | 132 +++++++++++++++++++++++++----------- file_name | 1 + 6 files changed, 195 insertions(+), 140 deletions(-) create mode 100644 blah.txt create mode 100644 blah2.txt create mode 100644 file_name diff --git a/.gitignore b/.gitignore index cbc2b23..53482e9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,96 +1,96 @@ -# Created by https://www.gitignore.io/api/python - -### Python ### -# Byte-compiled / optimized / DLL files -__pycache__/ -*.py[cod] -*$py.class - -# C extensions -*.so - -# Distribution / packaging -.Python -env/ -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -wheels/ -*.egg-info/ -.installed.cfg -*.egg - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*,cover -.hypothesis/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -target/ - -# Jupyter Notebook -.ipynb_checkpoints - -# pyenv -.python-version - -# celery beat schedule file -celerybeat-schedule - -# dotenv -.env - -# virtualenv -.venv/ -venv/ -ENV/ - -# Spyder project settings -.spyderproject - -# Rope project settings -.ropeproject - -# End of https://www.gitignore.io/api/python +# Created by https://www.gitignore.io/api/python + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*,cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# dotenv +.env + +# virtualenv +.venv/ +venv/ +ENV/ + +# Spyder project settings +.spyderproject + +# Rope project settings +.ropeproject + +# End of https://www.gitignore.io/api/python diff --git a/README.md b/README.md index 98561eb..e69caa4 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# ToolBox-Pickling -Pickling Project Toolbox starter code - -Full instructions at [the course website](https://sd17spring.github.io/toolboxes/pickling/) +# ToolBox-Pickling +Pickling Project Toolbox starter code + +Full instructions at [the course website](https://sd17spring.github.io/toolboxes/pickling/) diff --git a/blah.txt b/blah.txt new file mode 100644 index 0000000..82dd31b --- /dev/null +++ b/blah.txt @@ -0,0 +1 @@ +€K. \ No newline at end of file diff --git a/blah2.txt b/blah2.txt new file mode 100644 index 0000000..dde5455 --- /dev/null +++ b/blah2.txt @@ -0,0 +1 @@ +€K. \ No newline at end of file diff --git a/counter.py b/counter.py index 38ec816..d3c3565 100644 --- a/counter.py +++ b/counter.py @@ -1,40 +1,92 @@ -""" 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 - - -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 - -if __name__ == '__main__': - if len(sys.argv) < 2: - import doctest - doctest.testmod() - else: - print("new value is " + str(update_counter(sys.argv[1]))) +""" 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 +import pickle +import os.path + +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 + """ + if os.path.exists(file_name)== False: + f = open(file_name, '') + pickle.dump(1, f) + f.close() + if reset == True: + f = open(file_name, 'wb') + pickle.dump(1, f) + f.close() + else: + f = open(file_name, 'br') + #print(f) + filecounter = pickle.load(f) + f.close() + f = open(file_name, 'wb') + + filecounter+=1 + + pickle.dump(filecounter, f) + f.close() + + + input_file = open(file_name, 'rb') + counter = pickle.load(input_file) + #print(counter) + f.close() + return counter + +""" +These files were used in my textmining project +""" +def loadbooks(): + """ + Loads books from gutenberg.org. Book id has to be manualy changed each book. + """ + downloaded_book = requests.get('http://www.gutenberg.org/ebooks/1522.txt.utf-8').text + return downloaded_book + +def savebook(book_text, filename): + """ + Saves a the text of a book into a file. + """ + f = open(filename, 'wb') + pickle.dump(book_text, f) + f.close() + +def opensavedbook(file): + """ + Opens a file that is saved on the computer + """ + input_file = open(file, 'rb') + opened_text = pickle.load(input_file) + return opened_text + +if __name__ == '__main__': + if len(sys.argv) <= 2: + import doctest + doctest.testmod() + else: + print("new value is " + str(update_counter(sys.argv[1]))) diff --git a/file_name b/file_name new file mode 100644 index 0000000..46c77e7 --- /dev/null +++ b/file_name @@ -0,0 +1 @@ +€K. \ No newline at end of file