Annotation of /trunk/indra/lib/python/indra/util/iterators.py
Parent Directory
|
Revision Log
Revision 100 -
(view)
(download)
(as text)
Original Path: linden_release/linden/indra/lib/python/indra/util/iterators.py
| 1 : | mjm | 100 | """\ |
| 2 : | @file iterators.py | ||
| 3 : | @brief Useful general-purpose iterators. | ||
| 4 : | |||
| 5 : | $LicenseInfo:firstyear=2008&license=mit$ | ||
| 6 : | |||
| 7 : | Copyright (c) 2008-2009, Linden Research, Inc. | ||
| 8 : | |||
| 9 : | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 10 : | of this software and associated documentation files (the "Software"), to deal | ||
| 11 : | in the Software without restriction, including without limitation the rights | ||
| 12 : | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 13 : | copies of the Software, and to permit persons to whom the Software is | ||
| 14 : | furnished to do so, subject to the following conditions: | ||
| 15 : | |||
| 16 : | The above copyright notice and this permission notice shall be included in | ||
| 17 : | all copies or substantial portions of the Software. | ||
| 18 : | |||
| 19 : | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 20 : | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 21 : | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 22 : | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 23 : | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 24 : | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| 25 : | THE SOFTWARE. | ||
| 26 : | $/LicenseInfo$ | ||
| 27 : | """ | ||
| 28 : | |||
| 29 : | from __future__ import nested_scopes | ||
| 30 : | |||
| 31 : | def iter_chunks(rows, aggregate_size=100): | ||
| 32 : | """ | ||
| 33 : | Given an iterable set of items (@p rows), produces lists of up to @p | ||
| 34 : | aggregate_size items at a time, for example: | ||
| 35 : | |||
| 36 : | iter_chunks([1,2,3,4,5,6,7,8,9,10], 3) | ||
| 37 : | |||
| 38 : | Values for @p aggregate_size < 1 will raise ValueError. | ||
| 39 : | |||
| 40 : | Will return a generator that produces, in the following order: | ||
| 41 : | - [1, 2, 3] | ||
| 42 : | - [4, 5, 6] | ||
| 43 : | - [7, 8, 9] | ||
| 44 : | - [10] | ||
| 45 : | """ | ||
| 46 : | if aggregate_size < 1: | ||
| 47 : | raise ValueError() | ||
| 48 : | |||
| 49 : | def iter_chunks_inner(): | ||
| 50 : | row_iter = iter(rows) | ||
| 51 : | done = False | ||
| 52 : | agg = [] | ||
| 53 : | while not done: | ||
| 54 : | try: | ||
| 55 : | row = row_iter.next() | ||
| 56 : | agg.append(row) | ||
| 57 : | except StopIteration: | ||
| 58 : | done = True | ||
| 59 : | if agg and (len(agg) >= aggregate_size or done): | ||
| 60 : | yield agg | ||
| 61 : | agg = [] | ||
| 62 : | |||
| 63 : | return iter_chunks_inner() |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

