11

I'm used to C++, and I build my data handling classes/functions to handle stream objects instead of files. I'd like to know how I might modify the following code, so that it can handle a stream of binary data in memory, rather than a file handle.

def get_count(self):
    curr = self.file.tell()
    self.file.seek(0, 0)
    count, = struct.unpack('I', self.file.read(c_uint32_size))
    self.file.seek(curr, 0)
    return count

In this case, the code assumes self.file is a file, opened like so:

file = open('somefile.data, 'r+b')

How might I use the same code, yet instead do something like this:

file = get_binary_data()

Where get_binary_data() returns a string of binary data. Although the code doesn't show it, I also need to write to the stream (I didn't think it was worth posting the code for that).

Also, if possible, I'd like the new code to handle files as well.

Nick Bolton
  • 38,276
  • 70
  • 174
  • 242

5 Answers5

17

You can use an instance of StringIO.StringIO (or cStringIO.StringIO, faster) to give a file-like interface to in-memory data.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 1
    _the above mentioned link does not work because ("StringIO and cStringIO" are now gone in Python 3.0. They exist only in 2.0. io has replaced them in 3.0")_ Sorry for this comment, but it seems a tradition for this answer... – tampe125 Jan 12 '17 at 13:56
  • @tampe125 that is not correct. StringIO still exists in Python 3, it's simply been moved to the io module. – Kevin S Feb 26 '19 at 18:17
6

Take a look at Python's StringIO module, docs here, which could be pretty much what you're after.

af.
  • 1,648
  • 1
  • 11
  • 11
  • the above mentioned link does not work because ("StringIO and cStringIO" are now gone in Python 3.0. They exist only in 2.0. io has replaced them in 3.0"). – Bhavana Jul 22 '16 at 06:23
5

Have a look at 'StringIO' (Read and write strings as files)

ChristopheD
  • 112,638
  • 29
  • 165
  • 179
  • the above mentioned link does not work because ("StringIO and cStringIO" are now gone in Python 3.0. They exist only in 2.0. io has replaced them in 3.0"). – Bhavana Jul 22 '16 at 06:23
5

Use StringIO.

jldupont
  • 93,734
  • 56
  • 203
  • 318
  • the above mentioned link does not work because ("StringIO and cStringIO" are now gone in Python 3.0. They exist only in 2.0. io has replaced them in 3.0"). – Bhavana Jul 22 '16 at 06:23
1

I like the timing of the answer. (except mine)

We can see response time in milliseconds ?

of-course StringIO

dsgdfg
  • 1,492
  • 11
  • 18