Skip to content

Instantly share code, notes, and snippets.

@zcaceres
zcaceres / Error-Handling-Patterns-Express.md
Last active August 3, 2023 13:40
error handling patterns in Express

Handling Errors

Express.js makes it a breeze to handle errors in your routes.

Express lets you centralizes your error-handling through middleware.

Let's look at patterns for how to get the most out of your error-handling.

First, our error-handling middleware looks like this:

@simonw
simonw / recover_source_code.md
Last active May 21, 2024 19:04
How to recover lost Python source code if it's still resident in-memory

How to recover lost Python source code if it's still resident in-memory

I screwed up using git ("git checkout --" on the wrong file) and managed to delete the code I had just written... but it was still running in a process in a docker container. Here's how I got it back, using https://pypi.python.org/pypi/pyrasite/ and https://pypi.python.org/pypi/uncompyle6

Attach a shell to the docker container

Install GDB (needed by pyrasite)

apt-get update && apt-get install gdb
@gati
gati / guess_candidate_model.py
Created November 2, 2016 15:42
Code for training an LSTM model for text classification using the keras library (Theano backend). Was used for guesscandidate.com.
from sklearn.cross_validation import train_test_split
from keras.preprocessing import sequence, text
from keras.models import Sequential
from keras.layers import (Dense, Dropout, Activation, Embedding, LSTM,
Convolution1D, MaxPooling1D)
# Embedding
max_features = 20000
maxlen = 100
embedding_size = 32
@crowsonkb
crowsonkb / dmsqn.py
Last active February 26, 2017 03:56
Experimental quasi-Newton optimizer for image synthesis from CNNs
import numpy as np
from scipy.linalg import blas
from scipy.ndimage import zoom
# Machine epsilon for float32
EPS = np.finfo(np.float32).eps
# pylint: disable=no-member
def dot(x, y):
--[[ An experimental quasi-Newton optimizer.
Incorporates Hessian damping, momentum, and per-feature learning rate scaling.
Also implements optional polynomial-decay averaging (similar to ASGD).
ARGS:
- 'opfunc' : a function that takes a single input (X), the point
of a evaluation, and returns f(X) and df/dX
- 'x' : the initial point
- 'config` : a table with configuration parameters for the optimizer
@siemanko
siemanko / tf_lstm.py
Last active July 26, 2023 06:57
Simple implementation of LSTM in Tensorflow in 50 lines (+ 130 lines of data generation and comments)
"""Short and sweet LSTM implementation in Tensorflow.
Motivation:
When Tensorflow was released, adding RNNs was a bit of a hack - it required
building separate graphs for every number of timesteps and was a bit obscure
to use. Since then TF devs added things like `dynamic_rnn`, `scan` and `map_fn`.
Currently the APIs are decent, but all the tutorials that I am aware of are not
making the best use of the new APIs.
Advantages of this implementation:
# author: Aaditya Prakash
# NVIDIA-SMI does not show the full command, and when it was launched and its RAM usage.
# PS does but it does but you need PIDs for that
# lsof /dev/nvidia gives PIDs but only for the user invoking it
# usage:
# python programs_on_gpu.py
# Sample Output
@cburgdorf
cburgdorf / xor_keras.py
Last active November 18, 2020 11:23
Comparing XOR between tensorflow and keras
import numpy as np
from keras.models import Sequential
from keras.layers.core import Activation, Dense
training_data = np.array([[0,0],[0,1],[1,0],[1,1]], "float32")
target_data = np.array([[0],[1],[1],[0]], "float32")
model = Sequential()
model.add(Dense(32, input_dim=2, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
@cameronbourke
cameronbourke / immutable-array-methods.es6.js
Last active October 30, 2016 08:29
After watching Dan Abramov's egghead series on Redux, https://egghead.io/lessons/javascript-redux-the-single-immutable-state-tree, I quickly played around with how native array methods in javascript could be immutable. This is focused on the methods not like map, reduce, filter etc which already don't mutate the array.
const pop = (array) => array.splice(0, -1);
const push = (array, el) => [...array, el];
const splice = (array = [], startCount, deleteCount = 0, ...elements) => {
const { length } = array;
let remainder = startCount + deleteCount;
if(startCount > length || startCount <= -length) {
startCount = 0;
@pauldambra
pauldambra / checkout-kata.exs
Created December 29, 2015 15:33
the checkout kata in elixir
defmodule Checkout do
defstruct [
basket: %{A: 0, B: 0, C: 0, D: 0}
]
def scan(checkout, code) do
new_value = checkout.basket[code] + 1
%{checkout | basket: Map.put(checkout.basket, code, new_value)}
end