Skip to content

Instantly share code, notes, and snippets.

View bbengfort's full-sized avatar
🎯
Focusing

Benjamin Bengfort bbengfort

🎯
Focusing
View GitHub Profile
@bbengfort
bbengfort / cleangcr.py
Last active February 12, 2023 19:22
A helper script that uses gcloud commands to cleanup old GCR images and reduce storage costs. By default it keeps any images tagged with a semver label and automatically removes any images with no tags. Users can specify a minimum number of images to keep and a grace period to allow images to stay for a specific time period.
#!/usr/bin/env python3
# Uses gcloud commands to cleanup old GCR images and reduce storage costs.
import re
import json
import argparse
import subprocess
from datetime import datetime, timedelta, timezone
@bbengfort
bbengfort / publisher.go
Created October 4, 2022 16:45
A quick ensign publisher routine.
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"time"
api "github.com/rotationalio/ensign/pkg/api/v1beta1"
@bbengfort
bbengfort / counter.go
Created September 19, 2022 15:19
Atomic vs Mutex Counter Benchmark
package counter
import (
"sync"
"sync/atomic"
)
type Counter interface {
Inc()
Load() uint64
@bbengfort
bbengfort / merge.go
Created August 24, 2022 20:35
Merging a struct from another struct using reflection in Go. https://go.dev/play/p/m4dXDahCgyW?v=goprev
// Merging a struct from another struct using reflection.
package main
import (
"encoding/json"
"errors"
"fmt"
"reflect"
)
@bbengfort
bbengfort / editor.go
Last active January 10, 2022 18:42
Edit files from a Go program by calling a CLI editor like vim with exec.
/*
Wrapper for a command line editor to edit files.
*/
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
@bbengfort
bbengfort / ca.go
Created December 30, 2020 20:11
Implements a simple CLI certificate authority for self-signed certs
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
@bbengfort
bbengfort / wide_argparse.py
Created November 3, 2020 14:38
Create a wider argument parser for better options on --help
def main(args):
pass
def make_wide(formatter, width=120, max_help_position=42):
"""
Increase space between arguments and help text, if possible.
See: https://stackoverflow.com/questions/5462873/
"""
try:
@bbengfort
bbengfort / zipr.py
Last active December 5, 2021 01:34
Dealing with Zip archives and json data in Python
#!/usr/bin/env python3
import os
import json
import random
import zipfile
config = {
"color": "red",
"amount": 42.24,
@bbengfort
bbengfort / ani.py
Last active October 6, 2023 03:08
Experiments with live animation using asyncio and writing to a file. Works with two different processes, but only data generator works in asyncio, not the animation itself.
import json
import random
import asyncio
import argparse
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from functools import partial
@bbengfort
bbengfort / requires
Last active March 3, 2021 15:06
Automatically manage requirements.txt with Python
#!/usr/bin/env python
# requires
# Creates a requirements.txt file using pip freeze.
#
# Author: Benjamin Bengfort <[email protected]>
# Created: Fri Jan 22 08:50:31 2016 -0500
#
# Copyright (C) 2016 Bengfort.com
# For license information, see LICENSE.txt
#