Skip to content

Commit

Permalink
Type hints and docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
yunzheng committed May 17, 2024
1 parent 3bbb513 commit b4e41b2
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions flow/record/utils.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,48 @@
from __future__ import annotations

import base64
import os
import sys
from functools import wraps
from typing import BinaryIO, TextIO

_native = str
_unicode = type("")
_bytes = type(b"")


def get_stdout(binary=False):
def get_stdout(binary: bool = False) -> TextIO | BinaryIO:
"""Return the stdout stream as binary or text stream.
This function is the preferred way to get the stdout stream in flow.record.
Arguments:
binary: Whether to return the stream as binary stream.
Returns:
The stdout stream.
"""
fp = getattr(sys.stdout, "buffer", sys.stdout) if binary else sys.stdout
fp._is_stdout = True
return fp


def get_stdin(binary=False):
def get_stdin(binary: bool = False) -> TextIO | BinaryIO:
"""Return the stdin stream as binary or text stream.
This function is the preferred way to get the stdin stream in flow.record.
Arguments:
binary: Whether to return the stream as binary stream.
Returns:
The stdin stream.
"""
fp = getattr(sys.stdin, "buffer", sys.stdin) if binary else sys.stdin
fp._is_stdin = True
return fp


def is_stdout(fp):
def is_stdout(fp: TextIO | BinaryIO) -> bool:
"""Returns True if `fp` is the stdout stream."""
return fp in (sys.stdout, sys.stdout.buffer) or hasattr(fp, "_is_stdout")


Expand Down

0 comments on commit b4e41b2

Please sign in to comment.