how to refer to partial list, slice is too slow?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Fri May 11 04:34:20 EDT 2007


In <1178862971.447773.21760 at o5g2000hsb.googlegroups.com>,
人言落日是天涯,望极天涯不见家 wrote:

> I make a sample here for the more clearly explanation
> 
> s = " ..... - this is a large string data - ......."
> 
> def parser1(data)
>      # do some parser
>      ...
>      # pass the remainder to next parser
>      parser2(data[100:])
> 
> def parser2(data)
>      # do some parser
>      ...
>      # pass the remainder to next parser
>      parser3(data[100:])
> 
> def parser3(data)
>      # do some parser
>      ...
>      # pass the remainder to next parser
>      parser4(data[100:])
> 
> ...

Do you need the remainder within the parser functions?  If not you could
split the data into chunks of 100 bytes and pass an iterator from function
to function.  Untested:

def iter_chunks(data, chunksize):
    offset = chunksize
    while True:
        result = data[offset:offset + chunksize]
        if not result:
            break
        yield result


def parser1(data):
    chunk = data.next()
    # ...
    parser2(data)


def parser2(data):
    chunk = data.next()
    # ...
    parser3(data)

# ...

def main():
    # Read or create data.
    # ...
    parser1(iter_chunks(data, 100))

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list