Skip to content

Commit 2d74af2

Browse files
markborkumdmlb2000
authored andcommitted
Generalize subscripts using collections.abc module (#23)
Closes #20
1 parent 29d53e8 commit 2d74af2

File tree

5 files changed

+17
-12
lines changed

5 files changed

+17
-12
lines changed

jsonpath2/subscripts/arrayindex.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
33
"""Array Index subscript of the parse tree."""
4+
from collections.abc import Sequence
45
import json
56
from typing import Generator
67
from jsonpath2.node import MatchData
@@ -23,7 +24,7 @@ def __jsonpath__(self) -> Generator[str, None, None]:
2324

2425
def match(self, root_value: object, current_value: object) -> Generator[MatchData, None, None]:
2526
"""Match the root value against the current value."""
26-
if isinstance(current_value, list):
27+
if isinstance(current_value, Sequence) and not isinstance(current_value, str):
2728
if self.index < 0:
2829
new_index = self.index + len(current_value)
2930

jsonpath2/subscripts/arrayslice.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
33
"""Array slicing module."""
4+
from collections.abc import Sequence
45
import itertools
56
import json
67
from typing import Generator
@@ -32,7 +33,7 @@ def __jsonpath__(self) -> Generator[str, None, None]:
3233

3334
def match(self, root_value: object, current_value: object) -> Generator[MatchData, None, None]:
3435
"""Match an array slice between values."""
35-
if isinstance(current_value, list):
36+
if isinstance(current_value, Sequence) and not isinstance(current_value, str):
3637
start = None if (self.start is None) else (
3738
self.start + (len(current_value) if (self.start < 0) else 0))
3839

jsonpath2/subscripts/callable.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
33
"""Callable subscript."""
4+
from collections.abc import Mapping, Sequence
45
import itertools
56
import json
67
from typing import Generator, Tuple, Union
@@ -80,13 +81,13 @@ class EntriesCallableSubscript(CallableSubscript):
8081

8182
def __call__(self, root_value: object, current_value: object) -> Generator[MatchData, None, None]:
8283
"""Perform entries() call."""
83-
if isinstance(current_value, dict):
84+
if isinstance(current_value, Mapping):
8485
if not self.args:
8586
value = list(map(list, current_value.items()))
8687

8788
yield MatchData(SubscriptNode(TerminalNode(), [self]),
8889
root_value, value)
89-
elif isinstance(current_value, list):
90+
elif isinstance(current_value, Sequence) and not isinstance(current_value, str):
9091
if not self.args:
9192
value = list(map(list, enumerate(current_value)))
9293

@@ -101,13 +102,13 @@ class KeysCallableSubscript(CallableSubscript):
101102

102103
def __call__(self, root_value: object, current_value: object) -> Generator[MatchData, None, None]:
103104
"""Perform keys() call."""
104-
if isinstance(current_value, dict):
105+
if isinstance(current_value, Mapping):
105106
if not self.args:
106107
value = list(current_value.keys())
107108

108109
yield MatchData(SubscriptNode(TerminalNode(), [self]),
109110
root_value, value)
110-
elif isinstance(current_value, list):
111+
elif isinstance(current_value, Sequence) and not isinstance(current_value, str):
111112
if not self.args:
112113
value = list(range(len(current_value)))
113114

@@ -122,7 +123,7 @@ class LengthCallableSubscript(CallableSubscript):
122123

123124
def __call__(self, root_value: object, current_value: object) -> Generator[MatchData, None, None]:
124125
"""Perform length() call."""
125-
if isinstance(current_value, list):
126+
if isinstance(current_value, Sequence) and not isinstance(current_value, str):
126127
if not self.args:
127128
value = len(current_value)
128129

@@ -166,13 +167,13 @@ class ValuesCallableSubscript(CallableSubscript):
166167

167168
def __call__(self, root_value: object, current_value: object) -> Generator[MatchData, None, None]:
168169
"""Perform values() call."""
169-
if isinstance(current_value, dict):
170+
if isinstance(current_value, Mapping):
170171
if not self.args:
171172
value = list(current_value.values())
172173

173174
yield MatchData(SubscriptNode(TerminalNode(), [self]),
174175
root_value, value)
175-
elif isinstance(current_value, list):
176+
elif isinstance(current_value, Sequence) and not isinstance(current_value, str):
176177
if not self.args:
177178
value = current_value
178179

jsonpath2/subscripts/objectindex.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
33
"""Object index subscript module."""
4+
from collections.abc import Mapping
45
import json
56
from typing import Generator
67
from jsonpath2.node import MatchData
@@ -23,6 +24,6 @@ def __jsonpath__(self) -> Generator[str, None, None]:
2324

2425
def match(self, root_value: object, current_value: object) -> Generator[MatchData, None, None]:
2526
"""Match the current value against the root value."""
26-
if isinstance(current_value, dict) and (self.index in current_value):
27+
if isinstance(current_value, Mapping) and (self.index in current_value):
2728
return [MatchData(SubscriptNode(TerminalNode(), [self]), root_value, current_value[self.index])]
2829
return []

jsonpath2/subscripts/wildcard.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
33
"""Wild cart subscript module."""
4+
from collections.abc import Mapping, Sequence
45
import itertools
56
from typing import Generator
67
from jsonpath2.node import MatchData
@@ -18,12 +19,12 @@ def __jsonpath__(self) -> Generator[str, None, None]:
1819

1920
def match(self, root_value: object, current_value: object) -> Generator[MatchData, None, None]:
2021
"""Match the root value against the current value."""
21-
if isinstance(current_value, dict):
22+
if isinstance(current_value, Mapping):
2223
return itertools.chain(*map(
2324
lambda index: ObjectIndexSubscript(
2425
index).match(root_value, current_value),
2526
current_value.keys()))
26-
if isinstance(current_value, list):
27+
if isinstance(current_value, Sequence) and not isinstance(current_value, str):
2728
return itertools.chain(*map(
2829
lambda index: ArrayIndexSubscript(
2930
index).match(root_value, current_value),

0 commit comments

Comments
 (0)