Skip to content

Instantly share code, notes, and snippets.

@dannvix
dannvix / EnableCopy.js
Last active August 29, 2015 14:01
Bookmarklet which enables select, context menu, and so on.
(function() {
function Revive (evnt) {
onEvent = "on" + evnt;
if(window.addEventListener) {
window.addEventListener(onEvent, function (e) {
for (var node = e.originalTarget; node; node = node.parentNode) {
[node] = null;
}
}, true);
window[onEvent] = null;
@dannvix
dannvix / django-middlewares.py
Created July 29, 2014 12:10
_method override & request.params["id"]
class DjangoMethodOverrideMiddleware(object):
METHODS = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'TRACE', 'OPTIONS', 'CONNECT']
def process_request(self, request):
method = request.REQUEST.get("_method", None)
if method is None:
return
else:
method = method.upper()
if method in self.METHODS:
request.method = method
@dannvix
dannvix / django-models-record-status-history-mixin.py
Last active August 29, 2015 14:04
Django models RecordStatusHistoryMixin (pre_save hook)
from django.db import models
from jsonfield import JSONField
from dirtyfields import DirtyFieldsMixin
import time
class RecordStatusHistoryMixin(DirtyFieldsMixin):
def save(self, *args, **kwargs):
if not self.pk:
# first create
current_time = int(time.time())
@dannvix
dannvix / list-more-than-2-files.sh
Last active August 29, 2015 14:07
List all directories that contain >= 2 files/directories
#!/bin/sh
# dependencies: find, grep, sort, uniq, awk
echo 'List all files...'
find $1
# output:
# a
# a/b
# a/b/c
# a/b/c/d
@dannvix
dannvix / cpp11-type-traits.cpp
Last active August 29, 2015 14:09
Playing with C++11 type traits and templates
#include <iostream>
#include <iterator>
#include <vector>
#include <set>
// overload for all STL containers
template <
typename T,
typename = typename std::enable_if <
std::is_convertible<
@dannvix
dannvix / hamming.py
Last active August 29, 2015 14:10
Hamming distance implementation benchmark
#!/usr/bin/env python
# ref. https://medium.com/on-coding/shorter-code-is-inconsiderate-41cce917b51b
import timeit
setup = '''
def hamming_fp(a, b):
return sum(x != y for x, y in map(None, a, b))
def hamming(s1, s2):
hamming_number = 0
#include <iostream>
template <typename lhs_t, typename func_t>
inline func_t operator > (lhs_t const &lhs, func_t rhs) {
return rhs;
}
template <typename rhs_t, typename func_t>
inline auto operator < (func_t const &lhs, rhs_t const &rhs) -> decltype(lhs())
{
@dannvix
dannvix / ext_id.cpp
Last active August 29, 2015 14:15
Clang 3.3+ supports extended identifiers
// clang++ hello.cpp
#include <iostream>
template <typename lhs_t, typename rhs_t>
inline int operator, (lhs_t lhs, rhs_t b) {
std::cout << "飲冰室茶集" << std::endl;
return 0;
}
template <typename lhs_t, typename rhs_t>
@dannvix
dannvix / member_function_reflection.cpp
Last active August 29, 2015 14:16
SFINAE test for specific member function using C++11
#include <iostream>
#include <type_traits>
class Foo {
public:
int ToString();
};
class Bar {
public:
@dannvix
dannvix / bitset.js
Last active August 29, 2015 14:17
Very simple bitset operations in JavaScript
/* bitset.js
*
* bitset = new Bitset(64);
* bitset.toString(); // "0000000000000000000000000000000000000000000000000000000000000000"
* bitset.setBit(48, 1).setBit(23, 1).setBit(52, 1);
* bitset.toString(); // "0000000000010001000000000000000000000000100000000000000000000000"
* bitset.toInt(); // 4785074612469760
*
* bitset.fromInt(679642164705874);
* bitset.toString(); // "0000000000000010011010100010000110000101110011010011101001010010"