def test_list_like(value, expected): assert list_like(value) == expected if expected: assert_list_like(value) else: with pytest.raises(TypeError, match='Must be a list like: '): assert_list_like(value)
def sumifs(sum_range, *args): # Excel reference: https://support.office.com/en-us/article/ # SUMIFS-function-C9E748F5-7EA7-455D-9406-611CEBCE642B # WARNING: # - The following is not currently implemented: # The sum_range argument does not have to be the same size and shape as # the range argument. The actual cells that are added are determined by # using the upper leftmost cell in the sum_range argument as the # beginning cell, and then including cells that correspond in size and # shape to the range argument. assert_list_like(sum_range) assert len(args) and len(args) % 2 == 0, \ 'Must have paired criteria and ranges' # count the number of times a particular cell matches the criteria index_counts = Counter( it.chain.from_iterable( find_corresponding_index(rng, criteria) for rng, criteria in zip(args[0::2], args[1::2]))) ifs_count = len(args) // 2 max_idx = len(sum_range) indices = tuple(idx for idx, cnt in index_counts.items() if cnt == ifs_count and idx < max_idx) return sum(_numerics(sum_range[idx] for idx in indices))
def sumifs(sum_range, *args): # Excel reference: https://support.office.com/en-us/article/ # SUMIFS-function-C9E748F5-7EA7-455D-9406-611CEBCE642B assert_list_like(sum_range) assert len(args) and len(args) % 2 == 0, \ 'Must have paired criteria and ranges' size = len(sum_range), len(sum_range[0]) for rng in args[0::2]: assert size == (len(rng), len(rng[0])), \ "Size mismatch criteria range, sum range" # count the number of times a particular cell matches the criteria index_counts = Counter( it.chain.from_iterable( find_corresponding_index(rng, criteria) for rng, criteria in zip(args[0::2], args[1::2]))) ifs_count = len(args) // 2 indices = tuple(idx for idx, cnt in index_counts.items() if cnt == ifs_count) return sum( _numerics((sum_range[r][c] for r, c in indices), keep_bools=True))
def sumifs(sum_range, *args): # Excel reference: https://support.office.com/en-us/article/ # SUMIFS-function-C9E748F5-7EA7-455D-9406-611CEBCE642B assert_list_like(sum_range) assert len(args) and len(args) % 2 == 0, \ 'Must have paired criteria and ranges' size = len(sum_range), len(sum_range[0]) for rng in args[0::2]: assert size == (len(rng), len(rng[0])), \ "Size mismatch criteria range, sum range" # count the number of times a particular cell matches the criteria index_counts = Counter(it.chain.from_iterable( find_corresponding_index(rng, criteria) for rng, criteria in zip(args[0::2], args[1::2]))) ifs_count = len(args) // 2 indices = tuple(idx for idx, cnt in index_counts.items() if cnt == ifs_count) return sum(_numerics( (sum_range[r][c] for r, c in indices), keep_bools=True))