Skip to content

Commit 6c033a5

Browse files
author
Jonathan Stewmon
committed
implemented items, to_object and zip functions
1 parent 4eaa65b commit 6c033a5

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

jmespath/compat.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ def __new__(cls, name, this_bases, d):
1515
if PY2:
1616
text_type = unicode
1717
string_type = basestring
18+
iteritems = dict.iteritems
1819
from itertools import izip_longest as zip_longest
20+
from itertools import imap as map
1921

2022
def with_str_method(cls):
2123
"""Class decorator that handles __str__ compat between py2 and py3."""
@@ -50,6 +52,8 @@ def get_methods(cls):
5052
else:
5153
text_type = str
5254
string_type = str
55+
iteritems = dict.items
56+
map = map
5357
from itertools import zip_longest
5458

5559
def with_str_method(cls):

jmespath/functions.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
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
6-
from jmespath.compat import get_methods, with_metaclass
9+
from jmespath.compat import with_metaclass
710

811

912
# python types -> jmespath types
@@ -181,6 +184,10 @@ def _func_to_array(self, arg):
181184
else:
182185
return [arg]
183186

187+
@signature({'types': ['array']})
188+
def _func_to_object(self, pairs):
189+
return dict(pairs)
190+
184191
@signature({'types': []})
185192
def _func_to_string(self, arg):
186193
if isinstance(arg, STRING_TYPE):
@@ -277,6 +284,10 @@ def _func_sort(self, arg):
277284
def _func_sum(self, arg):
278285
return sum(arg)
279286

287+
@signature({'types': ['object']})
288+
def _func_items(self, arg):
289+
return list(map(list, iteritems(arg)))
290+
280291
@signature({"types": ['object']})
281292
def _func_keys(self, arg):
282293
# To be consistent with .values()
@@ -336,6 +347,10 @@ def _func_max_by(self, array, expref):
336347
'min_by')
337348
return max(array, key=keyfunc)
338349

350+
@signature({'types': ['array'], 'variadic': True})
351+
def _func_zip(self, *arguments):
352+
return list(map(list, zip(*arguments)))
353+
339354
def _create_key_func(self, expref, allowed_types, function_name):
340355
def keyfunc(x):
341356
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": [
@@ -171,6 +172,18 @@
171172
"expression": "floor(str)",
172173
"error": "invalid-type"
173174
},
175+
{
176+
"expression": "items(objects)",
177+
"result": [["foo", "bar"], ["bar", "baz"]]
178+
},
179+
{
180+
"expression": "items(empty_hash)",
181+
"result": []
182+
},
183+
{
184+
"expression": "items(numbers)",
185+
"error": "invalid-type"
186+
},
174187
{
175188
"expression": "length('abc')",
176189
"result": 3
@@ -185,7 +198,7 @@
185198
},
186199
{
187200
"expression": "length(@)",
188-
"result": 12
201+
"result": 13
189202
},
190203
{
191204
"expression": "length(strings[0])",
@@ -475,6 +488,10 @@
475488
"expression": "to_array(false)",
476489
"result": [false]
477490
},
491+
{
492+
"expression": "to_object(pairs)",
493+
"result": {"a": "first", "b": "second", "c": "third"}
494+
},
478495
{
479496
"expression": "to_string('foo')",
480497
"result": "foo"
@@ -571,6 +588,18 @@
571588
"expression": "not_null()",
572589
"error": "invalid-arity"
573590
},
591+
{
592+
"expression": "zip(strings, numbers)",
593+
"result": [["a", -1], ["b", 3], ["c", 4]]
594+
},
595+
{
596+
"expression": "zip(strings, numbers, decimals)",
597+
"result": [["a", -1, 1.01], ["b", 3, 1.2], ["c", 4, -1.5]]
598+
},
599+
{
600+
"expression": "zip(str)",
601+
"error": "invalid-type"
602+
},
574603
{
575604
"description": "function projection on single arg function",
576605
"expression": "numbers[].to_string(@)",

0 commit comments

Comments
 (0)