Writing Example Calls Using StringIO
In order to follow the function design recipe, we need to write example calls. Writing these calls using real files would involve creating test files for each of the situations you want to demonstrate. This is fragile, because it means that you can’t just give the program to someone—you need to remember to include the test files in case they want to try your function, and it’s also not optimal because anyone trying to understand the function needs to open the input and output files.
Python provides a class, StringIO, in module io, that can be used as a mock open file. That means that you can read from it using the regular file-reading techniques as if it were a real file. StringIO objects can be used anywhere ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access