Skip to content

Instantly share code, notes, and snippets.

View apg's full-sized avatar
🐢
Turtle

Andrew Gwozdziewycz apg

🐢
Turtle
View GitHub Profile
# instead of:
match = someregex.match(line)
if match:
print match.groups()
else:
print 'boo'
# why not make use of the 'as' keyword and do something like:
if someregex.match(line) as match:
# quick and dirty inverted index
import re, os
class InvertedIndex(object):
def __init__(self):
self._data = {}
def add(self, words, document):
for word in words:
# broken, hacked up version of
# http://bret.appspot.com/entry/how-friendfeed-uses-mysql for sqlite
# database = bequas.Database('something.db')
# database.initialize(); database.create_index('hello', unique=True);
# database.insert({'hello': 'world'})
import sqlite3
import uuid
import datetime
import zmq, time
def main(addr):
print addr
context = zmq.Context(1, 1)
socket = context.socket(zmq.SUB)
socket.setsockopt(zmq.SUBSCRIBE, "")
socket.connect(addr)
#!/bin/bash
# run in cron every 15 minutes to capture your tweets while on vacation
DATE=`date +"%Y-%m-%d-%H-%M"`
curl -u username:password http://twitter.com/statuses/friends_timeline.rss >
/home/apgwoz/vacation-dumps/twitter/$DATE.rss
"""ARGF from Ruby in Python.
"""
import sys, os
class _ARGF(object):
def __init__(self):
self.lineno = 1
# -*- coding: utf-8 -*-
from javax.swing import JFrame, JLabel, JTextField, JButton, JOptionPane
from java.awt import FlowLayout
from java.awt.event import ActionListener, MouseAdapter
import math
TO_CEL = u"→"
TO_FAHR = u"←"
def hashCode(s):
"""Computes the java `hashCode()` of a str
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1], where n is len(s)
"""
TWO_31 = 2147483648
TWO_32 = 4294967296
l = len(s)
code = 0
@contextmanager
def assertingBeforeAfter(test, pre_post_f, *args, **kwargs):
""">>> x = [1, 2, 3]
>>> with assertingBeforeAfter(lambda o, n: o != n, lambda: len(x))
... x.append(1)
>>>
>>> x = [1, 2, 3]
>>> with assertingBeforeAfter(lambda o, n: o != n, lambda: len(x))
... print "hi"
Traceback (most recent call last):
@apg
apg / *scratch.clj
Created August 16, 2010 16:28
Multi-line strings with margin, scala style
user=> (use '[clojure.contrib.str-utils2 :only [split join]])
user=> (defn ml-str
"Scala style multiline string"
[s]
(let [l (split-lines s)]
(join "\n" (map (fn [b]
(let [i (.indexOf b "|")]
(if (> i 0)
(.substring b (inc i))
b))) l))))