Skip to content

Instantly share code, notes, and snippets.

View crawftv's full-sized avatar
🎯
Focusing

Crawford Collins crawftv

🎯
Focusing
View GitHub Profile
@crawftv
crawftv / skopt-gp-test.py
Created April 11, 2019 05:11
Skopt Tutorial - Test Best GP model
model = create_model(gp_result.x[0],gp_result.x[1],gp_result.x[2],gp_result.x[3],gp_result.x[4],gp_result.x[5])
model.fit(X_train,y_train, epochs=3)
model.evaluate(X_test,y_test)
@crawftv
crawftv / flask-app-highlights.py
Last active April 25, 2019 02:52
Javascript, jinja tutorial
#The important imports for this tutorial
from flask import Flask, render_template, json
#Skiping a whole lot of the file to get to the useful parts.
"""Focusing on just this route that display a linechart for the sentiment of a topic over time."""
@app.route('/line', methods =["GET"])
def line_chart():
"""
@crawftv
crawftv / javascript-jinja.html
Last active May 22, 2019 21:18
JavaScript-jinja-html
<!--make the html element to contain the chart.-->
<canvas id="line-chart" width="3" height="1"></canvas>
{% block javascript %}
<script>
// load the objects like you would with a normal jinja template i.e. {{ object }}, but add | tojson
// pass it into the parse function
labels = JSON.parse({{ labels | tojson }})
data = JSON.parse({{ data | tojson }})
new Chart(document.getElementById("line-chart"),{
@crawftv
crawftv / genius_song_api_query.py
Last active May 22, 2019 16:48
Genius API Song Query
def request_song_info(session, song_num, song_urls):
base_url = "https://api.genius.com/songs/" + str(song_num)
headers = {"Authorization": "Bearer " + CLIENT_ACCESS_TOKEN}
response = requests.get(base_url, headers=headers)
try:
if response.json()["meta"]["status"] == 200:
song_urls.append(
(
song_num,
response.json()["response"]["song"]["title"],
@crawftv
crawftv / gather-api-query.py
Last active May 22, 2019 18:10
Gather-api-queries
async def get_data_asynchronous(min_song, max_song, song_urls):
"""
1. Establish an executor and number of workers
2. Establish the session
3. Establish the event loop
4. Create the task by list comprehensions
5. Gather tasks.
"""
with ThreadPoolExecutor(max_workers=40) as executor:
with requests.Session() as session:
@crawftv
crawftv / asyncio-query-imports.py
Last active May 22, 2019 16:46
asyncio query imports
from decouple import config
import json
import requests
import sys
import asyncio
from concurrent.futures import ThreadPoolExecutor
import nest_asyncio
nest_asyncio.apply()
@crawftv
crawftv / executre-asyncio-tasks.py
Last active May 22, 2019 18:10
execute asyncio tasks
def execute_async_event_loop(min_song, max_song, song_urls):
"""
This function does something analogous to compiling the get_data_asynchronously function,
Then it executes loop.
1. Call the get_data_function
2. Get the event_loop
3. Run the tasks (Much easier to understand in python 3.7, "ensure_future" was changed to "create_task")
4. Edge_list and top_interactions will be passed to the next functions
"""
future = asyncio.create_task(
@crawftv
crawftv / pickle-save.py
Created May 22, 2019 22:58
Pickling a thing
import pickle
filename = "saved_thing"
outfile = open(filename,'wb')
pickle.dump(thing, outfile)
outfile.close()
@crawftv
crawftv / unpickle-thing.py
Last active May 22, 2019 23:16
unpickling a thing
import pickle
filename = "saved_thing"
infile = open(filename, "rb")
thing = pickle.load(infile)
infile.close()
@crawftv
crawftv / postgreSQL_connection.py
Created May 31, 2019 05:47
connecting to postgres database
import psycopg2
import sqlalchemy
from decouple import config
#AWS_DATABASE_URL is 'postgres://username:password@database-instance-name.randomchars.us-east-2.rds.amazonaws.com:5432/databaseName
host = config('AWS_DATABASE_URL')
db = sqlalchemy.create_engine(host)