Skip to content

Instantly share code, notes, and snippets.

@adam704a
Created April 7, 2016 15:50
Show Gist options
  • Save adam704a/a899cf4c9db0ea4cbcdd4beea2470142 to your computer and use it in GitHub Desktop.
Save adam704a/a899cf4c9db0ea4cbcdd4beea2470142 to your computer and use it in GitHub Desktop.
sample notebook
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use the API to create some test data\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Local Docker\n",
"auth_url = 'http://web:8000/api-token-auth/'\n",
"participant_url = 'http://web:8000/participant/'\n",
"submission_url = 'http://web:8000/submission/'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# AWS (demo)\n",
"auth_url = 'https://researchnet.ictedge.org/api-token-auth/'\n",
"participant_url = 'https://researchnet.ictedge.org/participant/'\n",
"submission_url = 'https://researchnet.ictedge.org/submission/'"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Local\n",
"auth_url = 'http://127.0.0.1:8000/api-token-auth/'\n",
"participant_url = 'http://127.0.0.1:8000/participant/'\n",
"submission_url = 'http://127.0.0.1:8000/submission/'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import requests\n",
"payload = {'username': 'researcher', 'password':'******'}\n",
"r = requests.post(auth_url, json=payload)\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'**************************'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"r.json()['token']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Make up some stuff"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import random\n",
"\n",
"first_names = ['Pat', 'Jessie', 'Same', 'Sue', 'Chris', 'Piper']\n",
"last_names = ['Smith', 'Jones', 'Roberts', 'Walters', 'Thomas']\n",
"gender_options = ['Male', 'Female']\n",
"\n",
"# lat/long\n",
"fake_places = [\n",
" (\"40.5895466\", \"-105.0751243\"),\n",
" (\"41.5895466\", \"-104.0751243\"),\n",
" (\"39.7640021\",\"-105.1353041\"),\n",
" (\"38.7640021\",\"-104.1353041\"),\n",
" (\"39.7724466\",\"-105.9249464\"),\n",
" (\"35.7724466\",\"-103.9249464\"),\n",
" (\"39.1490189\",\"-107.1444289\"),\n",
" (\"38.1490189\",\"-106.1444289\"),\n",
" (\"36.0017455\",\"-79.0249976\"),\n",
" (\"37.0017455\",\"-80.0249976\"),\n",
" (\"35.9661857\",\"-79.1067084\"),\n",
" (\"34.9661857\",\"-78.1067084\"),\n",
" (\"35.8436867\",\"-78.7851426\"),\n",
" (\"34.8436867\",\"-75.7851426\"),\n",
" (\"45.5423508\",\"-122.7945071\"),\n",
" (\"44.5423508\",\"-121.7945071\")\n",
" ]\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from random import randrange\n",
"from datetime import timedelta, datetime\n",
"\n",
"def random_date(start, end):\n",
" \"\"\"\n",
" This function will return a random datetime between two datetime \n",
" objects.\n",
" \"\"\"\n",
" delta = end - start\n",
" int_delta = (delta.days * 24 * 60 * 60) + delta.seconds\n",
" random_second = randrange(int_delta)\n",
" return start + timedelta(seconds=random_second)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"d1 = datetime.strptime('1/1/1967', '%m/%d/%Y')\n",
"d2 = datetime.strptime('1/1/1987', '%m/%d/%Y')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load Enrollments\n",
"The step step here is to generate some sample data. Feel free to adjust this as needed."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for i in range(10):\n",
" random_birthday = random_date(d1, d2)\n",
" payload = {'username': 'testuser'+str(i), \n",
" 'password':'testpassword', \n",
" 'first_name': random.choice(first_names),\n",
" 'last_name': random.choice(last_names),\n",
" 'email': '[email protected]',\n",
" 'dob': str(random_birthday.strftime('%Y-%m-%d')),\n",
" 'gender': random.choice(gender_options),\n",
" }\n",
" \n",
" r = requests.post(participant_url, json=payload)\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load Submissions"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Randomize test data (its more interesting this way)\n",
"moods_category = ['good', 'bad', 'fine', 'unsure', 'no response', 'asleep']\n",
"mood_scale = ['1','2','3','4','5','6','7','8','9','0']\n",
"mood_image = ['1','2','3','4','5','6','7','8','9','0']"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import random\n",
"\n",
"# loop through the same participants loaded earlier\n",
"for i in range(10):\n",
" \n",
" # Get the token\n",
" auth_payload = {'username': 'testuser'+str(i), 'password':'testpassword'}\n",
" r_auth = requests.post(auth_url, json=auth_payload)\n",
" auth_token = r_auth.json()['token']\n",
" \n",
" for y in range(random.randrange(1, 10, 2)):\n",
" headers = {'Authorization': 'Token '+auth_token }\n",
" submission_payload = {'device_id': 'TEST-49D5-4CAD-AE42-E5CE922A3346', \n",
" 'lat' : random.choice(fake_places)[0],\n",
" 'long': random.choice(fake_places)[1], \n",
" 'response': { \n",
" 'mood cateogry': random.choice(moods_category),\n",
" 'mood scale': random.choice(mood_scale),\n",
" 'mood image': random.choice(mood_image)\n",
" }\n",
" }\n",
" \n",
" r_sub = requests.post(submission_url, json=submission_payload, headers=headers)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'{\"id\":82,\"user\":\"testuser9\",\"time_start\":\"2016-03-30T14:54:18.925491\",\"time_complete\":\"2016-03-30T14:54:18.925502\",\"timestamp\":\"2016-03-30T20:54:18.926070Z\",\"device_id\":\"TEST-49D5-4CAD-AE42-E5CE922A3346\",\"response\":{\"mood scale\":\"4\",\"mood image\":\"8\",\"mood cateogry\":\"fine\"},\"lat\":39.1490189,\"long\":-103.9249464}'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"r_sub.text"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'Fort Collins'"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"import geocoder\n",
"g = geocoder.google([40.5895466, -105.0751243], method='reverse')\n",
"g.city"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment