Skip to content
/ skrypt Public

A Python utility library that streamlines the creation of robust, configurable, and well-logged scripts.

License

Notifications You must be signed in to change notification settings

alexksr/skrypt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

skrypt

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

Features

  • 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 or viprint 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.

Installation

You can install skrypt locally for development:

pip install -e .

Or add it to your pyproject.toml as a dependency.


Usage

1. Basic Script Structure

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()

2. Config File Support

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.

3. Output Redirection

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.

4. Verbose Printing

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")

Example

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  ║
╚════════════════════╝

Output Files

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)

API Reference

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.


Development


License

MIT License (see LICENSE)


Acknowledgements

  • Inspired by the need for reproducible, well-logged, and configurable scientific scripts.
  • Uses standard Python libraries and minimal dependencies.

AI

  • This README was partially generated by an AI.

About

A Python utility library that streamlines the creation of robust, configurable, and well-logged scripts.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages