Skip to content

Commit

Permalink
remove unused code, more flattening
Browse files Browse the repository at this point in the history
  • Loading branch information
ojii committed Jan 5, 2024
1 parent ce6e63f commit b87ccd0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
4 changes: 4 additions & 0 deletions src/aiodynamo/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ def __and__(self, other: Condition) -> Condition:
return AndCondition(self.children.extending(other.children))
else:
return AndCondition(self.children.appending(other))
elif isinstance(other, AndCondition):
return AndCondition(other.children.prepending(self))
return AndCondition(MinLen2AppendOnlyList.create(self, other))

def __or__(self, other: Condition) -> Condition:
Expand All @@ -390,6 +392,8 @@ def __or__(self, other: Condition) -> Condition:
return OrCondition(self.children.extending(other.children))
else:
return OrCondition(self.children.appending(other))
elif isinstance(other, OrCondition):
return OrCondition(other.children.prepending(self))
return OrCondition(MinLen2AppendOnlyList.create(self, other))

def __invert__(self) -> Condition:
Expand Down
21 changes: 3 additions & 18 deletions src/aiodynamo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,31 +213,16 @@ class MinLen2AppendOnlyList(Generic[T]):
def create(cls, first: T, second: T, *rest: T) -> MinLen2AppendOnlyList[T]:
return cls(first, second, rest)

def prepending(self, value: T) -> MinLen2AppendOnlyList[T]:
return MinLen2AppendOnlyList(value, self.first, (self.second, *self.rest))

def appending(self, value: T) -> MinLen2AppendOnlyList[T]:
return MinLen2AppendOnlyList(self.first, self.second, (*self.rest, value))

def extending(self, values: Iterable[T]) -> MinLen2AppendOnlyList[T]:
return MinLen2AppendOnlyList(self.first, self.second, (*self.rest, *values))

def __contains__(self, item: Any) -> bool:
return item == self.first or item == self.second or item in self.rest

def __getitem__(self, index: int) -> T:
if index == 0:
return self.first
elif index == 1:
return self.second
return self.rest[index - 2]

def __len__(self) -> int:
return len(self.rest) + 2

def __iter__(self) -> Generator[T, None, None]:
yield self.first
yield self.second
yield from self.rest

def __reversed__(self) -> Generator[T, None, None]:
yield from reversed(self.rest)
yield self.second
yield self.first
20 changes: 20 additions & 0 deletions tests/unit/test_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ def test_update_expression_debug(expr: UpdateExpression, expected: str) -> None:
)
),
),
(
F("a").equals("a") & (F("b").equals("b") & F("c").equals("c")),
AndCondition(
MinLen2AppendOnlyList.create(
Comparison(F("a"), "=", "a"),
Comparison(F("b"), "=", "b"),
Comparison(F("c"), "=", "c"),
)
),
),
(
(F("a").equals("a") & F("b").equals("b"))
& (F("c").equals("c") & F("d").equals("d")),
Expand Down Expand Up @@ -194,6 +204,16 @@ def test_update_expression_debug(expr: UpdateExpression, expected: str) -> None:
)
),
),
(
F("a").equals("a") | (F("b").equals("b") | F("c").equals("c")),
OrCondition(
MinLen2AppendOnlyList.create(
Comparison(F("a"), "=", "a"),
Comparison(F("b"), "=", "b"),
Comparison(F("c"), "=", "c"),
)
),
),
(
(F("a").equals("a") | F("b").equals("b"))
| (F("c").equals("c") | F("d").equals("d")),
Expand Down

0 comments on commit b87ccd0

Please sign in to comment.