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
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 / benchmarks.mjs
Created November 16, 2021 04:27
module resolution benchmarks
import { build } from 'esbuild'
import typescript from '@rollup/plugin-typescript'
export async function esbuildResolve(id, dir) {
let result
await build({
stdin: {
contents: `import ${JSON.stringify(id)}`,
@hanayashiki
hanayashiki / Setup eslint.md
Created August 9, 2022 12:58
Setup eslint.md
pnpm install -D eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-prettier prettier eslint-plugin-import

.eslintrc.cjs

module.exports = {
  ignorePatterns: ['**/vendors/**'],
  plugins: ['import', '@typescript-eslint', 'prettier'],
  parser: '@typescript-eslint/parser',