Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Benjamin Bengfort bbengfort

🎯
Focusing
View GitHub Profile
@bbengfort
bbengfort / rpsls.py
Created May 9, 2013
Learning to program with Rock, Paper, Scissors, Lizard, Spock; a class based version with an indexed data store for reference.
View rpsls.py
#!/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
A helper method for converting POST and JSON data to an expected Boolean object (Python bool).
View cast.py
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
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…
View bucket.py
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
View etcinit.dhttp-proxy.sh
#!/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 May 1, 2020
Encrypting and decrypting aes128 ciphers with an initialization vector in node.js.
View cipher-func.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 Jun 18, 2013
Current workings for the encode/decode for AES-256.
View encode-decode
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 Dec 18, 2015
An init.d script for starting and stopping the WIMsService
View wimss.sh
#! /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 Jun 27, 2013
Quick concordance search for a corpus.
View concordance.py
#!/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 Dec 19, 2015
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.
View multiconn.py
#!/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 Jul 2, 2013
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.
View clock.py
#!/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",