Skip to content

Instantly share code, notes, and snippets.

View scruwys's full-sized avatar

Scott Cruwys scruwys

  • Segment
  • San Francisco, CA
View GitHub Profile
@scruwys
scruwys / example_dag.py
Created October 9, 2017 15:17
testing in airflow
# -*- coding: utf-8 -*-
"""
Example DAG to highlight testing process.
Owner: Scott Cruwys (scruwys@activecampaign.com)
Runs: @daily
"""
############### Airflow dependencies ###############
import pandas
import numpy as np
import operator
from itertools import compress
from sklearn.tree import DecisionTreeClassifier
from sklearn.feature_selection import RFE
# monkey patch to add coef_ class variable...
class DecisionTreeClassifierWithCoef(DecisionTreeClassifier):
def fit(self, *args, **kwargs):
@scruwys
scruwys / ajax_form.js
Created July 7, 2015 19:28
dynamic ajax forms with jquery
$.fn.extend({
submitAsync: function(params) {
var method = $(this).attr('method');
var defaults = {
type : ( method === undefined ) ? 'POST' : method,
url : $(this).attr('action'),
data : $(this).serialize(),
dataType : 'json',
encode : true
};
@scruwys
scruwys / levenshein.py
Created May 24, 2015 14:55
Fuzzy matching with Levenshtein distance http://www.scottcruwys.com/levenshtein-distance
def levenshtein(string1, string2):
if string1 == string2:
return 0
# make sure string1 is larger than string2
if len(string1) < len(string2):
return levenshtein(string2, string1)
# if string2 is empty, then we know that the distance is
# just the length of string1
if len(string2) == 0:
return len(string1)
@scruwys
scruwys / google_translate.py
Created May 20, 2015 16:06
Google Translate API class
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests as req
class GoogleTranslate(object):
end_point = "https://www.googleapis.com/language/translate/v2"
def __init__(self, key):
@scruwys
scruwys / all_time_box_office.py
Created May 15, 2015 20:27
all_time_box_office.py
from bs4 import BeautifulSoup
import requests
import csv
import re
def write_to_tsv(filename, results):
with open(filename, 'ab') as outfile:
writer = csv.DictWriter(outfile, fieldnames=results[0].keys(), delimiter="\t")
writer.writerows(results)
@scruwys
scruwys / ajax.js
Created February 16, 2015 20:10
async requests in JavaScript
// needs better support for 4xx responses, but a good starting off point...
var ajax = {};
ajax.build_payload = function(data) {
var payload = [];
// prepare to pass variables as URL string...
for( var n in data ) {
payload.push(encodeURIComponent(n) + '=' + encodeURIComponent(data[n]));
};
@scruwys
scruwys / acts_as_trackable.rb
Last active August 29, 2015 14:13
extension of paper_trail gem to act as an audit log and a recent activity feed
# app/models/concerns/acts_as_trackable.rb
module Trackable
extend ActiveSupport::Concern
module ActiveRecord
def acts_as_trackable(field_name = nil)
# create a reader on the class to access the field name
class << self; attr_reader :displayable; end
@displayable = field_name
@scruwys
scruwys / simple_search.rb
Last active August 29, 2015 14:11
quick and dirty search for ActiveRecord
# app/models/concerns/simple_search.rb
module SimpleSearch
extend ActiveSupport::Concern
module ActiveRecord
def simple_search
include SimpleSearch
end
end
@scruwys
scruwys / crawl.py
Last active August 29, 2015 14:09
web crawler class using requests and BeautifulSoup
import requests
from bs4 import BeautifulSoup
class Crawler:
def __init__(self, base_url):
self.base_url = base_url
# Returns raw HTML based on URL and parameters
def get(self, url, payload={}):