pyyeti.writer.vecwrite

pyyeti.writer.vecwrite(f, string, *args, postfunc=None, pfargs=None, so=None)[source]

Vectorized write.

Parameters:
  • f (string or file_like or 1 or None) – Either a name of a file, or is a file_like object as returned by open() or io.StringIO. Input as integer 1 to write to stdout. Can also be the name of a directory or None; in these cases, a GUI is opened for file selection.

  • string (string) – The formatting string for the write, Python 3 format as in: string.format(a,b)

  • *args (list of variables) – Variables to write. Must be compatibly sized (scalars or vectors or numpy arrays of compatible sizes). numpy arrays of length 1 are considered scalars. For 2-d numpy arrays, each row is written on one line and each element of the row must have a conversion specifier. 1-d numpy arrays are treated like a column 2-d numpy array. Strings are considered scalars.

  • postfunc (function or None) – If a function, it is called with the final string (for each line) as the argument and it must return a string. The return string is what gets output. This can be handy for final string substitutions, for example. This input must be named and must be after the arguments to be printed; see example.

  • pfargs (iterable or None) – If an iterable, contains extra arguments to pass to postfunc after the string argument. Must be named and after the arguments to be printed.

  • so (slice object or None) – Allows selection of limited range and custom increment; eg: slice(0, 10, 2). Scalars are not sliced. Must be named and after the arguments to be printed.

Returns:

None.

Notes

The expected vector length is determined from the first non-scalar input. Note that scalar values are repeated automatically as necessary.

Raises:

ValueError – When the lengths of print arguments do not match (for lengths > 1). Note that the slice object so can make otherwise incompatible arguments compatible; for example, arguments of length 10 and length 100 would be compatible if so = slice(10) (or similar).

Examples

>>> from pyyeti import writer
>>> import sys
>>> import numpy as np
>>> r = np.array([1.2, 45.8])
>>> s = 'test string'
>>> i = 5
>>> v = ['short string', 'a bit longer string']
>>> frm = '{:3}, {:5.1f}, {:<25}, {}' + chr(10)
>>> writer.vecwrite(sys.stdout, frm, i, r, v, s)
  5,   1.2, short string             , test string
  5,  45.8, a bit longer string      , test string
>>> r = np.array([[1.1, 1.2, 1.3], [10.1, 10.2, 10.3]])
>>> frm = '{:2}, {:=^25} : ' + '  {:6.2f}'*3 + chr(10)
>>> writer.vecwrite(sys.stdout, frm, i, v, r)
 5, ======short string======= :     1.10    1.20    1.30
 5, ===a bit longer string=== :    10.10   10.20   10.30
>>> def pf(s):
...     return s.replace('0 ', '  ')
>>> writer.vecwrite(sys.stdout, frm, i, v, r, postfunc=pf)
 5, ======short string======= :     1.1     1.2     1.30
 5, ===a bit longer string=== :    10.1    10.2    10.30
>>> def pf(s, s_old, s_new):
...     return s.replace(s_old, s_new)
>>> writer.vecwrite(1, frm, i, v, r, postfunc=pf,
...                 pfargs=['0 ', '  '])
 5, ======short string======= :     1.1     1.2     1.30
 5, ===a bit longer string=== :    10.1    10.2    10.30