Skip to content

Instantly share code, notes, and snippets.

View jacob414's full-sized avatar
💭
Retired but semi-active, hobby projects & activism

Jacob Oscarson jacob414

💭
Retired but semi-active, hobby projects & activism
View GitHub Profile
@jacob414
jacob414 / quick-fetchy.js
Last active April 29, 2016 14:04
A little convenience for many `fetch()` use cases
"use strict";
const qfApi = {
form: (src) => {
let form = new FormData()
for(let name in src) {
form.append(name, src[name])
}
return form
},
@jacob414
jacob414 / flask-stream-quickie.py
Created June 3, 2015 11:01
Flask streaming + path.py quickie
from path import path
import mimetypes
from flask import stream_with_context, Response
@app.route('/download/<id>')
def download(id):
# fp_as_string = my_source.get_a_file(id)
guessed, _ = mimetypes.guess_type(fp_as_string)
return Response(stream_with_context(
path(fp_as_string).chunks(2048, mode='rb')), mimetype=guessed)
# My alternative to `.first_or_404()` that can also handle ValueError's (common use case for me).
# could of course be made to check for more common errors, but for now this has been what I need.
from flask import abort
def vcoerce(validate_p):
"Sanitises the result of a function from ValueError's and empty results"
try:
return validate_p() or abort(404)
except ValueError:
-----BEGIN PGP MESSAGE-----
Version: GnuPG v1
owFtkntQVFUcx3fBlkDXHqJkjxm4ksC0Mufec5+rpLQgRZlpmdqg6z2Pu1wXdmF3
AVEpS0lzoNBAEypzakrF0FEcKrFBS8sUFQwc0nEUmSmXt1MpitpdxiZrOv+c8zvn
8/ue3/f8ToU13BRmfjknzTy2vTnRfPwKMr3yaJWwgkFeUszYVzA4R6eeQGjlUXMp
Y2fctBipfpqse2M9XkKTl/pj7zI2ppD6/LrXY1AgWUwGTIkthIeSNd3jor48nx7S
YnhNEHlN42TCKSoEUFJlVmA1IkAR8zxgASUiJpxsSGZ7/YF/3cqMaDp1YuwavGOE
T08THWkjfMHIAdGoIiOk8FSVAMCKLCAZCywBnCGrAhAC/dR319JSFXsRz/KhenOp
z51DnT6vd8Q0DughhOVZFnAyJ7JGRao/20gSiQwQUEVZ0LAmQaSJGsCYYABlSiEv
@jacob414
jacob414 / backing-off.coffee
Last active August 29, 2015 14:06
backing-off.coffee
# Re-try function with exponential back-off time
# `max`: Maximum amount of attempts
# `wait`: Wait time between attemps, in milliseconds
# `attempt`: Function to make one attempt, accepts arguments <nth try>, <callback upon success>
gently = @gently = (max, wait, attempt) ->
new Promise (fulfill, fail) ->
recur = (max, wait, attempt, n) ->
pending = setTimeout (->
if n is max
@jacob414
jacob414 / closest.coffee
Last active August 29, 2015 14:01
Interpolation of element closest to point by sampling in expanding circles
@closest = (x, y) ->
el = document.elementFromPoint x, y
unless el is null or el.nodeName is 'BODY' then el
else
slice = 2 * Math.PI / 8
for radius in [5...200] by 8
for step in [0...7]
angle = slice * step
[candX, candY] = [x + radius*Math.cos(angle), y + radius*Math.sin(angle)]
cand = document.elementFromPoint candX, candY
@jacob414
jacob414 / ipaddr.m
Created November 14, 2013 16:25
Get IP address of iOS device
#import <ifaddrs.h>
#import <arpa/inet.h>
/**
* Returns the IP adress of the device.
*/
- (NSString*)ipaddr {
NSString *address = nil;
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
_ = require 'underscore'
any = @any = (x) -> x
none = @none = (x) -> false
gt = @gt = (n) -> (x) -> x > n
eq = @eq = (p) -> (x) -> p == x
ne = @ne = (p) -> (x) -> x != p
match = @match = (P, L) ->
if P.length != L.length then false
@jacob414
jacob414 / blocks.m
Created May 24, 2012 19:39
With blocks and varargs Objective C might not be as bad after all
#include <stdarg.h>
#import <Foundation/Foundation.h>
@interface Cls : NSObject {}
-(int) takes_block: (int)limit withBlock:(int (^)(int first, ...))block;
@end
@implementation Cls
-(int) takes_block: (int)limit withBlock:(int (^)(int first, ...))block {
@jacob414
jacob414 / create_project.sh
Created May 16, 2012 20:18 — forked from shazow/create_project.sh
Setup deployment target for Nginx + Python/uWSGI + Supervisor + Git (for a Linode Stackscript)
#!/bin/bash
# Setup deployment target for Nginx + Python/uWSGI + Supervisor + Git
# From: https://gist.github.com/1210041
function usage() {
cat << EOF
Usage: $0 PROJECT_NAME [DOMAIN] [OPTIONS]
Options:
-y No prompts, assume yes to all.