View on GitHub

code-sketches

Code Sketches

A collection of boilerplates, random sketches, and useful stuff.

Bash

Some bash tools to use and learn. For debugging commands use explainshell.

Generate random alphanumeric string

# 16 character alphanumeric string
identifier=$(< /dev/urandom tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1)

Bash tempdir

function cleanup() {
    echo >&2 "CLEANING UP TEMPORARY DIRECTORY ${TEMP_DIR}"
    rm -rf "${TEMP_DIR}"
}

function create_temporary_directory_with_cleanup() {
    TEMP_DIR=$(mktemp --directory "/tmp/temporary_dir".XXXXXXXX)
    export TEMP_DIR
    trap cleanup EXIT
    echo "TEMPORARY WORKING DIRECTORY CREATED AT ${TEMP_DIR}"
}

Multiline conditional

if [[ -n "${_condition:-}" ]] && \
   [[ "${_condition:-}" != *"aaa"* ]] && \
   [[ "${_condition:-}" != *"bbb"* ]]; then
    echo "Checked!"
fi

Read to list from string

# cut at spaces
IFS=" " read -r -a version_list <<< "${VERSION_LIST}"

Iterate on list

for version in "${version_list[@]}"; do
    EXEC_STRING+=" --version ${version}"
done

Python

Python Virtualenv steps

Some String interpolation options in python

logger.warning("Bad expression: %s. No matches found at %s", expression, location)
parameter = "Stuff_and_{version}".format(version=version)
# f-strings in python 3.6
name = "Peter"
age = 21
f"Hello, {name}. You are {age}."

Read file to string

def read_testfile(test_file):
    with test_file.open() as infile:
        test_data = infile.read().splitlines() # json.load(infile)
    return test_data

Basic script boilerplate

import argparse


def parse_arguments():
    parser = argparse.ArgumentParser(description='Default.')
    parser.add_argument('--param', help='an integer for the accumulator')
    return parser.parse_args()


def main():
    args = parse_arguments()
    return args


if __name__ == '__main__':
    main()