Skip to content

Instantly share code, notes, and snippets.

@RainJayTsai
RainJayTsai / async-await.js
Created May 31, 2018 01:29 — forked from wesbos/async-await.js
Simple Async/Await Example
// 🔥 Node 7.6 has async/await! Here is a quick run down on how async/await works
const axios = require('axios'); // promised based requests - like fetch()
function getCoffee() {
return new Promise(resolve => {
setTimeout(() => resolve('☕'), 2000); // it takes 2 seconds to make coffee
});
}
@RainJayTsai
RainJayTsai / download-file.js
Created June 12, 2018 02:36 — forked from javilobo8/download-file.js
Download files with AJAX (axios)
axios({
url: 'http://localhost:5000/static/example.pdf',
method: 'GET',
responseType: 'blob', // important
}).then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'file.pdf');
document.body.appendChild(link);
@RainJayTsai
RainJayTsai / async_worker_pool.py
Created August 22, 2018 06:40 — forked from thehesiod/async_worker_pool.py
Asynchronous Worker Pool, allows for limiting number of concurrent tasks
import asyncio
from datetime import datetime, timezone
import os
def utc_now():
# utcnow returns a naive datetime, so we have to set the timezone manually <sigh>
return datetime.utcnow().replace(tzinfo=timezone.utc)
class Terminator:
pass
@RainJayTsai
RainJayTsai / _aiojob.py
Created October 22, 2018 08:16
python aiojob wrapper for sanic
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from aiojobs import create_scheduler
def get_scheduler_from_app(app):
return app.AIOJOBS_SCHEDULER
def get_scheduler(request):
@RainJayTsai
RainJayTsai / mongo_.py
Created December 7, 2018 02:50
fast dump mongo collection to memory, using ProcessPoolExecutor multiprocess
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import bson
from typing import Callable, Optional
import concurrent.futures as cf
from pymongo.collection import Collection
def decode(data: bytes, callback: Callable[[list], None]):
@RainJayTsai
RainJayTsai / coll_counts.py
Created December 7, 2018 02:56
fast_read_big_mongo_collection_doc_counts
from pymongo import MongoClient
client = MongoClient('IP')
db = client['DB']
collection_name = 'your_full_collectaion_name'
resp = db.command('collstats',collection_name)
print(resp.get('count')
@RainJayTsai
RainJayTsai / http_echo_and_proxy_server.py
Created January 4, 2019 08:10 — forked from pomack/http_echo_and_proxy_server.py
A simple python HTTP server that either serves as a proxy or as an echo server. If working as an echo server, it outputs the headers and body into log output and to the client, which is useful when debugging a reverse proxy or caching server to see what is being sent. If working as a proxy server, it outputs what the request headers will be to t…
#!/usr/bin/env python
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import StringIO
import argparse
import logging
import os
import random
import sys
import urllib2
@RainJayTsai
RainJayTsai / server.py
Created January 4, 2019 08:10 — forked from scturtle/server.py
python socks5 proxy server with asyncio (async/await)
#!/usr/bin/env python3.5
import socket
import asyncio
from struct import pack, unpack
class Client(asyncio.Protocol):
def connection_made(self, transport):
self.transport = transport
self.server_transport = None
@RainJayTsai
RainJayTsai / gmmhmm.py
Created February 20, 2019 06:29 — forked from kastnerkyle/gmmhmm.py
GMM-HMM (Hidden markov model with Gaussian mixture emissions) implementation for speech recognition and other uses
# (C) Kyle Kastner, June 2014
# License: BSD 3 clause
import scipy.stats as st
import numpy as np
class gmmhmm:
#This class converted with modifications from https://code.google.com/p/hmm-speech-recognition/source/browse/Word.m
def __init__(self, n_states):
self.n_states = n_states
@RainJayTsai
RainJayTsai / compress.py
Created March 18, 2019 03:45
python sanic compress
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
### fork from https://github.com/subyraman/sanic_compress
import gzip
DEFAULT_MIME_TYPES = frozenset([
'text/html', 'text/css', 'text/xml',
'application/json',