Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented items, to_object and zip functions #105

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions jmespath/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def __new__(cls, name, this_bases, d):
if PY2:
text_type = unicode
string_type = basestring
iteritems = dict.iteritems
from itertools import izip_longest as zip_longest
from itertools import imap as map

def with_str_method(cls):
"""Class decorator that handles __str__ compat between py2 and py3."""
Expand Down Expand Up @@ -50,6 +52,8 @@ def get_methods(cls):
else:
text_type = str
string_type = str
iteritems = dict.items
map = map
from itertools import zip_longest

def with_str_method(cls):
Expand Down
17 changes: 16 additions & 1 deletion jmespath/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
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, with_metaclass
from jmespath.compat import with_metaclass


# python types -> jmespath types
Expand Down Expand Up @@ -280,6 +283,14 @@ 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': ['array']})
def _func_from_items(self, items):
return dict(items)

@signature({"types": ['object']})
def _func_keys(self, arg):
# To be consistent with .values()
Expand Down Expand Up @@ -339,6 +350,10 @@ def _func_max_by(self, array, expref):
'min_by')
return max(array, key=keyfunc)

@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)
Expand Down
31 changes: 30 additions & 1 deletion tests/compliance/functions.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"empty_list": [],
"empty_hash": {},
"objects": {"foo": "bar", "bar": "baz"},
"items": [["a", "first"], ["b", "second"], ["c", "third"]],
"null_key": null
},
"cases": [
Expand Down Expand Up @@ -175,6 +176,22 @@
"expression": "floor(str)",
"error": "invalid-type"
},
{
"expression": "sort_by(items(objects), &[0])",
"result": [["bar", "baz"], ["foo", "bar"]]
},
{
"expression": "items(empty_hash)",
"result": []
},
{
"expression": "items(numbers)",
"error": "invalid-type"
},
{
"expression": "from_items(items)",
"result": {"a": "first", "b": "second", "c": "third"}
},
{
"expression": "length('abc')",
"result": 3
Expand All @@ -189,7 +206,7 @@
},
{
"expression": "length(@)",
"result": 12
"result": 13
},
{
"expression": "length(strings[0])",
Expand Down Expand Up @@ -575,6 +592,18 @@
"expression": "not_null()",
"error": "invalid-arity"
},
{
"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"
},
{
"description": "function projection on single arg function",
"expression": "numbers[].to_string(@)",
Expand Down