forked from x4nth055/pythoncode-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|