Skip to content

Instantly share code, notes, and snippets.

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@zgohr
zgohr / pyrasite.md
Created April 12, 2018 02:00
stack trace for all threads of a running python process
sudo apt-get install gdb
pip install pyrasite
pyrasite-shell {pid}
import sys, traceback
for tid, frame in sys._current_frames().items():
 print("Stack for thread {}".format(tid))
inp
|> String.split("\n", trim: true)
|> Enum.map(&String.split(&1, "\t"))
|> Enum.map(fn a -> a |> Enum.map(&String.to_integer/1) end)
|> Enum.map(fn a -> [Enum.max(a), Enum.min(a)] end)
|> Enum.map(fn [a, b] -> a - b end)
|> Enum.sum()
|> IO.inspect()
defmodule Helper do
@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()
@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 / 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 / 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 / chrome touch issue.md
Created July 9, 2014 20:47
An issue with touch events in chrome
@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 {
class Integer
def palindrome?
return self.to_s == self.to_s.reverse
end
end