Skip to main content

Helper module for creating simple Python 3 scripts

Project description

scripthelper

Helper module for simple command line Python scripts.

The documentation with inline examples can be found in pypi.

Basic usage

example1.py:

#!/usr/bin/env python3
import scripthelper

logger = scripthelper.bootstrap()

logger.critical("critical message")
logger.error("error message")
logger.warning("warning message")
logger.info("info message")
logger.verbose("verbose message")
logger.debug("debug message")
logger.spam("spam message")

It just works. Try --verbose and --quiet command line options, too. It uses colored log messages on a terminal. See --help for more information.

Adding other command line parameters

example2.py:

#!/usr/bin/env python3
import scripthelper

scripthelper.add_argument("-n", "--name", help="Name to greet")
logger, args = scripthelper.bootstrap_args()

if args.name:
    logger.debug("Name was provided")
    logger.info(f"Hello {args.name}")
else:
    logger.warning("Name was not provided")

Or:

example2b.py:

#!/usr/bin/env python3
import scripthelper

scripthelper.add_argument("--name", default="World")
logger = scripthelper.bootstrap()
name = scripthelper.args.name

logger.info(f"Hello {name}")

Progressbar works with logging, too

example3.py:

#!/usr/bin/env python3
import scripthelper
import time

logger = scripthelper.bootstrap()

logger.info("Doing the calculations...")
for i in scripthelper.progressbar(range(200)):
    if i % 20 == 0:
        logger.verbose(f"Iteration {i}")
    if i % 5 == 0:
        logger.debug(f"Iteration {i}")
    logger.spam(f"Iteration {i}")
    time.sleep(0.00001)
logger.info("Done")

Extended log levels can be used in modules

example4.py:

#!/usr/bin/env python3
import scripthelper
import example4module

scripthelper.bootstrap()
example4module.do_the_things()

example4module.py:

#!/usr/bin/env python3
import scripthelper

logger = scripthelper.getLogger(__name__)


def do_the_things():
    logger.verbose("Calling logger.verbose raises an exception if it does not work.")
    logger.info("Hello from a module.")

You can easily preserve logs in files

example5.py:

#!/usr/bin/env python3
import scripthelper

logger = scripthelper.bootstrap()
scripthelper.setup_file_logging()

logger.critical("critical message")
logger.error("error message")
logger.warning("warning message")
logger.info("info message")
logger.verbose("verbose message")
logger.debug("debug message")
logger.spam("spam message")

It handles exceptions, warnings

example6.py:

#!/usr/bin/env python3
import scripthelper
import warnings

scripthelper.bootstrap()


def uncaught_exception_test():
    this_variable = "will be displayed in stack trace"
    as_well_as = "the other variables"
    raise RuntimeError("This exception should be handled.")


warnings.warn("This user warning will be captured.")
uncaught_exception_test()

(The local variables will be displayed in stack trace.)

Has built-in colored pretty printer

example7.py:

#!/usr/bin/env python3
import scripthelper
from dataclasses import dataclass

scripthelper.bootstrap()

@dataclass
class Item:
    name: str
    value: int

something = {
    "string": "value1",
    "bool": True,
    "none": None,
    "integer": 1234,
    "item": Item("name", 999)
}

scripthelper.pp(something)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for scripthelper, version 21.2.1
Filename, size File type Python version Upload date Hashes
Filename, size scripthelper-21.2.1-py3-none-any.whl (5.8 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size scripthelper-21.2.1.tar.gz (4.8 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page