Skip to content

Instantly share code, notes, and snippets.

🎹
Piano

earonesty

🎹
Piano
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
earonesty / ntf.py
Last active Nov 20, 2019
NamedTemporaryFile drop in replacement that deletes on gc, not close(), and supports mode=None
View ntf.py
import os, tempfile, gc
class TemporaryFile:
def __init__(self, name, io, delete):
self.name = name
self.__io = io
self.__delete = delete
def __getattr__(self, k):
View safe_wrap.py
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
deleted.
@earonesty
earonesty / fun.py
Last active Oct 16, 2019
Python's 'if' statement gets privileged information from conditionals
View fun.py
# 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"):
x=schema.copy()
@earonesty
earonesty / scramble.py
Created Sep 27, 2019
randomize a generator stream within a fixed window
View scramble.py
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 Sep 19, 2019
Arbitrarily large fake filelike python
View fakefile.py
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 Sep 12, 2019
strict decorator for python classes
View strict.py
#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 Aug 6, 2019
Python Generator Multiplexter
View muxer.py
import queue
from threading import Lock
from collections import namedtuple
class Muxer():
Entry = namedtuple('Entry', 'genref listeners, lock')
already = {}
top_lock = Lock()
View aes-doesnt-leak-memory.py
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.