Skip to content

Instantly share code, notes, and snippets.

@amagee
amagee / redbaron_parse.py
Created July 25, 2017 06:13
Redbaron parse + explain file
>>> import redbaron; redbaron.RedBaron(open(filename).read()).help()
function parseQuery(qstr) {
var query = {};
var a = (qstr[0] === '?' ? qstr.substr(1) : qstr).split('&');
for (var i = 0; i < a.length; i++) {
var b = a[i].split('=');
query[decodeURIComponent(b[0])] = decodeURIComponent(b[1] || '');
}
return query;
}
@amagee
amagee / gist:4011434a611a127964b8b5c581a3e7f2
Last active March 17, 2017 05:05
Linux sort by column
cat file |sort -k3nr
# -n -- sort numerically (not lexicographically)
# -k3 -- sort by the 3rd column
# -r -- sort in reverse order (bigger values first)
This is different to |sort -nk3r for some reason
import argparse
def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument('--no-build', dest="build", default=True, action="store_false")
return parser.parse_args(argv)
>>> main([]).build
True
>>> main(['--no-build'].build
@amagee
amagee / gitstuff.sh
Last active March 3, 2017 03:10
Git stuff
# Find commit that deleted a line, given some content in the line
$ git log -S<content>
# Show file at a particular revision
$ git show <revision>:<file_path>
# Abort a merge
$ git merge --abort
# Get commits in one branch that aren't in another branch
# Http:
telnet localhost 80
GET / HTTP/1.1
HOST: gist.github.com
# Https:
openssl s_client -connect localhost 443
@amagee
amagee / pipe_stderr.sh
Created December 29, 2016 02:52
Pipe stderr
# http://stackoverflow.com/a/15936384/223486
{command} 2> >({command} 1>&2)
eg. nvim-qt 2> >(grep -v "Unknown Neovim function" 1>&2)
@amagee
amagee / ipython_autoreload
Created December 22, 2016 00:12
IPython auto-reload
# https://ipython.org/ipython-doc/3/config/extensions/autoreload.html
>>> %load_ext autoreload
>>> %autoreload 2
>>> from foo import some_function
>>> some_function()
>>> from foo import some_function
>>> some_function()
@amagee
amagee / selenium_switch_tabs.py
Last active December 15, 2016 01:14
Selenium switch tabs
# Current versions of Selenium are so silly that if an action on the page
# results in a new tab being opened, the browser will switch to the new tab,
# but the selenium driver will still think the old tab is visible, so if you
# try to access elements on the old tab, they will fail with 'element not visible'
# errors.
# One way to fix this is to explicitly switch to the new tab and close it
# (if you have an element that will close the tab).
driver.switch_to.window(driver.window_handles[1])
@amagee
amagee / clip_magic.py
Created December 7, 2016 05:02
IPython: copy a variable to the clipboard as JSON
# Inspiration from https://gist.github.com/nova77/5403446
# Docs from http://ipython.readthedocs.io/en/stable/config/custommagics.html
from IPython.core.magic import Magics, magics_class, line_magic
from subprocess import Popen, PIPE
import json
@magics_class
class ClipJsonMagic(Magics):
@line_magic