Skip to content

Commit

Permalink
Refactor ConditionHandler class in conditions.py
Browse files Browse the repository at this point in the history
Refactored the `_eval_conditions` method removing redundant code
blocks for evaluating condition segments as already moved to
another method.
  • Loading branch information
rahul4732saini committed Nov 3, 2024
1 parent 952a78a commit efbfdb1
Showing 1 changed file with 9 additions and 31 deletions.
40 changes: 9 additions & 31 deletions fise/query/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,39 +400,17 @@ def _eval_conditions(
- obj (BaseEntity): Metadata object for extracting field values.
"""

# Adds a `True and` condition at the beginning of the list to avoid
# explicit definition of a mechanism for evaluating a single condition.
segments: list[Any] = [True, constants.OP_CONJUNCTION] + conditions
ctr: int = 0

# Evaluates conditions separated by `and` operator.
for _ in range(len(segments) // 2):
if segments[ctr + 1] == constants.OP_DISJUNCTION:
# Increments the counter by 1 to skip the
# conditions separated by the `or` operator.
ctr += 2
continue

segments[ctr : ctr + 3] = [
self._eval_condition(segments[ctr], obj)
and self._eval_condition(segments[ctr + 2], obj)
]

# Evaluates conditions separated by `or` operator.
for _ in range(len(segments) // 2):
# Replaces the conditions with the evaluated boolean value.
segments[:3] = [
self._eval_condition(segments[0], obj)
or self._eval_condition(segments[2], obj)
]

if segments[0]:
return True
# Evaluates the conditions seperated by the conjunction operator.
conditions = self._eval_condition_segments(
conditions, constants.OP_CONJUNCTION, obj
)

# Extracts the singe-most boolean value from the list.
result: bool = segments[0]
# Evaluates the conditions seperated by the disjunction operator.
conditions = self._eval_condition_segments(
conditions, constants.OP_DISJUNCTION, obj
)

return result
return conditions[0]

def eval_conditions(self, obj: BaseEntity) -> bool:
"""
Expand Down

0 comments on commit efbfdb1

Please sign in to comment.