๐ฅ Pickle Serialization
The pickle module implements binary protocols for serializing and de-serializing a Python object structure.
Mastering this concept will significantly boost your Python data science skills!
๐ป Code Example:
import pickle import gzip from pathlib import Path from datetime import datetime # 1. Basic serialization class PynfinitySession: def __init__(self, user: str, score: float): self.user = user self.score = score self.timestamp = datetime.now() self._courses = [] # Private โ still picklable! def add_course(self, course: str): self._courses.append(course) def __repr__(self): return f"Session({self.user}, {self.score}, courses={self._courses})" session = PynfinitySession("santoshtvk", 95.5) session.add_course("Infinite Python") session.add_course("Infinite AI") # 2. Serialize to bytes and back raw_bytes = pickle.dumps(session, protocol=pickle.HIGHEST_PROTOCOL) print(f"Pickled size: {len(raw_bytes)} bytes") restored = pickle.loads(raw_bytes) print("Restored:", restored) print("Timestamp:", restored.timestamp) # 3. Save to / load from file pkl_path = Path("pynfinity_session.pkl") with open(pkl_path, "wb") as f: pickle.dump(session, f, protocol=4) with open(pkl_path, "rb") as f: loaded = pickle.load(f) print("\nLoaded from file:", loaded) # 4. Compressed pickle (saves disk space for large objects) data = {"users": list(range(100_000)), "metadata": {"platform": "pynfinity"}} gz_path = Path("pynfinity_data.pkl.gz") with gzip.open(gz_path, "wb") as f: pickle.dump(data, f) with gzip.open(gz_path, "rb") as f: recovered = pickle.load(f) print(f"\nGzipped data recovered | users count: {len(recovered['users']):,}") # 5. Selective pickling with __getstate__ / __setstate__ class SecureSession: def __init__(self, user, token, score): self.user = user self.token = token # Should NOT be pickled! self.score = score def __getstate__(self): state = self.__dict__.copy() del state["token"] # Exclude sensitive data return state def __setstate__(self, state): self.__dict__.update(state) self.token = None # Reset after unpickling s = SecureSession("dhruv", "secret_token_xyz", 88) loaded_s = pickle.loads(pickle.dumps(s)) print(f"\nSecure pickle โ token after restore: {loaded_s.token!r}") # Cleanup pkl_path.unlink(missing_ok=True) gz_path.unlink(missing_ok=True)
Keep exploring and happy coding! ๐ป