convert string to input stream

From: Titus Barik (titus_at_barik.net)
Date: 08/31/04


Date: Tue, 31 Aug 2004 12:32:20 -0400

Hi,

I've used Google quite a bit in an attempt to find an answer to this
question, but have found no leads, possibly because I'm not sure I'm
wording my question properly. So I thought I'd try here.

Basically I have a function that accepts an input file stream as an
argument:

        handle = open(SOME_FILE)
        my_function(handle)

But occasionally, I end up with a string ("Hello world") that I need to
convert to a stream in order to pass it into this function. Perhaps
something like:

        fake_handle = convert_to_handle("Hello world!")

so that it appears to this function to be a file handle. I've seen
functions in other languages that will take strings and convert them to
streams via IO Memory Buffers and such, but can't seem to find anything in
Python; or at least I don't know where to look.

One solution that I didn't like was to do the following:

        1. Open a temporary file.
        2. Write the string to that file.
        3. Read it back.

Any suggestions are appreciated.

Regards,

-- 
Titus Barik (titus@barik.net)
http://www.barik.net


Relevant Pages

  • Re: Strange problem with sharpziplib, xml and utf-8 encoding
    ... String s = System.Text.UTF8Encoding.UTF8.GetString; ... Which leads to the same strange behaviour. ... just pass the stream into XmlDocument.Load and let it deal with it. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Question about design, defmacro, macrolet, and &environment
    ... "Expects to find the literal string on the stream." ... (defun send (string &optional stream) ... (declaim (inline make-adjustable-string)) ...
    (comp.lang.lisp)
  • Re: Strange problem when not in debugger
    ... private string huidigWeb; ... int buffLength = 2048; ... // Opens a file stream to read the file to be uploaded ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ReplacerStream
    ... string, do a replace on that string and create a stream again to be ... If those are problems, and you are looking just for a single string, it seems to me that you could just read the stream one character at a time, checking to see if it matches the current character in your search string. ...
    (microsoft.public.dotnet.framework)
  • Re: Strange problem when not in debugger
    ... private string huidigWeb; ... int buffLength = 2048; ... // Opens a file stream to read the file to be uploaded ...
    (microsoft.public.dotnet.languages.csharp)