Util¶

The util module provides a collection of general purpose methods. Most importantly it provides the run() method which is the preferred entry point of a nutils application, taking care of command line parsing, output dir creation and initiation of a log file.

nutils.util.sum()

reduce(function, sequence[, initial]) -> value

Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). If initial is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty.

nutils.util.product()

reduce(function, sequence[, initial]) -> value

Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). If initial is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty.

class nutils.util.NanVec(**attrs)[source]

nan-initialized vector

nutils.util.obj2str(obj)[source]

compact, lossy string representation of arbitrary object

nutils.util.single_or_multiple(f)[source]

Method wrapper, converts first positional argument to tuple: tuples/lists are passed on as tuples, other objects are turned into tuple singleton. Return values should match the length of the argument list, and are unpacked if the original argument was not a tuple/list.

>>> class Test:
...   @single_or_multiple
...   def square(self, args):
...     return [v**2 for v in args]
...
>>> T = Test()
>>> T.square(2)
4
>>> T.square([2,3])
[4, 9]

Parameters: f (method) – Method that expects a tuple as first positional argument, and that returns a list/tuple of the same length. Wrapped method.