Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import typing
- from sys import _getframe
- class Enum(int):
- __slots__ = []
- """type: typing.FrozenSet[str]"""
- names2values = dict() # TODO: make it FrozenBimap
- """type: typing.Dict[str, int]"""
- values2names = dict()
- """type: typing.Dict[int, str]"""
- _counter = -1
- def __new__(cls, value):
- cls._counter = value
- return int.__new__(cls, int(value))
- enumcls_t = typing.Type[Enum]
- def _no_new_instances(*args):
- raise Exception("no new instances permitted")
- def _lock_enum_type(cls: typing.Type[Enum]):
- cls.__new__ = _no_new_instances
- def begin_enum(cls: typing.Type[Enum]):
- cls._snap_before = frozenset(_getframe(1).f_globals.keys())
- def end_enum(cls: typing.Type[Enum]):
- cls_globals = _getframe(1).f_globals
- names = frozenset(cls_globals.keys()) - cls._snap_before
- del cls._snap_before
- cls.names2values = {name: cls_globals[name] for name in names}
- cls.values2names = {cls_globals[name]: name for name in names}
- _lock_enum_type(cls)
- def next(cls: typing.Type[Enum]):
- return cls(cls._counter + 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement