onlyforbopi
9/30/2019 - 5:41 PM

Python.Modules.Pickle

#python #modules #mod #pickle #dataretention #data #comms

  • PickleTheory.py

  • PickleBasic.py : Basic pickle usage.

  • PickleAdvanced.py : Advanced pickle usage and functions.

  • Installation : cmd -> python -m pip install pickle

import pickle

test_list = [ 
				'a',
				'b',
				'c',
				'd'
				]


test_lsit2 = [ 1, 2, 3]

file_pkl = 'test_pickle.pkl'


# with file
with open(file_pkl, 'wb') as pickle_out:
		pickle.dump(test_list, pickle_out)



unpickled_list = []

with open(file_pkl, 'rb') as pickle_in:
	unpickled_list = pickle.load(pickle_in)

print(unpickled_list)


# with object

obj = pickle.dumps(test_list)
print(obj)

unpickled_list_obj = pickle.loads(obj)
print(unpickled_list_obj)



unpickled_list.append('new')

with open(file_pkl, 'wb') as pickle_out_new:
		pickle.dump(unpickled_list, pickle_out_new)

new_list = []

with open(file_pkl, 'rb') as pickle_in:
	new_list = pickle.load(pickle_in)

print(new_list)
class testclass:

	def __init__(self, param1, param2):
		self.data = []
		self.param1 = param1
		self.param2 = param2

	# when we pass 'self' as parameter
	# we can refer to the vars above
	def print_param1(self):
		print("Param1: " + str(self.param1))



def picklify(struct_in, file_ot):

	# test if struct_in is...

	try:
		import pickle
	except Exception as e:
		print("Error importing pickle: " + str(e))
		return False

	try:
		with open(file_ot, 'wb') as pickle_out:
				pickle.dump(struct_in, pickle_out)
	except:
		return False
	else:
		print("DataType pickled successfully.")
		# So we can string them together.
		return file_ot



def unpicklify(file_in):

	import pickle

	with open(file_in, 'rb') as pickle_in:
		datatype = pickle.load(pickle_in)

	return datatype


test_list = [ 
				'a',
				'b',
				'c',
				'd'
					]


test_dict = {
	
		'a' : 1,
		'b' : 2, 
		'c' : 3
					}

test_tup = (
				'a',
				'b',
				'c',
				'd'
					)


test_dict2 = {
	
		'a' : [ 1, 2, 3],
		
		'b' : {
				'h':1,
				'f':2
				}, 
		
		'c' : ('a', 'b', 'c')
		
				}



a = testclass(1, 2)
print(testclass)
print(a)
print(a.param1)

picklify(test_list, 'a.pkl')
print(unpicklify('a.pkl'))

print(unpicklify(picklify(test_list, 'b.pkl')))
print(unpicklify(picklify(test_dict, 'c.pkl')))
print(unpicklify(picklify(test_tup, 'd.pkl')))
print(unpicklify(picklify(test_dict2, 'e.pkl')))

b = unpicklify(picklify(a, 'f.pkl'))
print(b.param1)
b.print_param1()

c = unpicklify('test_pickle.pkl')
print(c)


# What is pickle:
#		Object serializer
#		Can save objects in binary in files, or in objects.
#		Used to store serialized objects.
#
#		IE: When we want to retain data after script run, we can store them serialized
#			in a .pkl file.
#
#		IE: When we want to communicate data from one script to another, we can extract
#			the data in a .pkl file and read it from the other script.
#
#		IE: When we want to manage incremental progress, we can save current to a pkl, 
#       load it the next day, import the new data, and save it again.