Skip to content

Instantly share code, notes, and snippets.

@oneroyalace
oneroyalace / congressGender.sql
Last active March 27, 2017 00:31
Number of females from each state in congress by year
--% of females elected to congress each y ear
with fLegs AS(
select date_part('year', start_date) as dated, state, count(b) as cnt
from persons a, person_roles b
where a.id = b.person_id
AND a.gender = 'F'
GROUP BY state, dated
ORDER BY dated
),
@oneroyalace
oneroyalace / numpy_array.py
Created May 28, 2018 05:39
Are numpy arrays really faster than python lists? Yes they are
import numpy as np
import timeit
std_arr = list(range(0,10000))
np_arr = np.array(std_arr)
#np_arr_vec = np.vectorize(lambda n: (n * 31) / 31)
def operate_on_std_array():
for index,elem in enumerate(std_arr):
std_arr[index] = elem * 31
@oneroyalace
oneroyalace / how_ye_feeling.py
Created June 6, 2018 20:44
Sentiment of Kanye's last tweet
import tweepy
from dotenv import load_dotenv
from nltk.tokenize import sent_tokenize
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import os
from os.path import join
load_dotenv(join(os.getcwd(), '.env'))
CONSUMER_TOKEN = os.environ['CONSUMER_TOKEN']
CONSUMER_SECRET = os.environ['CONSUMER_SECRET']

Preamble

Four score minus 78 years ago, Asa was struck by a particularly insightful thought--movies are fun to watch. So he started a movie club with Gordon for the purpose of watching movies. It occurred to the two of them that they could multiply the amount of fun being had in the movie club if they added more people1. So they added more people. Then the people wrote down movies they wanted to watch and week after week, the group watched them (they made it through 2 films that year). After its initial iteration, the movie club has respawneded in many forms. It is currently active in its current form. Anyway, that's the long and short of it. We the serious film critics of this esteemed movie club do ordain and establish this Constitution for the United Movie Club of America and Other Countries that Want to Participate.

Article I: Governance structure

As any real film bufftm could tell you, a movie club needs a very strong and active government. That's why the first article of thi

@oneroyalace
oneroyalace / random_imdb_movie.js
Created July 26, 2018 02:44
Outputs the name of a random movie from an imdb watchlist (runs in console)
var movieList = Array(document.getElementsByClassName('lister-item-header'))[0];
var randomNum = Math.floor(Math.random()*movieList.length);
console.log(movieList[randomNum].childNodes[0].innerText);
#include <stdio.h>
#include <string.h>
extern char *gets (char *__s);
int main()
{
char password[9];
char buffer[9];
strcpy(password,"security");
do
{
@oneroyalace
oneroyalace / readme.md
Created August 11, 2022 17:43
Solving Zorki session/intercept issues

Problem description

Zorki needs to end its Selenium session after it finishes an Instagram scrape, but because it's usually still forwarding requests when it finishes a scrape, it can't do so without raising Selenium errors.

To get around that, we need to ensure that Zorki ends its request forwarding earlier, waits for its queued requests to finish forwarding before it ends its Selenium session, or doesn't break when a session is quit while requests are still being forwarded.

Solution explanations

Wait for requests to finish forwarding before ending session

We could tell Zorki to sleep for n seconds before ending its Selenium session in the hopes that by then, all requests would be forwarded. But explicit sleeps aren't very nice, and there's always a chance that if we try to minimize the sleep duration, we'll run into an instance in which Selenium fails to forward requests before Zorki ends the Session (our problem right now).

from selenium.webdriver.common.by import By
import time
driver.get(url)
time.sleep(2)
link_pdf = driver.find_element(By.XPATH, "/html/body/div[1]/div[3]/div/main/article/div/div/div/div[3]/div[1]/table/tbody/tr[15]/td[2]/a").get_attribute('href')
# link_pdf = ...
driver.quit()