Skip to content

Instantly share code, notes, and snippets.

int unbiased_random_bit() {
int x1, x2, prev;
prev = 2;
x1 = rand() % 2;
x2 = rand() % 2;
for (;; x1 = rand() % 2, x2 = rand() % 2)
{
if (x1 ^ x2) // 01 -> 1, or 10 -> 0.
{
@ttback
ttback / install_libvips.sh
Last active August 29, 2015 14:02
Install libvips on Amazon EC2
LIBVIPS_FULL_VERSION=7.40.2
LIBVIPS_MAJOR_VERSION=echo $LIBVIPS_VERSION | sed 's/v//g' | sed 's/[^.]*$//' | sed 's/[.| ]*$//g'
curl -o vips-build.tar.gz http://www.vips.ecs.soton.ac.uk/supported/$LIBVIPS_MAJOR_VERSION/vips-$LIBVIPS_FULL_VERSION.tar.gz
mkdir vips-build && tar -xf vips-build.tar.gz -C vips-build --strip-components=1
cd vips-build
./configure
make && sudo make install
export LD_LIBRARY_PATH=/usr/local/lib
export LD_RUN_PATH=/usr/local/lib
@ttback
ttback / shortcuts_timesmachine.markdown
Last active December 2, 2019 19:00
Unofficial Keyboard Shortcuts for TimesMachine

esc: exit current user input context.

shift+/: open help modal


: move left

: move right

@ttback
ttback / gist:7474582
Last active December 28, 2015 08:49
Passing sys.stdin to sys.stdin of a subprocess
#!/usr/bin/python2.7
import subprocess
import os
import sys
from tempfile import NamedTemporaryFile
with NamedTemporaryFile('w', delete=False) as fout:
fout.write('import sys; print sys.stdin')
print subprocess.check_output(['python2.7', fout.name], stdin=sys.stdin)
@ttback
ttback / gist:7469849
Last active December 28, 2015 08:09
Python custom error handler with lock
class ErrorHandler (logging.Handler):
def __init__(self):
logging.Handler.__init__(self)
self.lock = multiprocessing.Lock()
self.errorLogMessages = list()
def emit (self, record):
print record
self.lock.acquire()
print "lock acquired"
try:

Compiling/Installing Node 0.8.4 (and Python 2.6, required by Node) on CentOS 5

Update system packages -- will migrate system forward to CentOS 5.8. (Optional?)

$ sudo yum update

Install the EPEL Repo:

uglify: {
development: {
options: {
banner: '/*! Development <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n',
mangle: false,
compress: false,
preserveComments: 'all',
beautify: true
},
all: {
@ttback
ttback / tiffblur.py
Last active December 26, 2015 21:59
Blur TIFF
from PIL import Image
from PIL import ImageFilter
pageImage = Image.open('sample.tiff')
pageImage = pageImage.convert('L')
pageImage.info['compression']='raw'
pageImage=pageImage.filter(ImageFilter.GaussianBlur(4))
pageImage.save('/out/sample.tiff')
@ttback
ttback / customLogHandlers
Last active December 23, 2015 11:18
Simple Custom Log Handlers in python
import custom_logger
import logging
import logging.handlers
class Runner ():
def __init__(self):
pass
def run(self):
#logging.basicConfig(filename="Task.log",filemode='w', fmt = '%(asctime)s - %(levelname)s - %(module)s - %(message)s',datefmt = '%m/%d/%Y %I:%M:%S %p', level=logging.DEBUG)
@ttback
ttback / express-multi-params
Created November 14, 2012 03:51
express multiple params
app.post('/postParams/:message/:description/:id', function(){
console.dir(req.query);
});
//nothing happens when trying to query
//http://localhost:8080/postParams?message=hello&description=desc&id=1