Skip to content

Instantly share code, notes, and snippets.

@SealtielFreak
Last active December 30, 2022 21:56
Show Gist options
  • Save SealtielFreak/2c18ce2a82e4ff7faafdbb67c6feddbc to your computer and use it in GitHub Desktop.
Save SealtielFreak/2c18ce2a82e4ff7faafdbb67c6feddbc to your computer and use it in GitHub Desktop.
from typing import Generic, TypeVar, Dict, Protocol, Optional
F = TypeVar("F")
class DefineDerivationFunction(Generic[F]):
def __init__(self, define: str):
self.__index_func: Dict[str, F] = {}
self.current_define = define
def bind(self, define: str):
def inner(func: F):
if define not in self.__index_func:
self.__index_func[define] = func
return func
return inner
def __call__(self, *args, **kwargs):
return self.__index_func[self.current_define](*args, **kwargs)
__CURRENT_PLATFORM__ = 'AVR'
class HelloWorld(Protocol):
def __call__(self) -> None: ...
hello_world: DefineDerivationFunction[HelloWorld] = DefineDerivationFunction(__CURRENT_PLATFORM__)
@hello_world.bind('x86')
def hello_world_x86():
print("Hello world from x86")
@hello_world.bind('AVR')
def hello_world_x64():
print("Hello world from AVR")
@hello_world.bind('ARM')
def hello_world_arm():
print("Hello world from ARM")
hello_world()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment