From 7637c5db0f89b77482f12c483b9c8755f8256eee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gra=C3=B1a?= Date: Sun, 9 Jun 2019 17:44:44 -0300 Subject: [PATCH 1/4] Use shorter idiomatic python for README examples (#24) --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8fb3fe6..db28a24 100644 --- a/README.md +++ b/README.md @@ -18,9 +18,9 @@ The `jsonpath2.path.Path` class represents a JSONPath. >>> from jsonpath2.path import Path >>> p = Path.parse_str('$["hello"]') ->>> list(map(lambda match_data: match_data.current_value, p.match(d))) +>>> [match_data.current_value for match_data in p.match(d)] ['Hello, world!'] ->>> list(map(lambda match_data: match_data.node.tojsonpath(), p.match(d))) +>>> [match_data.node.tojsonpath() for match_data in p.match(d)] ['$["hello"]'] ``` @@ -130,9 +130,9 @@ The syntax for a function call is the name of the function followed by the argum >>> from jsonpath2.path import Path >>> p = Path.parse_str('$["hello"][length()]') ->>> list(map(lambda match_data: match_data.current_value, p.match(d))) +>>> [m.current_value for m in p.match(d)] [13] ->>> list(map(lambda match_data: match_data.node.tojsonpath(), p.match(d))) +>>> [m.node.tojsonpath() for m in p.match(d)] ['$["hello"][length()]'] ``` From 08fa312809e4c1c37d0e7aa873b02cf9d3f78a30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gra=C3=B1a?= Date: Wed, 5 Jun 2019 01:18:06 -0300 Subject: [PATCH 2/4] Add handy shortcut to match string path This change aims to avoid the boilerplate of applying a JsonPath to a document ```python >>> import jsonpath2 as jsonpath >>> doc = {'hello': 'Hello, world!'} >>> [m.current_value for m in jsonpath.match('$["hello"]', doc)] ``` It resembles the use of `re` module in `re.match(pattern, string)` vs `re.compile(pattern).match(string)` --- jsonpath2/__init__.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/jsonpath2/__init__.py b/jsonpath2/__init__.py index a3668d7..b47961d 100644 --- a/jsonpath2/__init__.py +++ b/jsonpath2/__init__.py @@ -1,3 +1,13 @@ #!/usr/bin/python # -*- coding: utf-8 -*- """The jsonpath2 module.""" + +from typing import Generator +from .path import Path +from .node import MatchData + + +def match(path_str: str, root_value: object) -> Generator[MatchData, None, None]: + """Match root value of the path.""" + path = Path.parse_str(path_str) + return path.match(root_value) From d15b377f3b75177ce56c8c527a66b988c5f3aea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gra=C3=B1a?= Date: Sun, 9 Jun 2019 17:20:37 -0300 Subject: [PATCH 3/4] Add test case for jsonpath2.match shortcut --- jsonpath2/test/shortcuts_test.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 jsonpath2/test/shortcuts_test.py diff --git a/jsonpath2/test/shortcuts_test.py b/jsonpath2/test/shortcuts_test.py new file mode 100644 index 0000000..8743fe8 --- /dev/null +++ b/jsonpath2/test/shortcuts_test.py @@ -0,0 +1,18 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +"""Test package level shortcuts.""" + +from unittest import TestCase +import jsonpath2 + + +class TestShortcuts(TestCase): + """Test package level shortcuts.""" + + def test_match(self): + """Test match shortcut works.""" + data = {'hello': 'Hello, World!'} + self.assertEqual( + [x.current_value for x in jsonpath2.match('$.hello', data)], + ['Hello, World!'] + ) From fcf4dd288bafa651c00e368152c84c79321cc234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gra=C3=B1a?= Date: Sun, 9 Jun 2019 17:52:21 -0300 Subject: [PATCH 4/4] Update README --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index db28a24..bb9a5d4 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,19 @@ This repository contains an implementation of [JSONPath](http://goessner.net/art ## API + +### `match` shortcut function + +The `jsonpath2.match` function is a shortcut to match a given JSON data +structure against a JSONPath string + +```python +>>> import jsonpath2 +>>> doc = {'hello': 'Hello, world!'} +>>> [x.current_value for x in jsonpath2.match('$.hello', doc)] +['Hello, world!'] +``` + ### `Path` class The `jsonpath2.path.Path` class represents a JSONPath.