Skip to content

Instantly share code, notes, and snippets.

View zhoudaxia233's full-sized avatar
🎯
Focusing

Zheng Zhou zhoudaxia233

🎯
Focusing
View GitHub Profile

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.

@zhoudaxia233
zhoudaxia233 / README.md
Created September 13, 2022 12:22 — forked from thelonecabbage/README.md
django, current user

(cliped from http://stackoverflow.com/a/21786764/117292)

The least obstrusive way is to use a CurrentUserMiddleware to store the current user in a thread local object:

current_user.py

from threading import local

_user = local()
@zhoudaxia233
zhoudaxia233 / batch_queryset.py
Created August 6, 2022 16:08
django batch queryset
def batch_queryset(queryset, batchsize=2000):
queryset = queryset.order_by("pk")
total = queryset.count()
with transaction.atomic():
for start in range(0, total, batchsize):
end = min(start + batchsize, total)
yield queryset[start: end]
@zhoudaxia233
zhoudaxia233 / profiling_django.md
Created July 25, 2022 21:14
use line_profiler in django
import line_profiler
profile = line_profiler.LineProfiler()


@profile
def my_function_in_django_code():
    ....

 with open('output.txt', 'w') as stream:

Semantic Commit Messages

See how a minor change to your commit message style can make you a better programmer.

Format: <type>(<scope>): <subject>

<scope> is optional

Example

@zhoudaxia233
zhoudaxia233 / coro_life.py
Created April 20, 2020 21:03 — forked from ramalho/coro_life.py
John Conway's Game of Life implemented with coroutines, by Brett Slatkin
#!/usr/bin/env python3
# Copyright 2014 Brett Slatkin, Pearson Education Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
@zhoudaxia233
zhoudaxia233 / editWebpage.md
Last active April 19, 2020 16:30
Edit webpage
document.body.contentEditable=true;
@zhoudaxia233
zhoudaxia233 / git.md
Last active May 23, 2019 15:04
Undo a commit
  1. undo the commits from the local repository (it means you haven't git push them)

1.1 If you only want to add more changes to the previous commit, or change the commit message

git reset --soft HEAD~

1.2 If you also want to un-add the files which you added using git add command

git reset HEAD~
@zhoudaxia233
zhoudaxia233 / focalloss.py
Created May 22, 2019 14:05
Multi-class Focal Loss in Keras
from keras import backend as K
def focal_loss(gamma=2):
def loss(y_true, y_pred):
epsilon = K.epsilon()
y_pred = K.clip(y_pred, epsilon, 1 - epsilon)
pt = K.max(y_true * y_pred, axis=-1)
batch_loss = -K.pow((1 - pt), gamma) * K.log(pt)
return K.mean(batch_loss)
@zhoudaxia233
zhoudaxia233 / anaconda.md
Created October 26, 2018 22:16
Basic commands for using Anaconda
conda create -n NAME_OF_YOUR_ENV python=3.6

it will create a virtual env with python 3.6

conda env remove -n NAME_OF_YOUR_ENV

it will remove your virtual env