Skip to content

Commit

Permalink
fix:flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
cunla committed Oct 10, 2024
1 parent 83d9cf4 commit 6e0eea9
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions fakeredis/commands_mixins/bitmap_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init(self, *args: Any, **kwargs: Any) -> None:
def _bytes_as_bin_string(value: bytes) -> str:
return "".join([bin(i).lstrip("0b").rjust(8, "0") for i in value])

@command((Key(bytes), Int), (bytes,))
@command((Key(bytes), Int), (bytes,), flags=msgs.FLAG_DO_NOT_CREATE)
def bitpos(self, key: CommandItem, bit: int, *args: bytes) -> int:
if bit != 0 and bit != 1:
raise SimpleError(msgs.BIT_ARG_MUST_BE_ZERO_OR_ONE)
Expand All @@ -55,37 +55,35 @@ def bitpos(self, key: CommandItem, bit: int, *args: bytes) -> int:
if not bit_mode and not casematch(args[2], b"byte"):
raise SimpleError(msgs.SYNTAX_ERROR_MSG)

# Redis treats non-existent key as an infinite array of 0 bits.
# If the user is looking for the first clear bit return 0,
# If the user is looking for the first set bit, return -1.
if not key.value:
if key.value is None:
# The first clear bit is at 0, the first set bit is not found (-1).
return -1 if bit == 1 else 0

start = 0 if len(args) == 0 else Int.decode(args[0])
bit_chr = str(bit)
key_value = key.value if key.value else b""

if bit_mode:
value = self._bytes_as_bin_string(key_value)
value = self._bytes_as_bin_string(key.value)
end = len(value) if len(args) <= 1 else Int.decode(args[1])
length = len(value)
start, end = fix_range(start, end, length)
value = value[start:end]
else:
end = len(key_value) if len(args) <= 1 else Int.decode(args[1])
length = len(key_value)

start, end = fix_range(start, end, length)
end = len(key.value) if len(args) <= 1 else Int.decode(args[1])
length = len(key.value)
start, end = fix_range(start, end, length)
value = self._bytes_as_bin_string(key.value[start:end])

if start == end == -1:
return -1

value = value[start:end] if bit_mode else self._bytes_as_bin_string(key_value[start:end])
result = value.find(bit_chr)
if result != -1:
result += start if bit_mode else (start * 8)
# Redis treats the value as padded with zero bytes to an infinity
# if the user is looking for the first clear bit and no end is set.
elif bit == 0 and len(args) <= 1:
result = len(key_value) * 8
# Redis treats the value as padded with zero bytes to an infinity
# if the user is looking for the first clear bit and no end is set.
result = len(key.value) * 8
return result

@command(name="BITCOUNT", fixed=(Key(bytes),), repeat=(bytes,), flags=msgs.FLAG_DO_NOT_CREATE)
Expand Down

0 comments on commit 6e0eea9

Please sign in to comment.