In need of a binge-and-purge idiom
Jeremy Fincher
tweedgeezer at hotmail.com
Mon Mar 24 02:04:19 EST 2003
More information about the Python-list mailing list
Mon Mar 24 02:04:19 EST 2003
- Previous message (by thread): In need of a binge-and-purge idiom
- Next message (by thread): In need of a binge-and-purge idiom
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
mlh at furu.idi.ntnu.no (Magnus Lie Hetland) wrote in message news:<slrnb7ruo9.4ip.mlh at furu.idi.ntnu.no>... > I've noticed that I use the following in several contexts: > > chunk = [] > for element in iterable: > if isSeparator(element) and chunk: > doSomething(chunk) > chunk = [] > if chunk: > doSomething(chunk) > chunk = [] > > If the iterable above is a file, isSeparator(element) is simply > defined as not element.strip() and doSomething(chunk) is > yield(''.join(chunk)) you have a paragraph splitter. I've been using > the same approach for slightly more complicated parsing recently. Maybe something like this can work? def itersplit(iterable, isSeparator): acc = [] for element in iterable: if isSeparator(element): yield acc acc = [] else: acc.append(element) yield acc Then your paragraph splitter might look like this: def paragraphSplitter(file): for L in itersplit(file, lambda s: not s.split()): yield ''.join(L) Jeremy
- Previous message (by thread): In need of a binge-and-purge idiom
- Next message (by thread): In need of a binge-and-purge idiom
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list