Skip to content

Instantly share code, notes, and snippets.

@LeoHuckvale
LeoHuckvale / api_request.py
Last active June 8, 2024 11:05
Generic API request class based on Python Requests library
import json
from urllib.parse import urljoin
import logging
import requests
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
@LeoHuckvale
LeoHuckvale / rep
Created January 8, 2016 15:21
Running parallel commands in multiple similar directories
#!/usr/bin/env python
# Command Repeater
#
# For a directory tree like:
#
# MainProject/
# ├── project-one
# ├── project-two
# ├── project-three
# └── rep # This script
@LeoHuckvale
LeoHuckvale / env-context.py
Last active March 20, 2023 11:09 — forked from igniteflow/env-context.py
A Python context manager for setting/unsetting environment variables
"""
Environment variable context manager
------------------------------------
Support utility for managing environments in which e.g. git is run
"""
import os
from contextlib import contextmanager
from functools import wraps
@LeoHuckvale
LeoHuckvale / cSpinner.py
Created October 12, 2015 11:15 — forked from kxtells/cSpinner.py
Rotating Stick Class for Python
import sys
import threading
import time
class cSpinner(threading.Thread):
"""
Print things to one line dynamically
"""
chars = ["\\","|","/","-"]
index = 0
@LeoHuckvale
LeoHuckvale / compile_book.sh
Created February 15, 2015 12:19
Compile a PDF from a directory of JPG images using ghostscript
#! /bin/sh
parallel -j 6 jpg2pdf -- *.JPG
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sOutputFile=combined.pdf `ls -1 *.pdf`
mv combined.pdf combined.pdf.bak
rm *.pdf
foo="`basename "$PWD"`"
@LeoHuckvale
LeoHuckvale / gist:89683dc242f871c8e69b
Created February 2, 2015 16:53
matplotlib - Add subplots dynamically
@LeoHuckvale
LeoHuckvale / quiet.sh
Last active August 29, 2015 14:12
Run a command in shell with stdout and stderr redirected to null
#!/bin/sh
# Run a command with stdout and stderr redirected to null
# Usage:
# $ quiet foo
# Check command is valid
command -v $1 >/dev/null 2>&1 || { echo >&2 "$1: command not found"; exit 1; }
# Run command with all arguments
$@ >/dev/null 2>&1
@LeoHuckvale
LeoHuckvale / bar.py
Created October 16, 2014 10:44
Building a CLI with subparsers from separate modules
def build_subparser(subparsers):
"""
Build subparser for this command
"""
parser = subparsers.add_parser('bar', help='foo help')
parser.add_argument('b', type=int, help='b help')
parser.set_defaults(func=main)
def main(args):
@LeoHuckvale
LeoHuckvale / mjd_join.py
Last active August 29, 2015 14:06
Joining two lists of MJDs (or any two lists of floats) to within some tolerance
def mjd_join(mjd1, mjd2, tol=0.0001):
"""
Return indices of rows in mjd1 and mjd2 matching within tol
"""
diff = abs(mjd1 - mjd2[:, np.newaxis])
match = diff < tol
idx1 = np.mgrid[:len(mjd1), :len(mjd2)][0].T[match]
idx2 = np.mgrid[:len(mjd1), :len(mjd2)][1].T[match]
return idx1, idx2
@LeoHuckvale
LeoHuckvale / multiplotselector.py
Last active August 29, 2015 14:05
Select corresponding points in multiple subplots
import numpy as np
from matplotlib import pyplot as plt
class MultiPlotSelector:
def __init__(self, data, fig, ax):
"""
data in paired columns per subplot, i.e. M x (2 x N)
len(ax) == M