This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# If making a class decorator. A MethodType is required to make it bind to methods correctly, and pass "self" argument across. | |
import types | |
class Decorator(object): | |
def __init__(self, func): | |
self.func = func | |
def __call__(self, *args, **kwargs): | |
return self.func(*args, **kwargs) | |
def __get__(self, inst, _=None): | |
if inst: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import tee | |
from time import time | |
def t1(count): | |
l = [] | |
for i in range(count): | |
l.append(i) | |
def t2(count): | |
a, b = tee(range(count)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from concurrent.futures import ThreadPoolExecutor | |
import asyncio | |
def run_sync(task): | |
try: | |
loop = asyncio.get_event_loop() | |
except RuntimeError: | |
loop = None | |
if not loop or loop.is_running(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import heapq | |
import types | |
import itertools | |
import functools | |
import threading | |
import contextlib | |
import collections | |
import operator |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from concurrent.futures import ThreadPoolExecutor | |
_pool = ThreadPoolExecutor() | |
def defer(caller, *args, **kwargs): | |
def decorator(func): | |
global _pool | |
future = _pool.submit(caller, *args, **kwargs) | |
future.add_done_callback(lambda f: func(f.result())) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generate tracebacks from arbitrary information | |
import types, sys | |
def create_traceback(stack, _code=compile("__temp__()", "", "exec").co_code): | |
""" Create a traceback from given information, or None if provided stack is empty. """ | |
func = None | |
for frame in stack: | |
func = types.FunctionType( | |
types.CodeType( | |
0, 0, 0, 0, 1, 64, _code, (None,), ("__temp__",), (), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import os | |
import time | |
import errno | |
import socket | |
class FileLock(object): | |
CONTENTS = "Locked by\n * host: {}\n * process: {}".format( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import contextlib | |
@contextlib.contextmanager | |
def context_manager_manager(*managers): | |
for manager in managers: | |
manager.__enter__() | |
err = (None, None, None) | |
try: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright 2020 Jason Dixon | |
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from contextlib import contextmanager | |
from threading import current_thread | |
from collections import defaultdict | |
class Scope(object): | |
_scope = defaultdict(list) | |
_tagged = {} |