Skip to content

Instantly share code, notes, and snippets.

Avatar

Babak B. babakness

View GitHub Profile
@babakness
babakness / caselessDictionary.py
Created Oct 16, 2012 — forked from bloomonkey/caselessDictionary.py
A Python dictionary sub-class that is case-insensitive when searching, but also preserves the keys as inserted.
View caselessDictionary.py
class CaselessDictionary(dict):
"""Dictionary that enables case insensitive searching while preserving case sensitivity
when keys are listed, ie, via keys() or items() methods.
Works by storing a lowercase version of the key as the new key and stores the original key-value
pair as the key's value (values become dictionaries)."""
def __init__(self, initval={}):
if isinstance(initval, dict):
for key, value in initval.iteritems():
@babakness
babakness / README.md
Created Jun 21, 2017 — forked from benjie/README.md
Easy plv8 on OSX
View README.md

Heroku runs plv8 v1.4.2 (checked on 1st April 2016). On OSX it's easiest to install v1.4.3 since that allows V8 3.15 which is available via homebrew. (1.4.2 wants V8 3.14.5).

To install:

brew install v8-315
pip install pgxnclient
LIBRARY_PATH="/usr/local/opt/v8-315/lib" CPATH="/usr/local/opt/v8-315/include" pgxnclient install plv8=1.4.3
@babakness
babakness / README.md
Created Jun 21, 2017 — forked from benjie/README.md
Easy plv8 on OSX
View README.md

Heroku runs plv8 v1.4.2 (checked on 1st April 2016). On OSX it's easiest to install v1.4.3 since that allows V8 3.15 which is available via homebrew. (1.4.2 wants V8 3.14.5).

To install:

brew install v8-315
pip install pgxnclient
LIBRARY_PATH="/usr/local/opt/v8-315/lib" CPATH="/usr/local/opt/v8-315/include" pgxnclient install plv8=1.4.3
View plv8-modules.sh
#! /bin/bash
sudo apt-get install npm
cd /var/django
npm install moment moment-timezone
sudo -u postgres psql -d template1 -c "create extension plv8;"
sudo -u postgres psql -d template1 << EOF
View Firebase Database API Cheatsheet
There is no way to store an empty object/array/null value.
There are also no actual arrays. Array values get stored as objects with integer keys.
(If all keys are integers, it will be returned as an array.)
Basically, it's one giant tree of hashes with string keys.
Simply write a value to any location, and the intermediary locations will automatically come into existance.
── Classes ──
DataSnapshot : Container for a subtree of data at a particular location.
@babakness
babakness / create-class-like-functions.js
Last active Oct 5, 2018
Helpers for creating class like functions that can be invoked without new
View create-class-like-functions.js
const assoc = ( prop, value, obj ) =>
Object.assign( {}, obj, { [prop]: value })
const reducer = ( $values, accumulate, [key,val] ) => assoc( key, val.bind( undefined, ...$values ), accumulate )
export const bindValuesToMethods = ( $methods, ...$values ) =>
Object.entries( $methods ).reduce( reducer.bind( undefined, ...$values ), {} )
export const prepareInstance = (instanceMethods, staticMethods = ({}) ) => Object.assign(
@babakness
babakness / life-cycle.js
Created Feb 28, 2018
Example of component with lifecycle hooks
View life-cycle.js
// How to have render props that bind `this` context to functions that need them
// (note: not arrow functions)
// Example illustrates hooks at all stages
export class LifeCycle extends Component {
constructor(){
super()
}
shouldComponentUpdate(){
// return false to skip render
@babakness
babakness / example-partial-application-with-placeholders.js
Last active Apr 19, 2018
Simple partial application with placeholders, example for discussion
View example-partial-application-with-placeholders.js
export class Placeholder {}
export const _ = new Placeholder()
export const isPlaceholder = placeholder => placeholder instanceof Placeholder
export function bind( ...placeholders ) {
return ( ...fillers ) => this.call( this, ...placeholders.map(
item => isPlaceholder( item ) ? fillers.shift() : item
) )
}
@babakness
babakness / post-extract
Created May 21, 2018
Restrict dokku apps to an ip - WIP
View post-extract
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
APP="$1"
verify_app_name "$APP"
TMP_WORK_DIR="$2"
REV="$3" # optional, may not be sent for tar-based builds
HOSTS="/etc/hosts"
IP4="dokku_apps_ip4"
View working-flatten.ts
// classes
class Just<A> {
readonly _A!: A
constructor(readonly value: A) {}
fold<B>(whenNothing: B, whenJust: (a: A) => B): B {
return whenJust(this.value)
}
map<B>(fn: (a: A) => B): Maybe<B> {
return Maybe.of(fn(this.value))
}