Skip to content

Instantly share code, notes, and snippets.

View paultopia's full-sized avatar

Paul Gowder paultopia

View GitHub Profile
@paultopia
paultopia / twitterpost.py
Created January 16, 2017 01:08
simple post to twitter (requires tweepy) that strips away complicated tweepy api. requires setting up application and consumer tokens and keys per tutorial here: https://www.digitalocean.com/community/tutorials/how-to-create-a-twitter-app and then putting them in a json file.
import tweepy, json
def twitter_poster(json_creds):
"""Pass me a json file with twitter creds.
Fields:
'consumer_key',
'consumer_secret
'access_token',
'access_secret',
which are all in the order given in the twitter dev page
@paultopia
paultopia / dfs.cljs
Last active January 16, 2017 08:45
Depth-first search in clojurescript
#!/usr/bin/env planck
;; note: I haven't included any lein or boot tooling since there are no
;; dependencies. the easiest way to run is to use planck on osx, as a script.
;; can also be easily adapted to normal clojure or have some tooling added if you want to
;; run it and don't have a mac
(ns dfs.core
"depth-first search of tree. Makes a few assumptions for simplification purposes:
1. tree is directed graph
@paultopia
paultopia / clean_lines.py
Last active January 25, 2017 04:21
Quick and dirty clean up stray line breaks for, e.g., copy-pasted or OCRed text where paragraphs are separated by double line breaks or line-break-tabs and lines w/in paragraphs are separated by single line breaks, but you want paragraphs to be one long line separated by double-breaks.
import re
def tab_paras_to_double_lines(s):
return re.sub("\n\t", "\n\n", s)
def de_leading_trailing(s):
s1 = re.sub(" \n", "\n", s)
return re.sub("\n ", "\n", s1)
def fix_unspaced(mobj):
m = mobj.group(0)
@paultopia
paultopia / md-docx.py
Last active March 9, 2017 20:17
Simple Pythonista script to convert markdown to docx using docverter and then save inside pythonista filesystem.
import requests
import appex
url = "http://c.docverter.com/convert"
filename = appex.get_file_path()
with open(filename, 'rb') as f:
r = requests.post(url, data={'to':'docx','from':'markdown'},files={'input_files[]':f})
if r.ok:
outfile = filename.rpartition('/')[-1].rpartition('.')[0] + '.docx'
with open(outfile, 'wb') as o:
o.write(r.content)
@paultopia
paultopia / script-from-dropbox.py
Last active March 28, 2017 18:34
Easy pythonista script to use from dropbox iOS app share sheet to take dropbox link to python script and save it to pythonista filesystem. Also works with "raw" python scripts from github and gists.
import requests, appex
from urllib.parse import urlparse
url = appex.get_url().replace("dl=0", "dl=1")
r = requests.get(url)
filename = urlparse(url).path.rpartition("/")[-1].replace(".py", "-downloaded.py")
with open(filename, "wb") as outfile:
outfile.write(r.content)
print("script downloaded as " + filename)
@paultopia
paultopia / ordered-partitions.cljs
Created March 16, 2017 16:46
Replacement for clojure.math.combinatorics/partitions that preserves ordering --- see discussion at https://groups.google.com/forum/m/#!topic/clojure/DoEMT45VpAo
(require '[clojure.math.combinatorics :as c])
(defn breaks->partition
([v brks]
(breaks->partition 0 [] v brks))
([start pars v brks]
(if (empty? brks)
(conj pars (subvec v start (count v)))
(let [this-part (subvec v start (first brks))]
(recur (first brks) (conj pars this-part) v (rest brks))))))
@paultopia
paultopia / twitter-dejerk.py
Last active March 30, 2017 21:44
twitter-dejerk.py
# written for pythonista ios app, takes advantage of its library and access to ios twitter login. generates a list of followers who don't follow you back and lets you look at each individually to decide whethee to dispose of them or not. Only works for followers < 5000 amd following < 5000, else rate limits and cursors get imvolved and are icky. could use a real UI.
import twitter, json, webbrowser, random
account = twitter.get_all_accounts()[0]
followers = frozenset(json.loads(twitter.request(account, "https://api.twitter.com/1.1/followers/ids.json", "GET")[1].decode("utf-8"))["ids"])
ifollow = frozenset(json.loads(twitter.request(account, "https://api.twitter.com/1.1/friends/ids.json", "GET")[1].decode("utf-8"))["ids"])
not_following_back = list(ifollow.difference(followers))
def make_url(id):
return 'https://twitter.com/intent/user?user_id=' + str(id)
turls = [make_url(id) for id in not_following_back]
@paultopia
paultopia / basicFuncStuff.js
Last active April 21, 2017 01:05
Implementing basic functional idioms in JS---partial application, composition. Because I hate "bind." No, actually, just as a learning exercise, trying to get more familiar with JS quirks.
function partial(func, arg){
return function(){
var args = arguments ? [].slice.call(arguments) : []
args.unshift(arg);
return func.apply(null, args);
};
}
// comp applies functions right to left, like Clojure.
function comp(func1, func2){
@paultopia
paultopia / freecite_experiment.py
Last active September 4, 2017 00:33
Example of how to use http://freecite.library.brown.edu to parse citations and convert to json
from requests import post
from xml.etree.ElementTree import fromstring
from copy import deepcopy
endpoint = "http://freecite.library.brown.edu/citations/create"
# the example citations on their webpage
cite1 = "Udvarhelyi, I.S., Gatsonis, C.A., Epstein, A.M., Pashos, C.L., Newhouse, J.P. and McNeil, B.J. Acute Myocardial Infarction in the Medicare population: process of care and clinical outcomes. Journal of the American Medical Association, 1992; 18:2530-2536."
cite2 = "Fielderman, A., Silvester, G., Gatsonis, C.A., Hoenig, J., Flynn, S. Prognostic significance of flow cytometric DNA analysis and proliferative index in stage I non-small cell lung cancer. American Review of Respiratory Disease, 1992; 146:707-710"
@paultopia
paultopia / tweetstorm-old.py
Last active September 4, 2017 03:50
tweetstorm.py (for ios pythonista. Obsolete, use newer version a gist or two in)
from dialogs import text_dialog
import twitter
from time import sleep
acct = twitter.get_all_accounts()[0]
# if there are multiple accounts this will need to be changed to get different account. I only have one account so don't care.
# right now I also don't care to have authentication error handling, will need to experiment with unauthenticated devices to figure out what it throws.
def numerate_tweets(tweetlist):
numtweets = len(tweetlist)
newoutlist = []