forked from epam/ai-dial
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: supported writable stages/choices (epam#122)
- Loading branch information
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from typing import Protocol | ||
|
||
|
||
class ContentReceiver(Protocol): | ||
def append_content(self, content: str) -> None: ... | ||
|
||
|
||
class ContentStream: | ||
""" | ||
The ContentStream class allows using the receiver in contexts where typing.SupportsWrite[str] is expected. | ||
For example: | ||
1. Redirecting print statements: | ||
print("Hello, world", file=content_stream) | ||
2. Using with tqdm for progress bars: | ||
import tqdm | ||
for item in tqdm(items, file=content_stream): | ||
process(item) | ||
3. Redirecting logs to the content stream: | ||
import logging | ||
logging_handler = logging.StreamHandler(stream=content_stream) | ||
4. Writing CSV data: | ||
import csv | ||
csv.writer(content_stream).writerows(data) | ||
""" | ||
|
||
_receiver: ContentReceiver | ||
|
||
def __init__(self, receiver: ContentReceiver) -> None: | ||
self._receiver = receiver | ||
|
||
def write(self, s: str) -> None: | ||
self._receiver.append_content(s) |