Core Module
12 min forge
Python Standard Library Favorites
Why Python is "Batteries Included". Essential modules every engineer must know.
Python Standard Library Favorites
π‘οΈ What is the Standard Library?
Python's "Batteries Included" philosophy means it comes with a vast collection of modules ready to solve common tasks without installing external packages.
β° When to Use
- Data Serialization:
jsonfor APIs,picklefor Python-specific binary serializing. - File System:
osand the modernpathlibfor file/directory manipulation. - Concurrency:
threading,multiprocessing, andasyncio. - Utilities:
collections(namedtuple, deque, Counter),itertools(efficient looping), andfunctools(decorators/wrappers).
π Complexity & Performance
pathliboveros.path:pathliboffers an object-oriented approach which is more readable and less error-prone.collections.deque: Provides $O(1)$ appends and pops from both ends, compared to list's $O(N)$ for popping from the front.itertools: Generators initertoolsare memory-efficient because they process items one-by-one rather than loading entire lists into RAM.
π» Code Example: Utility Power
python Standardimport json from pathlib import Path from collections import Counter, namedtuple # 1. Modern Path handling logs_dir = Path("logs/daily") logs_dir.mkdir(parents=True, exist_ok=True) # 2. Advanced Collections Point = namedtuple('Point', ['x', 'y']) p = Point(10, 20) # 3. Frequency Identification text = "forge your path with the interview forge" counts = Counter(text.split()) print(counts.most_common(1)) # [('forge', 2)] # 4. JSON Serialization data = {"status": "cracked", "score": 99.5} json_string = json.dumps(data)
β οΈ Interview Pitfalls
pickleSecurity: Never unpickle data from an untrusted source, as it can execute arbitrary code.osvssubprocess: For running shell commands,subprocess.run()is the modern, secure standard; avoidos.system().- Deep vs Shallow Copy: Know the
copymodule (copy.copy()vscopy.deepcopy()) for handling nested objects. datetimetimezone management: Always prefer aware datetimes (withpytzor Python 3.9'szoneinfo) over naive ones to avoid daylight saving bugs.