Skip to content

Instantly share code, notes, and snippets.

View brizandrew's full-sized avatar

Andrew Briz brizandrew

View GitHub Profile
@brizandrew
brizandrew / saveFile.js
Last active October 25, 2016 21:35
A quick function to use eligrey's FileSaver. See gist for that git repo url.
/*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */
var saveAs=saveAs||function(e){"use strict";if(typeof e==="undefined"||typeof navigator!=="undefined"&&/MSIE [1-9]\./.test(navigator.userAgent)){return}var t=e.document,n=function(){return e.URL||e.webkitURL||e},r=t.createElementNS("http://www.w3.org/1999/xhtml","a"),o="download"in r,i=function(e){var t=new MouseEvent("click");e.dispatchEvent(t)},a=/constructor/i.test(e.HTMLElement),f=/CriOS\/[\d]+/.test(navigator.userAgent),u=function(t){(e.setImmediate||e.setTimeout)(function(){throw t},0)},d="application/octet-stream",s=1e3*40,c=function(e){var t=function(){if(typeof e==="string"){n().revokeObjectURL(e)}else{e.remove()}};setTimeout(t,s)},l=function(e,t,n){t=[].concat(t);var r=t.length;while(r--){var o=e["on"+t[r]];if(typeof o==="function"){try{o.call(e,n||e)}catch(i){u(i)}}}},p=function(e){if(/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(e.type)){return new Blob([String.fromCharCode(65279
@brizandrew
brizandrew / nodeScraping.js
Created December 6, 2016 21:12
Basic web scraping example with node
/*
Basic web scraping example with node
Includes scape and SQL insert
Variant of guide found here: https://scotch.io/tutorials/scraping-the-web-with-node-js
Scrapes the title of Anchorman 2 and places it into SQL table
*/
var express = require('express');
var fs = require('fs');
var request = require('request');
@brizandrew
brizandrew / arrayDict2Csv.py
Created March 24, 2017 19:52
Saving An Array of Python Dictionaries to a CSV file
def saveToCSV(arrayDict):
filename = 'name.csv' #call your file something
with open(filename, 'w') as output_file:
fieldnames = [ 'key1', # fill this array with the names of your keys in your dict
'key2',
...
'keyn'
]
writer = csv.DictWriter(output_file, fieldnames=fieldnames)
writer.writeheader()
@brizandrew
brizandrew / ajaxPromise.js
Created July 11, 2017 20:25
Wraps jQuery's ajax implementation inside an ES6 Promise structure.
// This code snippet requires jQuery to be loaded onto the page.
/**
* Wraps jQuery's ajax implementation inside an ES6 Promise structure.
* Source: https://stackoverflow.com/questions/35135110/jquery-ajax-with-es6-promises
* @function ajax
* @param {object} options - The config to pass to the ajax call.
* @return {object} - A Promise object to complete an ajax call.
*/
function ajax(options) {
@brizandrew
brizandrew / template.js
Created July 11, 2017 21:59
Renders a template string based on positional and keyword arguments.
/**
* Renders a template string based on positional and keyword arguments.
* Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
* @example
* // returns 'YAY!'
* let t1Closure = template`${0}${1}${0}!`;
* t1Closure('Y', 'A');
* @example
* // returns 'Hello World!'
* let t2Closure = template`${0} ${'foo'}!`;
@brizandrew
brizandrew / get.js
Created July 25, 2017 20:19
Routing get requests through a php file to go around cross-site-origin restrictions.
function get(url){
var dataString = 'url='+url;
$.ajax({
url: "get.php",
type: "POST",
data: dataString,
success: function(html) {
// Stuff...
// HTML = the page
},
@brizandrew
brizandrew / README.md
Created July 28, 2017 22:07
How to use node.js build routines and npm packages in Django.

Using Node.js With Django

When writing django apps it's easy to ignore the organization of your front end code. Often, these backend coders will just write a static js and css file, stick it in the static directory, and call it a day.

You can also build them as two completely independent parts. With a complex gulp build routine independent of the django app. But if you don't know gulp, node, or those kinds of systems it can be a daunting process to get started with.

Enter django-compressor-toolkit (the name doesn't quite roll off the tongue).

Setting Up Django-Compressor-Toolkit

Using django-compressor and django-compressor-toolkit you can write Javascript ES6 code with all its fancy import/export logic or style your pages with sass instead of css, and leave your deploy routine largely untouched.

@brizandrew
brizandrew / server.py
Last active December 19, 2023 10:16
Simple Flask Webhook Example
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask, request
from urllib import unquote_plus
import json
import re
app = Flask(__name__)
@brizandrew
brizandrew / sortDictList.py
Created April 26, 2018 15:00
Sort a list of dictionaries by a certain key
# Source: https://wiki.python.org/moin/SortingListsOfDictionaries
def sortDictList(arr, sort_key):
output = [(dict_[sort_key], dict_) for dict_ in arr]
output.sort()
return [dict_ for (key, dict_) in output]
@brizandrew
brizandrew / using-celery.md
Created August 15, 2018 21:29
Guide to setting up celery.

Setting Up Celery

Boilerplate Code

Install celery and redis

$ pipenv install celery redis

Configure the celery.py file in your exampleapp folder with your app name.