Skip to content

Instantly share code, notes, and snippets.

@rgkimball
rgkimball / rowcount.py
Last active August 9, 2021 03:11
File rowcount estimation
def count_file_iter(*ar, **kw):
"""
Small utility to iterate through individual rows of a file given a generator,
used only to count rows.
:param ar: args for open()
:param kw: keyword args for open()
:return: yields 1 for each row
"""
for _ in open(*ar, **kw):
@rgkimball
rgkimball / human_size.py
Created June 17, 2021 17:15
Human-readable Size
def readable_size(byte_size: int, base: int = 10, decimals: int = 1):
"""
Given the size of an object in bytes, returns a human-readable alternative.
For ex:
>>> readable_size(44000000)
42.0 MB
Supports sizes up to 1024 yobibytes in Binary or 1000 yottabytes in Decimal SI.
Inspired by the following StackOverflow questions:
@rgkimball
rgkimball / norvig.py
Last active February 26, 2018 01:14
Norvig Economy Simulation
import random
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import networkx as nx
from string import ascii_lowercase, digits
COLW = 10
chars = ascii_lowercase + digits
@rgkimball
rgkimball / fv.py
Created January 21, 2017 19:47
Future Value of a stream of cash flows and an initial investment
# In 000's
pv = 400
savings_rate = 0.10
ir = 0.025
periodicity = 4
years = 20
cf = pv * savings_rate
nper = periodicity * years
irper = ir / periodicity
@rgkimball
rgkimball / progress-bar.scss
Last active August 29, 2015 14:20
Sets progress bar visual based on which nth-child the active class is w/ a @for loop
$number-of-steps: 6;
.stage {
// etc.
&.active {
// Accents current stage's title.
.stage-title {
-webkit-box-shadow:-2px 2px 5px -1px darken($brand-blue,11%) inset;
-moz-box-shadow:-2px 2px 5px -1px darken($brand-blue,11%) inset;
box-shadow:-2px 2px 5px -1px darken($brand-blue,11%) inset;
@rgkimball
rgkimball / drupal-ajax.js
Created April 13, 2015 15:14
Prevents Drupal from throwing alert()s when AJAX is interrupted.
preventAlerts: function () {
//Copy drupal's original error function for later use if you want
Drupal.ajax.prototype.original_error = Drupal.ajax.prototype.error;
Drupal.ajax.prototype.error = function (response, uri) {
// This should catch the "terminated abnormally" error
if (response.readyState == 0 || response.readyState == 4) {
console.log(response);
}
else {
this.original_error(response, uri);
@rgkimball
rgkimball / git-ff.sh
Created April 2, 2015 14:45
Fast-forwards every local branch from remote repo in one command.
_gitff() {
REMOTES="$@";
if [ -z "$REMOTES" ]; then
REMOTES=$(git remote);
fi
REMOTES=$(echo "$REMOTES" | xargs -n1 echo)
CLB=$(git branch -l|awk '/^\*/{print $2}');
echo "$REMOTES" | while read REMOTE; do
git remote update $REMOTE
git remote show $REMOTE -n \
@rgkimball
rgkimball / sass-triangles-mixin.sass
Last active August 29, 2015 14:17
This is an extremely lightweight mixin for adding CSS triangles to pseudo elements.
@mixin triangle($direction: "right", $size: 5px, $color: '#FFFFFF') {
@if not index(top right bottom left, $direction) {
@warn "\"#{$direction}\" is not a valid direction value. Value must be one of: top, right, bottom, left";
}
@else {
@include transform(scale(1.01)); // @see https://brettstrikesback.com/de-pixelating-the-css-triangle/
border: $size solid transparent;
border-#{$direction}-color: $color;
border-#{opposite-position($direction)}-width: 0;
#{opposite-position($direction)}: -$size;
@rgkimball
rgkimball / hovering.js
Created March 8, 2015 21:42
:hovering jQuery element state
$(document).on({
mouseenter: function (evt) {
$(evt.target).data('hovering', true);
},
mouseleave: function (evt) {
$(evt.target).data('hovering', false);
}
}, "*");
jQuery.expr[":"].hovering = function (elem) {
return $(elem).data('hovering') ? true : false;
# Shortened drush sql-sync; drops all tables of destination environment,
# then syncs a non-cached version of the source environment
_dss () {
local timestamp=$(date "+%Y%m%d-%H%M%S")
local site=${1//[@]/}
local source=${2:-prod}
local dest=${3:-local}
if [ -z "$4" ]; then dump_path=../..; else dump_path=$4; fi
time {