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 compute_p(G, weight='weight', p='p'): | |
# Undirected | |
for node in G: | |
k_n = len(G[node]) | |
if k_n > 1: | |
sum_w = sum( G[node][neighbor][weight] for neighbor in G[node] ) | |
for neighbor in G[node]: | |
edge_weight = G[node][neighbor][weight] | |
p_ij = float(edge_weight)/sum_w | |
G[node][neighbor][p] = (1-p_ij)**(k_n-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
''' | |
Run multiple commands in parallel. | |
Run from CLI like | |
$ python purr.py commands.txt | |
where `commands.txt` is a text file containing one command per line. | |
''' | |
import subprocess |
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 redis | |
# Number of records to move at once | |
N = 10000 | |
# DB number and key | |
DB = 3 | |
KEY = 'deletion_notices' | |
source = redis.StrictRedis('<source host>', db=DB) |
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 functools | |
from flask import Flask, request | |
app = Flask(__name__) | |
def authenticate_mashape(func): | |
''' | |
Decorator to authenticate request with Mashape. | |
''' |
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
from .. import app | |
from celery.utils.log import get_task_logger | |
logger = get_task_logger(__name__) | |
# logger.setLevel(logging.DEBUG) | |
@app.task(name='test.noop', bind=True) | |
def noop(self): | |
logger.info("Completing NOOP task") |
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
function getHightlightCoords() { | |
var pageIndex = PDFViewerApplication.pdfViewer.currentPageNumber - 1; | |
var page = PDFViewerApplication.pdfViewer.getPageView(pageIndex); | |
var pageRect = page.canvas.getClientRects()[0]; | |
var selectionRects = Array.apply(this, window.getSelection().getRangeAt(0).getClientRects()); | |
var viewport = page.viewport; | |
var selected = selectionRects.map(function (r) { | |
return viewport.convertToPdfPoint(r.left - pageRect.left, r.top - pageRect.top).concat( | |
viewport.convertToPdfPoint(r.right - pageRect.left, r.bottom - pageRect.top)); | |
}); |
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 json | |
import requests | |
## `pip install tweepy` | |
import tweepy | |
auth = tweepy.OAuthHandler('consumer_key', 'consumer_secret') | |
auth.set_access_token('access_token_key', 'access_token_secret') | |
api = tweepy.API(auth, parser=tweepy.parsers.JSONParser()) |
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
""" | |
Add copy to clipboard from IPython! | |
To install, just copy it to your profile/startup directory, typically: | |
~/.ipython/profile_default/startup/ | |
Example usage: | |
%clip hello world | |
# will store "hello world" |
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
from django.db.models import Aggregate, fields | |
from django.db.models.sql.aggregates import Aggregate as SQLAggregate | |
class SQLConcat(SQLAggregate): | |
sql_function = 'GROUP_CONCAT' | |
sql_template = ('%(function)s(%(distinct)s%(field)s' | |
' SEPARATOR "%(separator)s")') | |
def __init__(self, col, separator=', ', distinct=False, **extra): | |
super(SQLConcat, self).__init__(col, separator=separator, | |
distinct=distinct and 'DISTINCT ' or '', **extra) |
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
nodes = [{'name': n, 'group': G.node[n]['question_id'], 'size': G.node[n]['count']} for n in G] | |
l = G.edges() | |
edges = [{'source': l.index(s), 'target': l.index(t), 'value': G[s][t]['weight']} for s,t in itertools.product(l, l) if s in G and t in G[s]] | |
json.dump({'nodes': nodes, 'links': edges}, open('filename.json', 'w')) |