From 50a941a968ede0804ed0461056cbb997d0294924 Mon Sep 17 00:00:00 2001 From: Jonathan Stewmon Date: Mon, 21 Mar 2016 09:50:18 -0500 Subject: [PATCH 1/5] implemented items, to_object and zip functions --- jmespath/functions.py | 15 +++++++++++++++ tests/compliance/functions.json | 31 ++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/jmespath/functions.py b/jmespath/functions.py index 11ab56a..774a24e 100644 --- a/jmespath/functions.py +++ b/jmespath/functions.py @@ -2,6 +2,9 @@ import json from jmespath import exceptions +from jmespath.compat import get_methods +from jmespath.compat import iteritems +from jmespath.compat import map from jmespath.compat import string_type as STRING_TYPE from jmespath.compat import get_methods @@ -185,6 +188,10 @@ def _func_to_array(self, arg): else: return [arg] + @signature({'types': ['array']}) + def _func_to_object(self, pairs): + return dict(pairs) + @signature({'types': []}) def _func_to_string(self, arg): if isinstance(arg, STRING_TYPE): @@ -281,6 +288,10 @@ def _func_sort(self, arg): def _func_sum(self, arg): return sum(arg) + @signature({'types': ['object']}) + def _func_items(self, arg): + return list(map(list, iteritems(arg))) + @signature({"types": ['object']}) def _func_keys(self, arg): # To be consistent with .values() @@ -346,6 +357,10 @@ def _func_max_by(self, array, expref): else: return None + @signature({'types': ['array'], 'variadic': True}) + def _func_zip(self, *arguments): + return list(map(list, zip(*arguments))) + def _create_key_func(self, expref, allowed_types, function_name): def keyfunc(x): result = expref.visit(expref.expression, x) diff --git a/tests/compliance/functions.json b/tests/compliance/functions.json index 7b55445..4332dfd 100644 --- a/tests/compliance/functions.json +++ b/tests/compliance/functions.json @@ -12,6 +12,7 @@ "empty_list": [], "empty_hash": {}, "objects": {"foo": "bar", "bar": "baz"}, + "pairs": [["a", "first"], ["b", "second"], ["c", "third"]], "null_key": null }, "cases": [ @@ -175,6 +176,18 @@ "expression": "floor(str)", "error": "invalid-type" }, + { + "expression": "items(objects)", + "result": [["foo", "bar"], ["bar", "baz"]] + }, + { + "expression": "items(empty_hash)", + "result": [] + }, + { + "expression": "items(numbers)", + "error": "invalid-type" + }, { "expression": "length('abc')", "result": 3 @@ -189,7 +202,7 @@ }, { "expression": "length(@)", - "result": 12 + "result": 13 }, { "expression": "length(strings[0])", @@ -479,6 +492,10 @@ "expression": "to_array(false)", "result": [false] }, + { + "expression": "to_object(pairs)", + "result": {"a": "first", "b": "second", "c": "third"} + }, { "expression": "to_string('foo')", "result": "foo" @@ -588,6 +605,18 @@ "comment": "function projection on single arg function", "expression": "array[].to_number(@)", "result": [-1, 3, 4, 5, 100] + }, + { + "expression": "zip(strings, numbers)", + "result": [["a", -1], ["b", 3], ["c", 4]] + }, + { + "expression": "zip(strings, numbers, decimals)", + "result": [["a", -1, 1.01], ["b", 3, 1.2], ["c", 4, -1.5]] + }, + { + "expression": "zip(str)", + "error": "invalid-type" } ] }, { From 24c1872578759e4677f628d4d97a28755291813a Mon Sep 17 00:00:00 2001 From: Jonathan Stewmon Date: Mon, 21 Mar 2016 12:46:29 -0500 Subject: [PATCH 2/5] sort items result to make result predictable --- tests/compliance/functions.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/compliance/functions.json b/tests/compliance/functions.json index 4332dfd..9f83c88 100644 --- a/tests/compliance/functions.json +++ b/tests/compliance/functions.json @@ -177,8 +177,8 @@ "error": "invalid-type" }, { - "expression": "items(objects)", - "result": [["foo", "bar"], ["bar", "baz"]] + "expression": "sort_by(items(objects), &[0])", + "result": [["bar", "baz"], ["foo", "bar"]] }, { "expression": "items(empty_hash)", From de56f529d6a0af654bd86db3f15e842168262991 Mon Sep 17 00:00:00 2001 From: Jonathan Stewmon Date: Thu, 15 Sep 2016 10:04:06 -0500 Subject: [PATCH 3/5] rename to_object to from_items --- jmespath/functions.py | 8 ++++---- tests/compliance/functions.json | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/jmespath/functions.py b/jmespath/functions.py index 774a24e..9c4e9ba 100644 --- a/jmespath/functions.py +++ b/jmespath/functions.py @@ -188,10 +188,6 @@ def _func_to_array(self, arg): else: return [arg] - @signature({'types': ['array']}) - def _func_to_object(self, pairs): - return dict(pairs) - @signature({'types': []}) def _func_to_string(self, arg): if isinstance(arg, STRING_TYPE): @@ -292,6 +288,10 @@ def _func_sum(self, arg): def _func_items(self, arg): return list(map(list, iteritems(arg))) + @signature({'types': ['array']}) + def _func_from_items(self, items): + return dict(items) + @signature({"types": ['object']}) def _func_keys(self, arg): # To be consistent with .values() diff --git a/tests/compliance/functions.json b/tests/compliance/functions.json index 9f83c88..0ddb433 100644 --- a/tests/compliance/functions.json +++ b/tests/compliance/functions.json @@ -12,7 +12,7 @@ "empty_list": [], "empty_hash": {}, "objects": {"foo": "bar", "bar": "baz"}, - "pairs": [["a", "first"], ["b", "second"], ["c", "third"]], + "items": [["a", "first"], ["b", "second"], ["c", "third"]], "null_key": null }, "cases": [ @@ -188,6 +188,10 @@ "expression": "items(numbers)", "error": "invalid-type" }, + { + "expression": "from_items(items)", + "result": {"a": "first", "b": "second", "c": "third"} + }, { "expression": "length('abc')", "result": 3 @@ -492,10 +496,6 @@ "expression": "to_array(false)", "result": [false] }, - { - "expression": "to_object(pairs)", - "result": {"a": "first", "b": "second", "c": "third"} - }, { "expression": "to_string('foo')", "result": "foo" From 8bc5ce38de555e5c31a23cb6e52bc3e89af860b8 Mon Sep 17 00:00:00 2001 From: springcomp Date: Fri, 28 Oct 2022 11:38:12 +0200 Subject: [PATCH 4/5] updated compat file. --- jmespath/compat.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jmespath/compat.py b/jmespath/compat.py index 50f8f27..6b12ccc 100644 --- a/jmespath/compat.py +++ b/jmespath/compat.py @@ -1,8 +1,8 @@ -import sys import inspect -from itertools import zip_longest +iteritems = dict.items +map = map text_type = str string_type = str From d8c61784730a72929f89083899cd018d0f9c6b01 Mon Sep 17 00:00:00 2001 From: Maxime Labelle Date: Tue, 15 Nov 2022 19:34:39 +0100 Subject: [PATCH 5/5] Removed duplicate imports. Co-authored-by: Jonathan Stewmon --- jmespath/functions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/jmespath/functions.py b/jmespath/functions.py index 9c4e9ba..d68ef2f 100644 --- a/jmespath/functions.py +++ b/jmespath/functions.py @@ -6,7 +6,6 @@ from jmespath.compat import iteritems from jmespath.compat import map from jmespath.compat import string_type as STRING_TYPE -from jmespath.compat import get_methods # python types -> jmespath types