Skip to content

Instantly share code, notes, and snippets.

import re
def normalize_text(t: str):
"""
:param t: English text
:return: Normalized text according to below:
# language-dependent part (assuming Western languages):
$norm_text = " $norm_text ";
$norm_text =~ tr/[A-Z]/[a-z]/ unless $preserve_case;
@hanayashiki
hanayashiki / socks5ping.py
Last active June 25, 2019 06:12
Ping a website with your sock5 proxy. Test your connectivity and speed.
# Usage:
#
# $socks5ping.py www.baidu.com
# Connected to www.baidu.com[:80]: seq=1 time=1.47s
# Connected to www.baidu.com[:80]: seq=2 time=1.27s
# Connected to www.baidu.com[:80]: seq=3 time=1.18s
# Connected to www.baidu.com[:80]: seq=4 time=1.15s
#
# $socks5ping.py -h
# usage: socks5ping.py [-h] [-s SOCKS5HOST] [-o SOCKS5PORT] [-p PORT]
@hanayashiki
hanayashiki / json_visualizer.py
Created July 18, 2019 04:10
Truncate long json objects so that it is human readable
import sys
import json
max_item = 3
def visualize(obj: dict):
if isinstance(obj, list):
if len(obj) > max_item:
return [visualize(o) for o in obj[0:max_item] + ["(%d items in total)" % len(obj)]]
else:
import socket
def extract_request_line(request):
first_line = request.split("\r\n")[0]
method, resource, ver = first_line.split(" ")
return method, resource, ver
def respond(method, resource):
if method == "GET" and resource == "/A":
return b"<h1>Resource A from my server!</h1>", 200
@hanayashiki
hanayashiki / patch-arrayBuffer.js
Created December 16, 2019 04:08
Safari 13.0.4: Blob.arrayBuffer is not a function
(function () {
File.prototype.arrayBuffer = File.prototype.arrayBuffer || myArrayBuffer;
Blob.prototype.arrayBuffer = Blob.prototype.arrayBuffer || myArrayBuffer;
function myArrayBuffer() {
// this: File or Blob
return new Promise((resolve) => {
let fr = new FileReader();
fr.onload = () => {
resolve(fr.result);
class CallbackSequence extends Function {
constructor() {
super();
this.callbacks = {};
return new Proxy(this, {
apply: (target, thisArg, argumentsList) => {
this.__call__();
}
});
}
@hanayashiki
hanayashiki / sliding_window.py
Created December 28, 2019 07:39
2d sliding window implementation for numpy
import numpy as np
from numpy.lib.stride_tricks import as_strided
from typing import *
def sliding_window_2d(a: np.array, wnd_size: Tuple[int, int]):
# a: an nd-array of (height, width, ...rest)
# wnd_size: a tuple of (wnd_width, wnd_height)
# returns an nd-array of (height - wnd_height + 1, width - wnd_width + 1, wnd_height, wnd_width, ...rest)
height, width, *rest = a.shape
s0, s1, *rest_strides = a.strides
@hanayashiki
hanayashiki / EventEmitter.swift
Last active July 9, 2020 21:37 — forked from brennanMKE/EventEmitter.swift
React Native Event Emitter for RCTEventEmitter in Objective-C and Swift
class EventEmitter {
/// Shared Instance.
public static var sharedInstance = EventEmitter()
// ReactNativeEventEmitter is instantiated by React Native with the bridge.
private static var eventEmitter: ReactNativeEventEmitter!
private init() {}
@hanayashiki
hanayashiki / podforceupdate.sh
Created July 10, 2020 11:19 — forked from mbinna/podforceupdate.sh
Clear CocoaPods cache, re-download and re-install all pods
#!/usr/bin/env bash
rm -rf "${HOME}/Library/Caches/CocoaPods"
rm -rf "`pwd`/Pods/"
pod update
@hanayashiki
hanayashiki / ios-screen-size.json
Last active January 20, 2023 16:35
A dictionary of different iPhone screen sizes (until iPhone 12)
{
"iPhone 6": {
"height": 667,
"width": 375
},
"iPhone 7": {
"height": 667,
"width": 375
},
"iPhone 8": {