Skip to content

Instantly share code, notes, and snippets.

@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')))
get_ipython().system(u'sqlite3 my.db')
@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>
@hjwp
hjwp / baroque.md
Last active August 24, 2017 15:27
Baroque, the decorating decorator decorator
@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 / family.py
Created August 13, 2014 18:22
example of testing super calls
x = None
class Parent:
def foo(self):
return 'parent'
class Child(Parent):
def foo(self):
if x:
return x
@hjwp
hjwp / Dockerfile
Created August 1, 2014 11:53
Building a postgres docker image...
FROM ubuntu:trusty
MAINTAINER [email protected]
# Add the PostgreSQL PGP key to verify their Debian packages.
# It should be the same key as https://www.postgresql.org/media/keys/ACCC4CF8.asc
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
# Add PostgreSQL's repository. It contains the most recent stable release
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main 9.4" > /etc/apt/sources.list.d/pgdg.list
@hjwp
hjwp / gist:455a77e21a3710c1b958
Last active August 29, 2015 14:01
"Easily" run all the tests in a particular file in Django <1.6
grep "class.\+Test" appname/tests/test_file.py | awk '{print $2;}' | awk -F"(" '{print "appname."$1}' | xargs python manage.py test
@hjwp
hjwp / tdd-webdev-draft-v1.1.diff
Created April 22, 2014 08:44
updates for python 3.4 and rewrite chap. 19
diff --git a/chapter_01.asciidoc b/chapter_01.asciidoc
index bee486f..f46cd90 100644
--- a/chapter_01.asciidoc
+++ b/chapter_01.asciidoc
@@ -265,25 +265,24 @@ Next we can add the rest of the contents of the current folder, `.`:
----
$ *git add .*
$ *git status*
-# On branch master
-#
@hjwp
hjwp / readme.md
Created March 12, 2014 10:39
Minimal repro of LiveServerTestCase / Selenium / Windows bug

This Gist is just a minimal repro of a bug (I think) in Django's LiveServerTestCase.

Once a site has some static files (eg CSS), then clicking on a link produces an ugly uncaught exception from the LiveServer thread, like this:

Exception happened during processing of request from ('127.0.0.1', 1925)
Traceback (most recent call last):
  File "c:\Python33\lib\site-packages\django\test\testcases.py", line 1035, in _handle_request_noblock
    self.process_request(request, client_address)
  File "c:\Python33\lib\socketserver.py", line 332, in process_request