Skip to content

Instantly share code, notes, and snippets.

@havron
havron / check_regexes.py
Last active May 15, 2021 00:55
Check the regexes of a file split by '"validation": '
import re
def sanitize(regex):
if regex.startswith('r"'):
regex = regex[2:]
elif regex.startswith('"'):
regex = regex[1:]
if regex.endswith('"'):
regex = regex[:-1]
return regex
@havron
havron / json_load.py
Last active May 14, 2021 20:23
Python function to load JSON easily
import json
def json_load(path: str) -> dict:
if not path:
return {}
try:
with open(path, 'r') as p:
return json.load(p) # expect dict
except (FileNotFoundError, json.decoder.JSONDecodeError) as e:
print(f"[WARNING] No recipients discovered. Reason: {e}")
@havron
havron / double-anchor.html
Last active October 31, 2020 16:26
Double anchors in Squarespace
<!-- On Squarespace config -->
<!-- In your blog post (text section), make an "insert point" (teardrop on the side) and choose code block. -->
<!-- In the code block on the top right, select "Markdown" display (but don't click on Display source checkmark) -->
<!-- Place footnote like below - populate slug https://support.squarespace.com/hc/en-us/articles/205814578: -->
A very long blog post, please see minor point[<div id="footnote-1-back"><sup>1</sup></div>](my_site.com/slug/#footnote-1)
A second but more major point[<div id="footnote-2-back"><sup>2</sup></div>](my_site.com/slug/#footnote-2)
@havron
havron / random_sample.py
Last active October 24, 2020 16:44
random_sample.py
#!/usr/bin/env python3
import argparse
import random
def main(path, samplesize):
"""
Prints random samples in order of selection.
@havron
havron / xrandr
Created July 29, 2019 23:22
change virtualbox window size
https://superuser.com/a/752510
@havron
havron / cornell_network.sh
Created June 14, 2019 23:46
Check if you're in the Cornell campus network, and if not spin up a VPN connection
curl -s -o /dev/null -w "%{http_code}" http://cuonly.cs.cornell.edu/ | grep -q 200 || sudo vpnc-connect
#!/usr/bin/env python3
# coding: utf-8
import pandas as pd
import sys
import os
# pass in infile and CMSX assignment name as commandline args
if len(sys.argv) < 3:
print("Usage: python3 {}".format(os.path.basename(__file__))\
@havron
havron / disk_analyze.sh
Last active December 11, 2018 20:32
figure out what is taking up all of the space on disk. also consider https://dev.yorhel.nl/ncdu
du -cha --max-depth=2 /home/ | grep -E "M|G" | sort -rh
@havron
havron / cross_validate.py
Last active November 10, 2018 19:42
Easier cross validation algorithm than what I could find on the WWW.
from sklearn.model_selection import KFold
from sklearn.base import clone
def cross_validate(features, labels, nsplits, model):
'''Returns tuple of (scores : list, average_score : float) over K folds.
Keyword arguments:
nsplits : int -- the number of folds to perform cross validation on.
model -- an object that is the model for CV. Assumes fit() and score() methods,
akin to sklearn model APIs.
features : pandas.DataFrame -- a Pandas DataFrame containing preprocessed training data features.
@havron
havron / android_permissions_labels.py
Last active October 29, 2018 14:16
Get full human-readable labels for Android permissions. Run `adb shell pm list permissions -g -f > permissions_dump.txt` on an Android device, then run this Gist to convert to pandas-friendly CSV.
'''
Get full human-readable labels for Android permissions.
Run `adb shell pm list permissions -g -f > permissions_dump.txt` on an Android device,
then run this Gist to convert to pandas-friendly CSV.
'''
from rsonlite import simpleparse
import pandas as pd
OUTFILE = 'android_permissions.csv'
def permission_labels(permissions_dumpfile):
print('Parsing permissions dumpfile...')