skrypt is a Python utility library designed to make writing robust, configurable, and well-logged scripts easier. It provides tools for:
- Command-line argument parsing with automatic config file support
- Flexible output redirection (stdout/stderr to files)
- Verbose, indented, and context-aware printing
- Easy config file management and updating
- Skrypt Context Manager: Wrap your script's main logic in a context manager that handles argument parsing, config loading, output redirection, and verbosity.
- Config File Handling: Load and update Python config files at runtime, with support for merging multiple configs.
- Verbose Printing: Use the
VerboseIndentationPrint
class orviprint
function for indented, context-rich output, including function names and timestamps. - Output Redirection: Redirect stdout and stderr to files with automatic file naming and directory management.
- Banner Printing: Print visually distinct banners for script start/end or section headers.
You can install skrypt locally for development:
pip install -e .
Or add it to your pyproject.toml
as a dependency.
import argparse
from skrypt import skrypt
def main_function(arg1, arg2, vip, **kwargs):
vip("Running main function")
# Your logic here
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('arg1', type=str)
parser.add_argument('arg2', type=str)
with skrypt.Skrypt(main=main_function, parser=parser) as skr:
skr.run()
Create a config file, e.g. default_config.py
:
foo = 'bar'
threshold = 0.5
Pass it to Skrypt:
with skrypt.Skrypt(main=main_function, parser=parser, config_file_path="default_config.py") as skr:
skr.run(pass_config_as='class') # or 'dict', or 'kwargs'
You can override config values at runtime with additional config files.
Enable output redirection with -R
:
python myscript.py arg1 arg2 -R
This will create myscript.out
and myscript.err
files with stdout and stderr content.
Use the vip
argument in your main function for indented, context-aware prints:
def main(arg1, vip, **kwargs):
vip("This is a verbose message")
vip("Another message", importance=1)
Or use the standalone function:
from skrypt.vip import viprint
viprint("Quick verbose print")
See examples/vip.py
for a demonstration of verbose printing and examples/load_config.py
for config file handling.
The example examples/touch.py
shows some exemplatory output of skrypt.
~$python3 examples/touch.py -vvv -R hello.txt
~$ ls
hello.txt
touch.err
touch.out
~$ cat touch.err
~$ cat touch.out
╔═════════╗
║ touch ║
╚═════════╝
Skrypt: call from CMD-line: ['examples/touch.py', '-vvv', '-R', 'hello.txt']
Skrypt: for the lazy copy-pasters: examples/touch.py -vvv -R hello.txt
Skrypt: with args: Namespace(path='hello.txt', directory=PosixPath('/home/some/path'), log_file=None)
Skrypt: left over kwargs (neither consumed by Skrypt nor the named args of the main function `touch` ):
log_file: None
# autorun
Skrypt: now starting to run the main function touch:
[10:15:15, touch] directory=PosixPath('/home/some/path')
[10:15:15, touch] _path=PosixPath('/home/some/path/hello.txt')
[10:15:15, touch] create new file
# manual run
directory=PosixPath('/home/some/path')
_path=PosixPath('/home/some/path/hello.txt')
just set the timestamp
Skrypt: ... exiting
╔════════════════════╗
║ done with: touch ║
╚════════════════════╝
Depending on your script's options, skrypt can produce up to three output files:
name.out
: Captures stdout (if-R
is used)name.err
: Captures stderr (if-R
is used)name.log
: Log file (if logging is enabled)
Context manager for script execution. Handles argument parsing, config loading, output redirection, and verbosity.
Class for loading and updating Python config files.
Class for verbose, indented, and context-aware printing.
Standalone function for quick verbose prints.
Context manager for redirecting stdout and stderr to files.
- Source code:
src/skrypt
- Example scripts:
examples
- To build:
python setup.py install
MIT License (see LICENSE)
- Inspired by the need for reproducible, well-logged, and configurable scientific scripts.
- Uses standard Python libraries and minimal dependencies.
- This README was partially generated by an AI.