API Reference

The following section outlines the API of bftools.

Core Utilities

These provide the main functionality of bftools.

BrainfuckTools Class

class bftools.BrainfuckTools(array_size: int = 30000, int_size: typing_extensions.Literal[8, 16, 32, 64] = 8)

The BrainfuckTools class is a wrapper for the compiler, decoder and encoder methods.

It comes with some tools to make it easier to use, such as caching the last compiled code, the last decoded code and the last encoded text.

last_compiled

The last compiled code.

Type:

Optional[CompiledBrainfuck]

last_decoded

The last decoded code.

Type:

Optional[DecodedBrainfuck]

last_encoded

The last encoded text.

Type:

Optional[EncodedBrainfuck]

property array_size: int

The array size.

property int_size: typing_extensions.Literal[8, 16, 32, 64]

The integer size.

compile(code: str, minify: Optional[bool] = None) CompiledBrainfuck

Compiles a brainfuck code into python code.

Parameters:
  • code (str) – The brainfuck code to compile.

  • minify (Optional[bool]) – Whether to minify the code or not.

Returns:

The compiled code.

Return type:

CompiledBrainfuck

decode(value: str) DecodedBrainfuck

Decodes brainfuck code into text.

Parameters:

value (str) – The brainfuck code to decode.

Returns:

The decoded code.

Return type:

DecodedBrainfuck

encode(value: str) EncodedBrainfuck

Encodes text into brainfuck code.

Parameters:

value (str) – The text to encode.

Returns:

The encoded text.

Return type:

EncodedBrainfuck

Shortcut Functions

bftools.compile_bf(code: str, minify: Optional[bool] = None) CompiledBrainfuck

Shortcut for BrainfuckTools.compile().

This is equivalent to BrainfuckTools().compile(code).

Parameters:

code (str) – The brainfuck code to compile.

Returns:

The compiled code.

Return type:

CompiledBrainfuck

bftools.decode_bf(code: str) DecodedBrainfuck

Shortcut for BrainfuckTools.decode().

This is equivalent to BrainfuckTools().decode(code).

Parameters:

code (str) – The brainfuck code to decode.

Returns:

The decoded text.

Return type:

DecodedBrainfuck

bftools.encode_text(value: str) EncodedBrainfuck

Shortcut for BrainfuckTools.encode().

This is equivalent to BrainfuckTools().encode(text).

Parameters:

value (str) – The text to encode.

Returns:

The encoded text.

Return type:

EncodedBrainfuck

Converted Classes

These classes are returned by various methods from the Core Utilities. You shouldn’t need to create these directly or use any of their methods.

class bftools.CompiledBrainfuck(array_size: int = 30000, int_size: typing_extensions.Literal[8, 16, 32, 64] = 8)

An object to represent python compiled from Brainfuck.

To receive the decoded text, use result or str(DecodedBrainfuck).

Warning

This class is not intended to be instantiated directly. Use compile_bf() or BrainfuckTools.compile() instead.

result

The result text. This will never be None unless parse() has not been called. Since the library always calls parse() before returning the object, this should never happen unless you override the functionality of the library.

Type:

Optional[str]

property raw_parsed: Optional[Tuple[Symbol, ...]]

Raw parsed code.

This will never be None unless parse() has not been called. Since the library always calls parse() before returning the object, this should never happen unless you override the functionality of the library.

Note

This is meant to be used internally and you should not need to use it.

Returns:

The raw parsed code.

Return type:

Optional[Tuple[Symbol]]

parse(value: str, minify: Optional[bool] = None) None

Parse the given code.

Note

You should not need to use this method. It is intended for internal use only, so you should only need to use it if you override the functionality of the library. This method is not dangerous like DecodedBrainfuck.parse() is.

Parameters:
  • value (str) – The code to parse.

  • minify (Optional[bool]) – Whether to minify the code. If None, this will be determined by whether python_minifier is installed.

class bftools.DecodedBrainfuck

An object to represent text decoded from Brainfuck.

To receive the decoded text, use result or str(DecodedBrainfuck).

Warning

This class is not intended to be instantiated directly. Use decode_bf() or BrainfuckTools.decode() instead.

result

The result text. This will never be None unless parse() has not been called. Since the library always calls parse() before returning the object, this should never happen unless you override the functionality of the library.

Type:

Optional[str]

parse(value: str) None

Parse the given code.

Note

You should not need to use this method. It is intended for internal use only, so you should only need to use it if you override the functionality of the library. See the warning below for more information.

Warning

This method uses the exec() function. It is therefore not safe to use this method with untrusted code. The library uses this internally and will ensure that user input is not directly passed to this method, unless you override the functionality of the library. If you invoke this method directly, you need to ensure that the code you pass is safe.

Parameters:

value (str) – The code to parse.

Raises:

SyntaxError – If the code is not syntactically correct.

class bftools.EncodedBrainfuck(array_size: int = 30000, int_size: typing_extensions.Literal[8, 16, 32, 64] = 8)

An object to represent text encoded into Brainfuck.

To receive the encoded Brainfuck, use result or str(EncodedBrainfuck).

Warning

This class is not intended to be instantiated directly. Use encode_text() or BrainfuckTools.encode() instead.

result

The result text. This will never be None unless parse() has not been called. Since the library always calls parse() before returning the object, this should never happen unless you override the functionality of the library.

Type:

Optional[str]

parse(value: str) None

Parse the given text.

Note

You should not need to use this method. It is intended for internal use only, so you should only need to use it if you override the functionality of the library. This method is not dangerous like DecodedBrainfuck.parse() is.

Parameters:

value (str) – The text to parse.

Tools

These are tools that are used internally by the Core Utilities.

bftools.factor(number: int) Tuple[int, int]

Factors number into 2 numbers, a and b, such that a + b is as low as possible.

Parameters:

number (int) – The number to factor

Returns:

A Tuple of 2 integers that factor into number.

Return type:

Tuple[int, int]

bftools.factor_recursive(number: int) Tuple[int, ...]

Factors number into an undetermined amount of numbers, such that the accumulative sum of those numbers is as low as possible.

Parameters:

number (int) – The number to factor

Returns:

A Tuple of integers that factor into number.

Return type:

Tuple[int, …]

bftools.factor_optimized(number: int, int_size: typing_extensions.Literal[8, 16, 32, 64]) Tuple[int, ...]

An optimized factoring algorithm that factors number into the best combination of numbers to be compiled into brainfuck loops.

This function leverages integer overflow to further shorten the code.

Parameters:
  • number (int) – The number to factor

  • int_size (IntegerSize) – The size of the integer. This is used to optimize the factorization with integer overflow.

Returns:

A Tuple of integers that factor into number.

Return type:

Tuple[int, …]