Skip to content

Instantly share code, notes, and snippets.

View clayadavis's full-sized avatar

Clayton A Davis clayadavis

View GitHub Profile
@clayadavis
clayadavis / clip_magic.py
Last active September 16, 2015 19:50 — forked from nova77/clip_magic.py
copy to clipboard ipython magic
"""
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"
@clayadavis
clayadavis / botornot_example.py
Last active December 9, 2015 03:27
BotOrNot REST Example
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())
@clayadavis
clayadavis / gist:8a2b025b24fdd4b6016d
Last active January 22, 2016 17:15 — forked from yurydelendik/gist:f2b846dae7cb29c86d23
PDF.js get/show hightlight (for Chromium)
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));
});
@clayadavis
clayadavis / celery_test_tasks.py
Last active August 23, 2016 15:06
Test tasks
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")
@clayadavis
clayadavis / flask_mashape.py
Last active August 23, 2016 15:03
A route decorator for Flask that performs Mashape authentication
import functools
from flask import Flask, request
app = Flask(__name__)
def authenticate_mashape(func):
'''
Decorator to authenticate request with Mashape.
'''
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)
@clayadavis
clayadavis / purr.py
Created November 11, 2016 17:41
Run multiple commands in parallel
'''
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
@clayadavis
clayadavis / multiscale_backbone.py
Last active October 26, 2018 21:51
Compute the multiscale backbone edge probabilities of a NetworkX graph
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)
@clayadavis
clayadavis / wiki_crawler.py
Created November 29, 2016 20:09
Wiki crawler
'''
(c)2016 Clayton A Davis
'''
import itertools
import random
import time
from html.parser import HTMLParser
from urllib.parse import urlparse, urljoin, unquote
import math
from collections import namedtuple
def haversine_distance(origin, destination):
""" Haversine formula to calculate the distance between two lat/long points on a sphere """
radius = 6371 # FAA approved globe radius in km
dlat = math.radians(destination.lat-origin.lat)
dlon = math.radians(destination.lng-origin.lng)