Skip to content

Instantly share code, notes, and snippets.

View ghoseb's full-sized avatar
🏋️‍♂️

Baishampayan Ghose ghoseb

🏋️‍♂️
View GitHub Profile
@mourjo
mourjo / with-local-redefs.clj
Last active October 10, 2023 13:25 — forked from gfredericks/with-local-redefs.clj
thread-local version of with-redefs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Commentary ;;
;; ;;
;; The goal for writing this started with the idea to have tests run in ;;
;; parallel using the leiningen plugin eftest ;;
;; https://github.com/weavejester/eftest. ;;
;; ;;
;; With tests using with-redefs, it was not possible to run them in ;;
;; parallel if they were changing the root binding of the same ;;
;; vars. Here, we are binding the root of the var to one function that ;;
@anthonytxie
anthonytxie / hodl20.py
Created March 24, 2018 21:01
Hodl 20 Rebalancing Algorithm
def calc_allocations(self, date, quantity, cap):
"""Figure out ideal allocations for a given date"""
# {
# coin_name: (percent_allocation, data)
# }
top_market = self.get_top_market(date, quantity)
total_cap = sum([coin.market_cap for coin in top_market])
allocations = [{

Scaling your API with rate limiters

The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.

In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.

Request rate limiter

This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.

@light44
light44 / lda.py
Created December 21, 2016 02:10
Latent Dirichlet Allocation using gensim
import nltk
import pandas as pd
import re
import pprint
import operator
import csv
import logging
from stop_words import get_stop_words
from collections import defaultdict
from gensim import corpora
@ragingwind
ragingwind / Backend Architectures Keywords and References.md
Last active April 2, 2024 09:37
Backend Architectures Keywords and References
@yanok
yanok / gist:4701552
Created February 3, 2013 12:15
Tests for HW3 challenge
val wc = Wildcard
val x = Variable "x"
val y = Variable "y"
val z = Variable "z"
fun tpl vs = Tuple vs
fun pair (a,b) = Tuple [a,b]
fun tp ps = TupleP ps
fun pp (a,b) = TupleP [a,b]
@kachayev
kachayev / barber.erl
Last active November 6, 2020 03:44
Solve "Sleeping Barber Problem" with Erlang (more about problem - http://en.wikipedia.org/wiki/Sleeping_barber_problem)
-module(barber).
-export([run/2, barber/1, clients/1, simulator/2]).
-define(CUT_DUTAION, 20).
run(RoomSize, Duration) ->
% create barber
BPid = spawn(?MODULE, barber, [?CUT_DUTAION]),
% run simulartor with barber PID
SPid = spawn(?MODULE, simulator, [BPid, RoomSize]),
(defbolt split-category ["category" "event"]
[tuple collector]
(let [event (.getString tuple 0)
category "testing_category2"]
(comment
(emit-bolt! collector [category event] :anchor tuple)
)
(emit-bolt! collector [category event] :anchor tuple :stream "3")
(ack! collector tuple)))
(ns example.storm.clj.spout.kafka-spout
(:import ; [example.storm.spout UnreliableKafkaSpout]
[storm.kafka HostPort KafkaSpout SpoutConfig StringScheme]))
(def ^:dynamic *kafka-hosts* ["kafka-1.example.net"
"kafka-2.example.net"
"kafka-3.example.net"])
(def ^:dynamic *kafka-ports* [9093
9094
@wmorgan
wmorgan / gist:3054620
Created July 5, 2012 16:18
Mustache templating for Clojure
(ns potato.core
(:use [slingshot.slingshot :only [throw+ try+]])
(:use [clojure.string :only [trim split]]))
;; use this provide template values at write time (i.e. not compile time).
;; "name" will be the name of the template variable. "context", when not nil,
;; will be the value previously returned by *template-value* for the enclosing
;; section.
(defn ^:dynamic *template-value* [name context])