Skip to content

Instantly share code, notes, and snippets.

View Lucretiel's full-sized avatar

Nathan West Lucretiel

View GitHub Profile
from sys import stdin
data = stdin.read()
print(next(port
for port in range(491, 600)
if str(port) not in data))
'''
Say you have a logger that adds empty lines between each logged
event, and each event is one more more lines. You want to iterate
over each logged event.
'''
def sectionalize(lines):
'''
Divide lines into sections, separated by empty lines. Technically
you could come up with a regex and do re.split, but that's doesn't
@Lucretiel
Lucretiel / kickstarter.py
Created October 4, 2014 01:49
Monitor a kickstarter
from bs4 import BeautifulSoup
from requests import get as http_get
from datetime import datetime
from time import sleep, perf_counter as now
import argparse
def get_properties(url):
'''
returns the number of backers, and total amount backed
@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 / 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
@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):