Skip to content

Instantly share code, notes, and snippets.

View alexpearce's full-sized avatar

Alex Pearwin alexpearce

View GitHub Profile
@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
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 / 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 / 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 / __init__.py
Created October 30, 2014 16:05
A Flask application configured to accept Shibboleth SSO headers to authenticate users.
from datetime import datetime
from flask import Flask, session, redirect
from flask_sso import SSO
def get_user_session_info(key):
return session['user'].get(
key,
'Key `{0}` not found in user session info'.format(key)
@alexpearce
alexpearce / ssotutorial.apacheconf
Last active October 21, 2021 02:31
Apache configuration file for a virtual host running Flask behind a uWSGI server, authentication with Shibboleth SSO
# Apache server configuration for ssotutorial.
# This sets up a Flask application over SSL with CERN SSO authentication via
# Shibboleth.
# Load the SSL and Shibboleth modules
LoadModule ssl_module modules/mod_ssl.so
LoadModule mod_shib /usr/lib64/shibboleth/mod_shib_22.so
# Disable TRACE HTTP requests on CERN advice
TraceEnable Off
@alexpearce
alexpearce / test_ttreeformula.py
Created January 19, 2015 08:48
TTreeFormula example
import ROOT
# Replace with location to your favourite ntuple
f = ROOT.TFile((
'/Users/apearce/Physics/Data/CharmProduction2010/Reprocessed'
'/DVntuple.2010.12.MagUp.root'
), 'read')
t = f.Get('TupleD0Tohh/DecayTree')
h1 = ROOT.TH1F('h1', 'h1', 170, 1780, 1950)
h2 = ROOT.TH1F('h2', 'h2', 170, 1780, 1950)
@alexpearce
alexpearce / simul.py
Created February 6, 2015 16:20
Simultaneous fit
import ROOT as R
from ROOT import RooFit as RF
# 'Unblind' or 'Blind'
# BLINDING = 'Blind'
BLINDING = 'Unblind'
BLIND_STR = '6LJgnoyA4Zr95gec'
def roovar_value(rv):
'''Return a pretty-printed central value and uncertainty of rv.
@alexpearce
alexpearce / offset_text.py
Last active November 5, 2020 07:03
Nice offset label formatting in matplotlib. See [the blog post](https://alexpearce.me/2014/04/exponent-label-in-matplotlib/) for more.
import numpy as np
from matplotlib import pyplot as plt
fig = plt.figure(figsize=(5, 4))
# Generate some data
mu, sigma = 1e7, 1e6
s = np.random.normal(mu, sigma, 10000)
# Plot it
plt.hist(s, 30, histtype='step')
# Format it