Files
qdrant-client/qdrant_client/embed/utils.py
George edde944bb3 Drop python 3.8 (#848)
* new: drop python 3.8 support, update type hints

* fix: remove 3.8 from ci

* fix: update type hints

* fix: make netlify use python3.10

* fix: try python3.9 for sphinx

* debug: try updating sphinx

* new: bump ffastembed to 0.4.2

* fix: install numpy<2 for mypy

* fix: install numpy via poetry
2025-01-16 13:38:04 +01:00

67 lines
2.1 KiB
Python

from typing import Optional
from pydantic import BaseModel, Field
class FieldPath(BaseModel):
current: str
tail: Optional[list["FieldPath"]] = Field(default=None)
def as_str_list(self) -> list[str]:
"""
>>> FieldPath(current='a', tail=[FieldPath(current='b', tail=[FieldPath(current='c'), FieldPath(current='d')])]).as_str_list()
['a.b.c', 'a.b.d']
"""
# Recursive function to collect all paths
def collect_paths(path: FieldPath, prefix: str = "") -> list[str]:
current_path = prefix + path.current
if not path.tail:
return [current_path]
else:
paths = []
for sub_path in path.tail:
paths.extend(collect_paths(sub_path, current_path + "."))
return paths
# Collect all paths starting from this object
return collect_paths(self)
def convert_paths(paths: list[str]) -> list[FieldPath]:
"""Convert string paths into FieldPath objects
Paths which share the same root are grouped together.
Args:
paths: List[str]: List of str paths containing "." as separator
Returns:
List[FieldPath]: List of FieldPath objects
"""
sorted_paths = sorted(paths)
prev_root = None
converted_paths = []
for path in sorted_paths:
parts = path.split(".")
root = parts[0]
if root != prev_root:
converted_paths.append(FieldPath(current=root))
prev_root = root
current = converted_paths[-1]
for part in parts[1:]:
if current.tail is None:
current.tail = []
found = False
for tail in current.tail:
if tail.current == part:
current = tail
found = True
break
if not found:
new_tail = FieldPath(current=part)
assert current.tail is not None
current.tail.append(new_tail)
current = new_tail
return converted_paths