Skip to content

Instantly share code, notes, and snippets.

View Lucretiel's full-sized avatar

Nathan West Lucretiel

View GitHub Profile
# Try appending ?ts=2 to the url of this page.
# Then try ?ts=4
# Then ?ts=8
def my_member_function(self, argument):
for value in range(10):
# Notice how, as the indents shift, all the arguments
# to this function stay vertically aligned.
self.my_other_func(argument,
value,
class SyncClientThread:
def __init__(self):
...
self.stop = False
self.nostop_depth = 0
def Stop(self):
self.stop = True
def IsStopped(self):
return self.nostop_depth == 0 and self.stop
def emit(list_of_elements):
for elem in list_of_elements:
if elem.thing1 > 0:
yield elem.thing1
if elem.thing2 < 0:
yield elem.thing2
if elem.thing3 != 0:
yield elem.thing3
//Global Data example
function load_problem(tokens) {
global_data = get_global_data(tokens)
return global_data.num_cases, function(tokens) {
case_specific_data = get_case_data(tokens)
return function() {
// Do a bunch of math with global_data and case_specific_data
return solution
}
}
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:
#!/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):
@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__ = []
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 / 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
@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,