OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/ |
| 2 # |
| 3 # Permission is hereby granted, free of charge, to any person obtaining a |
| 4 # copy of this software and associated documentation files (the |
| 5 # "Software"), to deal in the Software without restriction, including |
| 6 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 7 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 # persons to whom the Software is furnished to do so, subject to the fol- |
| 9 # lowing conditions: |
| 10 # |
| 11 # The above copyright notice and this permission notice shall be included |
| 12 # in all copies or substantial portions of the Software. |
| 13 # |
| 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 16 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 17 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 18 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 # IN THE SOFTWARE. |
| 21 |
| 22 def query_lister(domain, query='', max_items=None, attr_names=None): |
| 23 more_results = True |
| 24 num_results = 0 |
| 25 next_token = None |
| 26 while more_results: |
| 27 rs = domain.connection.query_with_attributes(domain, query, attr_names, |
| 28 next_token=next_token) |
| 29 for item in rs: |
| 30 if max_items: |
| 31 if num_results == max_items: |
| 32 raise StopIteration |
| 33 yield item |
| 34 num_results += 1 |
| 35 next_token = rs.next_token |
| 36 more_results = next_token != None |
| 37 |
| 38 class QueryResultSet: |
| 39 |
| 40 def __init__(self, domain=None, query='', max_items=None, attr_names=None): |
| 41 self.max_items = max_items |
| 42 self.domain = domain |
| 43 self.query = query |
| 44 self.attr_names = attr_names |
| 45 |
| 46 def __iter__(self): |
| 47 return query_lister(self.domain, self.query, self.max_items, self.attr_n
ames) |
| 48 |
| 49 def select_lister(domain, query='', max_items=None): |
| 50 more_results = True |
| 51 num_results = 0 |
| 52 next_token = None |
| 53 while more_results: |
| 54 rs = domain.connection.select(domain, query, next_token=next_token) |
| 55 for item in rs: |
| 56 if max_items: |
| 57 if num_results == max_items: |
| 58 raise StopIteration |
| 59 yield item |
| 60 num_results += 1 |
| 61 next_token = rs.next_token |
| 62 more_results = next_token != None |
| 63 |
| 64 class SelectResultSet(object): |
| 65 |
| 66 def __init__(self, domain=None, query='', max_items=None, |
| 67 next_token=None, consistent_read=False): |
| 68 self.domain = domain |
| 69 self.query = query |
| 70 self.consistent_read = consistent_read |
| 71 self.max_items = max_items |
| 72 self.next_token = next_token |
| 73 |
| 74 def __iter__(self): |
| 75 more_results = True |
| 76 num_results = 0 |
| 77 while more_results: |
| 78 rs = self.domain.connection.select(self.domain, self.query, |
| 79 next_token=self.next_token, |
| 80 consistent_read=self.consistent_r
ead) |
| 81 for item in rs: |
| 82 if self.max_items and num_results >= self.max_items: |
| 83 raise StopIteration |
| 84 yield item |
| 85 num_results += 1 |
| 86 self.next_token = rs.next_token |
| 87 if self.max_items and num_results >= self.max_items: |
| 88 raise StopIteration |
| 89 more_results = self.next_token != None |
| 90 |
| 91 def next(self): |
| 92 return self.__iter__().next() |
OLD | NEW |