|
74 | 74 | from pprint import pformat
|
75 | 75 | from types import MethodType
|
76 | 76 | from typing import (
|
| 77 | + IO, |
77 | 78 | TYPE_CHECKING,
|
78 | 79 | Any,
|
79 | 80 | Callable,
|
@@ -161,30 +162,87 @@ def list2str(the_list: Iterable[Any], sep: str = ',') -> str:
|
161 | 162 | return sep.join([str(x) for x in the_list])
|
162 | 163 |
|
163 | 164 |
|
164 |
| -def printr(obj: Any, *args, **kwargs) -> None: |
165 |
| - """ |
| 165 | +def printr( |
| 166 | + obj: Any, |
| 167 | + *values: object, |
| 168 | + sep: Optional[str] = ' ', |
| 169 | + end: Optional[str] = '\n', |
| 170 | + file: Optional[IO] = None, |
| 171 | + flush: bool = False, |
| 172 | + ) -> None: |
| 173 | + r""" |
166 | 174 | Print the :func:`repr` of an object.
|
167 |
| - """ |
168 | 175 |
|
169 |
| - print(repr(obj), *args, **kwargs) |
| 176 | + If no objects are given, :func:`~.printr` will just write ``end``. |
170 | 177 |
|
171 |
| - |
172 |
| -def printt(obj: Any, *args, **kwargs) -> None: |
173 |
| - """ |
| 178 | + :param obj: |
| 179 | + :param \*values: Additional values to print. These are printed verbatim. |
| 180 | + :param sep: The separator between values. |
| 181 | + :param end: The final value to print. |
| 182 | + Setting to ``''`` will leave the insertion point at the end of the printed text. |
| 183 | + :param file: The file to write to. |
| 184 | + If not present or :py:obj:`None`, :py:obj:`sys.stdout` will be used. |
| 185 | + :no-default file: |
| 186 | + :param flush: If :py:obj:`True` the stream is forcibly flushed after printing. |
| 187 | + """ |
| 188 | + |
| 189 | + print(repr(obj), *values, sep=sep, end=end, file=file, flush=flush) |
| 190 | + |
| 191 | + |
| 192 | +def printt( |
| 193 | + obj: Any, |
| 194 | + *values: object, |
| 195 | + sep: Optional[str] = ' ', |
| 196 | + end: Optional[str] = '\n', |
| 197 | + file: Optional[IO] = None, |
| 198 | + flush: bool = False, |
| 199 | + ) -> None: |
| 200 | + r""" |
174 | 201 | Print the type of an object.
|
| 202 | +
|
| 203 | + If no objects are given, :func:`~.printt` will just write ``end``. |
| 204 | +
|
| 205 | + :param obj: |
| 206 | + :param \*values: Additional values to print. These are printed verbatim. |
| 207 | + :param sep: The separator between values. |
| 208 | + :param end: The final value to print. |
| 209 | + Setting to ``''`` will leave the insertion point at the end of the printed text. |
| 210 | + :param file: The file to write to. |
| 211 | + If not present or :py:obj:`None`, :py:obj:`sys.stdout` will be used. |
| 212 | + :no-default file: |
| 213 | + :param flush: If :py:obj:`True` the stream is forcibly flushed after printing. |
175 | 214 | """
|
176 | 215 |
|
177 |
| - print(type(obj), *args, **kwargs) |
| 216 | + print(type(obj), *values, sep=sep, end=end, file=file, flush=flush) |
178 | 217 |
|
179 | 218 |
|
180 |
| -def stderr_writer(*args, **kwargs) -> None: |
181 |
| - """ |
182 |
| - Write to stderr, flushing stdout beforehand and stderr afterwards. |
| 219 | +def stderr_writer( |
| 220 | + *values: object, |
| 221 | + sep: Optional[str] = ' ', |
| 222 | + end: Optional[str] = '\n', |
| 223 | + ) -> None: |
| 224 | + r""" |
| 225 | + Print ``*values`` to :py:obj:`sys.stderr`, separated by ``sep`` and followed by ``end``. |
| 226 | +
|
| 227 | + :py:obj:`sys.stdout` is flushed before printing, and :py:obj:`sys.stderr` is flushed afterwards. |
| 228 | +
|
| 229 | + If no objects are given, :func:`~.stderr_writer` will just write ``end``. |
| 230 | +
|
| 231 | + :param \*values: |
| 232 | + :param sep: The separator between values. |
| 233 | + :param end: The final value to print. |
| 234 | + Setting to ``''`` will leave the insertion point at the end of the printed text. |
| 235 | +
|
| 236 | + :rtype: |
| 237 | +
|
| 238 | + .. versionchanged:: 3.0.0 |
| 239 | +
|
| 240 | + The only permitted keyword arguments are ``sep`` and ``end``. |
| 241 | + Previous versions allowed other keywords arguments supported by :func:`print` but they had no effect. |
183 | 242 | """
|
184 | 243 |
|
185 | 244 | sys.stdout.flush()
|
186 |
| - kwargs["file"] = sys.stderr |
187 |
| - print(*args, **kwargs) |
| 245 | + print(*values, sep=sep, end=end, file=sys.stderr, flush=True) |
188 | 246 | sys.stderr.flush()
|
189 | 247 |
|
190 | 248 |
|
|
0 commit comments