Skip to content

Instantly share code, notes, and snippets.

Robin Edwards geotheory

Block or report user

Report or block geotheory

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View tidyr-unite2.R
require(tidyverse)
unite2 = function(x, col, ..., sep = '_', remove = TRUE){
unite(x, 'tmpcol', ..., sep = '∞-∞', remove = remove) %>%
mutate(tmpcol = tmpcol %>% str_remove_all('∞-∞NA(?=∞|$)') %>% str_replace_all('∞-∞', sep)) %>%
rename(!!col := tmpcol)
}
x = tibble(x=c('the','a','my'), y=c('brown','cat',NA),z=c('dog',NA,'bird'))
unite2(x, col = 'xzy', c('x','y','z'), sep=' ')
View mount-raspberry-image-on-macos.sh
# http://www.janosgyerik.com/mounting-a-raspberry-pi-image-on-osx/
# MOUNT
hdiutil mount /pathto/image/file.img
ext4fuse /dev/disk2s2 raspi
# UNMOUNT
View report-ebay-snipes.py
# Report on snipes
import urllib.request, json
from time import sleep
from re import sub
from tabulate import tabulate
with urllib.request.urlopen("http://localhost:3647/snipes") as url:
snipes_arr = json.loads(url.read().decode())
snipes_arr.sort(key=lambda x: x['snipeTime'])
View PIL+image+sanitise.py
import base64
from io import BytesIO
from PIL import Image
from base64 import b64decode, b64encode
msg = b64decode('sjhdbvcojasdv...')
buf = BytesIO(msg)
img = Image.open(buf)
dims = img.size
View ebay-snipe.sh
## ebay snipe service based on
# https://github.com/ruippeixotog/ebay-snipe-server
# crontab entry for service
@reboot /home/pi/ebay-sniper/bin/ebay-snipe-server > /home/pi/ebay-sniper/snipe.log 2>&1 &
# auction info
snipe-auct(){ curl http://localhost:3647/auction/$1; }
# snipe info
View simple-https-server.py
import http.server
import ssl
httpd = http.server.HTTPServer(('localhost', 9200), http.server.BaseHTTPRequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket, certfile='./server.pem', server_side=True)
httpd.serve_forever()
View here-test.md
# setup
unlink('~/Downloads/here-test/', recursive = TRUE)
getwd()
#> [1] "/private/var/folders/_3/b8whcf8d1bb8w_lr2rrscb5m0000gp/T/RtmpAnPCwY/reprex5e3e200cebe1"
proj_path = '~/Downloads/here-test'
dir.create(proj_path)
setwd(proj_path)
here::set_here()
#> Created file .here in /Users/robinedwards/Downloads/here-test
View closest_countrycode.R
require(countrycode)
closest_field = function(x){
if(length(x) > length(unique(x))) stop('x must be a vector of unique values')
similarity = sapply(codelist, function(field){ 100 * sum(x %in% field) / length(x) })
similarity = data.frame(field = names(similarity), similarity = as.vector(similarity), stringsAsFactors=FALSE)
similarity = similarity[order(similarity$similarity, decreasing = TRUE),]
if(suppressWarnings(suppressMessages(!require(tibble)))) return(similarity)
return(as_tibble(similarity))
}
View closest_countrycode.R
require(tidyverse)
require(countrycode)
closest_field = function(x){
map_dbl(codelist, ~ 100 * sum(x %in% .x) / length(x)) %>% as.data.frame %>%
tibble::rownames_to_column() %>% `names<-`(c('field','pc_match')) %>% as_tibble() %>%
arrange(-pc_match)
}
closest_field(c('CM','AL','PA','MK','HA','CO','ZI','KU','TH','FI'))
@geotheory
geotheory / html_similarity.R
Created Mar 23, 2019
R utility to measure the similarity of HTML documents
View html_similarity.R
# Measuring the similarity of style and structure between HTML documents
# An R implementation of https://github.com/matiskay/html-similarity
# Dependencies: magrittr, rvest, stringr, xml2, dplyr
require(magrittr)
jaccard_similarity = function(x, y){
x = unique(x)
y = unique(y)
if(length(x) + length(y) == 0) return(1)
You can’t perform that action at this time.