Skip to content

Commit a329904

Browse files
author
Gerit Wagner
committed
refactor (private methods)
1 parent bfe0e8f commit a329904

File tree

5 files changed

+40
-123
lines changed

5 files changed

+40
-123
lines changed

docs/source/dev_docs/_autosummary/search_query.utils.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ search\_query.utils
1313

1414
.. autosummary::
1515

16-
format_query_string_pos
1716
format_query_string_positions

search_query/constants.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ def is_operator(self) -> bool:
6363
"""Check if token is an operator"""
6464
return self.type in (TokenTypes.LOGIC_OPERATOR, TokenTypes.PROXIMITY_OPERATOR)
6565

66+
def get_operator_type(self) -> str:
67+
"""Get operator type"""
68+
if self.value.upper() in {"&", "AND"}:
69+
return Operators.AND
70+
if self.value.upper() in {"|", "OR"}:
71+
return Operators.OR
72+
if self.value.upper() == "NOT":
73+
return Operators.NOT
74+
raise ValueError()
75+
6676

6777
@dataclass
6878
class ListToken:

search_query/pubmed/parser.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -96,42 +96,32 @@ def tokenize(self) -> None:
9696

9797
self.combine_subsequent_terms()
9898

99-
def get_operator_type(self, operator: str) -> str:
100-
"""Get operator type"""
101-
if operator.upper() in {"&", "AND"}:
102-
return Operators.AND
103-
if operator.upper() in {"|", "OR"}:
104-
return Operators.OR
105-
if operator.upper() == "NOT":
106-
return Operators.NOT
107-
raise ValueError()
108-
10999
def parse_query_tree(self, tokens: list) -> Query:
110100
"""Parse a query from a list of tokens"""
111101

112-
if self.is_compound_query(tokens):
102+
if self._is_compound_query(tokens):
113103
query = self._parse_compound_query(tokens)
114104

115-
elif self.is_nested_query(tokens):
105+
elif self._is_nested_query(tokens):
116106
query = self._parse_nested_query(tokens)
117107

118-
elif self.is_term_query(tokens):
108+
elif self._is_term_query(tokens):
119109
query = self._parse_search_term(tokens)
120110

121111
else:
122112
raise ValueError()
123113

124114
return query
125115

126-
def is_term_query(self, tokens: list) -> bool:
116+
def _is_term_query(self, tokens: list) -> bool:
127117
"""Check if the query is a search term"""
128118
return tokens[0].type == TokenTypes.SEARCH_TERM and len(tokens) <= 2
129119

130-
def is_compound_query(self, tokens: list) -> bool:
120+
def _is_compound_query(self, tokens: list) -> bool:
131121
"""Check if the query is a compound query"""
132122
return bool(self._get_operator_indices(tokens))
133123

134-
def is_nested_query(self, tokens: list) -> bool:
124+
def _is_nested_query(self, tokens: list) -> bool:
135125
"""Check if the query is nested in parentheses"""
136126
return (
137127
tokens[0].type == TokenTypes.PARENTHESIS_OPEN
@@ -157,7 +147,7 @@ def _get_operator_indices(self, tokens: list) -> list:
157147
i = i - 1
158148

159149
if i == 0 and token.type == TokenTypes.LOGIC_OPERATOR:
160-
operator = self.get_operator_type(token.value)
150+
operator = token.get_operator_type()
161151
if not first_operator_found:
162152
first_operator = operator
163153
first_operator_found = True
@@ -252,7 +242,7 @@ def _parse_search_term(self, tokens: list) -> Query:
252242

253243
# return field_values_list
254244

255-
def get_query_leaves(self, query: Query) -> list:
245+
def _get_query_leaves(self, query: Query) -> list:
256246
"""Retrieve all leaf nodes from a query,
257247
representing search terms and fields,
258248
and return them as a list"""
@@ -261,7 +251,7 @@ def get_query_leaves(self, query: Query) -> list:
261251

262252
leaves = []
263253
for child in query.children:
264-
leaves += self.get_query_leaves(child)
254+
leaves += self._get_query_leaves(child)
265255
return leaves
266256

267257
def parse(self) -> Query:
@@ -336,7 +326,7 @@ def get_operator_node_tokens(self, token_nr: int) -> list:
336326
)
337327
return operator_node_tokens
338328

339-
def parse_operator_node(self, token_nr: int) -> Query:
329+
def _parse_operator_node(self, token_nr: int) -> Query:
340330
"""Parse an operator node."""
341331

