Skip to content

Instantly share code, notes, and snippets.

@jobliz
jobliz / selenium_test_firefox.py
Created June 3, 2018 17:29
Selenium basic test using headless firefox and arbitrary binary and driver
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
# https://askubuntu.com/questions/870530/how-to-install-geckodriver-in-ubuntu
# https://stackoverflow.com/questions/25713824/setting-path-to-firefox-binary-on-windows-with-selenium-webdriver
# http://selenium-python.readthedocs.io/getting-started.html
binary = FirefoxBinary(PATH_TO_BINARY)
@jobliz
jobliz / create_uci_retail_dataset_schema.py
Created February 23, 2018 13:31
UCI retail dataset CSV to sqlalchemy. Old ideas, rough code.
# http://archive.ics.uci.edu/ml/datasets/online+retail
import csv
import sys
import time
import datetime
from sqlalchemy import *
from sqlalchemy import create_engine
from sqlalchemy import sql
@jobliz
jobliz / excel.py
Created June 17, 2014 03:24
Basic Python functions for reading and writing Excel files.
import xlrd
import xlwt
import numpy as np
def read_excel(filename, n=0):
"""Converts first sheet from an Excel file into an ndarray
Parameters
----------
filename : string
@jobliz
jobliz / deriv.py
Created March 15, 2013 02:55
The definition of derivative "just works" when coded in a programming language.
# from http://funcall.blogspot.sg/2009/03/not-lisp-again.html
def deriv(f, dx=.0001):
return lambda x: (f(x + dx) - f(x)) / dx
cube = lambda x: x**3
d = deriv(cube)
for n in xrange(5):
print n, d(n)
import sys
import math
import numpy as np
from matplotlib import pyplot as plt
# https://en.wikipedia.org/wiki/Binomial_distribution
# k successes, n trials, p probability of success
def binomial_pmf(k, n, p):
nk = math.factorial(n) / (math.factorial(k) * math.factorial(n-k))
return nk * p**k * (1.0-p)**(n-k)
import sys
import itertools
import numpy as np
from collections import Counter
from matplotlib import pyplot as plt
def dice_prob(n):
sides = [(1,2,3,4,5,6) for x in xrange(n)]
combined = itertools.product(*sides)
sums = [sum(i) for i in combined]
demand_dist = (0.30, 0.45, 0.25) # [0]high, [1]medium, [2]low
positions = ( 36, 48, 60, 72, 84, 96)
high_demand = (0.05, 0.10, 0.25, 0.30, 0.20, 0.10)
med_demand = (0.10, 0.20, 0.30, 0.25, 0.10, 0.05)
low_demand = (0.15, 0.25, 0.35, 0.15, 0.05, 0.05)
demand_type = position_generator(demand_dist)
# be careful with order!
demand_amount = [
def profit(supply, demand, sale_price=0.40, production_cost=0.25, welfare_price=0.10, lost=0.15):
operative_cost = production_cost * supply
if supply >= demand:
excedent = supply - demand
sale_income = demand * sale_price
welfare_gain = excedent * welfare_price
result = sale_income + welfare_gain - operative_cost
else:
not_baked_loss = lost * (demand - supply)
sale_income = supply * sale_price
heights = ["high", "medium", "short"]
dist = [0.2, 0.6, 0.2]
generator = position_generator(dist)
n = 10000
count = 0
high = 0
med = 0
low = 0
import csv
import pylab as p
data = [r for r in csv.reader(open('piratebay.csv', 'rb'))]
def categoryratio(c):
return sum([ int(r[3]) for r in data if r[1][0] == str(c) ])
# Adapted from # http://scienceoss.com/bar-plot-with-custom-axis-labels/
fig = p.figure()