Skip to content

Commit 49e084b

Browse files
committed
Merge tag 'JEP-13' into feature/vnext-preview
# Conflicts: # jmespath/functions.py
2 parents 255ecc3 + 8bc5ce3 commit 49e084b

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

jmespath/compat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import sys
21
import inspect
3-
from itertools import zip_longest
42

3+
iteritems = dict.items
54

5+
map = map
66
text_type = str
77
string_type = str
88

jmespath/functions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
from pydoc import resolve
55

66
from jmespath import exceptions
7+
from jmespath.compat import get_methods
8+
from jmespath.compat import iteritems
9+
from jmespath.compat import map
710
from jmespath.compat import string_type as STRING_TYPE
811
from jmespath.compat import get_methods
912

@@ -290,6 +293,14 @@ def _func_sort(self, arg):
290293
def _func_sum(self, arg):
291294
return sum(arg)
292295

296+
@signature({'types': ['object']})
297+
def _func_items(self, arg):
298+
return list(map(list, iteritems(arg)))
299+
300+
@signature({'types': ['array']})
301+
def _func_from_items(self, items):
302+
return dict(items)
303+
293304
@signature({"types": ['object']})
294305
def _func_keys(self, arg):
295306
# To be consistent with .values()
@@ -361,6 +372,10 @@ def _func_let(self, scope, expref, *args, **kwargs):
361372
kwargs.get('scopes').pushScope(scope)
362373
return expref.visit(expref.expression, expref.context, *args, **kwargs)
363374

375+
@signature({'types': ['array'], 'variadic': True})
376+
def _func_zip(self, *arguments):
377+
return list(map(list, zip(*arguments)))
378+
364379
def _create_key_func(self, expref, allowed_types, function_name):
365380
def keyfunc(x):
366381
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+
"items": [["a", "first"], ["b", "second"], ["c", "third"]],
1516
"null_key": null
1617
},
1718
"cases": [
@@ -175,6 +176,22 @@
175176
"expression": "floor(str)",
176177
"error": "invalid-type"
177178
},
179+
{
180+
"expression": "sort_by(items(objects), &[0])",
181+
"result": [["bar", "baz"], ["foo", "bar"]]
182+
},
183+
{
184+
"expression": "items(empty_hash)",
185+
"result": []
186+
},
187+
{
188+
"expression": "items(numbers)",
189+
"error": "invalid-type"
190+
},
191+
{
192+
"expression": "from_items(items)",
193+
"result": {"a": "first", "b": "second", "c": "third"}
194+
},
178195
{
179196
"expression": "length('abc')",
180197
"result": 3
@@ -189,7 +206,7 @@
189206
},
190207
{
191208
"expression": "length(@)",
192-
"result": 12
209+
"result": 13
193210
},
194211
{
195212
"expression": "length(strings[0])",
@@ -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)