Skip to main content

No project description provided

Project description

lazy-main

Generalized framework for main loop function.

Installation

pip install lazy-main

How to Use

from lazy_main import LazyMain

def main(*args, **kwargs):
    print("Hello World!")

def error_handler(exception):
    print("An error occurred!", exception)

if __name__ == "__main__":
    LazyMain(
        main=main,
        error_handler=error_handler, # This is optional.
        sleep_min=3,
        sleep_max=5,
        print_logs=True,
        loop_count=-1, # -1 Means it will loop infinitely.
    ).run()

Some aliases are also provided for loop_count.

...

LazyMain(
    ...
    # Sets `loop_count` to 1 if `True`.
    # Sets `loop_count` to -1 if `False`.
    # Does nothing if `None`.
    run_once=True,
    ...
)


# Or...

LazyMain(
    ...
    # Sets `loop_count` to -1 if `True`.
    # Sets `loop_count` to 1 if `False`.
    # Does nothing if `None`.
    run_forever=True,
    ...
)

...

You can also pass arguments to the main function.

from lazy_main import LazyMain

def main(*args, **kwargs):
    print(kwargs["hello"]) # World!

if __name__ == "__main__":
    LazyMain(
        main=main,
    ).run(
        hello="World!",
    )

Returning True will print the total elapsed time.

from lazy_main import LazyMain

def main():
    return True

if __name__ == "__main__":
    LazyMain(
        main=main,
    ).run() # Done in 0.10s.

If you don't like the logs, you can disable it.

from lazy_main import LazyMain

...

if __name__ == "__main__":
    LazyMain(
        ...
        print_logs=False,
        ...
    ).run()

...

Returning SIGTERM will terminate the loop.

from lazy_main import LazyMain
import signal

def main():
    return signal.SIGTERM

if __name__ == "__main__":
    LazyMain(
        main=main,
    ).run()

    print("I'm free!")

You can also use a generator for the return value.

from lazy_main import LazyMain
import signal

def main():
    for i in range(10):
        if i == 5:
            yield signal.SIGTERM

if __name__ == "__main__":
    LazyMain(
        main=main,
    ).run()

    print("I'm free!")

You can also provide dynamic kwargs via iteration.

from lazy_main import LazyMain

def main(*args, **kwargs):
    print(kwargs["hello"]) # 0, 1, 2, 3, ...

if __name__ == "__main__":
    i = 0
    
    for loop in LazyMain(
        main=main,
    ):
        loop(
            hello=i,
        )

        i += 1

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

lazy_main-0.6.0.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

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

lazy_main-0.6.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file lazy_main-0.6.0.tar.gz.

File metadata

  • Download URL: lazy_main-0.6.0.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for lazy_main-0.6.0.tar.gz
Algorithm Hash digest
SHA256 9e1fc3e4b5c8528e62aadbbd0e5e839a6d403accb012ef1e485b87215ab19c47
MD5 973f9b0aaa68b3bdba4d9ee6888acd33
BLAKE2b-256 d075faf540f5622bb61d3c35f1cd9b9965dfc1fcd3a2160d60287849e9190e58

See more details on using hashes here.

File details

Details for the file lazy_main-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: lazy_main-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for lazy_main-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3aed9013a5f715a5cda36a525db9d99de6f9a3b2266a33d24e274aca80113a30
MD5 d075205e30e9e1e5c64f6808f4b77988
BLAKE2b-256 575dac464c4f7a1a676a4442b2a7377420a355a6215e86177c0a451c32ba4774

See more details on using hashes here.

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