Skip to content

Instantly share code, notes, and snippets.

@hjwp
hjwp / deccy.py
Created October 23, 2014 09:26
Simple decorator challeng
def absolute(fn):
# this decorator currently does nothing
def modified_fn(x):
return fn(x)
return modified_fn
def foo(x):
return 1 - x
@hjwp
hjwp / baroque.md
Last active August 24, 2017 15:27
Baroque, the decorating decorator decorator
@hjwp
hjwp / togetherjshacks.html
Last active September 16, 2019 17:22
A No-UI TogetherJS integration
<script type="text/javascript">
$(function() {
// normal application code goes here
// eg initialize Ace text editor
MyApp.do.myStuff("etc");
// ... and then "reinitialize" togetherjs so that it finds all our dynamic ui stuff
TogetherJS.reinitialize();
});
</script>
get_ipython().system(u'sqlite3 my.db')
@hjwp
hjwp / load_env_from_bash_script.py
Last active August 29, 2015 14:21
Load environment variables from bash into current python process
import os
import subprocess
script = '/home/harry/.virtualenvs/my-project-name/bin/postactivate' # eg
env_to_json = 'import os, json; print(json.dumps(dict(os.environ)))'
environ_json = subprocess.check_output(
'. {} && python -c "{}"'.format(script, env_to_json),
shell=True, env={}
)
os.environ.update(json.loads(environ_json.decode('utf8')))
@hjwp
hjwp / test_chromedrive_autocapitalize_off.py
Created May 26, 2015 12:27
Minimal repro of chromedriver bug
import tempfile
from selenium import webdriver
htmlfile = tempfile.NamedTemporaryFile(suffix='.html', delete=False)
with htmlfile.file:
htmlfile.file.write('<!DOCTYPE html><html><input type="text" autocapitalize="off" /></html>')
def check_browser(browser):
browser.implicitly_wait(1)
@hjwp
hjwp / conftest.py
Last active August 29, 2015 14:23
pytest finalizer that prints?
import pytest
@pytest.yield_fixture
def print_on_fail():
yield
print('test failed')
@hjwp
hjwp / conftest.py
Last active August 29, 2015 14:23
pytest print in finalizer attempts
import time
import pytest
def pytest_runtest_call(item):
if hasattr(item, "_request"):
if hasattr(item._request, "_addoutput_on_failure"):
item._request._addoutput_on_failure()
@pytest.fixture
@hjwp
hjwp / _output.txt
Last active August 29, 2015 14:23
pytest print in finalizer 2
➜ t2 py.test tests.py
============================= test session starts =============================
platform linux -- Python 3.4.3 -- py-1.4.30 -- pytest-2.7.2
rootdir: /tmp/t2, inifile:
collected 2 items
tests.py .F
================================== FAILURES ===================================
______________________________ test_should_fail _______________________________
@hjwp
hjwp / conftest.py
Created June 30, 2015 15:41
pytest: print fixture output after test runs
import pytest
@pytest.mark.hookwrapper
def pytest_pyfunc_call(pyfuncitem):
yield
print('pyfunc call after')
if 'watch_logs' in pyfuncitem.funcargs:
print(pyfuncitem.funcargs['watch_logs']())