Utilities (m4opt.utils)#

m4opt.utils.console Module#

Utilities for console applications and text user interfaces.

Use the progress() and status() methods to create live feedback for a nested series of tasks. The elapsed time is shown for each task, along with whether it completed successfully or failed due to an exception being raised.

Examples

from m4opt.utils.console import progress, status
from time import sleep
with progress():
    with status("Prepare the dough"):
        with status("Proof the yeast"):
            pass  # do some work here
        with status("Mix the wet and dry ingredients"):
            pass  # do some work here
        with status("Knead for 10 minutes"):
            pass  # do some work here
        with status("Let rise for 1 hour"):
            pass  # do some work here
    with status("Preheat oven to 500° F"):
        pass  # do some work here
    with status("Assemble the pizza"):
        with status("Roll out the dough"):
            pass  # do some work here
        with status("Top with sauce and cheese"):
            pass  # do some work here
        with status("Add your favorite toppings"):
            pass  # do some work here
    with status("Bake until golden brown"):
        pass
    with status("Serve to your hungry guests"):
        raise RuntimeError("Sorry, I ate it all")
✓ Prepare the dough                 0:00:00
  ✓ Proof the yeast                 0:00:00
  ✓ Mix the wet and dry ingredients 0:00:00
  ✓ Knead for 10 minutes            0:00:00
  ✓ Let rise for 1 hour             0:00:00
✓ Preheat oven to 500° F            0:00:00
✓ Assemble the pizza                0:00:00
  ✓ Roll out the dough              0:00:00
  ✓ Top with sauce and cheese       0:00:00
  ✓ Add your favorite toppings      0:00:00
✓ Bake until golden brown           0:00:00
✗ Serve to your hungry guests       0:00:00
Traceback (most recent call last):
  File "/Users/lpsinger/src/m4opt/test.py", line 24, in <module>
    raise RuntimeError("Sorry, I ate it all")
RuntimeError: Sorry, I ate it all

Functions#

progress()

Context manager to create a live display for showing status of tasks.

status(description)

Context manager to track the runtime of a task.

m4opt.utils.functional Module#

Functional programming utilities.

Functions#

groupby_unsorted(iterable, key)

Group items like itertools.groupby, but without requiring the input to be sorted.

m4opt.utils.numpy Module#

Utilities for working with Numpy arrays.

Functions#

atmost_1d(a)

Force an array-like object to have no more than 1 dimension.

clump_nonzero(a)

Find intervals of nonzero values in an array, row by row.

clump_nonzero_inclusive(a)

Like clump_nonzero, but return closed rather than half-open intervals.

full_indices(n)

Calculate the indices of all of the elements of a square array.

m4opt.utils.optimization Module#

Miscellaneous optimization utilities.

Functions#

pack_boxes(wh, **kwargs)

Pack non-overlapping hypercubes into the smallest possible hypercube.

partition_graph(graph, n[, recursive, ...])

Partition a graph into contiguous subgraphs.

partition_graph_color(graph, partition, **kwargs)

Find a coloring for a partition of a graph.

solve_tsp(distances, **kwargs)

Solve the Traveling Salesman problem.

m4opt.utils.resource Module#

Functions for monitoring resource usage.

Functions#

get_maxrss_bytes()

Get the memory usage of the current process and all of its children.

m4opt.utils.sympy Module#

Symbolic algebra utilities for SymPy.

Functions#

collect_dependence(expr, symbols)

Collect terms in an expression that depend on like combinations of symbols.