Decorator which runs the tagged function if the current module is being run as a script. No more `if __name__ == "__main__"` madness.
Project description
@python_main
@python_main is a decorator which:
- Automatically calls the function(s) tagged with it, if the current module is being executed as a script.
- Does nothing if the current module is being imported.
It is, essentially, equivalent to the if __name__ == "__main__": construct, but as a decorator.
That's all it does.
Installation
pip install python-main # or
poetry add python-main # ...
Usage
from python_main import python_main
A = 10
B = 20
@python_main
def do_print():
"""This will run if this module is executed."""
print(A + B)
You can also tag multiple functions with @python_main and they will all run if the module is executed, in the order they are defined.
from python_main import python_main
A = 10
B = 20
C = 0
@python_main
def add_a_to_c():
global C
C += A
# ... other functions/ definitions ...
@python_main
def add_b_to_c():
global C
C += B
# At this point:
# - C will be 30 if this module is executed as a script.
# - C will be untouched if this module is imported.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_main-2.0.0.tar.gz.
File metadata
- Download URL: python_main-2.0.0.tar.gz
- Upload date:
- Size: 1.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f38764d52bac3b4edd21b78528144b0660acc0924c275e97f9a33418765b1ba
|
|
| MD5 |
d9446a311e259a5e20531e4f71f82711
|
|
| BLAKE2b-256 |
511b1a75c9e76e7eaffb1f6eacc6c3902335a79809b5994b13babed97e2bf70d
|
Provenance
The following attestation bundles were made for python_main-2.0.0.tar.gz:
Publisher:
publish.yml on flipbit03/main
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_main-2.0.0.tar.gz -
Subject digest:
7f38764d52bac3b4edd21b78528144b0660acc0924c275e97f9a33418765b1ba - Sigstore transparency entry: 153986587
- Sigstore integration time:
-
Permalink:
flipbit03/main@ebc2cf1cb49e5e44e544b3bfb64ea54a85c13ef0 -
Branch / Tag:
refs/tags/v2.0.0 - Owner: https://github.com/flipbit03
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ebc2cf1cb49e5e44e544b3bfb64ea54a85c13ef0 -
Trigger Event:
release
-
Statement type:
File details
Details for the file python_main-2.0.0-py3-none-any.whl.
File metadata
- Download URL: python_main-2.0.0-py3-none-any.whl
- Upload date:
- Size: 2.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5727b2825f19f9a154936fc41a780f65caf6312518face47bfb890345c07ae52
|
|
| MD5 |
d89ba4f4ad45469c12c66c82b975f1c4
|
|
| BLAKE2b-256 |
413bdfaf6ebf2d09f14489f04ab47fc1f23bc7b75d11a55a54679ca251af01e9
|
Provenance
The following attestation bundles were made for python_main-2.0.0-py3-none-any.whl:
Publisher:
publish.yml on flipbit03/main
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_main-2.0.0-py3-none-any.whl -
Subject digest:
5727b2825f19f9a154936fc41a780f65caf6312518face47bfb890345c07ae52 - Sigstore transparency entry: 153986588
- Sigstore integration time:
-
Permalink:
flipbit03/main@ebc2cf1cb49e5e44e544b3bfb64ea54a85c13ef0 -
Branch / Tag:
refs/tags/v2.0.0 - Owner: https://github.com/flipbit03
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ebc2cf1cb49e5e44e544b3bfb64ea54a85c13ef0 -
Trigger Event:
release
-
Statement type: