Skip to content

Instantly share code, notes, and snippets.

View lancetw's full-sized avatar
🐈

Hsin-lin Cheng lancetw

🐈
View GitHub Profile
@lancetw
lancetw / excel.txt.vba
Last active October 2, 2016 07:04
勤務派遣科 出勤效率 Excel 公式
=(
(DATEVALUE(
TEXT(
DATE(
MID(TRIM(J5),1,3)+1911,
MID(TRIM(J5),5,2),
MID(TRIM(J5),9,2))
,"YYYY-MM-DD")
)
+
@lancetw
lancetw / hch.py
Last active December 4, 2016 13:41
NTU hospital hsin-chu branch ER board. (updated 20161203)
# !/usr/bin/env python
# coding:UTF-8
import requests, json, re
from datetime import datetime
html = requests.get('http://reg.ntuh.gov.tw/EmgInfoBoard/NTUHEmgInfoT4.aspx', verify=False)
keys = ['pending_doctor', 'pending_ward', 'pending_icu', 'pending_bed']
pending0 = re.findall(r"<td(.*?)>(.+?)</td>", html.text)
@lancetw
lancetw / hcgh.py
Created December 3, 2016 08:16
Cathay general hospital Hsinchu branch ER board. (updated 20161203)
# !/usr/bin/env python
# coding:UTF-8
import requests, json, re
from datetime import datetime
html = requests.get('http://med.cgh.org.tw/unit/branch/Pharmacy/ebl/RealTimeInfoHC.html', verify=False)
keys = ['pending_doctor', 'pending_bed', 'pending_ward', 'pending_icu']
pending0 = re.findall(r"<td(.*?)>(.+?)</td>", html.text)
@lancetw
lancetw / hmmh.py
Last active December 19, 2018 15:27
Mackay memorial hospital Hsinchu branch ER board. (updated 20181219)
# !/usr/bin/env python
# coding:UTF-8
import requests, json, re
from datetime import datetime
requests.packages.urllib3.disable_warnings()
html = requests.get('https://wapps.mmh.org.tw/WebEMR/WebEMR/Default.aspx?a=HC', verify=False)
keys = ['pending_doctor', 'pending_bed', 'pending_ward', 'pending_icu']
@lancetw
lancetw / hch.py
Last active December 4, 2016 13:42 — forked from lanfon72/hch.py
NTU hospital hsin-chu branch ER board.
# !/usr/bin/env python
# coding:UTF-8
import requests, json, re
from datetime import datetime
html = requests.get('http://reg.ntuh.gov.tw/EmgInfoBoard/NTUHEmgInfoT4.aspx', verify=False)
keys = ['pending_doctor', 'pending_ward', 'pending_icu', 'pending_bed']
pending0 = re.findall(r"<td(.*?)>(.+?)</td>", html.text)
var fact = (function (fact) {
return (function (f) {
return f(function (f) {
return fact(function (n) {
return f(f)(n)
})
})
})(function (f) {
return f(f)
})
char** fizzBuzz(int n, int* returnSize) {
char** ret = calloc(n, sizeof(char*));
*returnSize = n;
char* sp;
for (int i = 1; i <= n; ++i) {
sp = *(ret + (i - 1)) = calloc(1, sizeof(char));
(i % 5 && i % 3) ? sprintf(sp, "%d", i) : sprintf(sp, "%s%s", (i % 3) ? "" : "Fizz", (i % 5) ? "" : "Buzz");
}

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.

#include <iostream>
#include <regex>
using namespace std;
auto split(const string &s, const string &gex = "\\s+") {
regex re(gex);
vector<string> ret{sregex_token_iterator(s.begin(), s.end(), re, -1), {}};
if (0 == ret.begin()->compare("")) ret.clear();
return ret;
}
const actions = [
() => Promise.resolve(0),
() => Promise.resolve(1),
() => Promise.resolve(2),
() => Promise.resolve(3),
() => Promise.reject(new Error('4')),
() => Promise.resolve(5),
() => Promise.reject(new Error('6')),
() => Promise.reject(new Error('7'))
]