Skip to content

Instantly share code, notes, and snippets.

View cbare's full-sized avatar
👹
¯\_(ツ)_/¯

Christopher Bare cbare

👹
¯\_(ツ)_/¯
View GitHub Profile
@cbare
cbare / dynamic_object.py
Created January 31, 2013 01:07
How to get a dynamic object in Python. A dynamic object is just a bag of properties, some of which might happen to be functions, just like objects in javascript. Forget OOP. This is QDP - quick-n-dirty programming!
class Dynamic(dict):
"""Dynamic objects are just bags of properties, some of which may happen to be functions"""
def __init__(self, **kwargs):
self.__dict__ = self
self.update(kwargs)
def __setattr__(self, name, value):
import types
if isinstance(value, types.FunctionType):
self[name] = types.MethodType(value, self)
@cbare
cbare / test_requests_redirect.py
Created December 19, 2012 00:05
Show how to follow redirects, including rePOSTing, with the python requests library.
## follow redirects, including rePOSTing, with the requests library.
######################################################################
import requests
## authentication with no redirects
ans = requests.post(url=endp_prod + "/session", data=json.dumps(d), headers=h)
## results in a successful login
ans.status_code
@cbare
cbare / poker.R
Created September 9, 2012 06:38
Evaluate poker hands in R
## Poker.R
## Evaluate poker hands
##
## by: Christopher Bare
############################################################
## define suits and ranks
suits <- c('c','d','h','s')
ranks <- c(2:10,"J","K","Q","A")
suit_names <- c(c="clubs", d="diamonds", h="hearts", s="spades")
@cbare
cbare / parse_dicom_dt.py
Created July 3, 2022 22:49
Parse a Dicom formatted datetime string and return a Python datetime.
import datetime as dt
import pytest
import re
def parse_dicom_dt(dicom_dt):
"""
Parse a Dicom formatted datetime string and return a Python datetime.
The Dicom format is "YYYYMMDDHHMMSS.FFFFFF&ZZXX" described here:
"""
A simple HTTPS server
Generate cert and key with:
```sh
openssl req -newkey rsa:4096 -x509 -sha256 -days 3650 -nodes -subj '/CN=localhost' -out ./ssl/cert.pem -keyout ./ssl/key.pem
```
Test with httpie:
@cbare
cbare / extract_darwin_to_csv.py
Created November 22, 2021 23:19
Convert JSON output from V7 Darwin to csv.
"""
Convert JSON output from V7 Darwin to csv.
"""
import json
import os.path
import pandas as pd
filenames = [f"{name}.json" for name in list('abcde')]
data_dir = "data/lizzie-reader-study-test-part-1"
@cbare
cbare / talented_people.R
Created September 7, 2012 23:08
Example of R's S4 classes
## S4 classes for talented people
##
## Code to go along with a blog post about
## object oriented programming in R:
##
## http://digitheadslabnotebook.blogspot.com/2012/09/oo-in-r.html
##
################################################################
# define an S4 class for people
@cbare
cbare / recursive_upload.py
Last active January 30, 2021 02:50
Example of how to upload a directory hierarchy into a Synapse project
import synapseclient
from synapseclient import Synapse, File, Folder, Project
import os
def upload_recursive(directory, parent):
for item in os.listdir(directory):
if os.path.isdir(os.path.join(directory,item)):
print "%s/" % os.path.basename(item)
folder = syn.store(Folder(os.path.basename(item), parent=parent))
upload_recursive(directory=os.path.join(directory, item), parent=folder)
@cbare
cbare / overlap.R
Created October 24, 2012 19:35
Compute the intersection of all combinations of a list of vectors
## Compute the intersection of all combinations of the
## elements in the list of vectors l. Might be useful
## for generating Venn/Euler diagrams.
## There might be better ways to do this!
overlap <- function(l) {
results <- list()
# combinations of m elements of list l
for (m in seq(along=l)) {
@cbare
cbare / checkpoint.py
Last active April 21, 2020 14:13
create a new directory and put a checkpoint in it
#!/usr/bin/python
# crontab -e
# */20 * * * * /home/ubuntu/checkpoint.py > /home/ubuntu/checkpoint.log 2>&1
import os
hosts = []
with open('/usr/local/Rmpi/hostfile.plain') as f:
for line in f: