0

Is there a means by which I can add processing to an io.TextIOBase subclass and produce another io.TextIOBase subclass? For example, if I want to convert from a string of Octal characters to Binary characters, I could do this with a generator function, but I'm interested in being able to read an arbitrary number of characters from the resultant stream, so this approach doesn't really work:

oct_hex = {
        "0": "000",
        "1": "001",
        "2": "010",
        "3": "011",
        "4": "100",
        "5": "101",
        "6": "110",
        "7": "111",
}

def _oct_to_bin(messaage: io.StringIO) -> Generator[str, None, None]:
        while char := messaage.read(1):
            yield oct_hex[char]
        raise StopIteration

What I'd like to achieve is something with a signature like, although the exact type of stream doesn't really matter:

def _oct_to_bin(message: io.StringIO) -> io.StringIO

I realise that I can use .getValue() to grab the entire stream in a single string. Process that outside of io.StringIO and then create a new io.StringIO from the resultant string. I'd ideally like an approach that is a bit more stream friendly and loads the necessary data on demand and could potentially be adapted to other classes in the io module.

Richard Vodden
  • 318
  • 3
  • 12

0 Answers0