Skip to content

Commit

Permalink
added object serialization tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
x4nth055 committed Nov 3, 2019
1 parent c4b7755 commit ea081e7
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions general/object-serialization/README.md
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 16 additions & 0 deletions general/object-serialization/save_data_structures.py
Original file line number Diff line number Diff line change
@@ -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)
25 changes: 25 additions & 0 deletions general/object-serialization/save_defined_objects.py
Original file line number Diff line number Diff line change
@@ -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"<Person name={self.first_name} {self.last_name}, age={self.age}, gender={self.gender}>"


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)
30 changes: 30 additions & 0 deletions general/object-serialization/save_dumps.py
Original file line number Diff line number Diff line change
@@ -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"<Person name={self.first_name} {self.last_name}, age={self.age}, gender={self.gender}>"

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)

0 comments on commit ea081e7

Please sign in to comment.