Skip to content

Instantly share code, notes, and snippets.

# Or: require 'facets/enumerable/find_yield'
module Enumerable
def map_detect
self.each do |member|
if (result = yield(member))
return result
end
end
nil
end
@tokland
tokland / monitor.sh
Last active December 17, 2015 18:29
Simple generic monitoring tool using a finite-state-machines implementation.
#!/bin/bash
#
# Simple monitoring using a finite state machines
set -e -u -o pipefail
# Print debug output to standard error
debug() {
echo "[${FUNCNAME[1]}] $@" >&2
}
@tokland
tokland / dhondt.py
Last active December 18, 2015 11:50
Pure functional implementation of D'Hondt method
# Pure functional implementation of D'Hondt method
from itertools import islice, count
from collections import Counter
from heapq import merge
def percentage(numerator, denominator):
return 100.0 * numerator / denominator
def take(n, it):
@tokland
tokland / importer.rb
Last active December 20, 2015 20:59 — forked from regedarek/importer.rb
class Importer
CODES = {:header, "00F", :data => "00I"}
def self.import_products(fileobj)
fileobj.lines.each_slice(4).map do |group_lines|
type, *data_lines = group_lines.map(&:strip)
case type
when CODES[:header]
# what to do/return here?
when CODES[:data]
@tokland
tokland / Annotations.java
Last active January 29, 2016 11:03
Basic Runtime Annotation
import java.lang.annotation.Target;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.ElementType;
import java.lang.reflect.Method;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
String name() default "";
@tokland
tokland / spanish_dni_nif_validator.js
Last active March 9, 2016 11:01
DNI/NIF validators (Spain ID documents)
/* Return sum of numbers in array xs */
function sum(xs) {
return xs.reduce(function(a, b) { return a + b }, 0);
}
/* Return true if string s is a valid DNI */
function validateDNI(s) {
var control_letters = 'TRWAGMYFPDXBNJZSQVHLCKE';
var regexp = /^[\dKLMXYZ]\d{7}[A-Z]$/;
import sys
import signal
from PyQt4 import QtCore, QtGui, QtWebKit
from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkCookieJar, QNetworkCookie
class ExtendedNetworkCookieJar(QNetworkCookieJar):
def mozillaCookies(self):
"""
Return all cookies in Mozilla text format
@tokland
tokland / coursera-download-videos.rb
Last active November 24, 2016 08:14
Download video lectures from Coursera courses.
#!/usr/bin/ruby
#
# Download lecture videos of courses from Coursera (http://www.coursera.org).
#
# Install requirements:
#
# $ gem install curb trollop nokogiri capybara ruby-progressbar
#
# Example -- Download all video lectures of courses "Calculus: Single Variable"
# and "Introduction to Astronomy":
require 'capybara'
# $ xvfb-run rspec -f documentation chrome-extension_spec.rb
#
# ./myext -- Directory containing the unpacked extension
class ChromeBrowser
include Capybara::DSL
def initialize(chrome_flags = {})
<html xmlns="http://www.w3.org/1999/xhtml" lang="es" xml:lang="es">
<head>
<title>Testing DataTable/ColOrder with async call</title>
<script src="http://code.jquery.com/jquery-1.12.4.js"></script>
<script src="http://nightly.datatables.net/js/jquery.dataTables.min.js"></script>
<script src="http://nightly.datatables.net/colreorder/js/dataTables.colReorder.js"></script>
<script>
$(document).ready(function() {
var table = $('#example').DataTable({