Skip to content

Instantly share code, notes, and snippets.

@tsaylor
tsaylor / bullets.js
Last active October 4, 2022 03:25
bulleted list data store and management functions in python and javascript
var BulletList = function() {
return {
data: [],
get_by_id: function(id, data) {
items = data.filter(d => d.id == id)
return items?items[0]:null
},
get_by_prev_sibling_id: function(id, data) {
@tsaylor
tsaylor / append_json.py
Last active January 6, 2016 15:38
Append a value to a json file in place on disk.
import datetime
import json
import os
outfile_name = "data-{}.json".format(datetime.date.today().isoformat())
if os.path.exists(outfile_name):
append_to_json_file(outfile_name, data) # get `data` from somewhere
else:
with open(outfile_name, 'w') as f:
@tsaylor
tsaylor / README
Last active August 29, 2015 14:23
Requires Websocketd (http://websocketd.com/)
Run this command:
./websocketd --port=8080 ./server.sh
Include socketlog.js on your page and call
log('log message')
@tsaylor
tsaylor / countdown.py
Last active August 29, 2015 14:12
An example of threaded python code. Starts with a number of seconds entered by the user, then spawns a thread to countdown those seconds and another to prompt the user to add more seconds to the clock. Demonstrates one thread manipulating another.
import threading
import time
(i, seconds) = (0, 5)
def countdown():
global seconds, i
while i < seconds:
time.sleep(1)
@tsaylor
tsaylor / statistical-caesar.py
Created May 2, 2011 14:59
An experiment in statistical decryption of simple letter replacement ciphers. Calculates the frequency of each letter and then replaces each according to the frequency of letters in the english language.
import string
cipher = '''
Vs lbh'er yvxr zr, lbh znl erpnyy Gehzcrg Jvafbpx, gung avsgl yvggyr ovg bs fbsgjner gung cebivqrq na vagresnpr sebz jvaqbjf gb gur GPC/VC cebgbpby fgnpx gung crbcyr nyy bire gur jbeyq hfrq gb pbaarpg gb gur vagrearg sbe gur svefg gvzr.
Fb, vg gheaf bhg, gur thl jub znqr guvf qvqa'g znxr penc bss uvf jbex. Gehzcrg Jvafbpx jnf fgbyra naq npgviryl tvira njnl ba vafgnyy qvfxf sebz nyy gur znwbe grpu zntnmvarf naq va pbecbengr vafgnyyf. Ur jnf whfg n fznyy gvzr thl naq uvf pbzcnal unq ab jnl gb svtug gur enzcnag gursg.
Uvf anzr jnf Crgre Gnggnz. Fbzrbar ba erqqvg gubhtug vg'q or n tbbq vqrn gb fgneg n qbangvba cbg gb frr vs sbyxf jbhyq cbal hc sbe gur fbsgjner gung znqr vg cbffvoyr sbe gurz gb trg gb gur ovt jvqr jro. V qvq. :)
Fb, tb ba ol vs lbh hfrq vg, gbff n puvc be gjb va. Vg'f n avpr guvat gb qb.'''
@tsaylor
tsaylor / matcher.py
Created April 23, 2011 02:08
This is a tool for measuring the similarity between two lists. The more items that are the same or ranked near each other, the higher the score. I believe I used this to determine the best match for rankings of some items, but I have no idea now.
import random, math
position_weighting = 3.0
max_pos = 10
near_miss_scaling = 1.0 # bigger means higher scores
total_points = 0
total_match_points = 0
sf=[]
import imaplib
import getpass
import re
import webbrowser
import sys
mail = imaplib.IMAP4_SSL('imap.gmail.com', 993)
pw = getpass.getpass() # get password
mail.login('example@gmail.com',pw)
import re, datetime
import twitter, PyRSS2Gen
def getURLs(text):
url=unicode(r"((http|ftp)://)?(((([\d]+\.)+){3}[\d]+(/[\w./]+)?)|([a-z]\w*((\.\w+)+){2,})([/][\w.~]*)*)")
return [a.group() for a in re.finditer(url,text)]
def mkFeedItem(url):
return PyRSS2Gen.RSSItem(