Skip to content

Instantly share code, notes, and snippets.

Brad Beattie bradbeattie

Block or report user

Report or block bradbeattie

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View Python regex tokenizer
import re
# Supported tokens and their operations
ORDER_OF_OPERATIONS = ["MULTIPLY", "DIVIDE", "ADD", "SUBTRACT"]
scanner = re.Scanner([
(r"([0-9]+)", lambda x, y: int(y)),
(r"\+", lambda x, y: "ADD"),
(r"-", lambda x, y: "SUBTRACT"),
(r"\*", lambda x, y: "MULTIPLY"),
(r"/", lambda x, y: "DIVIDE"),
@bradbeattie
bradbeattie / meetupfilter.js
Last active May 1, 2016
Filters meetup.com pages based on a given regex of linked member profiles. Useful for finding language exchange partners.
View meetupfilter.js
/* Initial definitions */
var regex_positive = /(france|french|paris|bonjour|salut|montr.al)/i;
var regex_negative = /(china|korea|spain|japan|taiwan|venezuela|costa rica|chile|vietnam|russia|honduras|brazil|iran|m.xico|czech|sweden|saudi|turkey|germany|india|argentina|colombia|netherlands|philippines)/i;
var now = new Date().getTime();
var ten_seconds = 10 * 1000;
var one_day = 86400 * 1000;
var one_week = one_day * 7;
/* Clear junk from the page */
jQuery("#chapterHeader, p").remove();
@bradbeattie
bradbeattie / cache_result.py
Last active Jun 27, 2016
Caches function results a little more intelligently
View cache_result.py
from datetime import timedelta
from django.core.cache import cache
from hashlib import md5
from importlib import import_module
from random import randint
from redis import Redis
from rq import Queue
EXPIRY_VARIANCE = 0.2
@bradbeattie
bradbeattie / prison_parser
Last active Aug 29, 2015
Parse and modify prison architect save files
View prison_parser
# These functions will allow you to
# - Read in Prison Architect save files
# - Modify the read content
# - Save content back into Prison Architect save file formats
import re
from collections import OrderedDict
from pprint import PrettyPrinter
@bradbeattie
bradbeattie / gist:b409e1594084027581f5
Created Jun 27, 2014
Show the effect of different decaying averages
View gist:b409e1594084027581f5
import random
import math
multipliers = (0.5, 0.8, 0.95, 0.99, 0.995)
values = [1] * len(multipliers)
for x in xrange(400):
numberline = ["-"] * 101
for index, multiplier in enumerate(multipliers):
if index == 0:
@bradbeattie
bradbeattie / Steam Reviews Javascript
Last active Sep 30, 2015
A script to add the most helpful Steam review to the front page
View Steam Reviews Javascript
// Initial definitions
var now = new Date().getTime();
var ten_seconds = 10 * 1000;
var one_day = 86400 * 1000;
var one_week = one_day * 7;
// Remove any pre-existing summary elements
jQuery(".summary-element").remove();
@bradbeattie
bradbeattie / translate_with_wiktionary.py
Last active Dec 14, 2015
Translates a given set of French words into English along with English definitions. Shouldn't be to difficult to adapt to other languages. Makes use of a simple two-column sqlite database to cache the API responses. Ignores duplicate words, obsolete words, etc.
View translate_with_wiktionary.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import random
import collections
import argparse
import json
import re
import requests
import sqlite3
@bradbeattie
bradbeattie / ted2csv.py
Last active Dec 10, 2015
Extracts a TED talk in two languages and creates a CSV of matching translated strings for flashcard use. Some potentially mistranslated entries will be stripped. Download the desired parallel corpus here: https://wit3.fbk.eu/mt.php?release=2012-02
View ted2csv.py
#!/usr/bin/python
import unicodecsv
import sys
import argparse
import re
parser = argparse.ArgumentParser()
parser.add_argument("source", help="Source language file")
parser.add_argument("target", help="Target language file")
args = parser.parse_args()
@bradbeattie
bradbeattie / extract_subtitles.py
Created Dec 15, 2012
This is some code I've been using to extract subtitles into CSV pairs for the purposes of language flashcards. The target encoding is hardcoded to iso-8859-1 and it requires both subtitle files to use the same timings, but otherwise it works just fine. Modified to handle multi-line sentences better. It drops some admittedly, but the flashcard qu…
View extract_subtitles.py
#!/usr/bin/python
import unicodecsv
import argparse
import sys
import re
from pysrt import SubRipFile
# Determine which languages to import
parser = argparse.ArgumentParser()
parser.add_argument("source", help="source language file to parse")
You can’t perform that action at this time.