Skip to content

Instantly share code, notes, and snippets.

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

Hideaki Kanehara vwrs

🏠
Working from home
View GitHub Profile
@vwrs
vwrs / unity-webgl-server.py
Created March 12, 2023 07:48
A simple python server for a Unity WebGL build
import sys
from http.server import SimpleHTTPRequestHandler, HTTPServer
class GzipRequestHandler(SimpleHTTPRequestHandler):
'''HTTPRequestHandler for gzip files'''
def end_headers(self):
'''Set Content-Encoding: gzip for gzipped files'''
if self.path.endswith('.gz'):
@vwrs
vwrs / unicode_to_char.js
Created March 10, 2023 09:48
convert unicode characters to string
// ref: https://stackoverflow.com/questions/17267329/converting-unicode-character-to-string-format
function unicodeToChar(text) {
return text.replace(/\\u[\dA-F]{4}/gi,
function (match) {
return String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16));
});
}
@vwrs
vwrs / ndcg-bq-udf.sql
Last active May 7, 2024 19:34
calculate NDCG in BigQuery
create temp function ndcg(rels array<int64>, k int64) returns float64 as (
(
with gain as (
select
(pow(2, rel) - 1) / log(i + 2) as g,
(pow(2, rel_sorted) - 1) / log(i + 2) as g_ideal
from
unnest(rels) as rel with offset as i
left join unnest((
select
@vwrs
vwrs / jupyter-notebook-template-bigquery.py
Last active April 14, 2023 07:32
jupyter notebook template bigquery
import pandas as pd
import matplotlib.pyplot as plt
%load_ext autoreload
%autoreload 2
# pandas settings
pd.set_option('display.max_columns', 200)
pd.set_option('display.max_rows', 100)
@vwrs
vwrs / del-all-failed-pods.sh
Created April 15, 2020 04:58
delete all failed pods from k8s
kubectl get pods --field-selector=status.phase=Failed | cut -d' ' -f 1 | sed -e '1d' | xargs kubectl delete pod
@vwrs
vwrs / watch-by-ping.py
Last active October 11, 2019 14:01
continuously watching a specific host by ping and raising an alert (post to the slack channel by webhook URL) when the host is unreachable
import time
import requests
import json
import pings # pip install pings
HOSTNAME = '{{ HOSTNAME }}'
PING_TIMES = 10
SLEEP_TIME = 100
WEBHOOK_URL = '{{ WEBHOOK_URL }}'
@vwrs
vwrs / apiview-swagger-example.py
Last active June 25, 2019 10:01
Simple Django's class-based View implementation with Swagger support using rest_framework and drf_yasg
from rest_framework.views import APIView
from rest_framework.decorators import parser_classes
from rest_framework.parsers import FormParser
from django.http import JsonResponse
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
class TestView(APIView):
'''Test API
@vwrs
vwrs / curl-post.sh
Created June 25, 2019 07:09
Send POST request by cURL
# use --trace-ascii to show the payload
curl -i -d 'key=value' [-v] [--trace-ascii /dev/stdout] https://example.com/
@vwrs
vwrs / show-progress.py
Created June 8, 2019 15:20
print utility for checking training progress in command line
import sys
def show_progress(e, i, t, l, v):
'''
Prints the training progress recursively.
Args:
e (int): the current epoch
i (int): the current batch
t (int): the total batches
import os
PATH_DIR = '/path/to/dir'
if not os.path.exists(PATH_DIR):
print('mkdir', PATH_DIR)
os.mkdir(PATH_DIR)