Skip to content

Instantly share code, notes, and snippets.



Block or report user

Report or block earonesty

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
earonesty /
Last active Nov 20, 2019
NamedTemporaryFile drop in replacement that deletes on gc, not close(), and supports mode=None
import os, tempfile, gc
class TemporaryFile:
def __init__(self, name, io, delete): = name
self.__io = io
self.__delete = delete
def __getattr__(self, k):
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())))
View delete-squashed-branches
#!/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
earonesty /
Last active Oct 16, 2019
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):
View mofnop_operations.json
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"):
earonesty /
Created Sep 27, 2019
randomize a generator stream within a fixed window
import random
def scramble(gen, buffer_size):
buf = []
i = iter(gen)
while True:
e = next(i)
if len(buf) >= buffer_size:
earonesty /
Created Sep 19, 2019
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 /
Created Sep 12, 2019
strict decorator for python classes
#pylint: disable=protected-access
import inspect, itertools, functools
class StrictError(TypeError):
def strict(cls):
cls._x_frozen = False
cls._x_setter = getattr(cls, "__setattr__", object.__setattr__)
earonesty /
Last active Aug 6, 2019
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)
You can’t perform that action at this time.