Skip to content

Commit 974fd67

Browse files
authored
Merge pull request #2 from jmespath-community/jep/objects
JEP-13 Object Manipulation Functions
2 parents fcc3513 + d8c6178 commit 974fd67

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
import json
33

44
from jmespath import exceptions
5-
from jmespath.compat import string_type as STRING_TYPE
65
from jmespath.compat import get_methods
6+
from jmespath.compat import iteritems
7+
from jmespath.compat import map
8+
from jmespath.compat import string_type as STRING_TYPE
79

810

911
# python types -> jmespath types
@@ -302,6 +304,14 @@ def _func_sort(self, arg):
302304
def _func_sum(self, arg):
303305
return sum(arg)
304306

307+
@signature({'types': ['object']})
308+
def _func_items(self, arg):
309+
return list(map(list, iteritems(arg)))
310+
311+
@signature({'types': ['array']})
312+
def _func_from_items(self, items):
313+
return dict(items)
314+
305315
@signature({"types": ['object']})
306316
def _func_keys(self, arg):
307317
# To be consistent with .values()
@@ -519,6 +529,10 @@ def _func_max_by(self, array, expref):
519529
else:
520530
return None
521531

532+
@signature({'types': ['array'], 'variadic': True})
533+
def _func_zip(self, *arguments):
534+
return list(map(list, zip(*arguments)))
535+
522536
def _create_key_func(self, expref, allowed_types, function_name):
523537
def keyfunc(x):
524538
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)