Spaces:
Build error
Build error
File size: 2,386 Bytes
441d880 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | import re
import datetime
import textwrap
from Config import Config
from DataDictionary import *
import pandas as pd
import numpy as np
class FastFacts:
def __init__(self):
self.facts = None # Lazily initialised
def add_fact(self, fact):
"""
Add a single fact to the list, ensuring lazy initialisation.
"""
if not isinstance(fact, str):
print("Only strings are allowed as facts.")
return
# Initialise the list if it doesn't exist
if self.facts is None:
self.facts = []
self.facts.append(fact)
def add_facts(self, facts):
"""
Add multiple facts to the list, ensuring lazy initialisation.
"""
if not isinstance(facts, (set, list)):
print("Facts must be provided as a set or list.")
return
# Initialise the list if it doesn't exist
if self.facts is None:
self.facts = []
for fact in facts:
if isinstance(fact, str):
self.facts.append(fact)
else:
print(f"Skipping non-string fact: {fact}")
def __repr__(self):
if not self.facts:
return f"{self.__class__.__name__}: No facts available"
formatted_facts = ", ".join(f"<{fact}>" for fact in self.facts)
return f"{self.__class__.__name__}: {formatted_facts}"
def to_dict(self):
"""
Convert the FastFacts to a dictionary. Return an empty list if no facts are available.
"""
return {"facts": self.facts or []}
@staticmethod
def read_from_excel(fact_file):
"""
Read facts from an Excel file and populate a FastFacts object.
Args:
fact_file (str): Path to the Excel file.
Returns:
FastFacts: A populated FastFacts object.
"""
try:
df = pd.read_excel(fact_file)
facts_list = df["FastFacts"].dropna().tolist() # Assuming the facts are in a column named 'FastFacts'
# Create a FastFacts object and populate it with facts
fast_facts_obj = FastFacts()
fast_facts_obj.add_facts(facts_list)
return fast_facts_obj
except Exception as e:
print(f"An error occurred while reading from the Excel file: {e}")
return None |