Spaces:
Paused
Paused
Download agent_zero_framework/python/helpers/print_catch.py from Oldmangrizzz/Agent-Node: direct link, hf CLI and curl.
- Browser
- Download file 1.01 kB
-
https://huggingface.co/spaces/Oldmangrizzz/Agent-Node/resolve/main/agent_zero_framework/python/helpers/print_catch.py
- Command line
-
hf download hf://spaces/Oldmangrizzz/Agent-Node/agent_zero_framework/python/helpers/print_catch.py
-
curl -L -o print_catch.py https://huggingface.co/spaces/Oldmangrizzz/Agent-Node/resolve/main/agent_zero_framework/python/helpers/print_catch.py
1.01 kB
| import asyncio | |
| import io | |
| import sys | |
| from typing import Callable, Any, Awaitable, Tuple | |
| def capture_prints_async( | |
| func: Callable[..., Awaitable[Any]], | |
| *args, | |
| **kwargs | |
| ) -> Tuple[Awaitable[Any], Callable[[], str]]: | |
| # Create a StringIO object to capture the output | |
| captured_output = io.StringIO() | |
| original_stdout = sys.stdout | |
| # Define a function to get the current captured output | |
| def get_current_output() -> str: | |
| return captured_output.getvalue() | |
| async def wrapped_func() -> Any: | |
| nonlocal captured_output, original_stdout | |
| try: | |
| # Redirect sys.stdout to the StringIO object | |
| sys.stdout = captured_output | |
| # Await the provided function | |
| return await func(*args, **kwargs) | |
| finally: | |
| # Restore the original sys.stdout | |
| sys.stdout = original_stdout | |
| # Return the wrapped awaitable and the output retriever | |
| return asyncio.create_task(wrapped_func()), get_current_output |