Skip to content

Instantly share code, notes, and snippets.

View paultopia's full-sized avatar

Paul Gowder paultopia

View GitHub Profile
@paultopia
paultopia / backup-to-git.py
Last active July 16, 2023 07:32
quick python script to run to backup on an unversioned directory (like dropbox, icloud, where git would break) of files with given extension to git repo. Just modify the first three lines, and stick the target directory in a private github repo or something
#!/usr/bin/env python
target_dir = "FULL_PATH_TO_YOUR_TARGET_DIRECTORY_WITH_GIT_REPO" # example: /Users/you/github/backup/
source_dir = "FULL_PATH_TO_YOUR_TARGET_DIRECTORY_WITH_NO_GIT" # example: /Users/you/Dropbox/ImportantDocuments/
extensions = ["txt", "md"] # example list of extensions to copy, change to suit you.
import glob, shutil, subprocess, os
source_wildcards = [source_dir + "*." + x for x in extensions]
source_filenames = []
@paultopia
paultopia / planckpipe.cljs
Created January 5, 2017 00:40
getting planck to pipe (saving for later use in some markdown hacks)
;; thanks Mike Fikes for helping me figure out how to do this!
#!/usr/bin/env planck
(ns panpdf.core
(:require [planck.shell :refer [sh]]))
(def s "\"#foo\nbar\"")
(sh "bash" "-c" (str "echo " s " | pandoc -o fbb.html"))
@paultopia
paultopia / upload.cljs
Created December 21, 2016 21:50
clojurescript read uploaded text file in browser (derived from https://mrmcc3.github.io/post/csv-with-clojurescript/ )
(ns upload-file.core
(:require [reagent.core :refer [render atom]]
[cljs.core.async :refer [put! chan <! >!]])
(:require-macros [cljs.core.async.macros :refer [go go-loop]]))
;; derived from https://mrmcc3.github.io/post/csv-with-clojurescript/
;; and based on reagent-frontend template.
;; dependencies from project.clj in addition to clojure, clojurescript, and reagent:
;; [org.clojure/core.async "0.2.395"]
@paultopia
paultopia / euler2.clj
Last active December 8, 2016 05:10
euler problem 2 (sum even fib sequence < 4m) in clj/cljs
;; note: the extra case is to fit weird euler def. of fib seq that goes 1, 2, 3, 5, 8, 13, ... rather than 1, 1, 2, 3, 5, 8, 13
;; not nearly as cool as the fibs here: https://en.wikibooks.org/wiki/Clojure_Programming/Examples/Lazy_Fibonacci
(def fib
(memoize
(fn [n]
(case n
0 0
1 1
2 2
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@paultopia
paultopia / extracttrumpinsults.py
Created October 24, 2016 16:39
python 2 script to extract trump insults from NYT article
# you can extract the text from NYT article in javascript console with document.documentElement.outerText
# nyt article is here: http://www.nytimes.com/interactive/2016/01/28/upshot/donald-trump-twitter-insults.html
# first open the file. I saved it as trumpinsults2.txt on my hard drive
# I also changed non-ascii quotes to quotes and got rid of other non-ascii stuff using sublime text, and some code I
# wrote for the purpose from here: https://github.com/paultopia/ascii_punctuation
with open("trumpinsults2.txt") as t:
insults = t.readlines()
@paultopia
paultopia / gowder.py
Created September 28, 2016 15:40
I'm super lazy and want to be able to type, e.g., "gowder pdf test" and have it convert a markdown file to pdf without having to type flags etc. ditto "gowder dropbox test.txt"
#!/usr/bin/env python3
import sys
import os
mode = sys.argv[1]
command = sys.argv[2]
def markdown_to_pdf(root_filename):
md_filename = root_filename+".md"
pdf_filename = root_filename+".pdf"
print("converting {0} to {1}".format(md_filename, pdf_filename))
return "pandoc -o {0} {1}".format(pdf_filename, md_filename)
@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?)
@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 / 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