This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import daemon | |
import os | |
import time | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--daemon', action='store_true') | |
args = parser.parse_args() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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]}) | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import logging | |
import os | |
import re | |
import shlex | |
import signal | |
import sys | |
import subprocess |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Foo Docstring | |
""" | |
import argparse | |
import logging | |
from configparser import ConfigParser | |
from typing import Dict, Sequence | |
PROGRAM_NAME = 'foo' | |
log = logging.getLogger(PROGRAM_NAME) |