Skip to content

Instantly share code, notes, and snippets.

View Lucretiel's full-sized avatar

Nathan West Lucretiel

View GitHub Profile
@Lucretiel
Lucretiel / generic_test.py
Last active December 14, 2015 22:38
Here's an example of what I was talking about earlier- I found common patterns in the HomePage suite, reformatted them into a series of generic tests and decorators.
#Generic test methods.
@nose.tools.nottest
def test_css_element(driver, selector, test):
'''
Find an element, then perform a test on that element
'''
test(driver.find_element_by_css_selector(selector))
@nose.tools.nottest
import functools
def call_with_self(func):
return functools.wraps(func)(lambda *args, **kwargs: func(func, *args, **kwargs))
@Lucretiel
Lucretiel / tail_recursion.py
Last active December 15, 2015 16:59
Decorator to enable tail recursion in python.
from functools import wraps
class tail(object):
def __init__(self, *args, **kwargs):
try:
self.func = args[0]._tail_function
except AttributeError:
self.func = args[0]
self.args = args[1:]
self.kwargs = kwargs
def make_variable_name(var):
valid_chars = set(string.letters + string.digits + '_')
def replace_char(char):
if char == '-':
return '_'
elif char in valid_chars:
return char
return ''
return ''.join(replace_char(char) for char in var)
@Lucretiel
Lucretiel / pylisp.py
Created April 26, 2013 23:56
That moment when you thought you were programming in Python but it turns out you were actually programming in Lisp
import sys
from itertools import imap, ifilter
print tuple(reduce(
lambda totals, new: imap(lambda x, y: x+y, totals, new),
imap(
lambda line: imap(int, line.split()[:2]),
ifilter(
lambda line: line.strip(),
sys.stdin))))
@Lucretiel
Lucretiel / expose.py
Created May 28, 2013 19:52
Expose manages your __all__ for you.
from contextlib import contextmanager
@contextmanager
def module(module_contents):
def expose(obj):
module_contents.append(obj.__name__)
return obj
yield expose
#Example:
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 / switch.py
Last active April 15, 2022 15:40
A switch statement in python
from contextlib import contextmanager
class SwitchError(RuntimeError):
pass
@contextmanager
def switch(switch_value, *, ignore_nomatch=True):
blocks = {}
blocks.default = None
@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