Skip to content

Commit f770ccf

Browse files
committed
Reduced dependency on NumPy [skip ci]
1 parent d13c5dd commit f770ccf

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

pgvector/bit.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ def __init__(self, value: bytes | str | list[bool] | np.ndarray[tuple[int], np.d
1111
data = value
1212
else:
1313
if isinstance(value, str):
14-
value = [v != '0' for v in value]
14+
length = len(value)
15+
16+
if length % 8 != 0:
17+
value += '0'*(8 - (length % 8))
18+
19+
try:
20+
data = int(value, 2).to_bytes(len(value) // 8, byteorder='big')
21+
except ValueError:
22+
raise ValueError('expected bit string')
1523
else:
1624
value = np.asarray(value)
1725

@@ -30,8 +38,8 @@ def __init__(self, value: bytes | str | list[bool] | np.ndarray[tuple[int], np.d
3038
if value.ndim != 1:
3139
raise ValueError('expected ndim to be 1')
3240

33-
length = len(value)
34-
data = np.packbits(value).tobytes()
41+
length = len(value)
42+
data = np.packbits(value).tobytes()
3543

3644
self._value = pack('>i', length) + data
3745

tests/test_bit.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ def test_str(self):
1919
assert Bit('101').to_list() == [True, False, True]
2020

2121
def test_str_two(self):
22-
# TODO raise
23-
assert Bit('201').to_list() == [True, False, True]
22+
with pytest.raises(ValueError) as error:
23+
Bit('201')
24+
assert str(error.value) == 'expected bit string'
2425

2526
def test_bytes(self):
2627
assert Bit(b'\xff\x00\xf0').to_text() == '111111110000000011110000'

0 commit comments

Comments
 (0)