Refactor records_to_text function to handle non-Record items
This commit is contained in:
parent
c5fb239852
commit
4aa726706f
1 changed files with 7 additions and 6 deletions
|
|
@ -29,11 +29,12 @@ def records_to_text(template: str, records: list[Record]) -> str:
|
||||||
if isinstance(records, Record):
|
if isinstance(records, Record):
|
||||||
records = [records]
|
records = [records]
|
||||||
# Check if there are any format strings in the template
|
# Check if there are any format strings in the template
|
||||||
if any(not isinstance(record, Record) for record in records):
|
_records = []
|
||||||
item_that_is_not_record = next((record for record in records if not isinstance(record, Record)), None)
|
for record in records:
|
||||||
raise ValueError(
|
# If it is not a record, create one with the key "text"
|
||||||
f"All items in the list must be of type Record. Found: {item_that_is_not_record} of type {type(item_that_is_not_record)}."
|
if not isinstance(record, Record):
|
||||||
)
|
record = Record(text=record)
|
||||||
|
_records.append(record)
|
||||||
|
|
||||||
formated_records = [template.format(data=record.data, **record.data) for record in records]
|
formated_records = [template.format(data=record.data, **record.data) for record in _records]
|
||||||
return "\n".join(formated_records)
|
return "\n".join(formated_records)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue