Skip to content

Instantly share code, notes, and snippets.

@earonesty
earonesty / DPAPI keyring
Created Apr 22, 2021
Replacement keyring backend that uses DPAPI instead of credsmanager
View DPAPI keyring
import typing
from contextlib import suppress
from pathlib import Path
import logging
from logextension import LoggingContext
with LoggingContext(logging.getLogger("keyring.backend"), logging.ERROR):
import keyring.backend
View diffcount.py
#!/usr/bin/env python
import sys
import re
import configparser
from fnmatch import fnmatch
from unidiff import PatchSet
EXTS = ["py"]
View CodingGuidelines.md

Coding Guidelines

General

  • These are not to be blindly followed; strive to understand these and ask
    when in doubt. Sometimes standards are a bad idea.

  • Don't duplicate the functionality of a built-in library.

View testman.js
/*
* Very simple test runner for nodejs:
*
* Supports:
*
* before, after, beforeAll, afterAll
* fixture object passed to each test, that before/after/beforeAll/afterAll can modify
* -[t]est option on command line to pick tests to run
* -[l]inear option on command to disable parallel
* built in fixture logger, captures log lines, adds line numbers/file names/timestamps
View typed_enum.py
"""TypedEnum : type preserving enumeration metaclass."""
class TypedEnum(type):
"""This metaclass creates an enumeration that preserves isinstance(element, type)."""
def __new__(mcs, cls, bases, classdict):
"""Discover the enum members by removing all intrinsics and specials."""
object_attrs = set(dir(type(cls, (object,), {})))
member_names = set(classdict.keys()) - object_attrs
member_names = member_names - set(name for name in member_names if name.startswith("_") and name.endswith("_"))
@earonesty
earonesty / qserial.hpp
Last active Mar 22, 2020
Fast C++ serialization: header only with schema
View qserial.hpp
/// replaced with better tested: github.com/earonesty/qserial
#include <vector>
#include <memory>
#include <algorithm>
namespace qserial {
/*
* Simple but limited schema-driven serialization, header-only, small + fast.
@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 Jan 6, 2021
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):