-
Notifications
You must be signed in to change notification settings - Fork 203
Add support for 'arraylike' objects as JSON arrays #317
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
base: develop
Are you sure you want to change the base?
Changes from 4 commits
3fcb98b
cfd5cb7
21c37c4
5359e75
e748e4d
6ec6639
71a8edb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| Unreleased | ||
| ========== | ||
|
|
||
| * Added support for Arraylike python objects as json arrays. | ||
|
|
||
| 1.0.1 | ||
| ===== | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import math | ||
| import json | ||
| from typing import Sequence | ||
|
|
||
| from jmespath import exceptions | ||
| from jmespath.compat import string_type as STRING_TYPE | ||
|
|
@@ -35,6 +36,14 @@ | |
| } | ||
|
|
||
|
|
||
| def is_array(arg): | ||
| return hasattr(arg, "__array__") and arg.shape != () | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth linking to https://numpy.org/doc/stable/user/basics.interoperability.html#the-array-method in a comment? Also, is it guaranteed that an object with
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The numpy docs describes that
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I see what you mean. Updated to: |
||
|
|
||
|
|
||
| def is_arraylike(arg): | ||
| return (isinstance(arg, Sequence) and not isinstance(arg, (str, bytes))) or is_array(arg) | ||
|
|
||
|
|
||
| def signature(*arguments): | ||
| def _record_signature(func): | ||
| func.signature = arguments | ||
|
|
@@ -180,7 +189,7 @@ def _func_not_null(self, *arguments): | |
|
|
||
| @signature({'types': []}) | ||
| def _func_to_array(self, arg): | ||
| if isinstance(arg, list): | ||
| if is_arraylike(arg): | ||
| return arg | ||
| else: | ||
| return [arg] | ||
|
|
@@ -297,7 +306,7 @@ def _func_type(self, arg): | |
| return "string" | ||
| elif isinstance(arg, bool): | ||
| return "boolean" | ||
| elif isinstance(arg, list): | ||
| elif is_arraylike(arg): | ||
| return "array" | ||
| elif isinstance(arg, dict): | ||
| return "object" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,11 @@ | ||
| wheel==0.38.1 | ||
| parameterized==0.9.0 | ||
| pytest==6.2.5 | ||
| pytest-cov==3.0.0 | ||
| hypothesis==3.1.0 ; python_version < '3.8' | ||
| hypothesis==5.5.4 ; python_version == '3.8' | ||
| hypothesis==5.35.4 ; python_version == '3.9' | ||
| astropy>=3.1 | ||
| dask>=2.0.0 | ||
| numpy>=1.15.0 | ||
| xarray>=0.18.0 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| import astropy.units as u | ||
| import dask.array as da | ||
| import numpy as np | ||
| import xarray as xr | ||
| from parameterized import parameterized, parameterized_class | ||
|
|
||
| import jmespath | ||
| import jmespath.functions | ||
| from tests import unittest | ||
|
|
||
|
|
||
| @parameterized_class(("name", "data"), [ | ||
| ("list", { | ||
| "value": { | ||
| "data": [[1,2,3],[4,5,6],[7,8,9]] | ||
| }, | ||
| "same": { | ||
| "data": [[1,2,3],[4,5,6],[7,8,9]] | ||
| }, | ||
| "other": { | ||
| "data": [[2,2,3],[4,5,6],[7,8,9]] | ||
| } | ||
| }), | ||
| ("tuple", { | ||
| "value": { | ||
| "data": ((1,2,3),(4,5,6),(7,8,9)) | ||
| }, | ||
| "same": { | ||
| "data": ([1,2,3],[4,5,6],[7,8,9]) | ||
| }, | ||
| "other": { | ||
| "data": [[2,2,3],[4,5,6],[7,8,9]] | ||
| } | ||
| }), | ||
| ("numpy", { | ||
| "value": { | ||
| "data": np.array([[1,2,3],[4,5,6],[7,8,9]]) | ||
| }, | ||
| "same": { | ||
| "data": (np.array([1,2,3]),np.array([4,5,6]),np.array([7,8,9])) | ||
| }, | ||
| "other": { | ||
| "data": np.array([[2,2,3],[4,5,6],[7,8,9]]) | ||
| } | ||
| }), | ||
| ("dask", { | ||
| "value": { | ||
| "data": da.from_array([[1,2,3],[4,5,6],[7,8,9]]) | ||
| }, | ||
| "same": { | ||
| "data": (da.from_array([1,2,3]),da.from_array([4,5,6]),da.from_array([7,8,9])) | ||
| }, | ||
| "other": { | ||
| "data": da.from_array([[2,2,3],[4,5,6],[7,8,9]]) | ||
| } | ||
| }), | ||
| ("xarray", { | ||
| "value": { | ||
| "data": xr.DataArray([[1,2,3],[4,5,6],[7,8,9]]) | ||
| }, | ||
| "same": { | ||
| "data": (xr.DataArray([1,2,3]),xr.DataArray([4,5,6]),xr.DataArray([7,8,9])) | ||
| }, | ||
| "other": { | ||
| "data": xr.DataArray([[2,2,3],[4,5,6],[7,8,9]]) | ||
| } | ||
| }), | ||
| ("astropy", { | ||
| "value": { | ||
| "data": u.Quantity([[1,2,3],[4,5,6],[7,8,9]]) | ||
| }, | ||
| "same": { | ||
| "data": (u.Quantity([1,2,3]),u.Quantity([4,5,6]),u.Quantity([7,8,9])) | ||
| }, | ||
| "other": { | ||
| "data": u.Quantity([[2,2,3],[4,5,6],[7,8,9]]) | ||
| } | ||
| }), | ||
| ]) | ||
| class TestArrayNumeric(unittest.TestCase): | ||
| @parameterized.expand([ | ||
| ["self", "@", lambda data: data], | ||
| ["get", "value.data", lambda data: data["value"]["data"]], | ||
| ["slice_horizontal", "value.data[1][:]", lambda data: np.array(data["value"]["data"])[1,:]], | ||
| ["slice_horizontal2", "value.data[:3:2][:]", lambda data: np.array(data["value"]["data"])[:3:2,:]], | ||
| ["slice_vertical", "value.data[:][1]", lambda data: np.array(data["value"]["data"])[:,1]], | ||
| ["slice_vertical2", "value.data[:][:3:2]", lambda data: np.array(data["value"]["data"])[:,:3:2]], | ||
| ["flatten", "value.data[]", lambda data: np.array(data["value"]["data"]).flatten()], | ||
| ["compare_self", "value.data == value.data", lambda _: True], | ||
| ["compare_same", "value.data == same.data", lambda _: True], | ||
| ["compare_other", "value.data == other.data", lambda _: False], | ||
| ["compare_literal_scalar", "value.data[0][0] == `1`", lambda _: True], | ||
| ["compare_literal_slice", "value.data[1][:] == `[4, 5, 6]`", lambda _: True], | ||
| ["compare_literal", "value.data == `[[1,2,3],[4,5,6],[7,8,9]]`", lambda _: True], | ||
| ["compare_flattened", "value.data[] == `[1,2,3,4,5,6,7,8,9]`", lambda _: True], | ||
| ]) | ||
| def test_search(self, test_name, query, expected): | ||
| result = jmespath.search(query, self.data) | ||
| np.testing.assert_array_equal(result, expected(self.data), test_name) | ||
|
|
||
|
|
||
| @parameterized_class(("name", "data"), [ | ||
| ("numpy", { | ||
| "value": { | ||
| "data": np.array([["test", "messages"],["in", "numpy"]]) | ||
| }, | ||
| "same": { | ||
| "data": np.array([["test", "messages"],["in", "numpy"]]) | ||
| }, | ||
| "other": { | ||
| "data": np.array([["test", "messages"],["other", "numpy"]]) | ||
| } | ||
| }) | ||
| ]) | ||
| class TestArrayStr(unittest.TestCase): | ||
| @parameterized.expand([ | ||
| ["self", "@", lambda data: data], | ||
| ["get", "value.data", lambda data: data["value"]["data"]], | ||
| ["slice_horizontal", "value.data[1][:]", lambda data: data["value"]["data"][1,:]], | ||
| ["slice_vertical", "value.data[:][1]", lambda data: data["value"]["data"][:,1]], | ||
| ["flatten", "value.data[]", lambda data: data["value"]["data"].flatten()], | ||
| ["compare_self", "value.data == value.data", lambda _: True], | ||
| ["compare_same", "value.data == same.data", lambda _: True], | ||
| ["compare_other", "value.data == other.data", lambda _: False], | ||
| ["compare_literal_scalar", "value.data[0][0] == 'test'", lambda _: True], | ||
| ["compare_literal_slice", "value.data[1][:] == ['in', 'numpy']", lambda _: True], | ||
| ["compare_literal", "value.data == [['test', 'messages'],['in', 'numpy']]", lambda _: True], | ||
| ["compare_flattened", "value.data[] == ['test', 'messages', 'in', 'numpy']", lambda _: True], | ||
| ]) | ||
| def test_search(self, name, query, expected): | ||
| result = jmespath.search(query, self.data) | ||
| np.testing.assert_array_equal(result, expected(self.data), name) |
Uh oh!
There was an error while loading. Please reload this page.