Skip to content

Instantly share code, notes, and snippets.

View gartenfeld's full-sized avatar

David Rosson gartenfeld

View GitHub Profile
@gartenfeld
gartenfeld / blockspring.js
Created August 28, 2015 01:55
Serverless Slack Bots
// https://www.blockspring.com/blog/serverless-slack-bots
blockspring = require('blockspring');
var webhook = function(team_domain, service_id, token, user_name, team_id, user_id, channel_id, timestamp, channel_name, text, trigger_word, raw_text) {
// Basic bot will just echo back the message
response = ["*", user_name, "* said _", text, "_"].join('')
return {
@gartenfeld
gartenfeld / trigger_handlers.js
Created August 28, 2015 18:24
Organizing handler functions using a hash.
var handlers = {
onConnect: function () {
console.log('Connected!');
},
onMessage: function () {
console.log('Message!');
}
};
var map = {
@gartenfeld
gartenfeld / exact_class.py
Created July 10, 2014 06:07
Matching exact class attribute.
soup(lambda tag: tag.name == 'div' and tag.get('class') == ['some-class'])
@gartenfeld
gartenfeld / random_file.py
Created July 10, 2014 06:11
Choose random file in directory.
import os, random
random.choice(os.listdir("INSERT-DIR"))
@gartenfeld
gartenfeld / load_dir.py
Created July 12, 2014 02:30
Load directory.
# Non-recursive
import os
def load_directory(data_path):
files_list = []
try:
for file_name in os.listdir(data_path):
if file_name.endswith(".html"):
files_list.append(file_name)
@gartenfeld
gartenfeld / unicode_wrapper.py
Last active August 29, 2015 14:04
Unicode HTML wrapper
file_header = "<html>\n<head>\n<meta charset='utf-8'>\n</head>\n<body>\n"
file_footer = "\n</body>\n</html>"
@gartenfeld
gartenfeld / lcsub.py
Last active August 29, 2015 14:08
Find longest common substring.
def longest_common_substring(s1, s2):
m = [[0] * (1 + len(s2)) for i in range(1 + len(s1))]
longest, x_up_to = 0, 0
for x in range(1, 1 + len(s1)):
for y in range(1, 1 + len(s2)): # match every char in s2 against every char in s1
if s1[x - 1] == s2[y - 1]: # record a char match
m[x][y] = m[x - 1][y - 1] + 1 # char match tally will accumulate if previous char also matched
if m[x][y] > longest:
longest = m[x][y]
x_up_to = x # record char position of last found match
@gartenfeld
gartenfeld / triage_fi_en.py
Created November 1, 2014 11:11
Cleaning up and separating lexical data files.
from bs4 import BeautifulSoup
import re # Regular Expressions
import collections # Data Types
import sys # File operations
import codecs # UniCode support
import os
def clear_output_file(out_file):
file_header ="""<html>
<head>
@gartenfeld
gartenfeld / head_block_convert.py
Last active August 29, 2015 14:10
Re-formatting some XML using BeautifulSoup
from bs4 import BeautifulSoup
import re # Regular Expressions
import collections # Data Types
import sys # File operations
import codecs # UniCode support
import os
import locale
def extract_index_word(entry):
first_head = entry.find('div', class_="head-block")
@gartenfeld
gartenfeld / gloss_block_convert.py
Last active August 29, 2015 14:10
Making XML more semantic using BeautifulSoup.
from bs4 import BeautifulSoup
from bs4.element import Tag
import re # Regular Expressions
import collections # Data Types
import sys # File operations
import codecs # UniCode support
import os
import locale
def is_tag(tag):