Skip to content

Instantly share code, notes, and snippets.

@zgohr
zgohr / views.py
Created December 10, 2013 22:23
Basic django class based JSON view that can handle JSONP callbacks
from django.http import HttpResponse
from django.views import generic
import json
class JSONView(generic.View):
callback_parameter = 'callback'
def get_json_data(self, request, *args, **kwargs):
"""
@zgohr
zgohr / make pull use rebase.sh
Created January 22, 2014 14:34
instruct git so that any pull uses rebase instead than merge
git config --global branch.autosetuprebase always
git config --global pull.rebase preserve
class DimensionsValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
# I'm not sure about this:
dimensions = Paperclip::Geometry.from_file(value.queued_for_write[:original].path)
# But this is what you need to know:
width = options[:width]
height = options[:height]
record.errors[attribute] << "Width must be #{width}px" unless dimensions.width == width
record.errors[attribute] << "Height must be #{height}px" unless dimensions.height == height
class Integer
def palindrome?
return self.to_s == self.to_s.reverse
end
end
@zgohr
zgohr / stack.go
Last active August 29, 2015 13:57
CodeEval's stack interface implementation in Go
package main
import (
"os"
"bufio"
"fmt"
"strings"
)
type Stack struct {
@zgohr
zgohr / chrome touch issue.md
Created July 9, 2014 20:47
An issue with touch events in chrome
@zgohr
zgohr / Main.elm
Last active July 17, 2017 02:07
Binary file uploads in Elm 0.18
{-
Very basic single-file binary file uploading via Elm port.
One area for improvement is in error handling. Since I only care if a file
uploads successfully, that's all I'm passing in the callback port.
One known issue is since we execute the port on "change", uploading
the same file multiple times in a row will not have any affect since
the DOM element doesn't "change" if the same file gets selected.
@zgohr
zgohr / breadcrumbs.elm
Created August 11, 2017 14:36
Elm simplification questions from Slack
addBreadcrumb : Crumb -> List Crumb -> List Crumb
addBreadcrumb breadcrumb breadcrumbs =
let
cutBcrumbs bcrumbs newBcrumbs =
case bcrumbs of
Just crumbs ->
case List.head crumbs of
Just crumb ->
if breadcrumb /= crumb then
cutBcrumbs (List.tail crumbs) (List.append newBcrumbs [ crumb ])
@zgohr
zgohr / reindexer.py
Created October 16, 2017 21:26
Zero downtime elasticsearch reindex that uses elasticsearch-dsl doctypes and sql alchemy entities
def reindex(self, entities):
# Note - for some period of time, this doubles the memory usage of your ES cluster
# If this is a problem, the ES reindex function can take source/destination cluster so you can
# throw away the entire old cluster after everything is migrated.
# This assumes you use an alias (shown as `self.index` in this example) for your API calls
# 1. Create a new index with a unique name
# Set all of the index settings on the new index
# Set all of the doctype mappings on it
# 2. Use the Elasticsearch reindex helper to copy all documents over to new index
@zgohr
zgohr / day1.exs
Created December 3, 2017 18:08
Advent of Code 2017 day 1
digits = inp
|> String.codepoints()
|> (fn [hd | tl] -> [hd | tl] ++ [hd] end).()
|> Enum.map(&String.to_integer/1)
|> Enum.chunk_every(2, 1, :discard)
|> Enum.filter(fn [a, b] -> a == b end)
|> Enum.map(&List.first/1)
|> Enum.sum
|> IO.inspect()