Skip to content

Instantly share code, notes, and snippets.

@ryanrichholt
ryanrichholt / daemon.py
Created April 6, 2020 20:14
Python script with optional daemon mode
import argparse
import daemon
import os
import time
parser = argparse.ArgumentParser()
parser.add_argument('--daemon', action='store_true')
args = parser.parse_args()
@ryanrichholt
ryanrichholt / configaction.py
Last active April 19, 2019 16:17
Arbitrary configuration through command-line args with argparse actions (Python)
"""Action subclass for argparse that allows an arbitrary configuration object
to be built from command-line args.
Example command line for a script using this feature:
python3 your_script.py -c int:answer.foo.bar 42 -c something.another other -c json:supported '[123,456,789]'
Resulting namespace:
Namespace(banana={'answer': {'foo': {'bar': 42}}, 'something': {'another': 'other'}, 'supported': [123, 456, 789]})
"""
@ryanrichholt
ryanrichholt / bestargs.sh
Last active February 18, 2020 19:38
Squeaky clean pattern for Bash script with positional arguments
#!/bin/bash
HELP="bestargs.sh <arg1> <arg2> [ <arg3> ...]
Bash script positional arguments can be handled
with simple, one-line-per-argument statements. No
ifs, whiles, exits, or getopts required.
This makes use of some lesser-known parameter expansion
options in bash, :, ?, :-. The examples below are just a
@ryanrichholt
ryanrichholt / is_multiline.py
Last active February 20, 2019 19:38
Quickly check if a string contains multiple lines
"""Comparison of multiple ways to test if a string includes multiple lines
For all strings tested, my function was faster than some common alternatives. It was
much faster (5-14x) for long strings with many newlines:
Trial 1 - is_multiline1 w/nl - 0.34784991503693163
Trial 2 - is_multiline1 nonl - 0.32012753101298586
Trial 3 - len(s.splitlines()) w/nl - 5.211611718987115
Trial 4 - len(s.splitlines()) nonl - 0.9361785550136119
Trial 5 - s.count('\n') w/nl - 0.925270272011403
Trial 6 - s.count('\n') nonl - 0.9191283410182223
@ryanrichholt
ryanrichholt / java_wrapper.py
Created February 20, 2019 19:21
Python wrapper script for launching Java jar files
#!/usr/bin/env python
import argparse
import logging
import os
import re
import shlex
import signal
import sys
import subprocess
"""Open file and peek at it's contents before reading.
This should help with sniffing file contents"""
import io
import sys
filename = sys.argv[1]
# The io module includes a BufferedReader class that
# has a peek method. Now, sniffing data from stdin
# won't consume the bytes in the process.
import asyncio
from asyncio import create_subprocess_exec
# Spawning subprocesses asynchronously lets your code
# continue while waiting on the process to start.
status = 'new'
async def log():
"""Log the current status as fast as we can"""
@ryanrichholt
ryanrichholt / cli.py
Last active June 14, 2022 13:18
Python command line script boilerplate
"""
Foo Docstring
"""
import argparse
import logging
from configparser import ConfigParser
from typing import Dict, Sequence
PROGRAM_NAME = 'foo'
log = logging.getLogger(PROGRAM_NAME)