Femscript Python API Documentation
The Femscript
Python API allows seamless integration of Femscript code with Python. This API provides tools for parsing, executing, and interacting with Femscript, enabling you to bind Python functions, convert data between Python and Femscript, and handle Femscript tokens and AST (Abstract Syntax Tree).
Classes and Functions
TokenType
- "Str"
- "Int"
- "Bool"
- "None"
- "List"
- "Bytes"
- "Scope"
class Token
: TypedDict
- Structure:
class Variable
: TypedDict
- Structure:
name
: strvalue
: Token
function var
Creates a Femscript variable.
def var(
name: str,
value: Optional[Any] = None,
*,
variables: Optional[list[Variable]] = None
) -> Variable
- Parameters:
name
: str - Name of the variable.value
: Any - Python objectvariables
: [Variable]? - Optional scope for nested variables.
- Returns: Variable
- Example:
class Femscript
The Femscript
class provides the primary interface for parsing and executing Femscript code. It allows defining variables, registering custom Python functions, and running Femscript scripts asynchronously.
class Femscript:
def __init__(
code: Optional[str] = None,
*,
variables: Optional[list[Variable]] = None,
functions: Optional[list[Callable[[str, list[Token], list[Variable]], Token]]] = None
) -> None
- Parameters:
- Example:
classmethod to_fs
Converts a Python object into a Femscript token.
- Parameters:
obj
: Any - Python object to convert.
- Returns: Token
classmethod to_py
Converts a Femscript token into a Python object.
- Parameters:
token
: Token - Femscript token to convert.
- Returns: Any
method add_variable
Adds or updates a variable in the script's scope.
- Parameters:
variable
: Variable - A variable to add or update.
method wrap_function
Wraps a Python function to make it callable from Femscript.
def wrap_function(
self,
func: Optional[Callable[..., object]] = None,
*,
func_name: Optional[str] = None,
with_name: Optional[bool] = False
) -> Callable
- Parameters:
func
: Callable? - The Python function to wrap.func_name
: str? - Name to use for the function in Femscript (default: Python function name)with_name
: bool? - IfTrue
, the function receives its Femscript name as the first argument.
- Returns: Callable
- Example:
async method execute
Executes the parsed AST asynchronously.
- Parameters:
debug
: bool? - IfTrue
, the script execution environment includes theprint
anddebug
built-in functions.
- Returns: Any
- Example:
Examples
-
Basic Execution
-
Binding Python Functions