Skip to content

Instantly share code, notes, and snippets.

View alexpearce's full-sized avatar

Alex Pearwin alexpearce

View GitHub Profile
function tssh --description="SSH to a machine using its Tailscale IP." --argument-names=target_host
# Find the `tailscale` executable
set cmd '/Applications/Tailscale.app/Contents/MacOS/Tailscale'
if not test -x $cmd
set cmd (which tailscale)
if test $status != 0
set_color red
printf "ERROR "
set_color normal
echo "Could not find Tailscale binary" >&2
@alexpearce
alexpearce / token_bucket.py
Created February 27, 2023 11:07
An asyncio implementation of the token bucket rate-limiting algorithm.
"""Token bucket algorithm implementation and example application/consumer."""
import argparse
import asyncio
import datetime
import random
from loguru import logger
class TokenBucket:
@alexpearce
alexpearce / celeryev_consumer.py
Created August 4, 2022 16:23
Real-time consumer of Celery's event queue.
import pika
#: Name of the Celery events exchange
EXCHANGE_NAME = "celeryev"
#: Arbitrary name for the transient queue this script will create and consume from
QUEUE_NAME = "pikatest"
#: Global to hold our channel object in
channel = None
# Step #2
@alexpearce
alexpearce / app.py
Created June 17, 2022 03:08
FastAPI application demonstrating proper Bearer token usage.
import typing as t
from fastapi import Depends, FastAPI, Header, HTTPException
from fastapi.security.http import HTTPAuthorizationCredentials, HTTPBearer
from pydantic import BaseModel
from starlette import status
app = FastAPI()
# Placeholder for a database containing valid token values
@alexpearce
alexpearce / db.py
Created October 2, 2018 07:31
Snakemake remote file support for a Python dictionary backed by a shelve database.
import shelve
import string
import time
class MeasurementDB(object):
r"""Database for storing measurements, with export to LaTeX macros.
Example usage:
@alexpearce
alexpearce / gosu-entrypoint.sh
Created July 14, 2017 10:12
A entrypoint for a Docker container that can run commands as the user running Docker
#!/bin/bash
# Run commands in the Docker container with a particular UID and GID.
# The idea is to run the container like
# docker run -i \
# -v `pwd`:/work \
# -e LOCAL_USER_ID=`id -u $USER` \
# -e LOCAL_GROUP_ID=`id -g $USER` \
# image-name bash
# where the -e flags pass the env vars to the container, which are read by this script.
# By setting copying this script to the container and setting it to the
@alexpearce
alexpearce / bcolz.ipynb
Created March 22, 2017 07:36
bcolz experiments
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@alexpearce
alexpearce / nb.ipynb
Last active January 30, 2017 16:54
Lil' notebook.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@alexpearce
alexpearce / splot_with_roofit.py
Created December 21, 2016 13:42
Ue RooFit PDFs with hep_ml.splot
def pdf_probabilities(xs, dependents, yields, pdfs):
"""Return an array of normalised PDF probabilities.
For a list of N yields, each y_i, and a list of N PDFs f_i, the probability
for the ith PDF at the (possibly vector) point x is defined as
p_{i} = \frac{N_{i}f_{i}}{\sum_{i}^{N} N_{i}f_{i}}
Keyword arguments:
xs -- List of values at which to evaluate the PDFs
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.