-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ improved jmespath transformation
* fixed the transformation at root level * added unique and unique_count custom functions
- Loading branch information
Showing
5 changed files
with
98 additions
and
17 deletions.
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
""" | ||
We can define jmespath custom functions here | ||
See: https://jmespath.org/tutorial.html#custom-functions | ||
from biothings.utils.jmespath import options as jmp_options | ||
jmespath.search("unique(`foo`)", {}, options=jmp_options) | ||
or | ||
jmespath.compile("unique(`foo`)").search({}, options=jmp_options) | ||
""" | ||
import jmespath | ||
from jmespath import functions | ||
|
||
|
||
class CustomFunctions(functions.Functions): | ||
"""Create a subclass of functions.Functions. | ||
The function.Functions base class has logic | ||
that introspects all of its methods and automatically | ||
registers your custom functions in its function table. | ||
""" | ||
|
||
@functions.signature({"types": ["array"]}) | ||
def _func_unique(self, arr): | ||
"""return a list of unique values in an array""" | ||
return sorted(set(arr)) | ||
|
||
@functions.signature({"types": ["array"]}) | ||
def _func_unique_count(self, arr): | ||
"""return the number of unique values in an array""" | ||
return len(set(arr)) | ||
|
||
|
||
# pass this jmespath_options to search to use custom functions | ||
options = jmespath.Options(custom_functions=CustomFunctions()) |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import jmespath | ||
import pytest | ||
from jmespath.exceptions import UnknownFunctionError | ||
|
||
from biothings.utils.jmespath import options as jmp_options | ||
|
||
|
||
def test_customfunction_unique(): | ||
doc = { | ||
"foo": ["a", "b", "c", "e", "e", "c", "d", "a"], | ||
} | ||
# without passing jmp_options, it should raise UnknownFunctionError | ||
# this test should tell us if we accidentally override a build-in function | ||
with pytest.raises(UnknownFunctionError): | ||
jmespath.search("foo|unique(@)", doc) | ||
|
||
assert jmespath.search("foo|unique(@)", doc, options=jmp_options) == ["a", "b", "c", "d", "e"] | ||
|
||
|
||
def test_customfunction_unique_count(): | ||
doc = ["a", "b", "c", "e", "e", "c", "d", "a"] | ||
# without passing jmp_options, it should raise UnknownFunctionError | ||
# this test should tell us if we accidentally override a build-in function | ||
with pytest.raises(UnknownFunctionError): | ||
jmespath.search("unique_count(@)", doc) | ||
|
||
assert jmespath.compile("unique_count(@)").search(doc, options=jmp_options) == 5 |
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