Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_iteratee(case, arg, expected):
getter = _.iteratee(case)
assert _.map_(arg, getter) == expected
def iterdifference(array, other, comparator=None, iteratee=None):
"""Yield different values in `array` as compared to `other` using
`comparator` to determine if they are different.
"""
if not array or not other: # pragma: no cover
return
if comparator is None:
comparator = pyd.is_equal
iteratee = pyd.iteratee(iteratee)
def is_different(item, seen):
is_diff = True
if item not in seen:
for value in other:
if comparator(iteratee(item), iteratee(value)):
is_diff = False
break
if is_diff:
seen.append(item)
return is_diff
seen = []
not_seen = []
def iterintersection(array, other, comparator=None, iteratee=None):
"""Yield intersecting values between `array` and `other` using `comparator`
to determine if they intersect.
"""
if not array or not other: # pragma: no cover
return
if comparator is None:
comparator = pyd.is_equal
iteratee = pyd.iteratee(iteratee)
# NOTE: Maintain ordering of yielded values based on `array` ordering.
seen = []
for item in array:
cmp_item = iteratee(item)
if cmp_item in seen:
continue
seen.append(cmp_item)
seen_others = []
for value in other:
cmp_value = iteratee(value)
if cmp_value in seen_others:
Returns:
dict: Results of grouping by `iteratee`.
Example:
>>> results = group_by([{'a': 1, 'b': 2}, {'a': 3, 'b': 4}], 'a')
>>> assert results == {1: [{'a': 1, 'b': 2}], 3: [{'a': 3, 'b': 4}]}
>>> results = group_by([{'a': 1, 'b': 2}, {'a': 3, 'b': 4}], {'a': 1})
>>> assert results == {False: [{'a': 3, 'b': 4}],\
True: [{'a': 1, 'b': 2}]}
.. versionadded:: 1.0.0
"""
ret = {}
cbk = pyd.iteratee(iteratee)
for value in collection:
key = cbk(value)
ret.setdefault(key, [])
ret[key].append(value)
return ret
Returns:
int: Returns the index at which `value` should be inserted into
`array`.
Example:
>>> array = [{'x': 4}, {'x': 5}]
>>> sorted_last_index_by(array, {'x': 4}, lambda o: o['x'])
1
>>> sorted_last_index_by(array, {'x': 4}, 'x')
1
"""
if iteratee:
# Generate array of sorted keys computed using iteratee.
iteratee = pyd.iteratee(iteratee)
array = sorted(iteratee(item) for item in array)
value = iteratee(value)
return bisect_right(array, value)
Args:
array (list): List to process.
iteratee (mixed, optional): Iteratee applied per iteration.
Returns:
list: List of duplicates.
Example:
>>> duplicates([0, 1, 3, 2, 3, 1])
[3, 1]
.. versionadded:: 3.0.0
"""
if iteratee:
cbk = pyd.iteratee(iteratee)
computed = [cbk(item) for item in array]
else:
computed = array
# NOTE: Using array[i] instead of item since iteratee could have modified
# returned item values.
lst = uniq(array[i] for i, _ in iterduplicates(computed))
return lst
Example:
>>> max_by([1.0, 1.5, 1.8], math.floor)
1.0
>>> max_by([{'a': 1}, {'a': 2}, {'a': 3}], 'a')
{'a': 3}
>>> max_by([], default=-1)
-1
.. versionadded:: 4.0.0
"""
if isinstance(collection, dict):
collection = collection.values()
return max(iterator_with_default(collection, default),
key=pyd.iteratee(iteratee))
int: Returns the index at which `value` should be inserted into
`array`.
Example:
>>> array = [{'x': 4}, {'x': 5}]
>>> sorted_index_by(array, {'x': 4}, lambda o: o['x'])
0
>>> sorted_index_by(array, {'x': 4}, 'x')
0
.. versionadded:: 4.0.0
"""
if iteratee:
# Generate array of sorted keys computed using iteratee.
iteratee = pyd.iteratee(iteratee)
array = sorted(iteratee(item) for item in array)
value = iteratee(value)
return bisect_left(array, value)