From ea081e77163465f165889058a9509fc167acb234 Mon Sep 17 00:00:00 2001 From: x4nth055 Date: Sun, 3 Nov 2019 17:16:37 +0100 Subject: [PATCH] added object serialization tutorial --- README.md | 1 + general/object-serialization/README.md | 1 + .../save_data_structures.py | 16 ++++++++++ .../save_defined_objects.py | 25 ++++++++++++++++ general/object-serialization/save_dumps.py | 30 +++++++++++++++++++ 5 files changed, 73 insertions(+) create mode 100644 general/object-serialization/README.md create mode 100644 general/object-serialization/save_data_structures.py create mode 100644 general/object-serialization/save_defined_objects.py create mode 100644 general/object-serialization/save_dumps.py diff --git a/README.md b/README.md index cd5b80f4..836a034c 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy - [How to Download Files in Python](https://www.thepythoncode.com/article/download-files-python). ([code](general/file-downloader)) - [How to Compress and Decompress Files in Python](https://www.thepythoncode.com/article/compress-decompress-files-tarfile-python). ([code](general/compressing-files)) - [How to Extract PDF Tables in Python](https://www.thepythoncode.com/article/extract-pdf-tables-in-python-camelot). ([code](general/pdf-table-extractor)) + - [How to Use Pickle for Object Serialization in Python](https://www.thepythoncode.com/article/object-serialization-saving-and-loading-objects-using-pickle-python). ([code](general/object-serialization)) - ### [Web Scraping](https://www.thepythoncode.com/topic/web-scraping) - [How to Access Wikipedia in Python](https://www.thepythoncode.com/article/access-wikipedia-python). ([code](web-scraping/wikipedia-extractor)) diff --git a/general/object-serialization/README.md b/general/object-serialization/README.md new file mode 100644 index 00000000..793be15c --- /dev/null +++ b/general/object-serialization/README.md @@ -0,0 +1 @@ +# [How to Use Pickle for Object Serialization in Python](https://www.thepythoncode.com/article/object-serialization-saving-and-loading-objects-using-pickle-python) \ No newline at end of file diff --git a/general/object-serialization/save_data_structures.py b/general/object-serialization/save_data_structures.py new file mode 100644 index 00000000..e732166d --- /dev/null +++ b/general/object-serialization/save_data_structures.py @@ -0,0 +1,16 @@ +import pickle + +# define any Python data structure including lists, sets, tuples, dicts, etc. +l = list(range(10000)) + +# save it to a file +with open("list.pickle", "wb") as file: + pickle.dump(l, file) + +# load it again +with open("list.pickle", "rb") as file: + unpickled_l = pickle.load(file) + + +print("unpickled_l == l: ", unpickled_l == l) +print("unpickled l is l: ", unpickled_l is l) \ No newline at end of file diff --git a/general/object-serialization/save_defined_objects.py b/general/object-serialization/save_defined_objects.py new file mode 100644 index 00000000..db9c92d8 --- /dev/null +++ b/general/object-serialization/save_defined_objects.py @@ -0,0 +1,25 @@ +import pickle + +class Person: + def __init__(self, first_name, last_name, age, gender): + self.first_name = first_name + self.last_name = last_name + self.age = age + self.gender = gender + + def __str__(self): + return f"" + + +p = Person("John", "Doe", 99, "Male") + +# save the object +with open("person.pickle", "wb") as file: + pickle.dump(p, file) + +# load the object +with open("person.pickle", "rb") as file: + p2 = pickle.load(file) + +print(p) +print(p2) \ No newline at end of file diff --git a/general/object-serialization/save_dumps.py b/general/object-serialization/save_dumps.py new file mode 100644 index 00000000..bcd4e0ea --- /dev/null +++ b/general/object-serialization/save_dumps.py @@ -0,0 +1,30 @@ +import pickle + + +class Person: + def __init__(self, first_name, last_name, age, gender): + self.first_name = first_name + self.last_name = last_name + self.age = age + self.gender = gender + + def __str__(self): + return f"" + +p = Person("John", "Doe", 99, "Male") + +# get the dumped bytes +dumped_p = pickle.dumps(p) +print(dumped_p) + +# write them to a file +with open("person.pickle", "wb") as file: + file.write(dumped_p) + +# load it +with open("person.pickle", "rb") as file: + p2 = pickle.loads(file.read()) + +print(p) +print(p2) +