Skip to content

Instantly share code, notes, and snippets.

View bobby569's full-sized avatar
🏠
Working from home

Yuchen Zhang bobby569

🏠
Working from home
View GitHub Profile
@bobby569
bobby569 / goreleaser.md
Last active August 9, 2023 19:23
Publish release for OSS golang projects

This gist provides guidence to publish release using goreleaser for OSS golang projects that either not offer desired packages by any means or not release as frequent as desired.

Pre-requisite

Steps

  1. Fork the golang repo to your own repository
  2. Clone the repo
@bobby569
bobby569 / python-installer.sh
Created May 28, 2021 21:42
Scripts to install python on linux
#!/bin/bash
VERSION=$1
sudo apt update
sudo apt install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev curl libbz2-dev
curl -O https://www.python.org/ftp/python/$VERSION/Python-$VERSION.tar.xz
tar -xf Python-$VERSION.tar.xz
cd Python-$VERSION

Keybase proof

I hereby claim:

  • I am bobby569 on github.
  • I am bobby569 (https://keybase.io/bobby569) on keybase.
  • I have a public key ASCLts_ENuSQJDgeUowFsMRiWbC2a8IuXcBaVwveJmCNdQo

To claim this, I am signing this object:

@bobby569
bobby569 / urllib-extend.py
Created June 20, 2020 21:56
Extended usage of urllib with assist of BeautifulSoup.
import bs4
import re
import urllib.request
def getFullHtml(url: str) -> str:
"""Retrieve the html content of the given url."""
return urllib.request.urlopen(url).read().decode('utf-8')
@bobby569
bobby569 / google-search.go
Created June 20, 2020 08:12
Taking how google search works to demonstrate the concurrency idea of golang.
package main
import (
"fmt"
"math/rand"
"time"
)
var (
// Web is the web search engine
@bobby569
bobby569 / consistent-hashing.py
Created June 20, 2020 07:26
Simple scalable entry-to-machine assignment policy using consistent hashing.
import bisect
import hashlib
def my_hash(key):
'''Return a hash in the range [0,1).'''
return (int(hashlib.md5(key.encode('utf-8')).hexdigest(), 16) % 1000000) / 1000000
class ConsistentHashing:
@bobby569
bobby569 / LargeFileReader.java
Created June 20, 2020 06:55
High-level implementation of a large file reader in java.
public class LargeFileReader {
private static final long PAGE_SIZE = Integer.MAX_VALUE;
private List<MappedByteBuffer> buffers = new ArrayList<>();
private final byte raw[] = new byte[1];
LargeFileReader(FileChannel channel) throws IOException {
long start = 0, length = 0;
for (long index = 0; start + length < channel.size(); index++) {
length = channel.size() / PAGE_SIZE == index ? channel.size() - index * PAGE_SIZE : PAGE_SIZE;
start = index * PAGE_SIZE;
@bobby569
bobby569 / fenwick_tree.py
Created August 19, 2019 04:32
Fenwick Tree/Binary Index Tree Implementation in Python
class FenwickTree:
def __init__(self, nums):
self.lowbit = lambda x: x & (-x)
self.vals = [0] * (len(nums)+1)
for i, v in enumerate(nums, 1):
self.update(i, v)
def update(self, i, val):
while i < len(self.vals):
self.vals[i] += val
@bobby569
bobby569 / count-up.py
Last active June 20, 2020 07:05
Multithreading examples in Python
"""
To count up for a global variable.
"""
from threading import Thread, Lock
var = 0
lock = Lock()
class IncrementThread(Thread):
def run(self):
@bobby569
bobby569 / index.js
Last active June 20, 2020 06:50
Multithreading for WebWorker
const { Worker } = require('webworker-threads');
const app = require('express')();
app.get('/', (req, res) => {
const worker = new Worker(function() {
this.onmessage = function() {
let cnt = 0;
while (cnt < 1e9) {
cnt++;
}