I have a function that's intended to take a binary file format and convert it to a readable text format, e.g.:
def textualize(binary_stuff): # magic to turn binary stuff into text return text_stuff
There are a few different types I could accept as input or produce as output, and I'm unsure what to use. Here are some options and corresponding objections I can think of:
- Take a bytes object as input and return a string.
- Problematic if, say, the input is originating from a huge file that now has to be read into memory.
- Take a file-like object as input, read it, and return a string.
- Relies on the caller to open the file in the right mode.
- The asymmetry of this disturbs me for reasons I can't quite put a finger on.
- Take two file-like objects; read from one and write to the other instead of returning anything.
- Again relies on the caller to open the files in the right mode.
- Makes the most common cases (named file to named file, or bytes to string) more unwieldly than they need to be.
- Take two filenames and handle opening stuff myself.
- What if the caller wants to convert data that isn't in a named file?
- Accept multiple possible input types.
- Possibly complicated to program.
- Still leaves the question of what to return.
Is there an established Right Thing to do for conversions like this? Are there additional tradeoffs I'm missing?