machetli.tools

This module is derived from tools.py of Lab (<https://lab.readthedocs.io>). Functions and classes that are not needed for this project were removed.

machetli.tools.batched(iterable, n)[source]

Batch data into tuples of length n. The last batch may be shorter.

Example

batched('ABCDEFG', 3) # --> ABC DEF G
machetli.tools.configure_logging(level=20)[source]

Set up internal loggers to only print messages at least as important as the given log level.Warnings and error messages will be printed on stderr, and critical messages will terminate the program. All messages will be prefixed with the current time.

machetli.tools.get_python_executable()[source]

Get path to the main Python executable.

machetli.tools.get_script_dir()[source]

Get absolute directory of the main script.

machetli.tools.get_script_path()[source]

Get absolute path to main script, or the current working directory, if the Python session is interactive.

machetli.tools.parse(content, pattern, type=<class 'int'>)[source]

Look for matches of pattern in content. If any matches are found, the first group present in the regular expression is cast as type and returned.

Example

content = '''
Runtime: 23.5s
Heuristic value: 42
Search successful
'''
t = parse(content, r"Runtime: (\.+)s", float)
h = parse(content, r"Heuristic value: (\d+)", int)
machetli.tools.read_state(file_path: Union[pathlib.Path, str])[source]

Use pickle to read a state from disk.

machetli.tools.run(command, *, cpu_time_limit=None, memory_limit=None, core_dump_limit=0, input_filename=None, stdout_filename=None, stderr_filename=None, **kwargs)[source]

This function is a wrapper for the run function of the Python subprocess module (see subprocess). It is meant as a convenience to ease common use cases of Machetli. A majority of the keyword parameters of subprocess.run are supported with the following changes: - capture_output is disallowed since we always capture output. - input, stdout, and stderr are replaced with input_filename,

stdout_filename, and stderr_filename, respectively. They expect a string or None as input rather than a file or anything else. If they are set to None, the output is sent to subprocess.PIPE instead of written to files.

Parameters
  • command

    A list of strings defining the command to execute. For details, see the Python module subprocess.

  • cpu_time_limit – Time in seconds after which the command is terminated. Because states are evaluated in sequence in Machetli, it is important to use resource limits to make sure a command eventually terminates. There also is the parameter timeout from subprocess.run which is a wallclock time limit and generates an exception whereas we terminate after the program has been killed by the system. Instead of passing an integer, the time limit can also be passed as a string containing an integer and a suffix s (seconds), m (minutes), or h (hours).

  • memory_limit – Memory limit in MiB to use for executing the command. Alternatively, a string containing an integer and a suffix K (KiB), M (MiB), or G (GiB).

  • core_dump_limit – Limit in MiB of data written to disk in case the executed command crashes. By default we allow no core dump.

  • input_filename – In case the process takes input on stdin, you can pass a path to a file here that will be piped to stdin of the process. With the default value of None, nothing is passed to stdin.

  • stdout_filename – Redirect output to stdout to be written to the file of the given name.

  • stderr_filename – Redirect output to stderr to be written to the file of the given name.

machetli.tools.write_state(state, file_path: Union[pathlib.Path, str])[source]

Use pickle to write a given state to disk.