Skip to content

Commit 54dfcdc

Browse files
committed
Improved readability [skip ci]
1 parent 7482ce1 commit 54dfcdc

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

pgvector/halfvec.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,20 @@ def from_text(cls, value: str) -> HalfVector:
6666
@classmethod
6767
def from_binary(cls, value: bytes) -> HalfVector:
6868
dim, unused = struct.unpack_from('>HH', value)
69+
data = value[4:]
6970

70-
if len(value) != 4 + 2 * dim:
71+
if len(data) != 2 * dim:
7172
raise ValueError('invalid length')
7273

7374
if unused != 0:
7475
raise ValueError('expected unused to be 0')
7576

76-
vec = cls.__new__(cls)
77-
vec._value = array.array('H', value[4:])
77+
arr = array.array('H', data)
7878
if sys.byteorder != 'big':
79-
vec._value.byteswap()
79+
arr.byteswap()
80+
81+
vec = cls.__new__(cls)
82+
vec._value = arr
8083
return vec
8184

8285
@classmethod

pgvector/vector.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,20 @@ def from_text(cls, value: str) -> Vector:
6464
@classmethod
6565
def from_binary(cls, value: bytes) -> Vector:
6666
dim, unused = struct.unpack_from('>HH', value)
67+
data = value[4:]
6768

68-
if len(value) != 4 + 4 * dim:
69+
if len(data) != 4 * dim:
6970
raise ValueError('invalid length')
7071

7172
if unused != 0:
7273
raise ValueError('expected unused to be 0')
7374

74-
vec = cls.__new__(cls)
75-
vec._value = array.array('f', value[4:])
75+
arr = array.array('f', data)
7676
if sys.byteorder != 'big':
77-
vec._value.byteswap()
77+
arr.byteswap()
78+
79+
vec = cls.__new__(cls)
80+
vec._value = arr
7881
return vec
7982

8083
@classmethod

0 commit comments

Comments
 (0)