Skip to content

Instantly share code, notes, and snippets.

@slinkp
slinkp / qs_to_postgres.py
Last active May 25, 2017 16:38
Print postgres-specific output from django QuerySet w/o evaluating the QuerySet
from django.db import connection
def queryset_to_postgres(qs):
"""
Given a `QuerySet`, return a correctly formatted (but not pretty-printed) sql string that can be run
on postgres.
For debugging only, as `cursor.mogrify` is specific to psycopg2, not part of python db api.
Needed because the usual `str(qs.query)` gives generic sql that may not execute correctly on postgres,
eg. case-sensitive names may not be cased correctly.
@slinkp
slinkp / gist:26c0fa47c124552b774698e803e2913a
Created April 27, 2017 19:50
django RequestFactory query param with multiple values
In [3]: from django.test import RequestFactory
In [4]: rf = RequestFactory()
In [5]: req = rf.get('/foo', {'bar': ['baz', 'bat']})
In [6]: req # this looks like expected:
Out[6]: <WSGIRequest: GET '/foo?bar=baz&bar=bat'>
In [7]: req.GET['bar'] # Why does this give me only one value?
@slinkp
slinkp / slow_queries.sh
Last active October 4, 2016 17:49
Couple useful shell commands for mysql slow query logs
# Find slowest queries
grep "^SELECT " mysql-slowquery.log | cut -d ' ' -f 1-5 | sort | uniq -c | sort -n
# Now find times for one of those queries. Wrap in single quotes and backslash-escape wildcards
QUERY='^SELECT count(\*) FROM `foo` INNER JOIN `blah`'
grep -B2 "$QUERY" mysql-slowquery.log | grep Query_time | cut -d ' ' -f 3 | sort -n

For example if I paste this:

--- 
thing: 
  description: "Oh boy."
  stuff: 
    - "i am a string"
    - null
    - ~
 - "null"
@slinkp
slinkp / gist:f3c37d766d07e4377b63
Created October 14, 2014 14:54
MAGICK_TIME_LIMIT doesn't work in 6.6.9 either
$ convert --version
Version: ImageMagick 6.6.9-7 2014-03-06 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2011 ImageMagick Studio LLC
Features: OpenMP
$ for i in `seq 10`; do time /usr/bin/env MAGICK_TIME_LIMIT=1 convert test.jpg test.png || echo FAILED; done
real 0m19.098s
user 0m18.652s
sys 0m0.112s
@slinkp
slinkp / gist:4dbe7b13165a9f375ee4
Last active August 29, 2015 14:07
MAGICK_TIME_LIMIT doesn't work
$ convert --version
Version: ImageMagick 6.8.9-8 Q16 x86_64 2014-10-14 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC OpenMP
Delegates: fontconfig freetype jng jpeg pangocairo png x xml zlib
$ for i in `seq 10`; do time /usr/bin/env MAGICK_TIME_LIMIT=1s convert test.jpg test.png || echo FAILED; done
real 0m6.919s
user 0m6.648s
@slinkp
slinkp / gist:c3ec1f47d7ecfe682ad4
Last active August 10, 2023 10:11
How to use Mock Specs Properly with Classes
"""
TL/DR:
Never instantiate mock.Mock() directly.
Instead use either mock.create_autospec(YourClass) OR mock.patch('YourClass', autospec=True).
The "spec" feature of Mock is great, it helps avoid your mocked API drifting out of sync with your real API.
But there is a gotcha if you are mocking classes directly - it's easy to mock the class but you need to
ensure the spec applies to the *instance* as well.
"""
@slinkp
slinkp / til.py
Created May 20, 2014 19:33
Fun with the "%s" string formatter in python
# It accepts a precision??
In [13]: print '%.5s' % "Oh hi this is a long string"
Oh hi
# It magically knows whether you need a unicode or bytestring representation.
In [10]: class Foo2(object):
....: def __str__(self): return "stringy"
....: def __unicode__(self): return u"unicodey"
@slinkp
slinkp / gist:9515631
Created March 12, 2014 20:28
Shellcheck build error
searching for ghc in path.
found ghc at /usr/bin/ghc
("/usr/bin/ghc",["--numeric-version"])
/usr/bin/ghc is version 7.6.3
looking for tool "ghc-pkg" near compiler in /usr/bin
found ghc-pkg in /usr/bin/ghc-pkg
("/usr/bin/ghc-pkg",["--version"])
/usr/bin/ghc-pkg is version 7.6.3
("/usr/bin/ghc",["--supported-languages"])
("/usr/bin/ghc",["--info"])
@slinkp
slinkp / pinocchio-report-slow-tests..diff
Created February 19, 2014 19:02
Patch pinocchio nose plugin to report slowest N tests.
diff -u -r /usr/local/lib/python2.7/dist-packages/pinocchio/stopwatch.py /home/vagrant/tmp/pinocchio/stopwatch.py
--- /usr/local/lib/python2.7/dist-packages/pinocchio/stopwatch.py 2014-02-19 18:55:30.000000000 +0000
+++ /home/vagrant/tmp/pinocchio/stopwatch.py 2014-02-19 18:50:40.064935116 +0000
@@ -37,6 +37,14 @@
default=".nose-stopwatch-times",
help="Store test timing results in this file.")
+ parser.add_option("--stopwatch-report-slowest",
+ action="store",
+ dest="stopwatch_report_slowest",