Skip to content

Instantly share code, notes, and snippets.

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

Christopher Bare cbare

👹
¯\_(ツ)_/¯
View GitHub Profile
@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
## first, let's dump some data into a file
python generate_random_data.py > data.txt
## Create a project in synapse
synapse create -name "provenance test project" Project
## Add a couple of entities to our project, first a Data entity...
synapse add -parentid syn1661878 -name "data.txt" -description "My randomly generated data" data.txt
## ...and a Code entity that holds the script
@cbare
cbare / load_synapse_data_file.R
Created January 29, 2013 22:25
The R client for Sage Synapse works transparently with R data files. Using other types of files requires a bit more work. Here's a short demonstration of how to do it.
## Loading with files in the R synapse client
##################################################
## If you haven't already, install the synapse R client
# source('http://depot.sagebase.org/CRAN.R')
# pkgInstall(c("synapseClient"))
library(synapseClient)
## list the files in a project
@cbare
cbare / test_filehandle_upload.py
Created January 30, 2013 23:59
Test out the new fileHandle API from python.
##
## upload a file with the synapse fileHandle service
############################################################
## import the synapse library and sign in
import synapseclient
syn = synapseclient.Synapse()
syn.login('[email protected]', 'secret')
## the file we're going to upload
@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 / synapse_stats.py
Created January 31, 2013 21:33
Gather statistics on public data sets in Sage Synapse
import synapseclient
import re, sys
import requests
syn = synapseclient.Synapse()
syn.login('[email protected]', 'secret')
repoEndPoint = 'https://repo-prod.prod.sagebase.org/repo/v1'
headers = {'Accept': 'application/json', 'sessionToken': syn.sessionToken}
##
## example session with the python synapse client
## Note: You'll have to insert your credentials and a project ID
######################################################################
## import the synapse library and sign in
import synapseclient
syn = synapseclient.Synapse()
syn.login('[email protected]', 'secret')
@cbare
cbare / regex_capture_groups.R
Last active December 12, 2015 04:48
An example of regular expressions using capture groups in R.
## An example of regular expressions using capture groups
## in R. See:
## http://stackoverflow.com/questions/14700799/r-regex-gsub-extract-part-of-pattern/14714370#14714370
############################################################
# example data
data <-
"Station lat lon
1940 K01R 31-08N 092-34W
1941 K01T 28-08N 094-24W
Playing with Earthquake Data
========================================================
This is a little example inspired by Jeff Leek's [Data analysis class](https://class.coursera.org/dataanalysis-001/). Jeff pointed us towards some data from the [US Geological Survey](http://www.usgs.gov/) on earthquakes. I believe these are the earthquakes for the last 7 days. I decided to see what I could do with it.
```{r}
fileUrl <- "http://earthquake.usgs.gov/earthquakes/catalogs/eqs7day-M1.txt"
download.file(fileUrl,destfile="./data/earthquake_7day.csv",method="curl")
eq <- read.csv("./data/earthquake_7day.csv")
```
@cbare
cbare / entity.py
Last active December 14, 2015 08:58
A first cut at an entity implementation for the Python client that (mostly) resolves the weird split between properties and annotations.
import collections
class DictObject(collections.MutableMapping):
def __init__(self, *args, **kwargs):
for arg in args:
if isinstance(arg, collections.Mapping):
self.__dict__.update(arg)
self.__dict__.update(kwargs)