Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
.. code::
yaql> [1, 2, 3, 1].distinct()
[1, 2, 3]
yaql> [{'a'=> 1}, {'b'=> 2}, {'a'=> 1}].distinct()
[{"a": 1}, {"b": 2}]
yaql> [['a', 1], ['b', 2], ['c', 1], ['a', 3]].distinct($[1])
[['a', 1], ['b', 2], ['a', 3]]
"""
distinct_values = set()
for t in collection:
key = t if key_selector is None else key_selector(t)
if key not in distinct_values:
distinct_values.add(key)
utils.limit_memory_usage(engine, (1, distinct_values))
yield t
Returns string repeated count times.
:signature: left * right
:arg left: left operand
:argType left: string
:arg right: right operator, how many times repeat input string
:argType right: integer
:returnType: string
.. code::
yaql> "ab" * 2
"abab"
"""
utils.limit_memory_usage(engine, (-right + 1, u''), (right, left))
return left * right
predicate = lambda fd, ctx: fd.is_method and function_filter(fd, ctx)
all_overloads = context.collect_functions(
name, predicate, use_convention=use_convention)
if not all_overloads:
if receiver is utils.NO_VALUE:
raise exceptions.NoFunctionRegisteredException(name)
else:
raise exceptions.NoMethodRegisteredException(name, receiver)
else:
delegate = choose_overload(
name, all_overloads, engine, receiver, data_context, args, kwargs)
try:
result = delegate()
utils.limit_memory_usage(engine, (1, result))
return result
except StopIteration as e:
six.reraise(
exceptions.WrappedException,
exceptions.WrappedException(e),
sys.exc_info()[2])
def dict_(engine, *args):
""":yaql:dict
Returns dictionary of provided keyword values.
:signature: dict([args])
:arg [args]: arguments to create a dictionary
:argType [args]: mappings
:returnType: dictionary
.. code::
yaql> dict(a => 1, b => 2)
{ "a": 1, "b": 2}
"""
utils.limit_memory_usage(engine, *((1, arg) for arg in args))
return utils.FrozenDict((t.source, t.destination) for t in args)
:argType aggregator: lambda
:returnType: list
.. code::
yaql> [["a", 1], ["b", 2], ["c", 1], ["d", 2]].groupBy($[1], $[0])
[[1, ["a", "c"]], [2, ["b", "d"]]]
yaql> [["a", 1], ["b", 2], ["c", 1]].groupBy($[1], $[0], $.sum())
[[1, "ac"], [2, "b"]]
"""
groups = {}
new_aggregator = GroupAggregator(aggregator, allow_aggregator_fallback)
for t in collection:
value = t if value_selector is None else value_selector(t)
groups.setdefault(key_selector(t), []).append(value)
utils.limit_memory_usage(engine, (1, groups))
return select(six.iteritems(groups), new_aggregator)
Returns dict with replacements keys set to replacements values.
:signature: dict.set(replacements)
:receiverArg dict: input dictionary
:argType dict: dictionary
:arg replacements: mapping with keys and values to be set on input dict
:argType key: dictionary
:returnType: dictionary
.. code::
yaql> {"a" => 1, "b" => 2}.set({"b" => 3, "c" => 4})
{"a": 1, "c": 4, "b": 3}
"""
utils.limit_memory_usage(engine, (1, d), (1, replacements))
return utils.FrozenDict(itertools.chain(
six.iteritems(d), six.iteritems(replacements)))
def build_list(engine, *args):
""":yaql:list
Returns list of provided args.
:signature: list([args])
:arg [args]: arguments to create a list
:argType [args]: any types
:returnType: list
.. code::
yaql> list(1, "", 2)
[1, "", 2]
"""
utils.limit_memory_usage(engine, *((1, t) for t in args))
return tuple(args)
:arg items: list of pairs [key, value] for building dictionary
:argType items: list
:returnType: dictionary
.. code::
yaql> dict([["a", 2], ["b", 4]])
{"a": 2, "b": 4}
"""
result = {}
for t in items:
it = iter(t)
key = next(it)
value = next(it)
result[key] = value
utils.limit_memory_usage(engine, (1, result))
return utils.FrozenDict(result)
:returnType: list
.. code::
yaql> generate(0, $ < 10, $ + 2)
[0, 2, 4, 6, 8]
yaql> generate(1, $ < 10, $ + 2, $ * 1000)
[1000, 3000, 5000, 7000, 9000]
"""
past_items = None if not decycle else set()
while predicate(initial):
if past_items is not None:
if initial in past_items:
break
past_items.add(initial)
utils.limit_memory_usage(engine, (1, past_items))
if selector is None:
yield initial
else:
yield selector(initial)
initial = producer(initial)