Repository navigation
FEATURE: Add vector upsert command #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| """Encoding of the vkey, vector and metadata that vector commands carry.""" | ||
|
|
||
| import json | ||
| import math | ||
| import struct | ||
| from collections.abc import Sequence | ||
| from typing import Any | ||
|
|
||
| from arcus._validation import validate_vkey | ||
|
|
||
|
|
||
| def encode_vkey(vkey: str) -> bytes: | ||
| validate_vkey(vkey) | ||
| return vkey.encode() | ||
|
|
||
|
|
||
| def encode_vector(vector: Sequence[float] | bytes) -> bytes: | ||
| match vector: | ||
| case bytes() | bytearray() | memoryview(): | ||
| data = bytes(vector) | ||
|
Comment on lines
+19
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 바이트 벡터에도 비유한 값 검사를 적용하세요.
PR 요구사항의 “벡터에 포함된 NaN 또는 infinity를 거부”하는 계약에 근거합니다.
|
||
| case Sequence(): | ||
| if not math.isfinite(sum(vector)): | ||
| raise ValueError("Vector must hold only finite numbers") | ||
| data = struct.pack(f"<{len(vector)}f", *vector) | ||
| case _: | ||
| raise TypeError(f"Vector must be a sequence, not {type(vector).__name__}") | ||
|
|
||
| if not data: | ||
| raise ValueError("Vector must not be empty") | ||
| if len(data) % 4: | ||
| raise ValueError(f"Vector bytes must be a multiple of 4: {len(data)}") | ||
| return data | ||
|
|
||
|
|
||
| def encode_meta(meta: dict[str, Any] | None) -> bytes: | ||
| if meta is None: | ||
| return b"" | ||
| if not isinstance(meta, dict): | ||
| raise TypeError(f"Meta must be dict, not {type(meta).__name__}") | ||
|
|
||
| return json.dumps( | ||
| meta, ensure_ascii=False, allow_nan=False, separators=(",", ":") | ||
| ).encode() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
실패 상태는
False대신 예외로 전달하세요.인덱스 부재, 타입 불일치, 용량 초과가 모두
False로 합쳐집니다. 호출자는 실패 원인을 구분할 수 없습니다. 응답 상태를 보존하는Arcus예외를 발생시키고 공개 반환 계약과 응답 테스트를 함께 수정하세요.경로 지침의 “Signal failure by raising, not by returning None or a sentinel.”에 근거합니다.
Source: Path instructions