Skip to content

Instantly share code, notes, and snippets.

View masnick's full-sized avatar

Max Masnick masnick

View GitHub Profile
@masnick
masnick / loadsql.ado
Last active December 8, 2017 19:52
Stata: load and execute SQL from a file on disk
/*
Copyright 2017 Max Masnick
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OT
@masnick
masnick / trim.scpt
Created November 8, 2017 18:06
Trim whitespace with AppleScript
on trimThis(pstrSourceText, pstrCharToTrim, pstrTrimDirection)
-- http://macscripter.net/viewtopic.php?id=18519
-- pstrSourceText : The text to be trimmed
-- pstrCharToTrim : A list of characters to trim, or true to use default
-- pstrTrimDirection : Direction of Trim left, right or any value for full
set strTrimedText to pstrSourceText
-- If undefinied use default whitespaces
if pstrCharToTrim is missing value or class of pstrCharToTrim is not list then
tell application "Keyboard Maestro Engine"
--- GET VARIABLE ---
-- IF KM Variable does NOT exist, the AS Variable will be set to empty string --
set the_path to getvariable "Path"
end tell
tell application "iTerm"
activate
create window with default profile
@masnick
masnick / bookmarklet.js
Last active July 26, 2017 16:43
Universal "find at my library" bookmarklet for journal articles
var url = 'http://something.youruniversity.edu/foo/bar';
var doi;
var suffixes = {
"pmid": "?sid=Entrez:PubMed&id=pmid:",
"doi": "?sid=Entrez:DOI&id=doi:"
};
var re = /^[0-9]{5,15}/;
var found = jQuery(jQuery(".rprtid dd")[0]).text().match(re);
@masnick
masnick / balance.scpt
Last active March 31, 2022 19:04
macOS: Set sound output balance to middle
-- Based on the AppleScript posted here: https://secure.macscripter.net/viewtopic.php?pid=190067
tell application "System Preferences"
activate
reveal anchor "output" of pane id "com.apple.preference.sound"
delay 0.5 -- If you get an error, it's possible this delay isn't long enough.
end tell
tell application "System Events"
tell slider 1 of group 1 of tab group 1 of window 1 of process "System Preferences"
set value to 0.5
end tell
@masnick
masnick / stored_proc_2_result_sets.py
Last active July 14, 2017 15:45
Loading data from a stored proc with multiple results sets into a Pandas DataFrame
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('mysql://{}:{}@{}/{}'.format(username, password, server, database_name))
connection = engine.connect().connection
cursor = connection.cursor()
cursor.execute('call storedProcName(%s, %s, ...)', params)
# Result set 1
* NOTE: this ADO uses shell calls to run `rm`, so it could potentally delete files
* from your computer. Use at your own risk.
cap drop program loadsql
program define loadsql
*! Load the output of an SQL file into Stata, version 1.3 (dvmaster@gmail.com)
*! Based on http://stackoverflow.com/a/12272627/173351
version 14.1
syntax using/, DSN(string) [User(string) Password(string) CLEAR NOQuote LOWercase SQLshow ALLSTRing DATESTRing]
@masnick
masnick / md2jira.js
Created May 26, 2016 18:02
Convert Markdown to JIRA markup
// https://github.com/FokkeZB/J2M/blob/master/script.js
function toJ(converted) {
converted = converted.replace(/^(.*?)\n([=-])+$/gm, function (match,content,level) {
return 'h' + (level[0] === '=' ? 1 : 2) + '. ' + content;
});
converted = converted.replace(/^([#]+)(.*?)$/gm, function (match,level,content) {
return 'h' + level.length + '.' + content;
});
@masnick
masnick / pyenv.md
Last active May 23, 2016 17:24
pyenv setup instructions

OS X ships with an old version of Python 2. Here's how I set up my system:

brew install pyenv pyenv-virtualenv

Then add to .zshrc or .bashrc:

eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Then restart Terminal.app, and build the newest version of Python:

@masnick
masnick / private.xml
Created May 9, 2016 17:36
Disable Command-F1 with Karabiner
<?xml version="1.0"?>
<root>
<item>
<name>Disable Command-F1</name>
<identifier>private.disable.command.f1</identifier>
<autogen>--KeyToKey-- ConsumerKeyCode::BRIGHTNESS_DOWN, ModifierFlag::COMMAND_L, ConsumerKeyCode::BRIGHTNESS_DOWN</autogen>
<autogen>--KeyToKey-- ConsumerKeyCode::BRIGHTNESS_DOWN, ModifierFlag::COMMAND_R, ConsumerKeyCode::BRIGHTNESS_DOWN</autogen>
</item>
</root>