This file contains 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 functools | |
def accumulate(accum_type): | |
def outer_wrapper(f): | |
@functools.wraps(f) | |
def inner_wrapper(*args, **kwds): | |
return accum_type(iter(f(*args, **kwds))) | |
return inner_wrapper | |
return outer_wrapper |
This file contains 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 passwords which can be typed without using any finger to press two | |
# different keys in a row. | |
from math import log, ceil | |
# For each finger, write the letters *you* type with that finger. | |
finger_classes = [ | |
'qaz', | |
'wsx', | |
'edc', |
This file contains 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
#!/usr/bin/env python3 | |
from os import system | |
from sys import argv | |
# Example: | |
# youtube-terminal.py foo bar baz | |
# calls youtube-dl 'ytsearch:foo bar baz' --max-downloads 1 -o - | cvlc - --no-video | |
call_str = 'youtube-dl "ytsearch:{}" --max-downloads 1 -o -' | |
call_str += ' | cvlc - --no-video --play-and-exit' |
This file contains 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
#!/usr/bin/env python3 | |
import os | |
import subprocess | |
import tempfile | |
EDITOR = os.environ.get('EDITOR', 'vim') | |
def input_new_names(old_names): | |
with tempfile.NamedTemporaryFile(suffix='.tmp') as fname_file: |
This file contains 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 builtins | |
def testable(f): | |
def run_test(test): | |
print(test.__doc__) | |
args = {name: value for (name, value) in test.__annotations__.items() | |
if name != 'return'} | |
assert f(**args) == test.__annotations__['return'] | |
return test | |
f.test = run_test |
This file contains 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
#!/usr/bin/env python3 | |
import re | |
import subprocess | |
import sys | |
if len(sys.argv) < 2: | |
print("Usage: {} <findregex> [pythonregex]...") | |
sys.exit() |
This file contains 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
class LazyDict: | |
'''Use a 2-tuple generator as a lazily-evaluated dictionary.''' | |
def __init__(self, gen): | |
self.gen = gen | |
self.dict = {} | |
def _generate_to(self, key): | |
while key not in self.dict: | |
found_key, found_val = next(self.gen) | |
self.dict[found_key] = found_val |
This file contains 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
#!/usr/bin/env python3 | |
import os | |
import sys | |
import textwrap | |
import time | |
def usage(): | |
print(textwrap.dedent(""" | |
Usage: {} <pipepath> |
This file contains 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
print(__import__('random').choice(__import__('sys').stdin.read().strip())) |