Skip to content
Back to top
View this page

# `classenumtype`

Module for ClassEnumType and ClassEnumTypeBase.

## `class_enum_type`

Submodule for `ClassEnumType`.

class lihzahrd.terraria.data.classenumtype.class_enum_type.ClassEnumType(name: str, bases: tuple[type, ...], namespace: dict[str, Any], *, register: bool = True, **kwargs: Any)

  Abstract metaclass that defines methods in common to all ClassEnum.

  static __new__(classenumtype: type[Self], name: str, bases: tuple[type, ...], namespace: dict[str, Any], *, register: bool = True, **kwargs: Any) → Self

    Initialize a new class based on this metaclass, possibly registering it as ClassMember.

    Calls `_register()` if `register` is `True`.

    The following positional arguments are automatically passed on class creation, but are documented here for completeness.

    Parameters:

      - name – The name of the class to create.

      - bases – The classes from which the class will inherit.

      - namespace – The namespace (dictionary mapping variable names to values) that the class will have once created.

    The following keyword arguments have to be manually provided after the `metaclass=...` expression.

    Parameters:

      register – Whether the new class should be registered as ClassMember or not. Useful to define some ClassMemberBase.

    Example

    This would create a new enumeration:

        class MyEnum(ClassEnumType):
            ...

    This would register a variant on the created enumeration:

        class MyVariant(metaclass=MyEnum):
            ...

    This would create a class from which enumeration variants can inherit from, but which is not registered on the enumeration itself:

        class MyBaseVariant(metaclass=MyEnum, register=False):
            ...

  abstractmethod classmethod register(classenum: Self, **kwargs) → None

    Register a new ClassMember with the given identifier.

    Called after a ClassMember with `register=True` is created.

    Parameters:

      - classenum – The ClassMember to register.

      - kwargs – Keyword arguments specified as metaclass keyword parameters, excluding ones captured by `__init__()`, like `register`.

## `class_enum_type_dict`

Submodule for `ClassEnumTypeDict`.

class lihzahrd.terraria.data.classenumtype.class_enum_type_dict.ClassEnumTypeDict(name: str, bases: tuple[type, ...], namespace: dict[str, Any], *, register: bool = True, **kwargs: Any)

  A kind of `ClassEnumType` which uses metaclass kwargs to index the created class instances in `dict`, and sets them as attributes on the created class instance.

  Inheritors MUST set `INDEXES` to a new `collections.defaultdict`, so that they have their own unique indexes.

  Example

      >>> class MyEnum(ClassEnumTypeDict):
      ...     INDEXES = defaultdict(dict)
      ...
      >>> class MyClass(ClassEnumTypeDict, ID=123, NAME="My Class"):
      ...     ID: int
      ...     NAME: str
      ...     UNINDEXED_PROPERTY: str = "Hello world!"
      ...
      >>> assert MyClass.ID == 123
      >>> assert MyClass.NAME == "My Class"
      >>> assert MyClass.UNINDEXED_PROPERTY == "Hello world!"
      >>> assert MyEnum.INDEXES["ID"][123] == MyClass
      >>> assert MyEnum.INDEXES["NAME"]["My Class"] == MyClass
      >>> assert "Hello world!" not in MyEnum.INDEXES["UNINDEXED_PROPERTY"]

  INDEXES: defaultdict[str, dict[Any, Self]]

    `defaultdict` mapping index names to their respective indexes.