Skip to content

pickle #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 201 additions & 0 deletions pickle.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Understanding the Python `pickle` Module\n",
"\n",
"The `pickle` module in Python implements binary protocols for serializing and de-serializing a Python object structure. \n",
"\"\"Pickling\"\" is the process of converting a Python object hierarchy into a byte stream, and \"\"unpickling\"\" is the inverse operation, converting a byte stream back into a Python object hierarchy."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Why use `pickle`?\n",
"\n",
"- **Persistence**: Save Python objects to a file so they can be loaded later.\n",
"- **Transfer**: Send Python objects over a network (though JSON is often preferred for cross-language compatibility)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Key Functions: `pickle.dump()` and `pickle.load()`\n",
"\n",
"### `pickle.dump(obj, file, protocol=None)`\n",
"Writes the pickled representation of `obj` to the open file object `file`.\n",
"\n",
"### `pickle.load(file)`\n",
"Reads the pickled representation of an object from the open file object `file` and returns the reconstructed object.\n",
"\n",
"Let's see some examples."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 1: Pickling a Dictionary and a List"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pickle\n",
"\n",
"data_dict = {\"name\": \"Alice\", \"age\": 30, \"city\": \"New York\"}\n",
"data_list = [1, 2, 3, {'a': 10, 'b': 20}, 'hello']\n",
"\n",
"# Pickling (serializing) the objects\n",
"with open('data.pickle', 'wb') as f:\n",
" pickle.dump(data_dict, f)\n",
" pickle.dump(data_list, f) # You can dump multiple objects to the same file\n",
"\n",
"print(\"Objects pickled successfully to data.pickle\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 2: Unpickling the Objects"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pickle\n",
"\n",
"# Unpickling (de-serializing) the objects\n",
"with open('data.pickle', 'rb') as f:\n",
" loaded_dict = pickle.load(f)\n",
" loaded_list = pickle.load(f)\n",
"\n",
"print(f\"Loaded Dictionary: {loaded_dict}\")\n",
"print(f\"Loaded List: {loaded_list}\")\n",
"\n",
"# Verify types\n",
"print(f\"Type of loaded_dict: {type(loaded_dict)}\")\n",
"print(f\"Type of loaded_list: {type(loaded_list)}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 3: Pickling a Custom Class Instance"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pickle\n",
"\n",
"class MyClass:\n",
" def __init__(self, name, value):\n",
" self.name = name\n",
" self.value = value\n",
"\n",
" def greet(self):\n",
" return f\"Hello, my name is {self.name} and my value is {self.value}\"\n",
"\n",
"obj = MyClass(\"TestObject\", 123)\n",
"print(f\"Original Object Greeting: {obj.greet()}\")\n",
"\n",
"# Pickle the custom object\n",
"with open('myclass.pickle', 'wb') as f:\n",
" pickle.dump(obj, f)\n",
"\n",
"print(\"Custom object pickled successfully\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 4: Unpickling a Custom Class Instance"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pickle\n",
"\n",
"# The class definition must be available in the environment where you unpickle\n",
"class MyClass:\n",
" def __init__(self, name, value):\n",
" self.name = name\n",
" self.value = value\n",
"\n",
" def greet(self):\n",
" return f\"Hello, my name is {self.name} and my value is {self.value}\"\n",
"\n",
"# Unpickle the custom object\n",
"with open('myclass.pickle', 'rb') as f:\n",
" loaded_obj = pickle.load(f)\n",
"\n",
"print(f\"Loaded Object Greeting: {loaded_obj.greet()}\")\n",
"print(f\"Type of loaded_obj: {type(loaded_obj)}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Security Warning!\n",
"\n",
"**Never unpickle data received from an untrusted or unauthenticated source!**\n",
"\n",
"The `pickle` module is not secure against maliciously constructed data. Maliciously constructed pickle data can execute arbitrary code upon unpickling. This is due to the fact that `pickle` can reconstruct arbitrary Python objects, including instances of custom classes, and can call methods on these objects during the unpickling process.\n",
"\n",
"For cross-language compatibility or when security is a concern, consider alternatives like JSON, XML, or Protocol Buffers, which are typically safer for data exchange because they only serialize data, not arbitrary code."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Conclusion\n",
"\n",
"The `pickle` module is a powerful tool for Python object serialization, especially useful for saving and loading Python-specific data structures. However, its use requires caution regarding security, particularly when dealing with data from untrusted sources."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}