Skip to content

Instantly share code, notes, and snippets.

View Flushot's full-sized avatar
👀

Chris Lyon Flushot

👀
View GitHub Profile
@Flushot
Flushot / loggedonuser.cmd
Created February 19, 2014 02:31
Same as loggedonuser.sh, but for windows
@echo off
for /f "usebackq delims=\ tokens=2 skip=1" %%a in (`wmic ComputerSystem Get UserName`) do echo:%%a
@Flushot
Flushot / ones_complement.py
Last active August 29, 2015 14:06
One's complement in Python
def ones_complement(x):
return x ^ ((1 << x.bit_length()) - 1)
@Flushot
Flushot / php_emit.php
Last active August 29, 2015 14:10
Keeps a clean separation between legitimate content and error messages
#!/usr/bin/env php
<?php
$_emit_shutdown_function_registered = false;
/**
* Emits (using echo()) the return value of a closure function.
* Keeps a clean separation between legitimate content and error messages.
*
* If any type of fatal error or Exception was encountered during evaluation,
#!/usr/bin/env python
import re
primePat = re.compile(r'^1?$|^(11+?)\1+$')
# http://primes.utm.edu/lists/small/1000.txt
primes = [int(x.strip()) for x in '''
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97 101 103 107 109 113
from random import random
from bisect import bisect
def weighted_choice(choices):
values, weights = zip(*choices)
total = 0
cum_weights = []
for w in weights:
total += w
cum_weights.append(total)
@Flushot
Flushot / html_builder.php
Last active August 29, 2015 14:17
HTML builder for PHP
<?php
/**
* HTML builder
* @author Chris Lyon <flushot@gmail.com>
*
* This functional HTML builder was inspired by JSX and eliminates the need to use the unsightly
* and error-prone string concatenation method of building HTML markup. You can now cleanly use
* nested functions as children and associative arrays as tag attributes.
*
def dfs_visit_key(tree, key_re, visitor_fn):
return dfs_walk(tree, \
lambda k, fk, val: \
visitor_fn(fk, val) \
if re.match(key_re, '.'.join(fk)) \
else val)
def dfs_walk(tree, visitor_fn, fullkey=None):
fullkey = list(fullkey or [])
if isinstance(tree, dict): # subtree
@Flushot
Flushot / worker.py
Last active August 29, 2015 14:21
Multithreaded queue worker
from abc import ABCMeta, abstractmethod
import logging
import multiprocessing
import threading
import time
try:
import queue
except ImportError:
import Queue as queue
@Flushot
Flushot / json_decode_ex.php
Created June 2, 2015 01:53
Improved version of json_decode that throws on decode failures and uses more sane defaults
/**
* A more sane version of json_decode()
*
* @param $json
* @param $assoc when TRUE, json will be decoded as an associative array instead of an object.
* @param $depth max stack depth for recursion.
* @throws DomainException when parsing failed.
* @return the decoded value as an associative array or object (depending on what $assoc was set to).
*/
function json_decode_ex($json, $assoc=true, $depth=512) {
@Flushot
Flushot / github_link_graph.py
Last active September 17, 2015 22:47
Builds a graph visualization (viewable in yEd) of linked pages in a GitHub wiki repository