Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Benjamin Bengfort bbengfort

🎯
Focusing
View GitHub Profile
@bbengfort
bbengfort / balance.py
Created Dec 7, 2017
Database transactions blog post.
View balance.py
#!/usr/bin/env python3
import os
import logging
import psycopg2 as pg
from decimal import Decimal
from functools import wraps
from psycopg2.pool import ThreadedConnectionPool
@bbengfort
bbengfort / zipr.py
Last active Oct 21, 2021
Dealing with Zip archives and json data in Python
View zipr.py
#!/usr/bin/env python3
import os
import json
import random
import zipfile
config = {
"color": "red",
"amount": 42.24,
@bbengfort
bbengfort / venv_cheat_sheet.md
Created Aug 20, 2014
My virtualenv and virtualenv wrapper cheat sheet. I alias the commands of virtualenv and virtualenv wrapper for my own development environment.
View venv_cheat_sheet.md

Ben's VirtualEnv Cheatsheet

This cheat sheet describes my usage/implementation of virtualenv with virtualenv wrapper and the bash foo that I added with the help of many blogs to make it all tick together in fun land.

Quick Reference

$ echo $WORKON_HOME
/Users/benjamin/.virtualenvs

$ echo $PROJECT_HOME
@bbengfort
bbengfort / interval.py
Last active Sep 7, 2021
Run a function every n seconds using Python threading.
View interval.py
from threading import Timer
from functools import partial
class Interval(object):
def __init__(self, interval, function, args=[], kwargs={}):
"""
Runs the function at a specified interval with given arguments.
"""
self.interval = interval
@bbengfort
bbengfort / pproc.py
Created Jun 14, 2017
Runs multiple subprocesses in parallel, serializing stdout.
View pproc.py
#!/usr/bin/env python3
# pproc
# Runs multiple subprocesses in parallel, serializing stdout.
#
# Author: Benjamin Bengfort <benjamin@bengfort.com>
# Created: Wed Jun 14 15:20:05 2017 -0400
#
# Copyright (C) 2017 Bengfort.com
# For license information, see LICENSE.txt
#
@bbengfort
bbengfort / locks.go
Created Mar 3, 2017
Synchronized, thread safe buffer using mutex embedding
View locks.go
package main
import (
"flag"
"fmt"
"log"
"sync"
"time"
)
@bbengfort
bbengfort / sentiment.py
Last active Aug 10, 2021
An end-to-end demonstration of a Scikit-Learn SVM classifier trained on the positive and negative movie reviews corpus in NLTK.
View sentiment.py
import os
import time
import string
import pickle
from operator import itemgetter
from nltk.corpus import stopwords as sw
from nltk.corpus import wordnet as wn
from nltk import wordpunct_tokenize
@bbengfort
bbengfort / pi.c
Created Oct 17, 2015
OpenMP parallel integration to compute Pi.
View pi.c
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_THREADS 8
static long steps = 1000000000;
double step;
int main (int argc, const char *argv[]) {
@bbengfort
bbengfort / noplog.go
Created Mar 20, 2017
No-op logger to turn off logging in Go without too much of a performance hit.
View noplog.go
package noplog
import "log"
var noplog = &NopLogger{
log.New(NullWriter(1), "", log.LstdFlags),
}
// NullWriter implements the io.Write interface but doesn't do anything.
type NullWriter int
@bbengfort
bbengfort / nx2gt.py
Created Jun 26, 2016
Convert a networkx to graph-tool graph
View nx2gt.py
import networkx as nx
import graph_tool as gt
def get_prop_type(value, key=None):
"""
Performs typing and value conversion for the graph_tool PropertyMap class.
If a key is provided, it also ensures the key is in a format that can be