Skip to content

Instantly share code, notes, and snippets.

@dminkovsky
dminkovsky / dict_to_view.py
Last active July 10, 2020 01:13
Django: Generate corresponding JSON and rendered template (HTML) views from the same data dictionary.
from django.http import HttpResponse
from django.utils.functional import wraps
from django.template.context import RequestContext
from django.template.loader import get_template
from django.utils import simplejson as json
def dict_to_template_view(**template_args):
"""
Takes a function that returns a dict and decorates it into a template-rendering view
"""
from django.db import models
from django.utils.decorators import wraps
from django.core.exceptions import ValidationError
def constraint(func):
@wraps(func)
def inner(*args, **kwargs):
func._is_constraint = True
return func(*args, **kwargs)
return inner
# MAC manipulators
alias random_mac='sudo ifconfig en0 ether `openssl rand -hex 6 | sed "s/\(..\)/\1:/g; s/.$//"`'
alias restore_mac='sudo ifconfig en0 ether YOUR_ORIGINAL_MAC_ADDRESS_GOES_HERE'

tmux cheat sheet

(C-x means ctrl+x, M-x means alt+x)

Prefix key

The default prefix is C-b. If you (or your muscle memory) prefer C-a, you need to add this to ~/.tmux.conf:

remap prefix to Control + a

@dminkovsky
dminkovsky / Observations about binary and binary operations
Last active December 19, 2015 22:19
Round up to the next power of 2!
# Observations about binary and binary operations:
* Bitshifting by 1 either adds or subtracts a multiple of 2 from each factor of 2.
** Take, for example, the number 10 (decimal)
** 10 = 8 + 2 = 1*(2^3) + 0*(2^2) + 1*(2^1) + 0*(2^0), or 1010 in binary.
** If you shift each of these bits to the left by 1, you get 10100.
** This took each of the 1 bits (multiples of 2) in the binary representation and multplied each by 2:
*** 10100 is 1*(2^4) + 0*(2^3) + 1*(2^2) + 0*(2^1) + 0*(2^0) = 16 + 0 + 4 + 0 + 0 = 20.
*** Notice that 20 = 16 + 4 = 2*8 + 2*2 = 2*(8 + 2).

tl;dr: The problems inherent in jQuery dependency should not be resolved by not depending on jQuery or some other $-providing library. Rather, library authors should write library code that expects a $ will be injected that meets the library's needs.

The problem:

In the front-end JS world, there is a movement to be dependency-free, and specifically to be jQuery dependency free. This problem seems to be the result of a pre-package management mindset. But those days are long gone, or, they should be.

The result of this mindset, however, tends to not be pretty: library authors write their own, often minimally tested, DOM manipulation and utility belt routines. If you come to depend on multiple or many such libraries, your code, via their code, then comes to contain dozens of diverse implementations of similarly or identically-purposed functions.

users table:

[
  {
     id: 123,
     things: [1, 2, 3]
  }
]
@dminkovsky
dminkovsky / Program
Created July 2, 2014 17:02
64-bit random numbers in Node
var bignum = require('bignum');
var crypto = require('crypto');
var randomBignum = function() { return bignum.fromBuffer(crypto.pseudoRandomBytes(8)); }
var avg = randomBignum();
var avg_len = avg.bitLength();
var lengths = {};
for (var i = 0; i < 1e6; i++) {
// Index:
r.table("heroes").indexCreate("idEquipment", function(doc) {
return doc("equipment").map(function(equipment) {
return [doc("id"), equipment]
})
}, {multi: true}).run(conn, callback)
// Query
r.table("heroes").getAll([1, "boots"], {index: "idEquipment"}).run(conn, callback)
r.db('test').table('crap').get("dd3badb0-9c30-4ef5-978a-c034461d58ca").update({
unit_data: r.literal(r.row('unit_data').without('state'))
})