Skip to content

Instantly share code, notes, and snippets.

View bbengfort's full-sized avatar
🎯
Focusing

Benjamin Bengfort bbengfort

🎯
Focusing
View GitHub Profile
@bbengfort
bbengfort / rpsls.py
Created May 9, 2013 01:05
Learning to program with Rock, Paper, Scissors, Lizard, Spock; a class based version with an indexed data store for reference.
#!/usr/bin/env python
"""
The idea of this program is to equate the strings "rock", "paper",
"scissors", "lizard", "Spock" to numbers as follows:
0 - rock
1 - Spock
2 - paper
3 - lizard
4 - scissors
@bbengfort
bbengfort / cast.py
Created May 9, 2013 01:33
A helper method for converting POST and JSON data to an expected Boolean object (Python bool).
FALSE_STRINGS = ("0", "false", "[]", "{}", "()", "None", "no")
def boolean(obj):
"""
A helper method for converting POST and JSON data
to an expected Boolean object (Python bool).
We expect that the following are false:
0
@bbengfort
bbengfort / bucket.py
Created May 23, 2013 19:14
A chunker that appends data to file buckets at a specified directory, such that no file exceeds the size specified. It will append the data to the first file that has enough space available, if none do, then it appends the data to a new file. Note that an exception is raised if the data is bigger than the maximum file size. To test it with rando…
import os
import sys
DEFAULT_SIZE = 128 * 1024 * 1024 #128 MB
class Bucket(object):
"""
Handles the writing of a stream into a bucket.
Pass a path to a directory which holds the bucket, and
#!/bin/bash
DIR=/var/www/YOUR_APP_NAME
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NODE_PATH=/usr/local/lib/node_modules
case $1 in
start)
nohup "node" "$DIR/proxy.js" 1>>"$DIR/logs/proxy.log" 2>&1 &
echo $! > "$DIR/pids/proxy.pid";
;;
@bbengfort
bbengfort / cipher-func.js
Last active December 20, 2021 01:13
Encrypting and decrypting aes128 ciphers with an initialization vector in node.js.
var crypto = require('crypto');
var secret = crypto.randomBytes(24);
function encrypt(plaintext) {
var cipher = crypto.createCipher('aes-256-cbc', secret);
cipher.setAutoPadding(false);
var ciphertext = '';
for (var i=0; i < plaintext.length; i+=16) {
ciphertext += cipher.update(plaintext.substr(i, i+16), 'utf8', 'base64');
@bbengfort
bbengfort / encode-decode
Created June 18, 2013 19:37
Current workings for the encode/decode for AES-256.
var crypto = require('crypto');
var secret = crypto.randomBytes(24);
function encrypt(plaintext) {
var encoded = new Buffer(plaintext, 'utf8', 'hex');
var cipher = crypto.createCipher('aes-256-cbc', secret);
// cipher.setAutoPadding(false);
var ciphertext = cipher.update(encoded, 'hex');
return ciphertext.toString('base64');
@bbengfort
bbengfort / wimss.sh
Last active December 18, 2015 21:49
An init.d script for starting and stopping the WIMsService
#! /bin/sh
# Copyright (c) 2013 OpenWIMs.org
# All rights reserved
#
# Author: Benjamin Bengfort, 2013
#
# /etc/init.d/wimss
### BEGIN INIT INFO
@bbengfort
bbengfort / concordance.py
Created June 27, 2013 20:40
Quick concordance search for a corpus.
#!/usr/bin/env python
from nltk import Text
from nltk.corpus import PlaintextCorpusReader
CORPUS_ROOT = '/Users/benjamin/Development/corpora/gutenburg/'
if __name__ == "__main__":
reader = PlaintextCorpusReader(CORPUS_ROOT, '.*')
@bbengfort
bbengfort / multiconn.py
Last active December 19, 2015 02:49
An example showing how to fetch multiple links from the same host with the same, open httplib.HTTPConnection object. The real question is what happens when you send a Connection:Close header.
#!/usr/bin/env python
# multiconn.py
#
# Author: Benjamin Bengfort <benjamin@bengfort.com>
# $ID: multiconn.py [1] benjamin@bengfort.com $
"""
An example showing how to fetch multiple links from the same host with the
same, open httplib.HTTPConnection object. This is the basis for an attempt
@bbengfort
bbengfort / clock.py
Created July 2, 2013 19:01
A quick command line utility for printing out dates formatted as you'd like them. I use this utility to quickly print out JSON formatted strings, or strings for use in code documentation.
#!/usr/bin/env python
import sys
from datetime import datetime
from dateutil.tz import tzlocal
class Clock(object):
FORMATS = {
"code":"%a %b %d %H:%M:%S %Y %z",