Skip to content

Commit 42dedc7

Browse files
Jonathan StewmonMaxime LABELLE
authored andcommitted
implemented items, to_object and zip functions
1 parent bbe7300 commit 42dedc7

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

jmespath/functions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
import json
33

44
from jmespath import exceptions
5+
from jmespath.compat import get_methods
6+
from jmespath.compat import iteritems
7+
from jmespath.compat import map
58
from jmespath.compat import string_type as STRING_TYPE
69
from jmespath.compat import get_methods
710

@@ -185,6 +188,10 @@ def _func_to_array(self, arg):
185188
else:
186189
return [arg]
187190

191+
@signature({'types': ['array']})
192+
def _func_to_object(self, pairs):
193+
return dict(pairs)
194+
188195
@signature({'types': []})
189196
def _func_to_string(self, arg):
190197
if isinstance(arg, STRING_TYPE):
@@ -281,6 +288,10 @@ def _func_sort(self, arg):
281288
def _func_sum(self, arg):
282289
return sum(arg)
283290

291+
@signature({'types': ['object']})
292+
def _func_items(self, arg):
293+
return list(map(list, iteritems(arg)))
294+
284295
@signature({"types": ['object']})
285296
def _func_keys(self, arg):
286297
# To be consistent with .values()
@@ -346,6 +357,10 @@ def _func_max_by(self, array, expref):
346357
else:
347358
return None
348359

360+
@signature({'types': ['array'], 'variadic': True})
361+
def _func_zip(self, *arguments):
362+
return list(map(list, zip(*arguments)))
363+
349364
def _create_key_func(self, expref, allowed_types, function_name):
350365
def keyfunc(x):
351366
result = expref.visit(expref.expression, x)

tests/compliance/functions.json

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"empty_list": [],
1313
"empty_hash": {},
1414
"objects": {"foo": "bar", "bar": "baz"},
15+
"pairs": [["a", "first"], ["b", "second"], ["c", "third"]],
1516
"null_key": null
1617
},
1718
"cases": [
@@ -175,6 +176,18 @@
175176
"expression": "floor(str)",
176177
"error": "invalid-type"
177178
},
179+
{
180+
"expression": "items(objects)",
181+
"result": [["foo", "bar"], ["bar", "baz"]]
182+
},
183+
{
184+
"expression": "items(empty_hash)",
185+
"result": []
186+
},
187+
{
188+
"expression": "items(numbers)",
189+
"error": "invalid-type"
190+
},
178191
{
179192
"expression": "length('abc')",
180193
"result": 3
@@ -189,7 +202,7 @@
189202
},
190203
{
191204
"expression": "length(@)",
192-
"result": 12
205+
"result": 13
193206
},
194207
{
195208
"expression": "length(strings[0])",
@@ -479,6 +492,10 @@
479492
"expression": "to_array(false)",
480493
"result": [false]
481494
},
495+
{
496+
"expression": "to_object(pairs)",
497+
"result": {"a": "first", "b": "second", "c": "third"}
498+
},
482499
{
483500
"expression": "to_string('foo')",
484501
"result": "foo"
@@ -588,6 +605,18 @@
588605
"comment": "function projection on single arg function",
589606
"expression": "array[].to_number(@)",
590607
"result": [-1, 3, 4, 5, 100]
608+
},
609+
{
610+
"expression": "zip(strings, numbers)",
611+
"result": [["a", -1], ["b", 3], ["c", 4]]
612+
},
613+
{
614+
"expression": "zip(strings, numbers, decimals)",
615+
"result": [["a", -1, 1.01], ["b", 3, 1.2], ["c", 4, -1.5]]
616+
},
617+
{
618+
"expression": "zip(str)",
619+
"error": "invalid-type"
591620
}
592621
]
593622
}, {

0 commit comments

Comments
 (0)