Skip to content

Commit

Permalink
fix loop. loop is now part of constraints.Lookback
Browse files Browse the repository at this point in the history
  • Loading branch information
tandav committed Sep 17, 2023
1 parent fa7deb2 commit 6836aea
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
2 changes: 0 additions & 2 deletions opseq/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@ def __init__(
prefix_constraints: Iterable[Constraint[Op]] = (),
unique_key_op: UniqueKeyOp[Op] | None = None,
unique_key_seq: UniqueKeySeq[Op] | None = None,
# loop: bool = False,
):
self.n = n
self.generator = generator
self.constraints = list(constraints)
self.prefix_constraints = list(prefix_constraints)
self.seen_keys = set()
self.prefix = prefix
# self.loop = loop

if unique_key_op is not None:
self.prefix_constraints.append(constraints_.UniqueOp(unique_key_op))
Expand Down
12 changes: 9 additions & 3 deletions opseq/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,27 @@


class Lookback:
def __init__(self, index, constraint):
def __init__(self, index, constraint, loop: bool = False):
if index >= 0:
raise KeyError('constraint index must be negative')
self.index = index
self.constraint = constraint
self.loop = loop

@classmethod
def from_dict(cls, d: dict[int, tp.Callable[[Op, Op], bool]]) -> list[Lookback]:
return [cls(index, constraint) for index, constraint in d.items()]
def from_dict(cls, d: dict[int, tp.Callable[[Op, Op], bool]], loop: bool = False) -> list[Lookback]:
return [cls(index, constraint, loop) for index, constraint in d.items()]

def __call__(self, seq: Seq[Op]) -> bool:
if len(seq) == 1:
return True
if abs(self.index) >= len(seq):
return True
if self.loop:
return all(
self.constraint(seq[(i + self.index) % len(seq)], seq[i])
for i in range(abs(self.index))
)
return self.constraint(seq[self.index - 1], seq[-1])


Expand Down
38 changes: 38 additions & 0 deletions tests/constraints_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
def identity(x):
return x

def is_different_startswith(a: str, b: str) -> bool:
return a[0] != b[0]


def is_equal_endswith(a: str, b: str) -> bool:
return a[1] == b[1]


@pytest.mark.parametrize('constraint, expected', [
(lambda seq: seq[0] != 0, [(1, 0), (1, 1)]),
])
Expand Down Expand Up @@ -81,6 +89,36 @@ def test_unique_key_op(generator, key, expected):
def test_lookback_constraint(opseq, expected):
assert list(opseq) == expected


@pytest.mark.parametrize('opseq, expected', [
(
OpSeq(
3,
AppendOp(options=[0, 1, 2]),
prefix_constraints=constraints_.Lookback.from_dict({
-1: lambda prev, curr: prev != curr,
-2: lambda prev, curr: prev != curr,
},loop=True)
),
[(0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0)],
),
])
def test_lookback_constraint_loop1(opseq, expected):
assert list(opseq) == expected


def test_lookback_constraint_loop2():
opseq = OpSeq(
4,
AppendOp(options=('A0', 'A1', 'C0', 'D0', 'D1')),
constraints=constraints_.Lookback.from_dict({-1: is_different_startswith, -2: is_equal_endswith}, loop=True),
)
assert all(
is_different_startswith(seq[0], seq[-1]) and is_equal_endswith(seq[1], seq[-1])
for seq in opseq
)


def is_even(x: int) -> bool:
return x % 2 == 0

Expand Down
9 changes: 0 additions & 9 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ def test_seq_length_error(n, generator, expected):



# def is_different_startswith(a: str, b: str) -> bool:
# return a[0] != b[0]


# def is_equal_endswith(a: str, b: str) -> bool:
# return a[1] == b[1]


# def even_odd_interchange(prev, curr):
# return is_even(prev) ^ is_even(curr)
Expand All @@ -72,8 +65,6 @@ def test_seq_length_error(n, generator, expected):





# def test_prev_curr(options):
# for cycle in OpSeq(5, options=options, curr_prev_constraint={-1: even_odd_interchange}, loop=True): # type: ignore[var-annotated]
# assert even_odd_interchange(cycle[-1], cycle[0])
Expand Down

0 comments on commit 6836aea

Please sign in to comment.