Skip to main content

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

PyPI - Python Version PyPI - Version

@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

python_main-2.0.0.tar.gz (1.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_main-2.0.0-py3-none-any.whl (2.2 kB view details)

Uploaded Python 3

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

Hashes for python_main-2.0.0.tar.gz
Algorithm Hash digest
SHA256 7f38764d52bac3b4edd21b78528144b0660acc0924c275e97f9a33418765b1ba
MD5 d9446a311e259a5e20531e4f71f82711
BLAKE2b-256 511b1a75c9e76e7eaffb1f6eacc6c3902335a79809b5994b13babed97e2bf70d

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_main-2.0.0.tar.gz:

Publisher: publish.yml on flipbit03/main

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for python_main-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5727b2825f19f9a154936fc41a780f65caf6312518face47bfb890345c07ae52
MD5 d89ba4f4ad45469c12c66c82b975f1c4
BLAKE2b-256 413bdfaf6ebf2d09f14489f04ab47fc1f23bc7b75d11a55a54679ca251af01e9

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_main-2.0.0-py3-none-any.whl:

Publisher: publish.yml on flipbit03/main

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page