Checkpoint: Stdlib Power Tools
A checkpoint is a chance to consolidate: here you'll review eight standard-library modules and combine several of them in one realistic build challenge before moving on.
Learn Checkpoint: Stdlib Power Tools in our free Python course — an interactive lesson with runnable examples, a practice exercise and a quick reference.
Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
You've covered statistics , operator , struct , textwrap , timeit , warnings , inspect , and weakref . Let's tie them together.
Each of these modules solves a specific problem with zero external dependencies. Here's the one-line reminder of what to reach for, and when:
Here's the mission, in three steps that each use a different module:
Start from the runnable starter below. It already builds the binary log and unpacks the records — your job is to finish the statistics and formatting. Then compare with the full solution.
The full version computes mean, median, and standard deviation, then formats a multi-stat report wrapped to width 30 with a left bar:
Why it works: calcsize(RECORD) tells the loop how many bytes each record occupies, so slicing raw[i:i+size] hands struct.unpack exactly one record at a time. statistics turns the list of floats into summary numbers, and textwrap.fill with matching initial_indent / subsequent_indent draws the left bar on every line.
1. You need the middle value of a sorted dataset, unaffected by outliers. Which statistics function do you call?
statistics.median . It returns the central value, so a single extreme outlier barely moves it — unlike mean . For example median([3, 1, 2, 4]) is 2.5 .
2. What's the cleanest way to sort a list of (name, score) tuples by score?
sorted(rows, key=operator.itemgetter(1)) . itemgetter(1) is faster and clearer than lambda r: r[1] , and you can pass several indexes to sort by multiple fields.
3. Given RECORD = ">Hf" , how many bytes does one record take, and how do you find out in code?
6 bytes — struct.calcsize(">Hf") returns 6 (2 for the H short + 4 for the f float). The > prefix forces big-endian with no padding, so the size is predictable.
4. Why use textwrap.shorten instead of slicing a string with [:n] ?
shorten truncates on a word boundary and adds a placeholder like " [...]" , so previews read cleanly — e.g. shorten("one two three four five", width=15) gives "one two [...]" . Slicing can cut a word in half.
5. When benchmarking with timeit , why report a relative result instead of absolute nanoseconds?
Absolute times depend on the CPU, OS, and current load, so they vary between runs and machines. A relative claim — "approach A beats approach B" or "A is about 3x faster" — stays true everywhere. Use repeat and take the min to reduce noise.
6. What does a weakref.ref let you do that a normal variable cannot, and why does a cache want that?
A weak reference points to an object without keeping it alive, so the object can be garbage-collected once all strong references are gone. A WeakValueDictionary cache uses this to auto-drop entries nothing else uses, preventing memory leaks.
Checkpoint cleared — you wield the stdlib power tools!
You reviewed all eight modules, combined struct , statistics , and textwrap in a real parse-analyze-present pipeline, and tested your recall with the quiz. These tools ship with every Python install — and now you know exactly when to reach for each one.
🚀 Up next: Final Project Ideas — put everything together in a project of your own.
Practice quiz
You need the middle value of a dataset, unaffected by outliers. Which statistics function do you call?
- statistics.mean
- statistics.mode
- statistics.median
- statistics.stdev
Answer: statistics.median. median returns the central value, so a single extreme outlier barely moves it — unlike mean.
What is the cleanest way to sort a list of (name, score) tuples by score?
- sorted(rows, key=operator.itemgetter(1))
- sorted(rows, key=lambda r: r)
- sorted(rows, key=operator.attrgetter("score"))
- sorted(rows, reverse=True)
Answer: sorted(rows, key=operator.itemgetter(1)). operator.itemgetter(1) pulls the score from each tuple; it is faster and clearer than a lambda and can take several indexes.
Given RECORD = ">Hf", how many bytes does one record take and how do you find out in code?
- 4 bytes — struct.calcsize(RECORD)
- 8 bytes — len(RECORD)
- 2 bytes — struct.size(RECORD)
- 6 bytes — struct.calcsize(RECORD)
Answer: 6 bytes — struct.calcsize(RECORD). struct.calcsize(">Hf") returns 6: 2 bytes for the H short plus 4 for the f float. The > forces big-endian with no padding.
Why use textwrap.shorten instead of slicing a string with [:n]?
- It is faster
shorten truncates at a word boundary and appends a placeholder, so previews read cleanly; slicing can cut a word in half.
When benchmarking with timeit, why report a relative result instead of absolute nanoseconds?
- Absolute times depend on CPU, OS, and load; a relative claim stays true across machines
- Nanoseconds are too small to measure
- timeit cannot return absolute times
- Relative results are required by PEP 8
Answer: Absolute times depend on CPU, OS, and load; a relative claim stays true across machines. Absolute times vary with hardware and load. A relative claim like "A is about 3x faster than B" holds everywhere; use repeat and take the min to cut noise.
What does a weakref.ref let you do that a normal variable cannot?
- Make an object immutable
- Copy an object deeply
- Point to an object without keeping it alive, so it can be garbage-collected
- Lock an object against changes
Answer: Point to an object without keeping it alive, so it can be garbage-collected. A weak reference does not keep its target alive, so a WeakValueDictionary cache can auto-drop entries nothing else uses, preventing memory leaks.
Which module would you reach for to format a long string into a neatly wrapped block?
- struct
- textwrap
- inspect
- operator
Answer: textwrap. textwrap.fill, indent, and shorten handle wrapping and formatting of text blocks.
Which inspect function returns the call signature of a function at runtime?
- inspect.getsource
- inspect.isfunction
- inspect.stack
- inspect.signature
Answer: inspect.signature. inspect.signature returns a Signature object describing the parameters of a callable.
In the build challenge, which module unpacks the packed binary sensor records?
- statistics
- struct
- textwrap
- pickle
Answer: struct. struct.unpack with the ">Hf" format reads each fixed-size record back into a Python id and float.
Which module is the right choice for emitting a non-fatal alert that does not stop the program?
- logging only
- sys.exit
- warnings
- assert
Answer: warnings. warnings.warn emits a non-fatal alert; catch_warnings lets you capture or filter them without crashing the program.