diff options
author | dvora-h <67596500+dvora-h@users.noreply.github.com> | 2022-02-06 15:24:08 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-06 15:24:08 +0200 |
commit | b1ffbc91b2c0ee305dd9fd9ebe01654a7ec18e8b (patch) | |
tree | 93cf68b9246bcd53a7ddcadcd048f1d0ca0131f8 | |
parent | 9b99bf98375574540a5f1642d0fd7900781d0761 (diff) | |
download | redis-py-b1ffbc91b2c0ee305dd9fd9ebe01654a7ec18e8b.tar.gz |
add support for lcs (#1924)
-rw-r--r-- | redis/commands/core.py | 29 | ||||
-rw-r--r-- | tests/test_commands.py | 11 |
2 files changed, 40 insertions, 0 deletions
diff --git a/redis/commands/core.py b/redis/commands/core.py index 1b8cb80..cef0c28 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -1884,6 +1884,35 @@ class BasicKeyCommands: """ return self.execute_command("UNLINK", *names) + def lcs( + self, + key1: str, + key2: str, + len: Optional[bool] = False, + idx: Optional[bool] = False, + minmatchlen: Optional[int] = 0, + withmatchlen: Optional[bool] = False, + ) -> Union[str, int, list]: + """ + Find the longest common subsequence between ``key1`` and ``key2``. + If ``len`` is true the length of the match will will be returned. + If ``idx`` is true the match position in each strings will be returned. + ``minmatchlen`` restrict the list of matches to the ones of + the given ``minmatchlen``. + If ``withmatchlen`` the length of the match also will be returned. + For more information check https://redis.io/commands/lcs + """ + pieces = [key1, key2] + if len: + pieces.append("LEN") + if idx: + pieces.append("IDX") + if minmatchlen != 0: + pieces.extend(["MINMATCHLEN", minmatchlen]) + if withmatchlen: + pieces.append("WITHMATCHLEN") + return self.execute_command("LCS", *pieces) + class ListCommands: """ diff --git a/tests/test_commands.py b/tests/test_commands.py index 7d98665..0e27836 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -968,6 +968,17 @@ class TestRedisCommands: assert r.get("a") is None assert r.get("b") is None + @pytest.mark.onlynoncluster + # @skip_if_server_version_lt("7.0.0") turn on after redis 7 release + def test_lcs(self, unstable_r): + unstable_r.mset({"foo": "ohmytext", "bar": "mynewtext"}) + assert unstable_r.lcs("foo", "bar") == b"mytext" + assert unstable_r.lcs("foo", "bar", len=True) == 6 + result = [b"matches", [[[4, 7], [5, 8]]], b"len", 6] + assert unstable_r.lcs("foo", "bar", idx=True, minmatchlen=3) == result + with pytest.raises(redis.ResponseError): + assert unstable_r.lcs("foo", "bar", len=True, idx=True) + @skip_if_server_version_lt("2.6.0") def test_dump_and_restore(self, r): r["a"] = "foo" |