Skip to content

Instantly share code, notes, and snippets.

View mhkeller's full-sized avatar

Michael Keller mhkeller

View GitHub Profile
// const monthLengths = {
// '2020-03': 31,
// '2020-02': 29,
// '2020-01': 31,
// '2019-12': 31,
// '2019-11': 30,
// '2019-10': 31,
// '2019-09': 30,
// '2019-08': 31,
@mhkeller
mhkeller / mv.sh
Created April 13, 2020 22:46 — forked from premek/mv.sh
Rename files in linux / bash using mv command without typing the full name two times
# Put this function to your .bashrc file.
# Usage: mv oldfilename
# If you call mv without the second parameter it will prompt you to edit the filename on command line.
# Original mv is called when it's called with more than one argument.
# It's useful when you want to change just a few letters in a long name.
function mv() {
if [ "$#" -ne 1 ]; then
command mv "$@"
return
@mhkeller
mhkeller / genDateRange.js
Created April 10, 2020 03:09
Generate a range of dates
// Adapted from here: https://stackoverflow.com/questions/4413590/javascript-get-array-of-dates-between-2-dates
module.exports = function genDateRange(start, end, format) {
let arr;
let dt;
for (arr = [], dt = new Date(start); dt <= end; dt.setDate(dt.getDate() + 1)) {
arr.push(new Date(dt));
}
if (format === 'strings') {
return arr.map(v => v.toISOString().slice(0, 10));
}
<script>
import { LayerCake, Svg, calcExtents, flatten } from 'layercake';
import { tweened } from 'svelte/motion';
import * as eases from 'svelte/easing';
import Line from '../../components/Line.svelte';
export let data;
export let shared;
const y = tweened(undefined, {
@mhkeller
mhkeller / count-string-occurrences.sql
Created October 15, 2019 19:31
count occurrences of a string across rows of a pgsql table
--useful for seeing if a css class exists across all instances of scraped pages in a pgsql database
--adapted from here https://stackoverflow.com/questions/36376410/counting-the-number-of-occurrences-of-a-substring-within-a-string-in-postgresql
select sum(array_length(string_to_array(html, 'SELECTOR'), 1) - 1) from TABLE_NAME
--should equal rows in the table
--or this to get occurrences per row
select (array_length(string_to_array(html, 'SELECTOR'), 1) - 1) as ct from TABLE_NAME ORDER BY ct DESC
@mhkeller
mhkeller / throttle.js
Created April 17, 2019 19:14
Throttle requests
const sleep = ms => new Promise(f => setTimeout(f, ms));
async function do_things() {
for (const thing of things) {
await Promise.all([
doThing(thing),
sleep(200)
});
}
}
@mhkeller
mhkeller / gist:01f408c1a4e11e0577eb052e35f526bf
Created March 23, 2019 05:23
use python 2.7 with a node install for node-gyp
`npm install --python=python2.7` and `npm config set python python2.7`
@mhkeller
mhkeller / deps.R
Created March 20, 2019 01:58
Loading some sweet R packages
pkgs <- c("packages")
check <- sapply(pkgs, require, warn.conflicts = TRUE, character.only = TRUE)
if(any(!check)) {
pkgs.missing <- pkgs[!check]
install.packages(pkgs.missing)
check <- sapply(pkgs.missing, require, warn.conflicts = TRUE, character.only = TRUE)
}
rm(pkgs, check)
@mhkeller
mhkeller / index.js
Last active May 1, 2019 14:03
Basic puppeteer setup
const puppeteer = require('puppeteer');
main();
async function main () {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto(`https://webpage.com`, { waitUntil: 'load' });