Skip to content

Commit ade5bf3

Browse files
author
Jonathan Stewmon
committed
implemented items, to_object and zip functions
1 parent ef36ae7 commit ade5bf3

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
@@ -184,6 +187,10 @@ def _func_to_array(self, arg):
184187
else:
185188
return [arg]
186189

190+
@signature({'types': ['array']})
191+
def _func_to_object(self, pairs):
192+
return dict(pairs)
193+
187194
@signature({'types': []})
188195
def _func_to_string(self, arg):
189196
if isinstance(arg, STRING_TYPE):
@@ -280,6 +287,10 @@ def _func_sort(self, arg):
280287
def _func_sum(self, arg):
281288
return sum(arg)
282289

290+
@signature({'types': ['object']})
291+
def _func_items(self, arg):
292+
return list(map(list, iteritems(arg)))
293+
283294
@signature({"types": ['object']})
284295
def _func_keys(self, arg):
285296
# To be consistent with .values()
@@ -339,6 +350,10 @@ def _func_max_by(self, array, expref):
339350
'min_by')
340351
return max(array, key=keyfunc)
341352

353+
@signature({'types': ['array'], 'variadic': True})
354+
def _func_zip(self, *arguments):
355+
return list(map(list, zip(*arguments)))
356+
342357
def _create_key_func(self, expref, allowed_types, function_name):
343358
def keyfunc(x):
344359
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"
@@ -575,6 +592,18 @@
575592
"expression": "not_null()",
576593
"error": "invalid-arity"
577594
},
595+
{
596+
"expression": "zip(strings, numbers)",
597+
"result": [["a", -1], ["b", 3], ["c", 4]]
598+
},
599+
{
600+
"expression": "zip(strings, numbers, decimals)",
601+
"result": [["a", -1, 1.01], ["b", 3, 1.2], ["c", 4, -1.5]]
602+
},
603+
{
604+
"expression": "zip(str)",
605+
"error": "invalid-type"
606+
},
578607
{
579608
"description": "function projection on single arg function",
580609
"expression": "numbers[].to_string(@)",

0 commit comments

Comments
 (0)