boofun.core.io

File I/O for Boolean functions.

Supports multiple formats: - JSON: Full representation with metadata - .bf: Scott Aaronson’s Boolean Function Wizard format - DIMACS CNF: Standard SAT solver format

Usage:

from boofun.core.io import load, save

# Load from file (format auto-detected) f = load(“function.json”) f = load(“function.bf”) f = load(“function.cnf”)

# Save to file save(f, “output.json”) save(f, “output.bf”) save(f, “output.cnf”)

Functions

detect_format(path)

Detect file format from extension or content.

load(path[, format])

Load a Boolean function from file.

load_bf(path, **kwargs)

Load Boolean function from .bf file (Aaronson format).

load_dimacs_cnf(path, **kwargs)

Load Boolean function from DIMACS CNF file.

load_json(path, **kwargs)

Load Boolean function from JSON file.

save(func, path[, format])

Save a Boolean function to file.

save_bf(func, path[, include_inputs])

Save Boolean function to .bf file (Aaronson format).

save_dimacs_cnf(func, path[, comment])

Save Boolean function to DIMACS CNF file.

save_json(func, path[, representation, pretty])

Save Boolean function to JSON file.

Exceptions

FileIOError(message[, path])

Error during file I/O operations.

boofun.core.io.load(path: str | Path, format: str | None = None, **kwargs) BooleanFunction[source]

Load a Boolean function from file.

Parameters:
  • path – Path to the file

  • format – Optional format override (“json”, “bf”, “dimacs_cnf”)

  • **kwargs – Format-specific options

Returns:

BooleanFunction instance

Raises:
boofun.core.io.save(func: BooleanFunction, path: str | Path, format: str | None = None, **kwargs) None[source]

Save a Boolean function to file.

Parameters:
  • func – BooleanFunction to save

  • path – Destination path

  • format – Optional format override (“json”, “bf”, “dimacs_cnf”)

  • **kwargs – Format-specific options

Raises:

FileIOError – If file cannot be saved

boofun.core.io.load_json(path: str | Path, **kwargs) BooleanFunction[source]

Load Boolean function from JSON file.

JSON format stores the full representation including metadata.

Parameters:

path – Path to JSON file

Returns:

BooleanFunction instance

boofun.core.io.save_json(func: BooleanFunction, path: str | Path, representation: str = 'truth_table', pretty: bool = True, **kwargs) None[source]

Save Boolean function to JSON file.

Parameters:
  • func – BooleanFunction to save

  • path – Destination path

  • representation – Which representation to save

  • pretty – Whether to format with indentation

boofun.core.io.load_bf(path: str | Path, **kwargs) BooleanFunction[source]

Load Boolean function from .bf file (Aaronson format).

Format:

n (number of variables) 00…0 v₀ (optional input string, then output value) 00…1 v₁ … 11…1 v_{2^n-1}

Values can be 0, 1, or -1 (partial function, undefined).

Parameters:

path – Path to .bf file

Returns:

BooleanFunction instance

boofun.core.io.save_bf(func: BooleanFunction, path: str | Path, include_inputs: bool = True, **kwargs) None[source]

Save Boolean function to .bf file (Aaronson format).

Parameters:
  • func – BooleanFunction to save

  • path – Destination path

  • include_inputs – Whether to include input bit strings

boofun.core.io.load_dimacs_cnf(path: str | Path, **kwargs) BooleanFunction[source]

Load Boolean function from DIMACS CNF file.

DIMACS CNF is the standard format for SAT solvers:

c comment line p cnf <n_vars> <n_clauses> 1 -2 3 0 (clause: x1 OR NOT x2 OR x3) -1 2 0 (clause: NOT x1 OR x2) …

Positive integers are positive literals, negative are negated. Each clause ends with 0.

Parameters:

path – Path to .cnf file

Returns:

BooleanFunction with CNF representation

boofun.core.io.save_dimacs_cnf(func: BooleanFunction, path: str | Path, comment: str | None = None, **kwargs) None[source]

Save Boolean function to DIMACS CNF file.

Note: Converts function to CNF if not already in that representation.

Parameters:
  • func – BooleanFunction to save

  • path – Destination path

  • comment – Optional comment to include

boofun.core.io.detect_format(path: str | Path) str[source]

Detect file format from extension or content.

Parameters:

path – Path to the file

Returns:

“json”, “bf”, or “dimacs_cnf”

Return type:

Format string

Raises:

FileIOError – If format cannot be detected