Skip to content

Instantly share code, notes, and snippets.

View flyte's full-sized avatar
🎯
Focusing

Ellis Percival flyte

🎯
Focusing
  • London
View GitHub Profile
@flyte
flyte / result_nursery.py
Last active March 14, 2021 17:15
Add a results property to a nursery, containing the return values of the functions which have been started (and finished).
from functools import wraps
import trio
def set_result(results, key, func):
@wraps(func)
async def wrapper(*args, **kwargs):
results[key] = await func(*args, **kwargs)
@flyte
flyte / change_charset.py
Created November 26, 2020 13:09
Change MySQL/MariaDB character set and collation to something sane
from django.db import connection
DB_NAME = "your-db-name"
cursor = connection.cursor()
tables = connection.introspection.table_names()
cursor.execute(f"ALTER DATABASE `{DB_NAME}` CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci")
for table in tables:
cursor.execute(f"ALTER TABLE {table} CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci")
@flyte
flyte / scan_barcodes.py
Last active July 19, 2017 10:30
Scan barcodes in an image using zbar, cv2 and PIL/Pillow.
import zbar
import cv2
from PIL import Image
def scan_barcodes(img):
"""
Scan barcodes in image and return their data.
:param img: Source image
:type img: PIL.Image
@flyte
flyte / percentage_change.py
Created September 7, 2016 16:01
Percentage Change
from decimal import Decimal
def percentage_change(old_value, new_value):
"""
Accepts two integers, an old and a new number,
and then measures the percent change between them.
"""
change = abs(new_value - old_value)
try:
pct_change = (change / Decimal(old_value))
@flyte
flyte / install_mitmproxy.sh
Created August 11, 2016 12:17
Instructions for installing mitmproxy on a Raspberry Pi using Raspbian.
# Install jessie-backports APT repository and OpenSSL 1.0.2:
echo "deb http://ftp.debian.org/debian jessie-backports main" | sudo tee /etc/apt/sources.list.d/jessie-backports.list
gpg --keyserver pgpkeys.mit.edu --recv-key 8B48AD6246925553
gpg -a --export 8B48AD6246925553 | sudo apt-key add -
gpg --keyserver pgpkeys.mit.edu --recv-key 7638D0442B90D010
gpg -a --export 7638D0442B90D010 | sudo apt-key add -
sudo apt-get update
sudo apt-get -yt jessie-backports install libssl-dev openssl
@flyte
flyte / install-eth-drivers.sh
Created January 19, 2016 10:18
Build the drivers for Atheros AR8161, install them, bring the interface up and get an IP address.
#!/bin/sh
# ejp/imagination/20.12.2012: install-eth-drivers.sh
# Build the ethernet drivers for Atheros AR8161,
# install them, bring the interface up and get
# an IP address.
set -e
# Get the password before the user gets a cup of tea
@flyte
flyte / hits.sh
Created January 19, 2016 10:16
Parse Apache logs to calculate number of hits.
#!/bin/bash
cd /var/log/apache2
i=`date +%d/%b/%Y`
j=`cat other_vhosts_access.log | grep “$i” | grep "GET / HTTP/1." | wc -l`
echo "No. of Apache Access Count:$j"
@flyte
flyte / snoopback.py
Created January 19, 2016 09:54
Port scan a host and save the results to a CouchDB document.
from datetime import datetime
from collections import OrderedDict
import couchdbkit as cdb
import argparse
import nmap
import sys
db = None
p = argparse.ArgumentParser()
@flyte
flyte / hat.py
Last active January 6, 2016 17:14
Sorting hat for VGOC Premiershit 2016. Save as hat.py and run with "python hat.py".
#!/usr/bin/env python
from random import shuffle
from itertools import izip_longest
POOL_NAMES = ("Pool 1", "Pool 2")
if __name__ == "__main__":
@flyte
flyte / log_function_call.py
Created November 3, 2015 12:32
Function decorator to output the function name and arguments to logger.debug
def log_function_call(func):
"""
Function decorator to output the function name and arguments to logger.debug
"""
def inner(*args, **kwargs):
args_str = ", ".join([str(x) for x in args]) if args else ""
kwargs_str = ", ".join(
["%s=%s" % (str(key), str(value)) for key, value in kwargs.items()]) if kwargs else ""
all_args_str = args_str
if all_args_str and kwargs_str: