Skip to content

Instantly share code, notes, and snippets.

View KalebNyquist's full-sized avatar

Kaleb Nyquist KalebNyquist

View GitHub Profile
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@KalebNyquist
KalebNyquist / spec.json
Created September 20, 2022 03:41
Vega-Lite spec from Mon Sep 19 2022
{
"config": {
"view": {
"continuousWidth": 400,
"continuousHeight": 400,
"strokeWidth": 0,
"stroke": "#000000"
}
},
"layer": [
@KalebNyquist
KalebNyquist / fav_palettes.py
Created June 19, 2022 05:20
Favorite Palettes
# Some of my favorite color palettes, including the original sources and the code I used to customize them to my liking
# Used as reference: http://tsitsul.in/img/colors/all_palettes/tableau.20.colorblind.png | http://tsitsul.in/blog/coloropt/
xgfs_normal11 = [(235, 172, 35), (184, 0, 88), (0, 140, 249), (0, 110, 0), (0, 187, 173), (209, 99, 230), (178, 69, 2), (255, 146, 135), (89, 84, 214), (0, 198, 248), (135, 133, 0), (0, 167, 108)]
xgfs_greys = [(88, 88, 88), (238, 238, 238)]
tableau_18 = [[ 31, 119, 180], [174, 199, 232], [255, 127, 14], [255, 187, 120], [ 44, 160, 44], [152, 223, 138], [214, 39, 40], [255, 152, 150], [148, 103, 189], [197, 176, 213], [140, 86, 75], [196, 156, 148], [227, 119, 194], [247, 182, 210], [188, 189, 34], [219, 219, 141], [ 23, 190, 207], [158, 218, 229]]
tableau_grey = [[127, 127, 127], [199, 199, 199]]
# Convert to Hex
from matplotlib import colors
@KalebNyquist
KalebNyquist / lookupZoteroCollections.js
Created August 8, 2021 21:44
One of my frustrations with Zotero is that there is no easy way to list all the collections in which a file is stored. This is my band-aid of a solution. The code can be run under Tools > Developer > Run Javascript using "Sync" mode.
// Instructions:
// (1) Highlight file of interest.
// (2) Run code in Zotero's JavaScript console (found under Tools > Developer > Run Javascript) in sync mode.
function collections_output() {
zp = Zotero.getActiveZoteroPane()
items = zp.getSelectedItems()
collection_ids = items[0]["_collections"]
collections_cache = Zotero.Collections._objectCache
@KalebNyquist
KalebNyquist / fox2020webscrape.py
Last active November 10, 2020 02:38
Webscrape Election 2020 Presidential Results (County Level) from FoxNews.com
import requests
import pandas as pd
from bs4 import BeautifulSoup
# List of States
# source: https://gist.github.com/mshafrir/2646763#gistcomment-1583853
states = {"al":"alabama","ak":"alaska","az":"arizona","ar":"arkansas","ca":"california","co":"colorado","ct":"connecticut","dc":"district of columbia","de":"delaware","fl":"florida","ga":"georgia","hi":"hawaii","id":"idaho","il":"illinois","in":"indiana","ia":"iowa","ks":"kansas","ky":"kentucky","la":"louisiana","me":"maine","md":"maryland","ma":"massachusetts","mi":"michigan","mn":"minnesota","ms":"mississippi","mo":"missouri","mt":"montana","ne":"nebraska","nv":"nevada","nh":"new hampshire","nj":"new jersey","nm":"new mexico","ny":"new york","nc":"north carolina","nd":"north dakota","oh":"ohio","ok":"oklahoma","or":"oregon","pa":"pennsylvania","ri":"rhode island","sc":"south carolina","sd":"south dakota","tn":"tennessee","tx":"texas","ut":"utah","vt":"vermont","va":"virginia","wa":"washington","wv":"west virginia","wi":"wisconsin","wy":"wyoming"}
states =
@KalebNyquist
KalebNyquist / airtable_helper.py
Last active August 6, 2024 02:14
Simple Download/Upload of Airtable Data into/from Python using Airtable API
import requests
import json
import pandas as pd
def airtable_download(table, params_dict={}, api_key=None, base_id=None, record_id=None):
"""Makes a request to Airtable for all records from a single table.
Returns data in dictionary format.
Keyword Arguments:
@KalebNyquist
KalebNyquist / find_locations_in_pandas_dataframe.py
Last active February 9, 2023 14:34
Find Index Location Values in Pandas Dataframe for Specific String Value
def find_locations_in_pandas_dataframe(dataframe, search_term, starts_with_search_term=False):
""" Returns an ordered list of (dataframe column, dataframe row, specific string) index values where `search_term` is
found in Pandas `dataframe` string data.
Keyword arguments:
starts_with_search_term: if set to True, then returns only strings that start with `search_term`
i.e., "Re" in "Rebecca" but not "González, Rebecca"
Source: https://gist.github.com/KalebNyquist/6781634b4ad307576046352696d2d194
"""
@KalebNyquist
KalebNyquist / step7.py
Created September 16, 2019 18:20
Medium Post Step 7
import json
upload_data = json.dumps(upload_dict)
headers = {
"Authorization" : "Bearer INSERT AIRTABLE USER API KEY HERE",
'Content-Type': 'application/json',
}
URL = 'https://api.airtable.com/v0/INSERT AIRTABLE DOC KEY HERE/Online%20Media%20Hits'
response = requests.post(URL, headers=headers, data=upload_data)
@KalebNyquist
KalebNyquist / step6.py
Created September 16, 2019 18:11
Medium Post Step 6
def convert_authors_to_ids(authors):
author_ids = []
for author in authors:
author_ids.append(author_dict[author])
return author_ids
upload_dict = {
"fields": {
"URL" : article.url,
"Title" : article.title,
@KalebNyquist
KalebNyquist / step5.py
Created September 16, 2019 18:00
Medium Post Step 5
author_dict = create_author_dict()
for author in article.authors:
if author in list(author_dict.keys()):
print("Author already exists")
else:
print(add_new_author(author))
author_dict = create_author_dict()