def update(self, data): if self.multiple: for (old, new) in zip(self.result, data): old['common_friends'].extend(new['common_friends']) old['common_count'] = new['common_count'] else: assert isinstance(data, list) self.result.extend(data)
def test_reqn_friends_getmutual(): method = api.friends.getMutual args = dict(source_uid=1, target_uid=21) fetch_and_compare(method, args, n=35, batch_size=10) args = dict(source_uid=1, target_uids=[6, 21]) rb, rn = fetch(method, args, n=35, batch_size=10) for eb, en in zip(rb, rn): assert eb['common_friends'] == en['common_friends']
def reqn(partial_call, n=None, batch_size=None, **api_method_args): method = partial_call.method result = result_classes[method](api_method_args) # Get two batch size iterators for offset calculation and for constructing # the (offset,size)-schedule. sizes_offset, sizes_shedule = tee( result.batch_size_iter if (batch_size is None) else repeat(batch_size), 2) # Infinite generator of offsets: # (0, steps_1, steps_1 + steps_2, ..., \sum{i=1}{m} steps_i) offsets_all = accumulate(chain([0], sizes_offset)) # If `n' is set, cut offset sequence once it gets just below `n`. offsets = offsets_all if n is None \ else takewhile(lambda x: x < n, offsets_all) # (offset,size)-schedule schedule = list(zip(offsets, sizes_shedule)) if n is not None: # If `n' is set, change the last step to request exactly `n' items. # E.g. if the shedule ends with # (... (off_m, step_m)) # and (off_m + step_m) > n, the sequence becomes # (... (off_m, n - off_m)) schedule = [(off, size if (off + size) <= n else n - off) for off, size in schedule] # Sanity test for total number of items. assert n == sum(s[1] for s in schedule) for offset, size in schedule: logger.debug("Requesting batch: %s for offset=%d, count=%d" % (method, offset, size)) data = partial_call(**dict(api_method_args, offset=offset, count=size)) n_items = result.count_new_items(data) logger.debug('Fetched %d new items' % n_items) if n_items: result.update(data) else: break return result.result