Skip to content

Instantly share code, notes, and snippets.

View paultopia's full-sized avatar

Paul Gowder paultopia

View GitHub Profile
@paultopia
paultopia / upload.py
Last active February 7, 2016 21:24
Hacktastic quick ftp upload (for one-line web deployment etc.)
""" see http://stackoverflow.com/questions/35245929/python-ftplib-hangs-on-upload-stor-and-not-network-latency for the ftplib hell that led here
Obvs replace [SERVER] [USER] and [PASSWORD] with appropriate values, and add to the list of ascii extensions for whatever you use.
"""
import sys
import os
import datetime as dt
full = sys.argv[1]
path = 'public_html/' + full[:full.rfind('/') + 1]
# convert a directory full of M$word .docx files to PDF
# REQUIREMENTS:
# 1. Only works on Mac.
# 2. Assign the variable USERNAME to your home directory
# 3. Requires the PDFwriter printer driver, get it here: https://sourceforge.net/projects/pdfwriterformac/
# 3.5 (might require PDFwriter to be your default printer; on my machine it's the only printer, so I haven't tested with any other config
# 4. Requires the launch and appswitch apps from the wonderful Nicholas Riley, get them here: http://sabi.net/nriley/software/
import glob, os, time
homedir = os.getcwd()
@paultopia
paultopia / filter-by-other.clj
Last active May 5, 2016 19:56
filter one vector based on applying the predicate to a different vector, in clojure.
(defn filter-by-other
[target test pred]
(let [pairs (map vector target test)]
(mapv first (filter #(pred (second %)) pairs))))
; filters target based on applying pred to test. probably will blow up with infinite sequences, and god only knows what will
; happen with lazy ones. Assumes target and test are vectors of equal length, pred is a single-arity function
;
; EXAMPLE:
; (filter-by-other [:a :b :c :d] [1 2 3 4] even?)
@paultopia
paultopia / gradecheck.py
Last active May 19, 2016 22:38
for future personal reference: just compare filenames to grade CSV to make sure no grades are missed.
import csv, glob
examfiles = [x[0:4] for x in glob.glob("*.pdf")]
with open("count.csv", 'rU') as grades:
gradelist = [x[0] for x in csv.reader(grades, dialect=csv.excel_tab)]
# but csv is shit so:
grades = [x[0:4] for x in gradelist]
missinggrades = []
for i in examfiles:
if i not in grades:
missinggrades.append(i)
@paultopia
paultopia / regex-label-extraction.py
Created May 26, 2016 00:13
add labels for regex to dict of data
# Common data cleaning pattern: you need to find regex matches in some block of text, stored w/ labels in a
# dictionary (i.e., from json), extract those matches, and store them as an additional labels.
# here's a quick and dirty utility function to do so. can then be used in loop or listcomp over entire dataset
import re
from collections import Counter
def pattern_extractor(instance, pattern, textlabel, newlabel):
found = re.findall(pattern, instance[textlabel])
if found:
if isinstance(found[0], (list, tuple)):
matches = [filter(None, x)[0] for x in found]
@paultopia
paultopia / spacemacs.md
Last active November 7, 2016 02:52
everything I've managed to learn so far about spacemacs (gist can't handle pandoc tables, view in raw)

so it turns out pandoc markdown + github + lots of symbols = sadness and chaos. Here's a slightly less ugly version, though still pretty uggo:* http://paul-gowder.com/emacs.html

Actually getting Spacemacs to do stuff

(Notes, mostly to self, and based on my own needs. Shared in case others need too.)

Absolute basics

Emacs but with vim keybindings.

@paultopia
paultopia / navbar.cljs
Last active June 7, 2018 22:29
Floating navbar with only a single css property changed (thanks to the magic of reagent, a clojurescript interface to react)
;; I think this will work. It's extracted from a working version here:
;; https://github.com/paultopia/stdio (mostly in the nav namespace)
;; so if you try this and it doesn't work, I probably left something off, feel free to go to the original.
(ns navbar.core
(:require [reagent.core :as r :refer [render atom]]))
(defn loren [reps]
(apply str (take reps (repeat "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
@paultopia
paultopia / safe-merge.clj
Last active September 4, 2023 22:16
merge for clojure that doesn't overwrite existing values with nil values
(require '[clojure.core.match :refer [match]])
(defn discard-nils [a b]
(match [a b]
[_ nil] a
:else b))
(def safe-merge (partial merge-with discard-nils))
;; examples
@paultopia
paultopia / spideyscrape3.py
Last active August 7, 2016 13:27
spideyscrape3.py
# python3 for pythonista compatible version
from bs4 import BeautifulSoup as BS
import sys
from urllib.request import urlopen
def clearJunk(BSobj):
[s.extract() for s in BSobj(['style', 'script'])]
def makeSoup(url):
soup = BS(urlopen(url))
@paultopia
paultopia / simple-transducers.clj
Last active October 17, 2016 04:05
clojure[script] transducers: basic use
;; attempted translation of the basic parts of the clojure documentation page on transducers http://clojure.org/reference/transducers
;; into more comprehensible terms that don't require you to be as smart as Rich Hickey to understand.
;; transducers are a way to compose sequence functions with less than their normal arity into a combined transformation
;; which then gets applied more efficiently than just nesting calls to the transformation, using ->, etc.
;; In the below, xf is a transducer. The composition goes left to right, hence tests for oddness before testing for less-than-5ness
(def xf
(comp
(filter odd?)