mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
🏣 Clean up for arxiv
This commit is contained in:
parent
1b969e7db3
commit
41756cb10c
280 changed files with 2 additions and 11 deletions
58
book/ebook_src/python_examples/example_pickle.py
Normal file
58
book/ebook_src/python_examples/example_pickle.py
Normal file
|
@ -0,0 +1,58 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
import pickle
|
||||
|
||||
def import_pickle(filename):
|
||||
fh = None
|
||||
try:
|
||||
fh = open(filename, "rb")
|
||||
mydict2 = pickle.load(fh)
|
||||
return mydict2
|
||||
|
||||
except (EnvironmentError) as err:
|
||||
print ("{0}: import error: {0}".format(os.path.basename(sys.arg[0]), err))
|
||||
return false
|
||||
|
||||
finally:
|
||||
if fh is not None:
|
||||
fh.close()
|
||||
|
||||
|
||||
def test_import_pickle():
|
||||
pkl_file = 'test.dat'
|
||||
mydict = import_pickle(pkl_file)
|
||||
print(mydict)
|
||||
|
||||
|
||||
|
||||
def export_pickle(data, filename='test.dat', compress=False):
|
||||
|
||||
fh = None
|
||||
try:
|
||||
if compress:
|
||||
fh = gzip.open(filename, "wb") # write binary
|
||||
else:
|
||||
fh = open(filename, "wb") # compact binary pickle format
|
||||
pickle.dump(data, fh, pickle.HIGHEST_PROTOCOL)
|
||||
|
||||
except(EnvironmentError, pickle.PickingError) as err:
|
||||
print("{0}: export error: {1}".format(os.path.basename(sys.argv[0], err)))
|
||||
return False
|
||||
|
||||
finally:
|
||||
if fh is not None:
|
||||
fh.close()
|
||||
|
||||
|
||||
def test_export_pickle():
|
||||
mydict = {'a': 1, 'b': 2, 'c': 3}
|
||||
export_pickle(mydict)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_export_pickle()
|
||||
test_import_pickle()
|
Loading…
Add table
Add a link
Reference in a new issue