Skip to content

Instantly share code, notes, and snippets.

View Lucretiel's full-sized avatar

Nathan West Lucretiel

View GitHub Profile
@Lucretiel
Lucretiel / anonymous.py
Last active August 29, 2015 14:10
An idea for anonymous decorated functions
# There are many patterns in python where a decorator is used to add a function to a registry,
# without altering it at all. Often, the function doesn't need to exist outside of the registry,
# and a minimal def is supplied- just enough to be decorated. The functools.singledispatch
# decorator is an example of this:
@singledispatch
def func(arg):
print(arg)
@Lucretiel
Lucretiel / clip.sh
Last active October 5, 2021 16:22
Pipe stuff into your terminal's clipboard
#!/bin/sh
# Copy some stuff to the term clipboard
stuff="$(base64 -w0)"
exec echo -en "\x1b]52;c;${stuff}\x07"
@Lucretiel
Lucretiel / reg_match
Created January 22, 2015 17:42
Slightly more useful than grep, but doesn't make you want to punch the computer like sed or cut.
#!/usr/bin/env python
from __future__ import print_function, unicode_literals
import argparse
import re
from sys import stdin
parser = argparse.ArgumentParser()
@Lucretiel
Lucretiel / button_panel.py
Last active August 29, 2015 14:16
Simple prototype for a command panel
#!/usr/bin/env python
from __future__ import print_function
from sys import stdin, stdout, argv
import Tkinter
import sh
# PUBLIC DOMAIN.
# - Lucretiel
@Lucretiel
Lucretiel / monitor.py
Last active August 29, 2015 14:17
Monitor the availability of a port at a remote host.
#!/usr/bin/env python3
# Copyright 2015 Nathan West
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
@Lucretiel
Lucretiel / registry.py
Last active August 29, 2015 14:18
Descriptor-based class function registry
class FunctionNotFoundError(KeyError):
pass
class Registry:
def __init__(self):
self.registry = {}
def register(self, key):
def decorator(func):
self.registry[key] = func
from tkinter import *
import asyncio
from functools import wraps
import websockets
def runloop(func):
'''
This decorator converts a coroutine into a function which, when called,
runs the underlying coroutine to completion in the asyncio event loop.
'''
@Lucretiel
Lucretiel / export.py
Created April 28, 2015 14:20
A simple helper for populating __all__
def exporter(all):
def export(thing):
all.append(thing.__name__)
return thing
return export
###################################
__all__ = []
#!/usr/bin/env python
from __future__ import print_function, unicode_literals
from tempfile import SpooledTemporaryFile as TempFile
from shutil import copyfileobj
from argparse import ArgumentParser
from io import open
def trim_white(istr):
import asyncio
@asyncio.coroutine
def scan_remote(host, port, timeout=1):
try:
connection = asyncio.open_connection(host, port)
yield from asyncio.wait_for(connection, timeout)
except (OSError, asyncio.TimeoutError):
pass
else: