Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Avoiding Inappropriate Scenarios

00:00 So I’ve mentioned a scenario when you may not want to use io.StringIO, but let’s take a step back and look at a couple of other scenarios when it’s not the best choice.

00:09 First, when you’re working with binary data, io StringIO is designed for text data, not binary data. When you’re working with large files, because it reads the whole data into memory, and so you will run into memory problems if you’re working with really large files.

00:26 If you need to simulate complex file operations. Again, this is kind of the advantage of using io.StringIO, that it does abstract away from this complex file operations, but if you need to test those like actual IO operations or working with metadata, then of course the io.StringIO object won’t help you there.

00:46 It also doesn’t replicate any file system related errors, so if you need to test for those, then you’ll need to use something else.

00:54 Lastly, if the function input, like mentioned previously, isn’t the file object, but it is a path, then also you can’t work with io.StringIO.

01:05 On the next slide, let’s look at a couple of alternative solutions for each of these scenarios before then moving on to coding a little bit more. What you can use instead of io.StringIO in this mention scenarios, if you’re working with binary data, you can use the binary in-memory equivalent io.BytesIO.

01:24 Again, this has some of the same limitations as io.StringIO, but if you just want to simulate a binary file in memory, then this is what you can use.

01:32 If you’re working with large files, then you can use io.BufferedReader and io.BufferedWriter from the same io module.

01:40 Or you can use a memory-mapped file from the mmap module. Or you could also go for a third-party library like pandas that is optimized for chunking it and reading it.

01:49 Pick data streams buffered.

01:52 If you need to replicate complex file operations, then you can use the tempfile module where you can create temporary files, or you can also use unittest.mock.

02:02 And if the path of the file is the function argument, then you’ll also need to mock the file object. And let’s take a look at how to use unittest.mock to do that in the next lesson.

Become a Member to join the conversation.