pybnb.misc¶
Miscellaneous utilities used for development.
Copyright by Gabriel A. Hackebeil (gabe.hackebeil@gmail.com).
-
class
pybnb.misc.
MPI_InterruptHandler
(handler, disable=False)[source]¶ A context manager for temporarily assigning a handler to SIGINT and SIGUSR1, depending on the availability of these signals in the current OS.
-
pybnb.misc.
metric_format
(num, unit='s', digits=1, align_unit=False)[source]¶ Format and scale output with metric prefixes.
Example
>>> metric_format(0) '0.0 s' >>> metric_format(0, align_unit=True) '0.0 s ' >>> metric_format(0.002, unit='B') '2.0 mB' >>> metric_format(2001, unit='B') '2.0 KB' >>> metric_format(2001, unit='B', digits=3) '2.001 KB'
-
pybnb.misc.
time_format
(num, digits=1, align_unit=False)[source]¶ Format and scale output according to standard time units.
Example
>>> time_format(0) '0.0 s' >>> time_format(0, align_unit=True) '0.0 s ' >>> time_format(0.002) '2.0 ms' >>> time_format(2001) '33.4 m' >>> time_format(2001, digits=3) '33.350 m'
-
pybnb.misc.
get_gap_labels
(gap, key='gap', format='f')[source]¶ Get format strings with enough size and precision to print a given gap tolerance.
-
pybnb.misc.
as_stream
(stream, mode='w', **kwds)[source]¶ A utility for handling function arguments that can be a filename or a file object. This function is meant to be used in the context of a with statement.
Parameters: - stream (file-like object or string) – An existing file-like object or the name of a file to open.
- mode (string) – Assigned to the mode keyword of the built-in
function
open
when the stream argument is a filename. (default: “w”) - **kwds – Additional keywords passed to the built-in function
open
when the stream argument is a filename.
Returns: A file-like object that can be written to. If the input argument was originally an open file, a dummy context will wrap the file object so that it will not be closed upon exit of the with block.
Return type: file-like object
Example
>>> import tempfile >>> with tempfile.NamedTemporaryFile() as f: ... # pass a file ... with as_stream(f) as g: ... assert g is f ... assert not f.closed ... f.close() ... # pass a filename ... with as_stream(f.name) as g: ... assert not g.closed ... assert g.closed
-
pybnb.misc.
get_default_args
(func)[source]¶ Get the default arguments for a function as a dictionary mapping argument name to default value.
Example
>>> def f(a, b=None): ... pass >>> get_default_args(f) {'b': None}
-
pybnb.misc.
get_keyword_docs
(doc)[source]¶ Parses a numpy-style docstring to summarize information in the ‘Parameters’ section into a dictionary.
-
pybnb.misc.
get_simple_logger
(filename=None, stream=None, console=True, level=20, formatter=None)[source]¶ Creates a logging object configured to write to any combination of a file, a stream, and the console, or hide all output.
Parameters: - filename (string, optional) – The name of a file to write to. (default: None)
- stream (file-like object, optional) – A file-like object to write to. (default: None)
- console (bool, optional) – If True, the logger will be configured to print output to the console through stdout and stderr. (default: True)
- level (int, optional) – The logging level to use. (default:
logging.INFO
) - formatter (
logging.Formatter
, optional) – The logging formatter to use. (default: None)
Returns: A logging object
Return type: logging.Logger
-
pybnb.misc.
create_command_line_solver
(problem, parser=None)[source]¶ Convert a given problem implementation to a command-line example by exposing the
pybnb.solver.solve()
function arguments using argparse.