New web interface, move llm engine to separate process
This commit is contained in:
62
sia/util.py
62
sia/util.py
@@ -1,67 +1,5 @@
|
||||
import datetime
|
||||
import xml.dom.minidom
|
||||
import xml.etree.ElementTree as ET
|
||||
from typing import Iterator, Optional
|
||||
|
||||
def stop_before_value(iterator: Iterator[str], stop_value: str) -> Iterator[str]:
|
||||
"""
|
||||
Creates an iterator that yields values from the input iterator
|
||||
until it encounters the stop_value (exclusive).
|
||||
|
||||
Args:
|
||||
iterator: The source iterator
|
||||
stop_value: The value to stop before
|
||||
|
||||
Yields:
|
||||
Values from the iterator until stop_value is encountered
|
||||
If stop_value is part of an item, yields the part before stop_value
|
||||
"""
|
||||
for item in iterator:
|
||||
if stop_value in item:
|
||||
split_point = item.index(stop_value)
|
||||
if split_point > 0:
|
||||
yield item[:split_point]
|
||||
break
|
||||
yield item
|
||||
|
||||
def skip_prefix(iterator: Iterator[str], prefix: str) -> Iterator[str]:
|
||||
"""
|
||||
Creates an iterator that skips a prefix from the input iterator
|
||||
and yields only the content after the prefix.
|
||||
|
||||
Args:
|
||||
iterator: The source iterator
|
||||
prefix: The prefix to skip
|
||||
|
||||
Yields:
|
||||
Values from the iterator after the prefix has been fully skipped
|
||||
"""
|
||||
if not prefix:
|
||||
# If no prefix to skip, yield everything
|
||||
yield from iterator
|
||||
return
|
||||
|
||||
prefix_remaining = prefix
|
||||
|
||||
for item in iterator:
|
||||
if prefix_remaining:
|
||||
# If the item starts with the remaining prefix
|
||||
if prefix_remaining.startswith(item):
|
||||
# Skip this item entirely
|
||||
prefix_remaining = prefix_remaining[len(item):]
|
||||
continue
|
||||
elif item.startswith(prefix_remaining):
|
||||
# Yield only the part after the prefix
|
||||
yield item[len(prefix_remaining):]
|
||||
prefix_remaining = ""
|
||||
else:
|
||||
# Item doesn't match prefix pattern, yield everything
|
||||
# This is unexpected but we handle it gracefully
|
||||
yield item
|
||||
prefix_remaining = ""
|
||||
else:
|
||||
# No prefix remaining, yield all content
|
||||
yield item
|
||||
|
||||
def pretty_print_element(elem: ET.Element, level: int = 0, max_line: int = 80) -> str:
|
||||
"""Convert ElementTree element to pretty-printed string with custom formatting."""
|
||||
|
||||
Reference in New Issue
Block a user