Skip to content

Instantly share code, notes, and snippets.

View jamesdunham's full-sized avatar

James Dunham jamesdunham

  • Center for Security and Emerging Technology
  • New York
View GitHub Profile
library(testthat)
devtools::load_all()
names_to_levels = function(item_names, gt_names) {
vapply(item_names, function(item_name) {
sum(grepl(paste0('^', item_name, '_gt[0-9]+$'), gt_names))
}, integer(1))
}
test_that('names_to_levels recovers number of choices per item given item names', {
expect_equal(names_to_levels('abortion', 'abortion_gt1'), c('abortion' = 1))

Keybase proof

I hereby claim:

  • I am jamesdunham on github.
  • I am jdunham (https://keybase.io/jdunham) on keybase.
  • I have a public key ASBfo4fghAbkFJnfASnep_tSPk_gKGjT1Gx-PnY8SxgcfQo

To claim this, I am signing this object:

@jamesdunham
jamesdunham / copy_from.py
Created August 6, 2019 13:33
Load a DataFrame into Postgres
from io import StringIO
import psycopg2
from tqdm import tqdm
def copy_from(df: pd.DataFrame,
table: str,
connection: psycopg2.extensions.connection,
chunk_size: int = 10000):