342332
operator_node_tokens = self.get_operator_node_tokens(token_nr)
@@ -383,7 +373,7 @@ def parse(self) -> Query:
383373
self.linter.messages[token_nr] = parser.linter.messages
384374

385375
if query_element["type"] == ListTokenTypes.OPERATOR_NODE:
386-
query_element["query"] = self.parse_operator_node(
376+
query_element["query"] = self._parse_operator_node(
387377
token_nr
388378
# query_element["node_content"]
389379
)

search_query/wos/parser.py

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def tokenize(self) -> None:
102102
self.tokens.append(Token(value=value, type=token_type, position=position))
103103

104104
self.combine_subsequent_terms()
105-
self.add_unbalanced_quotes_to_adjacent_term()
105+
self._add_unbalanced_quotes_to_adjacent_term()
106106

107107
# Parse a query tree from tokens recursively
108108
# pylint: disable=too-many-branches
@@ -141,7 +141,7 @@ def parse_query_tree(
141141
# Handle closing parentheses
142142
elif token.type == TokenTypes.PARENTHESIS_CLOSED:
143143
return (
144-
self.handle_closing_parenthesis(
144+
self._handle_closing_parenthesis(
145145
children=children,
146146
current_operator=current_operator,
147147
search_field=search_field,
@@ -151,16 +151,12 @@ def parse_query_tree(
151151

152152
# Handle operators
153153
elif token.type == TokenTypes.LOGIC_OPERATOR:
154-
# Handle the operator
155-
# and update all changes within the handler
156-
(
157-
current_operator,
158-
current_negation,
159-
) = self.handle_operator(
160-
token=token,
161-
current_operator=current_operator,
162-
current_negation=current_negation,
163-
)
154+
current_operator = token.value.upper()
155+
156+
# Set a flag if the token is NOT and change to AND
157+
if current_operator == "NOT":
158+
current_negation = True
159+
current_operator = "AND"
164160

165161
# Handle search fields
166162
elif token.type == TokenTypes.FIELD:
@@ -169,7 +165,7 @@ def parse_query_tree(
169165
# Handle terms
170166
else:
171167
# Add term nodes
172-
children = self.add_term_node(
168+
children = self._add_term_node(
173169
index=index,
174170
value=token.value,
175171
search_field=search_field,
@@ -207,7 +203,7 @@ def parse_query_tree(
207203
# Raise an error if the code gets here
208204
raise NotImplementedError("Error in parsing the query tree")
209205

210-
def handle_closing_parenthesis(
206+
def _handle_closing_parenthesis(
211207
self,
212208
children: list,
213209
current_operator: str,
@@ -235,24 +231,6 @@ def handle_closing_parenthesis(
235231
+ str(children)
236232
)
237233

238-
def handle_operator(
239-
self,
240-
token: Token,
241-
current_operator: str,
242-
current_negation: bool,
243-
) -> typing.Tuple[str, bool]:
244-
"""Handle operators."""
245-
246-
# Set the current operator to the token
247-
current_operator = token.value.upper()
248-
249-
# Set a flag if the token is NOT and change to AND
250-
if current_operator == "NOT":
251-
current_negation = True
252-
current_operator = "AND"
253-
254-
return current_operator, current_negation
255-
256234
def combine_subsequent_terms(self) -> None:
257235
"""Combine subsequent terms in the list of tokens."""
258236
# Combine subsequent terms (without quotes)
@@ -304,7 +282,7 @@ def combine_subsequent_terms(self) -> None:
304282

305283
self.tokens = combined_tokens
306284

307-
def add_unbalanced_quotes_to_adjacent_term(self) -> None:
285+
def _add_unbalanced_quotes_to_adjacent_term(self) -> None:
308286
"""Add unbalanced quotes to adjacent terms."""
309287

310288
s = self.query_str
@@ -337,7 +315,7 @@ def add_unbalanced_quotes_to_adjacent_term(self) -> None:
337315
self.tokens = updated_tokens
338316

339317
# pylint: disable=too-many-arguments
340-
def add_term_node(
318+
def _add_term_node(
341319
self,
342320
*,
343321
index: int,

test/test_wos_parser_internals.py

Lines changed: 7 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_handle_closing_parenthesis_single_child() -> None:
2525
"""
2626
children = [Query(value="example", operator=False, platform=PLATFORM.WOS.value)]
2727
parser = WOSParser(query_str="", search_field_general="", mode="")
28-
result = parser.handle_closing_parenthesis(children, current_operator="")
28+
result = parser._handle_closing_parenthesis(children, current_operator="")
2929

3030
assert result == children[0]
3131

@@ -43,7 +43,7 @@ def test_handle_closing_parenthesis_with_operator() -> None:
4343
]
4444
current_operator = "AND"
4545
parser = WOSParser(query_str="", search_field_general="", mode="")
46-
result = parser.handle_closing_parenthesis(children, current_operator)
46+
result = parser._handle_closing_parenthesis(children, current_operator)
4747

4848
expected_result = Query(
4949
value=current_operator, operator=True, children=list(children)
@@ -54,66 +54,6 @@ def test_handle_closing_parenthesis_with_operator() -> None:
5454
assert result.children == expected_result.children
5555

5656

57-
def test_handle_operator_uppercase() -> None:
58-
"""
59-
Test the `handle_operator` method with an uppercase operator.
60-
61-
This test verifies that the `handle_operator` method correctly handles
62-
an uppercase operator and returns the expected values.
63-
"""
64-
token = Token(value="AND", type=TokenTypes.LOGIC_OPERATOR, position=(0, 3))
65-
current_operator = ""
66-
current_negation = False
67-
68-
parser = WOSParser(query_str="", search_field_general="", mode="")
69-
result_operator, result_negation = parser.handle_operator(
70-
token, current_operator, current_negation
71-
)
72-
73-
assert result_operator == "AND"
74-
assert result_negation is False
75-
76-
77-
def test_handle_operator_near_with_distance() -> None:
78-
"""
79-
Test the `handle_operator` method with a NEAR operator with distance.
80-
81-
This test verifies that the `handle_operator` method correctly handles
82-
a NEAR operator with a given distance and returns the expected values.
83-
"""
84-
token = Token(value="NEAR/2", type=TokenTypes.PROXIMITY_OPERATOR, position=(0, 6))
85-
current_operator = ""
86-
current_negation = False
87-
88-
parser = WOSParser(query_str="", search_field_general="", mode="")
89-
result_operator, result_negation = parser.handle_operator(
90-
token, current_operator, current_negation
91-
)
92-
93-
assert result_operator == "NEAR/2"
94-
assert result_negation is False
95-
96-
97-
def test_handle_operator_not() -> None:
98-
"""
99-
Test the `handle_operator` method with the NOT operator.
100-
101-
This test verifies that the `handle_operator` method correctly handles
102-
the NOT operator, sets the negation flag, and changes the operator to AND.
103-
"""
104-
token = Token(value="NOT", type=TokenTypes.LOGIC_OPERATOR, position=(0, 3))
105-
current_operator = ""
106-
current_negation = False
107-
108-
parser = WOSParser(query_str="", search_field_general="", mode="")
109-
result_operator, result_negation = parser.handle_operator(
110-
token, current_operator, current_negation
111-
)
112-
113-
assert result_operator == "AND"
114-
assert result_negation is True
115-
116-
11757
def test_combine_subsequent_terms_single_term() -> None:
11858
"""
11959
Test the `combine_subsequent_terms` method with a single term.
@@ -239,7 +179,7 @@ def test_add_term_node_without_current_operator() -> None:
239179
current_operator = ""
240180
children: typing.List[Query] = []
241181

242-
result = parser.add_term_node(
182+
result = parser._add_term_node(
243183
index=index,
244184
value=value,
245185
search_field=search_field,
@@ -280,7 +220,7 @@ def test_add_term_node_with_current_operator() -> None:
280220
current_operator = "AND"
281221
children: typing.List[Query] = []
282222

283-
result = parser.add_term_node(
223+
result = parser._add_term_node(
284224
index=index,
285225
value=value,
286226
search_field=search_field,
@@ -359,7 +299,7 @@ def test_add_term_node_with_near_operator() -> None:
359299
)
360300
]
361301

362-
result = parser.add_term_node(
302+
result = parser._add_term_node(
363303
index=index,
364304
value=value,
365305
search_field=search_field,
@@ -460,7 +400,7 @@ def test_add_term_node_with_existing_children() -> None:
460400
)
461401
]
462402

463-
result = parser.add_term_node(
403+
result = parser._add_term_node(
464404
index=index,
465405
value=value,
466406
search_field=search_field,
@@ -513,7 +453,7 @@ def test_add_term_node_with_current_negation() -> None:
513453
children: typing.List[Query] = []
514454
current_negation = True
515455

516-
result = parser.add_term_node(
456+
result = parser._add_term_node(
517457
index=index,
518458
value=value,
519459
search_field=search_field,

0 commit comments

Comments
 (0)