Skip to content

Instantly share code, notes, and snippets.

class Box(str):
def __new__(cls, s):
lines = s.split("\n")
width = max(len(line) for line in lines)
top_bottom = '-' * width
return super().__new__(cls, box_template.format(
middle="\n".join(f"| {line:{width}} |" for line in lines)))
def __add__(self, other):
alexmojaki /
Last active January 2, 2022 19:58
Mini xkcd password generator
#!/usr/bin/env python
import random
from urllib.request import urlopen
lines = urlopen(
words = list({
alexmojaki /
Created August 26, 2018 22:12
Track a value throughout a program
import sys
from bdb import Bdb
from traceback import print_stack
class Tracker(Bdb):
Debugger which looks for a given value by identity in local variables of all frames
it comes across. When it finds it and the given condition function called with the value
is true, prints out a stack trace and stops tracing.
def attr_to_arg(func):
This is stupid and I love it.
Basically instead of calling:
foo("thing", stuff)
You can call:
alexmojaki /
Created August 28, 2017 12:25
Analyses logs data copied from kibana to reconstruct the history of a single key storing JSON on S3
This script analyses logs data copied from kibana to reconstruct
the history of a single key storing JSON on S3.
It prints out the first value of the key, followed by coloured diffs
between consecutive values. To use the script:
Open kibana
Open Network tab in Chrome console
Search for the following in kibana:
"'Key': 'key_here'"
alexmojaki /
Created July 8, 2016 09:46
List all ECS task definitions not in use (i.e. not belonging to a task running in any cluster)
import re
import boto3
client = boto3.client('ecs', region_name='us-east-1')
used_tds = {re.match(r'arn:aws:ecs:us-east-1:667583086810:task-definition/([\w-]+):\d+$',
for c in client.list_clusters()['clusterArns']
for s in
client.describe_services(cluster=c, services=client.list_services(cluster=c)['serviceArns'])['services']
alexmojaki /
Created May 26, 2016 18:39
Simple Zalgo text generator in Python where the Zalgoness increases with each word.
from random import choice
marks = map(unichr, range(768, 879))
string = 'clear the current exception in between your catch and the bare raise OH GOD NO EVENTLET IT COMES'
words = string.split()
print(' '.join(''.join(c + ''.join(choice(marks)
for _ in range(i // 2 + 1)
) * c.isalnum()
for c in word)
for i, word in enumerate(words)))
alexmojaki /
Created November 23, 2015 13:13
Clean out old SQS queues
This script iterates through all your SQS queues (based on your default
AWS credentials), shows you a few helpful attributes, and asks if you want
to delete it. It's meant as an easy way to clean out old queues that are
no longer needed.
Requires boto3 (pip install boto3).
The names of queues are derived from their URLs which are assumed to have
a common prefix: replace the 12345 in the URL below. If you don't know what