Skip to content

Instantly share code, notes, and snippets.

@i-am-unknown-81514525
Last active June 2, 2025 12:01
Show Gist options
  • Select an option

  • Save i-am-unknown-81514525/447cc247ff824ec9cde6edb9256dce0f to your computer and use it in GitHub Desktop.

Select an option

Save i-am-unknown-81514525/447cc247ff824ec9cde6edb9256dce0f to your computer and use it in GitHub Desktop.
Rust enum?
import builtins
import io
import sys
import types
from ctypes import py_object
#from forbiddenfruit import curse
from typing import Never, Type, Optional, Any, Callable
class __MISSING_BASE(type):...
class MISSING(__MISSING_BASE, metaclass=__MISSING_BASE):
def __new__(cls, *args, **kwargs):
return cls
def __init__(self, *args, **kwargs):...
class Undefined:
def __init__(self, name: str):
self._name = name
self._value = MISSING()
def __undefined__(self) -> Never:
raise NameError(f'name \'{self._name}\' is undefined')
def __del__(self):
if not isinstance(self._value, MISSING):
globals()[self._name] = self._value
__add__ = __undefined__
__radd__ = __undefined__
__mul__ = __undefined__
__rmul__ = __undefined__
__sub__ = __undefined__
__rsub__ = __undefined__
__getitem__ = __undefined__
__setitem__ = __undefined__
__delitem__ = __undefined__
__str__ = __undefined__
__int__ = __undefined__
__bool__ = __undefined__
#def writer(self: io.TextIOWrapper, content):
# self.write(content)
# return self
#def reader(self: io.TextIOWrapper, val: Undefined):
# name = val._name
# content = self.readline()
# val._value = content
# if name in locals():
# locals()[name] = content
# elif name in globals():
# globals()[name] = content
# return content
#sys.ENDL = '\n'
class globols(dict):
__slots__ = ()
#def __getitem__(self, key: str):
# fn = self.__class__.globols.__getitem__
# del self.__class__.globols.__getitem__
# value = super().__getitem__(key)
# try:
# if isinstance(value, Undefined):
# value.__undefined__()
# finally:
# self.__class__.globols.__getitem__ = fn
# return value
def __missing__(_, x, builtins=__builtins__) -> Any:
try:
return builtins.__dict__[x]
except KeyError:
return Undefined(x)
globols.globols = globols
py_object.from_address(id(globals())+tuple.__itemsize__).value = globols
#curse(io.TextIOWrapper, '__lshift__', writer)
#curse(io.TextIOWrapper, '__rshift__', reader)
#curse(str, '__lshift__', lambda x, y: x+y)
#sys.stdin >> b
#sys.stdout << b << sys.ENDL
class _RsValueAlike:
def __init__(self, inner_cls, v):
self.inner_cls = inner_cls
inner_cls.__init__(self, *v)
self.values = v
class RsEnum:
value: Any
def __init_subclass__(cls):
ori_init = cls.__init__
def override_init(self, *args, **kwargs):
if self == cls:
return
return ori_init(self, *args, **kwargs)
cls.__init__ = override_init
def __new__(cls, *v):
cls.__is_enum_cls = True
for value in v:
if isinstance(value, Undefined):
cls.size = len(v)
return cls
obj = _RsValueAlike(cls, v)
return obj
def __init__(self, *v):
self.value = v
class Some(RsEnum):
def __init__(self, value):
self.value = (value,)
class Two(RsEnum):
def __init__(self, left, right):
self.value = (left, right)
class NoneEnum(RsEnum):...
_ = Undefined("_")
class Case:
def __init__(self, match_by: dict[RsEnum, Callable]):
self.match_by = match_by
def match(self, val):
if not isinstance(val, _RsValueAlike) and not val is None:
raise ValueError("Not enum")
for k, v in self.match_by.items():
if val is None:
if k is None:
return v()
else:
continue
elif val.inner_cls == k or (isinstance(k, _RsValueAlike) and val.inner_cls == k.inner_cls):
return v(*val.value)
elif (isinstance(k, Undefined) and k._name == "_") or k == _:
return v()
raise ValueError("No match")
case = Case(
{
Some(V): lambda V: 1,
None: lambda: 2,
_: lambda: 0
}
)
print(case.match(Some(1)))
print(case.match(Two(1, 2)))
print(case.match(None))
case = Case(
{
Some(V): lambda V: 1,
None: lambda: 2,
Two(L, R): lambda L, R: f"{L} {R}",
_: lambda: 0
}
)
print(case.match(Two(1, 2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment