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
#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 |
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 call_with_self(func): | |
return functools.wraps(func)(lambda *args, **kwargs: func(func, *args, **kwargs)) |
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
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 |
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
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) |
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 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)))) |
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
from contextlib import contextmanager | |
@contextmanager | |
def module(module_contents): | |
def expose(obj): | |
module_contents.append(obj.__name__) | |
return obj | |
yield expose | |
#Example: |
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
from sys import stdin | |
data = stdin.read() | |
print(next(port | |
for port in range(491, 600) | |
if str(port) not in data)) |
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
''' | |
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 |
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
from contextlib import contextmanager | |
class SwitchError(RuntimeError): | |
pass | |
@contextmanager | |
def switch(switch_value, *, ignore_nomatch=True): | |
blocks = {} | |
blocks.default = None |
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
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 |
OlderNewer