Skip to content

Instantly share code, notes, and snippets.

@swinton
swinton / percentages.js
Created August 8, 2012 20:59
Function to generate a percentage calculator.
// Function to generate a percentage calculator.
// Percentages will be rounded to nearest whole number.
// The calculator ensures the entire set of percentages adds up to 100.
var percentageCalculatorGenerator = function(numDataPoints, total) {
// Ensure cumulative total adds up to 100
var cumulativeTotal = 0;
// Return the generated function
return function(value, idx) {
@swinton
swinton / grequests_oauth_example.py
Created August 16, 2012 13:13
Example of using grequests with oauth
#!/usr/bin/env python
"""
Example of using grequests with OAuth.
See: https://github.com/kennethreitz/grequests
"""
import json
"""
From: http://sdiehl.github.com/gevent-tutorial/#queues
In this example we have the boss running simultaneously to the workers and have a restriction on the Queue that it can contain no more than three elements. This restriction means that the put operation will block until there is space on the queue. Conversely the get operation will block if there are no elements on the queue to fetch, it also takes a timeout argument to allow for the queue to exit with the exception gevent.queue.Empty if no work can found within the time frame of the Timeout.
"""
import gevent
from gevent.queue import Queue, Empty
tasks = Queue(maxsize=3)
@swinton
swinton / gevent_examples.py
Created August 16, 2012 22:21
Gevent examples
#!/usr/bin/env python
"""
Gevent examples.
"""
import json
import gevent
@swinton
swinton / callback.py
Created August 20, 2012 13:33
Twitterspawn HOWTO
# Define callback (can define 1 per request)
def callback(response):
print "Got", response
@swinton
swinton / twitterspawn-example.py
Created August 20, 2012 16:25
Example of fetching followers of multiple Twitter accounts recursively, using twitterspawn.
#!/usr/bin/env python
"""
Example of fetching followers of multiple Twitter accounts recursively, using twitterspawn.
"""
import atexit
import json
import twitterspawn
@swinton
swinton / client.py
Created August 29, 2012 11:54 — forked from micktwomey/client.py
python multiprocessing socket server example
import socket
if __name__ == "__main__":
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("localhost", 9000))
data = "some data"
sock.sendall(data)
result = sock.recv(1024)
print result
sock.close()
@swinton
swinton / settings.py
Created September 12, 2012 20:26
Example of connecting to the Twitter user stream using Requests.
# OAuth stuff...
ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"
ACCESS_TOKEN_SECRET = "YOUR_ACCESS_TOKEN_SECRET"
CONSUMER_KEY = "YOUR_CONSUMER_KEY"
CONSUMER_SECRET = "YOUR_CONSUMER_SECRET"
@swinton
swinton / talky_tweety.py
Created September 21, 2012 19:45
Connect to Twitter, track a hashtag, read out the tweets!
#!/usr/bin/env python
"""Connect to Twitter, track a hashtag, read out the tweets!"""
import getpass
import json
import random
import sys
import subprocess
import multiprocessing
@swinton
swinton / echobot.py
Created September 24, 2012 15:21
Connect to Twitter, track a hashtag, read out the tweets
#!/usr/bin/env python
"""Connect to Twitter, track a hashtag, read out the tweets!"""
import sys
import subprocess
import multiprocessing
import track