enum

Starlark module for working with enums.

An enumeration is a set of symbolic names (members) bound to unique, constant values. Within an enumeration, the members can be compared by identity, and the enumeration itself can be iterated over.

Because Enums are used to represent constants we recommend using UPPER_CASE names for enum members, and will be using that style in our examples.

enum.Enum(class_name, names_, module=None, qualname=None, type=None, start=1)

Create a new IMMUTABLE Enum class.

Example:

enum.Enum('_Languages', [('Java', 0), ('Python', 1), ('Larky', 2)])

Parameters:

  • class_name - Enum name.

  • names - names can be:

    • A string containing member names, separated either with spaces or commas. Values are incremented by 1 from start.

    • An iterable of member names. Values are incremented by 1 from start.

    • An iterable of (member name, value) pairs.

    • A mapping of member name -> value pairs.

Returns: Larky Enum representation.

enum.enum2(*sequential, **named)

One of the ways to initialize Enum

Example:

_enum = enum.enum2('_TRY', '_EXCEPT', '_ELSE', '_FINALLY', '_BUILD')

Parameters:

  • sequential - a list or tuple that we will use as enum values.

  • named - name/value mapping.

Returns: enum with reversed_mapping for output formatting.

enum.enumify_iterable(iterable, enum_dict, numerator=None)

A hacky function to turn an iterable into a dict with whose keys are the members of the iterable, and value is the index.

If the key is a tuple, it will iterate over the keys and assign the same enumerated position.

A numerator is a callable that takes the enumerated position and returns the expected number in order. For example, numerator=lambda x: x << 2 will map to 1, 2, 4, 8, 16 instead of 1, 2, 3, 4, 5

__ = -1  # Alias for the invalid class
RegexFlags = _enumify_iterable(iterable=[
...             ("I", "IGNORECASE"),
...             ("S", "DOTALL"),
...             ("M", "MULTILINE"),
...             ("U", "UNICODE"),
...             "LONGEST_MATCH",
...             ("A", "ASCII"),
...             "DEBUG",
...             ("L", "LOCALE"),
...             ("X", "VERBOSE"),
...             ("T", "TEMPLATE"),
... ], enum_dict={'__': __}, numerator=lambda x: 1 << x)
assert RegexFlags["I"] == 1
assert RegexFlags["DOTALL"] == RegexFlags["S"] == 2
assert RegexFlags["MULTILINE"] == RegexFlags["M"] == 4

Last updated