-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simpler module detection using
f.__module__
(#4)
* Simpler detection * Fix tests * Also ensure that the function is available in the module, if not called
- Loading branch information
Showing
3 changed files
with
43 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "python-main" | ||
version = "1.0.1" | ||
version = "1.0.2" | ||
homepage = "https://github.com/flipbit03/main" | ||
description = "Decorator which runs the tagged function if the current module is being run as a script. No more `if __name__ == \"__main__\"` madness." | ||
authors = ["Cadu <[email protected]>"] | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
import builtins | ||
import inspect | ||
from typing import Callable, Optional | ||
|
||
__RAN_AS_SCRIPT_MODULE = "__main__" | ||
__CALLABLE_MODULE_PROP = "__module__" | ||
|
||
def main(f: Callable[[], Optional[int]]) -> None: | ||
curr_frame = inspect.currentframe() | ||
assert curr_frame is not None | ||
assert curr_frame.f_back is not None | ||
|
||
upper_frame = curr_frame.f_back | ||
if upper_frame.f_locals["__name__"] == "__main__": | ||
builtins.exit(f() or 0) | ||
def main(f: Callable[[], Optional[int]]) -> Callable: | ||
if getattr(f, __CALLABLE_MODULE_PROP) == __RAN_AS_SCRIPT_MODULE: | ||
f() | ||
return f |
This file contains 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