Skip to content

synt.file ¤

File ¤

Abstract file containing arbitrary python code.

Examples:

file = File(
    id_("print").expr().call(litstr("Hello, World!")).stmt(),
    id_("x").expr().assign(litint(42)),
    if_(id_("x").expr().eq(litint(42))).block(
        id_("print").expr().call(litstr("x is 42")).stmt()
    )
)
assert file.into_str() == '''print('Hello, World!')
x = 42
if x == 42:
    print('x is 42')'''
Source code in synt/file.py
class File:
    r"""Abstract file containing arbitrary python code.

    Examples:
        ```python
        file = File(
            id_("print").expr().call(litstr("Hello, World!")).stmt(),
            id_("x").expr().assign(litint(42)),
            if_(id_("x").expr().eq(litint(42))).block(
                id_("print").expr().call(litstr("x is 42")).stmt()
            )
        )
        assert file.into_str() == '''print('Hello, World!')
        x = 42
        if x == 42:
            print('x is 42')'''
        ```
    """

    body: Block
    """Code lines in the file."""

    def __init__(self, *statements: Statement):
        """Initialize a file.

        Args:
            statements: Statements to include in the file.
        """
        self.body = Block(*statements)

    def into_str(self, indent_atom: str = "    ", indent_width: int = 0) -> str:
        """Convert the file into a string.

        Args:
            indent_width: number of `indent_atom`s per indentation level.
            indent_atom: string to use for indentation. E.g. `\\t`, whitespace, etc.
        """
        return self.body.indented(indent_width, indent_atom)

body instance-attribute ¤

body: Block = Block(*statements)

Code lines in the file.

__init__ ¤

__init__(*statements: Statement)

Initialize a file.

Parameters:

Name Type Description Default
statements Statement

Statements to include in the file.

()
Source code in synt/file.py
def __init__(self, *statements: Statement):
    """Initialize a file.

    Args:
        statements: Statements to include in the file.
    """
    self.body = Block(*statements)

into_str ¤

1
2
3
into_str(
    indent_atom: str = "    ", indent_width: int = 0
) -> str

Convert the file into a string.

Parameters:

Name Type Description Default
indent_width int

number of indent_atoms per indentation level.

0
indent_atom str

string to use for indentation. E.g. \t, whitespace, etc.

' '
Source code in synt/file.py
def into_str(self, indent_atom: str = "    ", indent_width: int = 0) -> str:
    """Convert the file into a string.

    Args:
        indent_width: number of `indent_atom`s per indentation level.
        indent_atom: string to use for indentation. E.g. `\\t`, whitespace, etc.
    """
    return self.body.indented(indent_width, indent_atom)