Display a message on a device. Parameters ---------- mesg : str Message to display. device : object Device to write message. If None, defaults to ``sys.stdout`` which is very similar to ``print``. `device` needs to have ``write()`` and ``flush()`
(mesg, device=None, linefeed=True)
| 1955 | |
| 1956 | |
| 1957 | def disp(mesg, device=None, linefeed=True): |
| 1958 | """ |
| 1959 | Display a message on a device. |
| 1960 | |
| 1961 | Parameters |
| 1962 | ---------- |
| 1963 | mesg : str |
| 1964 | Message to display. |
| 1965 | device : object |
| 1966 | Device to write message. If None, defaults to ``sys.stdout`` which is |
| 1967 | very similar to ``print``. `device` needs to have ``write()`` and |
| 1968 | ``flush()`` methods. |
| 1969 | linefeed : bool, optional |
| 1970 | Option whether to print a line feed or not. Defaults to True. |
| 1971 | |
| 1972 | Raises |
| 1973 | ------ |
| 1974 | AttributeError |
| 1975 | If `device` does not have a ``write()`` or ``flush()`` method. |
| 1976 | |
| 1977 | Examples |
| 1978 | -------- |
| 1979 | Besides ``sys.stdout``, a file-like object can also be used as it has |
| 1980 | both required methods: |
| 1981 | |
| 1982 | >>> from io import StringIO |
| 1983 | >>> buf = StringIO() |
| 1984 | >>> np.disp(u'"Display" in a file', device=buf) |
| 1985 | >>> buf.getvalue() |
| 1986 | '"Display" in a file\\n' |
| 1987 | |
| 1988 | """ |
| 1989 | if device is None: |
| 1990 | device = sys.stdout |
| 1991 | if linefeed: |
| 1992 | device.write('%s\n' % mesg) |
| 1993 | else: |
| 1994 | device.write('%s' % mesg) |
| 1995 | device.flush() |
| 1996 | return |
| 1997 | |
| 1998 | |
| 1999 | # See https://docs.scipy.org/doc/numpy/reference/c-api.generalized-ufuncs.html |