Skip to content

Instantly share code, notes, and snippets.

View pirate's full-sized avatar
🗃️
Archiving all the things!

Nick Sweeting pirate

🗃️
Archiving all the things!
View GitHub Profile
@goopi
goopi / django-profiling.sh
Created October 3, 2014 00:13
Profiling Django using runprofileserver and QCacheGrind
$ # install QCacheGrind (KCacheGrind)
$ brew install qcachegrind
$ brew install graphviz
$ brew linkapps
$ pip install django-extensions
$ # run profiling server
$ ./manage.py runprofileserver 0:3000 --kcachegrind --prof-path=path/to/profiles
stems = ARGF.read
.split
.each_cons(2)
.group_by { |word_pair| word_pair[0] }
def next_word ary
ary[rand(ary.length).to_i][1]
end
e = Enumerator.new do |e|

Python’s lru_cache is sensitive to order of keyword arguments

In case you were wondering, Python’s built-in LRU cache function decorator doesn’t cache function calls with the same keyword arguments written in a different order. That is, if you cache a function and then call it twice, with the same keyword arguments but written in a different order, on the second call you won’t get the cached result and the function will be re-run.

proof/example

Let’s construct an example. We’re interested in keyword arguments, so we’ll make our function only take keyword arguments (1). We want some side-effect to tell us whether it was a cache hit or miss, so we’ll print each keyword when we iterate over it (2). We want to cache a result, so we’ll return the sum of the values of the keyword arguments (3). Finally, we apply the lru_cache decorator to the function with a maxsize=None to let the cache grow unbounded. Putting all that together, we get:

@thomasfinch
thomasfinch / buildFishiOS.sh
Created March 23, 2017 06:20
Bash script to build the fish shell for a jailbroken iOS device
#!/bin/bash
# Before running this script:
# Find AC_CHECK_FILES([/proc/self/stat]) in configure.ac and comment it out
# cd into the fish source directory
FLAGS="-stdlib=libc++ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk -target armv7-apple-darwin16 -miphoneos-version-min=8.0.0"
PREFIX=$(pwd)"/deb"
# Build fish
"""Run some code after the response is returned to the user in django
Great for analytics or other slow tasks that you don't want to block the response
to the user for.
"""
class HttpResponseWithCallback(HttpResponse):
def __init__(self, *args, **kwargs):
self.callback = kwargs.pop('callback')
super().__init__(*args, **kwargs)
@pirate
pirate / get_python_function_name.py
Created July 9, 2019 00:02
Efficiently return the full module path and function name of the calling function in Python e.g. "app.module.class.method"
import sys
def current_function_name(above=1):
"""returns the calling function's __module__.__name__"""
# https://gist.github.com/JettJones/c236494013f22723c1822126df944b12#gistcomment-2962311
frame = sys._getframe()
for frame_idx in range(0, above):
frame = frame.f_back
@mwhite
mwhite / expanded_history.py
Last active April 20, 2020 19:21
Bash history with bash and git aliases expanded
"""
Outputs history with bash and git aliases expanded.
"""
from __future__ import print_function
import re
from subprocess import check_output
BASH_ALIASES = {}
for line in check_output('bash -i -c "alias -p"', shell=True).split('\n'):
#!/bin/bash
function f() {
sleep "$1"
echo "$1"
}
while [ -n "$1" ]
do
f "$1" &
shift
done
@stevemoser
stevemoser / IncompatibleAppsList.plist
Created June 20, 2019 19:38
List of 235 apps incompatible with macOS Catalina 10.15
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IncompatiblePaths</key>
<array>
<dict>
<key>Application Name</key>
<string>Sxs Memory Card Driver</string>
<key>Blurb</key>
@nysean79
nysean79 / relaytest.sh
Created January 19, 2012 14:36
SMTP relay test script
#!/usr/bin/env bash
## Version 1.0.2
my_email_body="Test from telnet"
mail_server_ip="127.0.0.1"
mail_server_port="25"
recipient="sryan@example.com"
sender="\"Ubuntu Server\"<ubuntu@example.com>"
nc ${mail_server_ip} ${mail_server_port} << EOF