This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""ARGF from Ruby in Python. | |
""" | |
import sys, os | |
class _ARGF(object): | |
def __init__(self): | |
self.lineno = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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"←" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) |
OlderNewer