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.5.0.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

lazy_main-0.5.0-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lazy_main-0.5.0.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.8.10 Linux/5.15.0-124-generic

File hashes

Hashes for lazy_main-0.5.0.tar.gz
Algorithm Hash digest
SHA256 7dde8e30829e3f2c77e1673a7ed1834fa9df3a8d1505fa5acf4dbbd453395873
MD5 31524b8cedb3f4d53a62b78bf5aa81a9
BLAKE2b-256 1bfe88b60bfe37d04ae6d51171cc0dd6fc31ff138261a122f49a468dfea7e327

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lazy_main-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.8.10 Linux/5.15.0-124-generic

File hashes

Hashes for lazy_main-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 33c8cc6b1625e24de16f532eb50fe3a9cf0be1669c9b8099fae0786e9965a1d1
MD5 51e59395af15eb32763c65909cdd2e13
BLAKE2b-256 505aad743a4e32440fb3c54eaaae47d96710574bc414e2a3f5e693942310ce95

See more details on using hashes here.

Supported by

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