Skip to content

Instantly share code, notes, and snippets.

@goldhand
goldhand / pdb_example.py
Created February 5, 2016 19:58
example using pdb for python debugging instead of print
import pdb; pdb.set_trace()
# --or--
import pudb; pudb.set_trace()
# l
# n >> nextline
# context >> get current context
@goldhand
goldhand / handleAjaxForm.js
Created February 18, 2016 14:53
Simple jquery handler for django json reponses
const handleAjaxForm = (form) => {
$.ajax({
url: form.action,
dataType: 'html',
type: 'POST',
data: $(form).serialize(),
success: data => {
$(form.parentElement).fadeOut();
setTimeout(() => $('#success-message').fadeIn(), 1000);
},
export default class queryCookie {
constructor() {
this.qs = getQueryString();
this.cookie = getCookie();
}
store(k, val) {
// store all cookies as root path so they can override if needed
document.cookie = `${k}=${val}; path=/`;
}
remove(k) {
@goldhand
goldhand / index.html
Last active March 4, 2016 23:45
Generate then render a series of imgages that incrementally increase in quality. This is targeted towards large image files like hero images that take a long time to load.
<html>
<img class="lowq"
data-src="/static/orginal-img.png"
data-lowq_path='/static/lowq_imgs/' data-qualities="5,10,15,20,25,30,35,50,60,70">
<script src="/main.js"></script>
</html>
@goldhand
goldhand / staticencoder.py
Last active May 17, 2022 21:00
Django template tag for encoding images in base64 and rendering with server
from django import template
from django.contrib.staticfiles.finders import find as find_static_file
from django.conf import settings
register = template.Library()
@register.simple_tag
def encode_static(path, encoding='base64', file_type='image'):
"""
@goldhand
goldhand / getDataset.js
Created March 18, 2016 16:18
collect dataset attributes from a dom element
const getDataset = (elem) => {
/* ie <= 10 does not support elem.dataset
* ie <= 10 also doesn't support Array.from
* collect an elements data properties and return them in a similar format
* to the elem.dataset method
*/
let attrs = Array.prototype.slice.call(elem.attributes).filter(attr => !!~attr.nodeName.indexOf('data-'));
return attrs.reduce((a, attr) => {
@goldhand
goldhand / heightTrigger.js
Created April 8, 2016 19:40
returns a value from a set that meets conditions
function getActiveHeightTrigger(triggersMap) {
// returns the smalles height from a set of heights that is more than the window height
return triggersMap.reduce((pV, cV) => {
// is this height more than window?
let checkWindow = (cV.height >= window.innerHeight);
// is this height less than previous height?
let checkPrev = (cV.height < pV.height);
if (checkWindow) {
if (checkPrev || !pV.height) return cV;
@goldhand
goldhand / ContextComponent.js
Last active May 19, 2016 18:18
The long version of what connect react-redux method replacing
class ContextComponent extend React.Componet {
static contextTypes = {
store: PropTypes.object,
}
componentDidMount() {
this.unsubscribe = this.context.store.subscribe(() => {
this.forceUpdate();
@goldhand
goldhand / Deep Object Destructuring es6 .js
Created May 17, 2016 15:39
Example of destructuring deep objects in es6
let katdawg = {
kat: {
says: {
meow: '...'
}
},
dawg: {
bark: 'woof'
}