Skip to content

Instantly share code, notes, and snippets.

View bmritz's full-sized avatar

Brian Ritz bmritz

  • Graham Allen Partners
  • South Bend, Indiana
  • 06:14 (UTC -04:00)
View GitHub Profile
REQUIRED := python3.11 curl
$(foreach bin,$(REQUIRED),\
$(if $(shell command -v $(bin) 2> /dev/null),,$(error Please install `$(bin)`)))
REPOSITORY_ROOT := $(PWD)
.poetry/bin/poetry: ## install poetry
@curl -sSL https://install.python-poetry.org | POETRY_HOME=$(REPOSITORY_ROOT)/$(shell basename $(dir $(abspath $(dir $(@))))) python3.11 - --version=1.4.2
@bmritz
bmritz / env.py
Last active August 3, 2023 13:16
Define and read env vars
"""
All environment variable access should be performed via this module.
- allows tracing of where variables are used
- provide sensible defaults
- reduce string typos
Declare env vars below with default values.
"""
import os
import sys
"""Logging for the project."""
import json
import logging
class JsonFormatter(logging.Formatter):
"""Formatter that outputs JSON strings after parsing the LogRecord.
Notes:
.PHONY: help
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
help: ## Print this help message
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(firstword $(MAKEFILE_LIST)) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
help: ## Show this help
@bmritz
bmritz / docker-images-by-size.sh
Created March 17, 2023 15:09
List docker images sorted by size
# https://tunzor.github.io/posts/docker-list-images-by-size/
docker image ls --format "{{.Repository}}:{{.Tag}} {{.Size}}" | \
awk '{if ($2~/GB/) print substr($2, 1, length($2)-2) * 1000 "MB - " $1 ; else print $2 " - " $1 }' | \
sed '/^0/d' | \
sort -n
@bmritz
bmritz / Makefile.terraform
Last active March 23, 2023 19:11
make bin/terraform
UNAME_S := $(shell uname -s)
UNAME_M := $(shell uname -m)
ifeq ($(UNAME_S),Darwin)
OS := darwin
else
OS := linux
endif
ifeq ($(UNAME_M),x86_64)
bin/cue:
$(eval TMP := $(shell mktemp -d))
$(eval TARFILE := cue.tar.gz)
if [[ $$OSTYPE == 'darwin'* ]]; then wget 'https://github.com/cue-lang/cue/releases/download/v0.5.0-alpha.1/cue_v0.5.0-alpha.1_darwin_amd64.tar.gz' -O $(TMP)/$(TARFILE); fi
@tar -xf $(TMP)/$(TARFILE) -C $(@D) cue*
rm -rf "$(TMP)"
@echo "Installed $@"
@bmritz
bmritz / smart_sample.py
Last active July 20, 2021 00:45
A way to sample a population to desired characteristics
import pandas as pd
import math, random, copy
CASEID = 'respid'
PATH = '/Users/bridgetlittleton/Box Sync/WWB/Survey Data - CONFIDENTIAL/WWB_Main_Survey_Data_20210629.xls'
profpos_mapping = {'Research scientist or similar [INDIA/UK/USA]': 'j',
'Masters / Ph.D. student [ITALY]': 'g',
'Masters / Ph.D. student [INDIA/UK/USA]': 'g',
'Assistant Professor / Lecturer [INDIA/UK/USA]': 'j',
@bmritz
bmritz / multiassert.py
Created September 18, 2020 15:11
Make several assertions and surface meaningful AssertionError messages for every one that fails
import inspect
import re
regex = 'assert_true[(]((.|\s)*)[)]'
class MultiAssert:
def __init__(self):
self.checks = []
@staticmethod
@bmritz
bmritz / stream.py
Created August 24, 2017 20:21 — forked from vgoklani/stream.py
Twitter Streaming API example
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import json
import oauth2 # https://raw.github.com/brosner/python-oauth2/master/oauth2/__init__.py
from pprint import pprint
import urllib2
stream_url = "https://stream.twitter.com/1/statuses/filter.json"