Skip to content

Instantly share code, notes, and snippets.

View earonesty's full-sized avatar
🎹
Piano

earonesty

🎹
Piano
View GitHub Profile
import threading
class SafeWrap():
"""
Wrap an unsafe sdk (like onedrivesdk, for example) in a thread lock.
Derive from this if you want to translate exceptions, or change the
type set
"""
__safe_types = (str, int, float, type(None), bool, bytes, type(iter(bytes())), type(iter(str())))
#!/usr/bin/env python
'''Delete local branches which have been merged via GitHub PRs.
Usage (from the root of your GitHub repo):
delete-squahsed-branches [--dry-run]
If you specify --dry-run, it will only print out the branches that would be
deleted.
@earonesty
earonesty / fun.py
Last active January 6, 2021 16:46
Python's 'if' statement gets privileged information from conditionals
# python 'if' statement is priviliged to the boolean output of compound conditionals
# other statements are not, and have to double-evaluate the output
# unless the output is "the last one"
class X:
def __init__(self):
self.v = False
self.c = 0
def __bool__(self):
import fastjsonschema as fjs
from urllib import parse as urlparse
from hashlib import md5
_schema_dir = os.path.dirname(__file__)
_file_loader = {'file': lambda f: json.loads(open(_schema_dir + "/" + urlparse.urlsplit(f).path.lstrip('/')).read())}
def gen_var_name(schema, step="format"):
x=schema.copy()
@earonesty
earonesty / scramble.py
Created September 27, 2019 19:48
randomize a generator stream within a fixed window
import random
def scramble(gen, buffer_size):
buf = []
i = iter(gen)
while True:
try:
e = next(i)
buf.append(e)
if len(buf) >= buffer_size:
@earonesty
earonesty / fakefile.py
Created September 19, 2019 18:31
Arbitrarily large fake filelike python
class FakeFile:
def __init__(self, size, repeat=b'0'):
self.loc = 0
self.size = size
self.repeat = repeat
self.closed = False
def fileno(self):
raise OSError()
@earonesty
earonesty / strict.py
Created September 12, 2019 13:15
strict decorator for python classes
#pylint: disable=protected-access
import inspect, itertools, functools
class StrictError(TypeError):
pass
def strict(cls):
cls._x_frozen = False
cls._x_setter = getattr(cls, "__setattr__", object.__setattr__)
@earonesty
earonesty / muxer.py
Last active August 6, 2019 14:07
Python Generator Multiplexter
import queue
from threading import Lock
from collections import namedtuple
class Muxer():
Entry = namedtuple('Entry', 'genref listeners, lock')
already = {}
top_lock = Lock()
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers import ( Cipher, algorithms, modes )
import os
nonce = os.urandom(16)
key = os.urandom(32)
mode = modes.GCM(nonce, None)
@earonesty
earonesty / deadlyexes.py
Created May 7, 2019 22:40
Monitor class for healthz maintenance
import time
import logging
import threading
log = logging.getLogger(__name__)
class MonitorInstance:
def __init__(self, parent, label, func, threshold, active, metric):
self.parent = parent
self.label = label
self.func = func