-
Notifications
You must be signed in to change notification settings - Fork 180
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
Add support for 'arraylike' objects as JSON arrays #317
Open
calgray
wants to merge
4
commits into
jmespath:develop
Choose a base branch
from
calgray:arraylike
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
===== | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is deprecated and should be
I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's only available in Python 3.9+ but the project still declares support for 3.7 and 3.8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's available in older versions. only subscripting like
Sequence[int]
(which is not used) is only available in newer versions.