Code reuse == Orgasmic

By 'orgasmic', I mean 'literally orgasmic'. No joke. Check the following python method:

def _readData(self,data,pos,maxLen=0):
"""I read bytes from the given 'data' stream from position=pos
bytes until I get a byte stream of length maxLen. If the given
data-stream was not enough, I return a tuple saying so. I
should be called with more chunks of data till get the
required stream length.

When the given data-stream did not have enough bytes, I return
a (False, new-position, None).

Once I get all the required bytes, I return a (True,
new-position, byte-stream)

@param data: data stream to read data from

@param pos: position from which data should be read

@param maxLen: The maximum length of the bytestream that I
should gather

@return tuple (bool, int, str): I return a tuple containing
the following: (1) boolean saying whether I have collected the
byte-stream completely or whether I would need more data. (2)
int saying the new-position from where the next read should be
done. (3) byte-stream containing the requested bytestream
collection after I have finished reading the entire length."""

left = len(data) - pos
assert left > 0

if self._Buffer is None:
self._Buffer = []
assert maxLen != 0
self._bufferRequiredLen = maxLen

if self._bufferRequiredLen > left:
npos = pos + left
self._bufferRequiredLen -= left
return (False, npos, None)
npos = pos + self._bufferRequiredLen
b = ''.join(self._Buffer)
self._Buffer = None
self._bufferRequiredLen = 0
return (True, npos, b)

Its a fairly trivial piece of code. Does no jing-bang stuff. It just accumulates buffers in pieces till it gets a defined total-length, and returns the whole thing as one single buffer.

I wrote the above method for a project back in Oct. 2007. Since then, I have copied this method 'as is' umpteen number of times for every protocol implementation (be it on twisted, or plain python sockets) thereafter. And, every time I slurp-plonk it into a new project, I feel this happiness deep within. I cannot explain exactly why/what. It definitely is not rocket science. Its just something about the relief that I dont have to do that 'collect the buffers' thingy again. Maybe I am lazy to the extent that I am orgasmic, for not having to do something _yet_ again. :P


15 days, 11 runs and finally a full 5k

The title explained it all :) The stitch comes around every run. Guess I have to fix my breathing pattern.