From fd49152ee5ff6b148402a6a2a5daf66c948636a7 Mon Sep 17 00:00:00 2001 From: John Horton Date: Sat, 8 Feb 2025 16:01:49 -0500 Subject: [PATCH] Fix repr --- edsl/results/Results.py | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/edsl/results/Results.py b/edsl/results/Results.py index 0236cca9..f0f8834a 100644 --- a/edsl/results/Results.py +++ b/edsl/results/Results.py @@ -52,17 +52,9 @@ def ensure_ready(method): """ Decorator for Results methods. - This decorator: - 1. Checks if self.completed is True. If so, it executes the method. - 2. Otherwise, attempts to fetch remote data using: - self.fetch_remote(self.job_info) - 3. After fetching, if self.completed is still False, it prints a message and returns - a NotReadyObject initialized with the method name. - - Usage: - @ensure_ready - def table(self, ...): - ... + If the Results object is not ready, for most methods we return a NotReadyObject. + However, for __repr__ (and other methods that need to return a string), we return + the string representation of NotReadyObject. """ from functools import wraps @@ -70,16 +62,18 @@ def table(self, ...): def wrapper(self, *args, **kwargs): if self.completed: return method(self, *args, **kwargs) - # Try to fetch the remote data if not completed. + # Attempt to fetch remote data try: if hasattr(self, "job_info"): self.fetch_remote(self.job_info) except Exception as e: print(f"Error during fetch_remote in {method.__name__}: {e}") - # Check again after fetching. if not self.completed: - #print(f"Results not ready to call {method.__name__}.") - return NotReadyObject(method.__name__) + not_ready = NotReadyObject(method.__name__) + # For __repr__, ensure we return a string + if method.__name__ == "__repr__": + return not_ready.__repr__() + return not_ready return method(self, *args, **kwargs) return wrapper @@ -91,7 +85,7 @@ def __init__(self, name: str): #print(f"Not ready to call {name}") def __repr__(self): - return f"Results not ready to call {self.name}." + return f"Results not ready." def __getattr__(self, _): return self @@ -398,11 +392,10 @@ def _repr_html_(self): return super()._repr_html_() + @ensure_ready def __repr__(self) -> str: - if not self.completed: - return "Results(..waiting on remote job to complete...)" - return f"Results({len(self)} observations)" - + return f"Results(data = {self.data}, survey = {repr(self.survey)}, created_columns = {self.created_columns})" + def table( self, *fields,