Spaces:
Build error
Build error
| 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 []} | |
| 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 |