Skip to content

Instantly share code, notes, and snippets.

@jerryan999
jerryan999 / contextblocker.js
Created April 3, 2021 14:44
contextblocker.js
window.addEventListener("contextmenu", function(e) { e.preventDefault(); })
@jerryan999
jerryan999 / limit_conc_asyncio.py
Created September 29, 2021 10:26
limit concurrency with Python asyncio
import asyncio
from random import randint
async def download(code):
wait_time = randint(1, 3)
print('downloading {} will take {} second(s)'.format(code, wait_time))
await asyncio.sleep(wait_time) # I/O, context will switch to main function
print('downloaded {}'.format(code))
@jerryan999
jerryan999 / asyncio-queue.py
Last active October 1, 2021 00:31
Implement Producer/Consumer pattern with asyncio.Queue
import asyncio
try:
loop = asyncio.get_event_loop()
except RuntimeError as ex:
if "There is no current event loop in thread" in str(ex):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
@jerryan999
jerryan999 / asyncio.py
Created October 9, 2021 12:04 — forked from Rotzke/asyncio.py
Asyncio with semaphores
import random
import asyncio
from aiohttp import ClientSession
async def fetch(url, session):
async with session.get(url) as response:
delay = response.headers.get("DELAY")
date = response.headers.get("DATE")
print("{}:{} with delay {}".format(date, response.url, delay))
return await response.read()
@jerryan999
jerryan999 / medium-adapt-design-pattern-golang.go
Last active November 20, 2021 16:33
medium-adapt-design-pattern-golang.go
// adapter design pattern application in go language
package main
import (
"fmt"
"strconv"
)
// LightBulb class to be adapted to the Lighting interface (Adaptee)
// IsOn is a boolean property
@jerryan999
jerryan999 / hash_table_implemnetaion_in_go.go
Last active November 26, 2021 09:40
Hash Tables Implementation in Go
// implementing a hash table in go
package main
import "fmt"
// A programmer from China, who loves Go, Python, AI, Machine Learning, and Data Science. To support me join Medium: https://jerryan.medium.com/membership
const SIZE = 15
// single linked list
@jerryan999
jerryan999 / golang-print-the-sequence-alternately.go
Created December 20, 2021 15:41
Use two goroutines to print the sequence alternately; one goroutine prints numbers, and the other prints letters.
package main
import (
"fmt"
"sync"
)
func main() {
letter, number := make(chan bool), make(chan bool)
// watches the source directory for changes and synchronize to destination directory
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
@jerryan999
jerryan999 / aes_128_ecb.go
Created February 17, 2022 09:08
AES-128-ECB PKCS7Padding
package main
import (
b64 "encoding/base64"
"fmt"
"github.com/andreburgaud/crypt2go/padding"
"golang.org/x/crypto/blowfish"
ecb "github.com/haowanxing/go-aes-ecb"
@app.route("/create", methods=["POST"])
def create_book():
""""
Create a new book
"""
payload = request.get_json()
if "id" in payload: # user cannot pass id when creating a new book
payload.pop("id")
status = BookModel.Schema().validate(payload, partial=("id",)) # no validation to id
if status: