Skip to content

Instantly share code, notes, and snippets.

View alexkahn's full-sized avatar

Alex Kahn alexkahn

  • Chicago, IL, USA
View GitHub Profile
@alexkahn
alexkahn / counts.js
Created February 9, 2015 14:44
Memjs + Mongoose = Easy caching of counts
var cache = require('memjs').Client.create();
var Location = require('models/Location');
var Report = require('models/Report');
cache.get('locationCount', function(err, value) {
// memjs doesn't throw an error if the key doesn't exist.
// but both err and value will be null.
if (err === null && value === null) {
Location.count( function(err, count) {
// todo: handle errors
@alexkahn
alexkahn / functions.js
Last active August 29, 2015 14:15
Function examples
// function example
// make a sum function that takes an array
// but it assumes the array will contain numbers
function sum(values) {
if (!(values instanceof Array)) {
throw new Error('Values must be an array');
}
var i = 0;
var l = values.length;
var total = 0;
@alexkahn
alexkahn / functions-note.md
Last active August 29, 2015 14:18
A quick note on functions

A quick note on functions

After our discussion on Wednesday, I wanted to make sure the information was presented in a nice way.

We left off talking about functions and what they do, either named or anonymous.

We also mentioned the idea of scope: so here are some examples and ideas to help get that all to sink in (hopefully).

@alexkahn
alexkahn / unicode_compatible.py
Created September 12, 2015 13:35
Stuff for every Django file in Python 2
"""
This goes at the top of every file.
"""
# -*- coding: utf-8 -*-
"""
Python 3 has it SOoOo easy...
"""
from __future__ import unicode_literals
@alexkahn
alexkahn / characters.js
Created September 18, 2015 15:34
A simple OOish JavaScript demonstration
function Character(name, health, strength) {
this.name = name;
this.health = health;
this.strength = strength;
}
Character.prototype.attack = function attack(target) {
if (this.health > 0) {
var damage = this.strength;
console.log(this.name + " is attacking " + target.name + " and deals " + damage + " damage points");
@alexkahn
alexkahn / views.py
Created September 23, 2015 16:47
pass a query param to a form
from .forms import AccountCreationForm
class IndexView(TemplateView):
template_name = 'myapp/mytemplate.html'
def get_context_data(self, **kwargs):
ctx = super(IndexView, self).get_context_data(**kwargs)
# you can use any query param key you'd like...
ctx['form'] = AccountCreationForm(initial={'promotion_code': self.request.GET.get('c', '')})
@alexkahn
alexkahn / opacity.js
Created November 4, 2015 00:02
opacityHandler
var header = document.getElementsByTagName('header');
var range = 200;
window.addEventListener('scroll', opacityHandler, false)
function opacityHandler() {
var scrollTop = $(this).scrollTop();
var offset = header.offset().top;
var height = header.outerHeight();
@alexkahn
alexkahn / godo.md
Created November 24, 2015 16:56
Get Started with Node.js

Getting Started with Node.js

This short guide will get you familiar with Node.js so we can go do some fun stuff.

Installation

OSX

brew install node

*nix

apt-get install node yum install node

@alexkahn
alexkahn / after.js
Created November 24, 2015 21:23
DRYing up the Code
var hoursLocations = [{
'days': 'mondays',
'times': '11AM - 3PM',
'location': 'City Hall Plaza - Fisher Park'
},
{
'days': 'TUESDAYS & THURSDAYS',
'times': "11AM - 3PM",
'location': "South End - Harrison Avenue and East Concord Street by BMC"
},
@alexkahn
alexkahn / before.js
Created November 30, 2015 17:36
exercise
var markerInfo = [
//MONDAYS
{
"location": "City Hall Plaza - Fisher Park", "latitude": "42.360082",
"longitude": "-71.058880",
"day": "Mondays"
},
// TUESDAYS & THURSDAYS
{
"location": "South End - Harrison Avenue and East Concord Street by BMC",