Skip to content
Back to top
View this page

# `lihzahrd._generate`

Utilities for code generation.

Used, for example, to convert Terraria static game data into Python-native code structures.

Warning

Requires the `generate` development dependency group to be installed.

Note

The reason why this module exists is to avoid having a `bs4` dependency on the final package.

## `class_name_factory`

class lihzahrd._generate.class_name_factory.ClassNameFactory

  A converter from strings to class names which ensures name uniqueness and validity.

  RE_UNWANTED = re.compile('[^a-zA-Z0-9]')

    Finds characters that are unwanted in class names for a given string.

    Example

        >>> RE_UNWANTED.sub("", r"Jack-o'-Lantern")
        'JackoLantern'

  RE_NUMBER = re.compile('^([0-9])')

    Checks if a string starts with a number.

    Example

        >>> RE_NUMBER.sub("N", r"67Statue")
        'N67Statue'

  used: set[str]

    Set of all the class names that were already generated by `__call__()`.

  __call__(base: str, *suffixes: str) → str

    Generate a class name from the given string.

    1. First, unwanted characters are removed using `RE_UNWANTED`

    2. Then, if `RE_NUMBER` detects that the name starts with a number, a `N` is prefixed to the name.

    3. Then, `used` is checked to see if the name does not already exist, and if it does, each of the `*suffixes` are tried in order, followed by a counter starting at 2, until a unique name is found.

    Example

        >>> factory = ClassNameFactory()
        >>> factory
        <ClassNameFactory (0 generated)>
        >>> factory("Jack-o'-Lantern")
        'JackoLantern'
        >>> factory("Jack-o'-Lantern")
        'JackoLantern2'
        >>> factory("Jack-o'-Lantern", "Block")
        'JackoLanternBlock'
        >>> factory("Jack-o'-Lantern", "Block")
        'JackoLantern3'
        >>> factory.used
        {'JackoLantern', 'JackoLantern2', 'JackoLanternBlock', 'JackoLantern3'}

## `terraria`

Module for generating Terraria data.

### `tiles_generator`

class lihzahrd._generate.terraria.tiles_generator.TilesGenerator(soup: BeautifulSoup)

  Class that reads a `tiles.xml` file from TerraMap and outputs Python code using `lihzahrd` structures to represent it.

  generate_blocks() → str

    Generate the new contents of `lihzahrd.terraria.data.classmembers.blocks`.

    Returns:

      The contents, as a `str`.

  generate_items() → str

    Generate the new contents of `lihzahrd.terraria.data.classmembers.items`.

    Returns:

      The contents, as a `str`.

  generate_npcs() → str

    Generate the new contents of `lihzahrd.terraria.data.classmembers.npcs`.

    Returns:

      The contents, as a `str`.

  generate_prefixes() → str

    Generate the new contents of `lihzahrd.terraria.data.classmembers.prefixes`.

    Returns:

      The contents, as a `str`.

  generate_walls() → str

    Generate the new contents of `lihzahrd.terraria.data.classmembers.walls`.

    Returns:

      The contents, as a `str`.