Skip to content

Batch Runner

bayescalc.batch

This module handles batch mode execution for the Bayesian Network calculator.

Classes

Functions

execute_commands(network, commands)

Executes a list of commands.

Source code in src/bayescalc/batch.py
def execute_commands(network: BayesianNetwork, commands: list[str]):
    """
    Executes a list of commands.
    """
    query_parser = QueryParser(network)
    command_handler = CommandHandler(network)

    for line in commands:
        line = line.strip()
        if not line or line.startswith("#"):
            continue

        print(f">> {line}")
        try:
            if line.lower() == "exit":
                break
            if line.startswith("P("):
                result = query_parser.parse_and_execute(line)
                for assignment, prob in result.probabilities.items():
                    print(f"  P({', '.join(assignment)}) = {prob:.6f}")
            else:
                result = command_handler.execute(line)
                print(result)
        except (ValueError, SyntaxError, KeyError) as e:
            print(f"Error processing command '{line}': {e}", file=sys.stderr)
        finally:
            print("-" * 20)

run_batch(network, commands_file)

Executes a list of commands from a file in batch mode.

Source code in src/bayescalc/batch.py
def run_batch(network: BayesianNetwork, commands_file: str):
    """
    Executes a list of commands from a file in batch mode.
    """
    try:
        with open(commands_file, "r") as f:
            commands = f.readlines()
            execute_commands(network, commands)

    except FileNotFoundError:
        print(f"Error: Commands file not found at '{commands_file}'", file=sys.stderr)