Skip to content

Instantly share code, notes, and snippets.

View paultopia's full-sized avatar

Paul Gowder paultopia

View GitHub Profile
@paultopia
paultopia / readzip.py
Created April 5, 2018 02:46
readzip.py
# quick and dirty script to download a compressed file and unzip it in memory to an appropriate folder
# useful in pythonista (ios) to handle the fact that ios has no native way to handle compressed files.
# can handle .tar.gzip and .zip files
import requests, zipfile, os, urllib, io, tarfile
url = input('url: ')
filename = os.path.basename(urllib.parse.urlparse(url).path)
dirname = filename.partition(".")[0]
extension = filename.rpartition(".")[-1]
try:
@paultopia
paultopia / read_apple_notes.ipynb
Created January 21, 2018 21:20
attempt to read apple notes from python
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@paultopia
paultopia / parallel_download.py
Last active January 5, 2018 04:08
Example of how to do simple multithreaded (no shared data/side-effectey-race-conditions) code in python
import requests, concurrent.futures, time
class FailedRequest(object):
def __init__(self, type_of_failure, details):
self.type = type_of_failure
self.message = details
def __download(url, headers, delay, timeout):
@paultopia
paultopia / fix-jpeg-rotation.py
Last active January 3, 2018 04:24
fix unsupported exif jpeg image rotation
"""
some images get broken in desktop browsers because they're rotate with exif tags, which desktop browsers don't support, but ios does.
(More specifically, neither desktop Chrome, Safari or Firefox on OSX as of 1/2/18 seem to respect exif tags when the image is embedded
in a page, though at least Chrome respects them when the file is opened in its own tab.)
see: https://www.howtogeek.com/254830/why-your-photos-dont-always-appear-correctly-rotated/ +
https://stackoverflow.com/questions/42401203/chrome-image-exif-orienation-issue
This leads to a weird situation where you can have a photo that was rotated in some exif-happy app that looks correct on ios but not
on any desktop browser. Let's fix this.
@paultopia
paultopia / weight-tracker.py
Created November 22, 2017 04:52
weight-tracker.py
# CONSTANTS: REPLACE WITH YOUR OWN INFO. IN PARTICULAR, THE TWO DROPBOX FIELDS ARE TO GET YOUR API KEY OUT OF THE PYTHONISTA KEYCHAIN
DATABASE_FILENAME = "weight-tracker-TEST.db"
CSV_FILENAME = 'weights-TEST2.csv'
DROPBOX_KEYCHAIN_NAME = "keychain name goes here, see pythonista keychain docs-- e.g. 'dropbox'"
DROPBOX_KEYCHAIN_FIELD = "keychain field goes here-- e.g. 'myaccount'"
import dropbox, keychain, sqlite3, datetime, csv, dialogs
from matplotlib.pyplot import plot_date, show, subplots, legend
@paultopia
paultopia / tweetstorm.py
Last active September 4, 2017 03:51
Easy tweetstorms for pythonista on ios. Usage: have a twitter account on device, run the script, approve access, type your text into the box.
# like the previous version (https://gist.github.com/paultopia/2cf73cbffe6dde1fd5f21b21d649e79b) but threading by reply previous tweet instead of first one
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):
@paultopia
paultopia / script-from-dropbox.py
Created September 4, 2017 02:34
script-from-dropbox.py
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 / 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 = []
@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 / 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){