Skip to content

Instantly share code, notes, and snippets.

View darius's full-sized avatar

Darius Bacon darius

View GitHub Profile
"""
Generate a random token.
"""
# After reading Zooko's, I was like oops, I'm an idiot.
# I should probably use his, but anyway here's a fix:
# correct cryptogen.sample() to cryptogen.choice().
import math
from random import SystemRandom
"""
machine = assignment*.
assignment = lvalue _? '=' _? expr _? '\n'.
expr = lvalue _ lvalue _ lvalue.
lvalue = name ('.' literal)*.
literal = '0' | '1' | name.
name = /[^.]+/.
_ = /\s+/.
comment = /#.*/.
"""
Not a venture capitalist, a version control thingy.
https://hackpad.com/Make-a-Local-Version-Control-System-qUPsMncVjBx
"""
import datetime, itertools, os, re, shutil, sys
def main(argv):
if len(argv) < 2:
print 'usage XXX'
superseded by https://github.com/darius/sketchbook/blob/master/trees/bplustrees.py
"""
Given a sorted array A[:n] with no dups, which has been rotated by some r,
0 <= r < n, find r in O(n lg n) time.
i.e. Return r such that A[r:] + A[:r] is sorted.
"""
def unrotate(A):
lo, hi = 0, len(A)
while lo+1 < hi:
if lo+2 == hi:
"Erase that part of the log not yet written to the logfile."
(setq keylogger-events '()))
(defun keylogger-log ()
;; (Would be `with-demoted-errors' instead if not for paranoia from
;; an actual user.)
(ignore-errors
(when (and (this-command-keys)
(keylogger-loggable-p))
(push (vector (current-time) major-mode (this-command-keys))
;;; Record all keystrokes, with timestamps at millisecond resolution.
;;; (Actually it's Emacs commands instead of keystrokes, but that's close enough.)
;;; Restricted to particular modes, but I really advise NOT RUNNING
;;; THIS AT ALL if you do much of anything sensitive inside Emacs.
(defvar keylogger-path "/home/darius/git/keystroke-analysis/emacs-key-log"
"Where to store the log.")
(defvar keylogger-major-modes '(text-mode)
;; Track the recent typing rate to see whether it exceeds a target rate, using
;; a method vaguely like https://en.wikipedia.org/wiki/Exponential_smoothing
;; Here's the scheme: We keep track of a smoothed rate. Each keystroke
;; increases it by 1; each second that passes multiplies it by a decay
;; factor less than 1. To deal with noninteger time intervals, the
;; decaying actually happens smoothly as an exponential in time.
;; This method has the disadvantage that the estimated rate just after
;; a keystroke is always >=1, which is 60/5 = 12wpm. The lower the
(setq target-cps 5.0)
(setq smoothed-rate 0)
(setq last-stroke-time 0)
(set-hook '(lambda ()
(let ((now (current-time)))
(setq smoothed-rate (+ 1 (decay-smoothed-rate now)))
(setq last-stroke-time now))))
(set-time '(lambda ()
;; Character codes from https://en.wikipedia.org/wiki/Block_Elements
(defconst barchart--bar-chars [32 #x258f #x258e #x258d #x258c #x258b #x258a #x2589 #x2588])
(defun barchart-make-bar (length fraction)
"Return a string of the given length that looks like a
horizontal bar representing a real number between 0 and 1 (the
fraction)."
(assert (<= 0 fraction 1))
(let ((f (* fraction length))
(chars '()))