Last active
July 26, 2022 15:50
-
-
Save darvil82/97e6a5d44591348b086dd1c175422e3b to your computer and use it in GitHub Desktop.
Finally non cringe entry points!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# utils.py | |
from sys import argv | |
from typing import Any, Callable | |
def entry_point( | |
func: Callable[[list[str]], Any] | Callable[[], Any] | |
) -> Callable[[], Any]: | |
new_func = lambda: func(argv) if func.__code__.co_argcount == 1 else func() # type: ignore | |
if func.__module__ == "__main__": | |
new_func() | |
return new_func | |
# runner.py | |
from utils import entry_point | |
@entry_point | |
def main(argv: list[str]): | |
print(argv) | |
@entry_point | |
def main2(): | |
print("nice") | |
# `if __name__ == "__main__": ...`? ew... ugly |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment