# ufl.utils package

## Submodules

## ufl.utils.counted module

Mixin class for types with a global unique counter attached to each object.

class ufl.utils.counted.Counted(count=None, counted_class=None)

  Bases: `object`

  Mixin class for globally counted objects.

  count()

    Get count.

## ufl.utils.formatting module

Various string formatting utilities.

ufl.utils.formatting.camel2underscore(name)

  Convert a CamelCaps string to underscore_syntax.

ufl.utils.formatting.estr(elements)

  Format list of elements for printing.

ufl.utils.formatting.istr(o)

  Format object as string, inserting ? for None.

ufl.utils.formatting.lstr(a)

  Pretty-print list or tuple, invoking str() on items instead of repr() like str() does.

ufl.utils.formatting.tree_format(expression, indentation=0, parentheses=True)

  Tree format.

ufl.utils.formatting.tstr(t, colsize=80)

  Pretty-print list of tuples of key-value pairs.

## ufl.utils.indexflattening module

Collection of utilities for mapping between multiindices and a flattened index space.

ufl.utils.indexflattening.flatten_multiindex(ii, strides)

  Return the flat index corresponding to the given multiindex.

ufl.utils.indexflattening.shape_to_strides(sh)

  Return a tuple of strides given a shape tuple.

ufl.utils.indexflattening.unflatten_index(i, strides)

  Return the multiindex corresponding to the given flat index.

## ufl.utils.sequences module

Various sequence manipulation utilities.

ufl.utils.sequences.max_degree(degrees)

  Maximum degree for mixture of scalar and tuple degrees.

ufl.utils.sequences.product(sequence)

  Return the product of all elements in a sequence.

## ufl.utils.sorting module

Utilites for sorting.

ufl.utils.sorting.canonicalize_metadata(metadata: None | dict | str | list | tuple | ndarray) → tuple

  Assuming metadata to be a dict with string keys and builtin python types as values.

  Transform dict to a tuple of (key, value) item tuples ordered by key, with dict, list and tuple values converted the same way recursively. Lists and tuples are converted to tuples. Other values are converted using str(). This is such that the end result can be hashed and sorted using regular <, because python 3 doesn’t allow e.g. (3 < “auto”) which occurs regularly in metadata.

ufl.utils.sorting.sorted_by_count(seq)

  Sort a sequence by the item.count().

ufl.utils.sorting.sorted_by_key(mapping)

  Sort dict items by key, allowing different key types.

ufl.utils.sorting.topological_sorting(nodes, edges)

  Return a topologically sorted list of the nodes.

  Implemented algorithm from Wikipedia (http://en.wikipedia.org/wiki/Topological_sorting).

  No error for cyclic edges…

## ufl.utils.stacks module

Various utility data structures.

class ufl.utils.stacks.Stack(*args)

  Bases: `list`

  A stack datastructure.

  peek()

    Peek.

  push(v)

    Push.

class ufl.utils.stacks.StackDict(*args, **kwargs)

  Bases: `dict`

  A dictionary type.

  A dict that can be changed incrementally with ‘d.push(k,v)’ and have changes rolled back with ‘k,v = d.pop()’.

  pop()

    Restore previous state for this key.

  push(k, v)

    Store previous state for this key.

## Module contents

Utilities.