Skip to content

Instantly share code, notes, and snippets.

View KenjiOhtsuka's full-sized avatar
👍
Hello, World!

Kenji Otsuka KenjiOhtsuka

👍
Hello, World!
View GitHub Profile
@KenjiOhtsuka
KenjiOhtsuka / PrintStackTrace.kt
Last active May 14, 2025 00:04
How to Print Stack Trace at Any Point in Kotlin
/*
* Throwable, StackTraceElement is in java.lang
*
* StackTraceElement has the following attributes (get methods)
* className
* methodName
* fileName
* lineNumber
*/
@KenjiOhtsuka
KenjiOhtsuka / extract_base64_image_in_notebook.py
Last active May 2, 2025 19:23
extract base64 image in jupyter notebook
import json
import base64
# ipynbファイルを読み込む
with open("notebook.ipynb", "r", encoding="utf-8") as f:
notebook = json.load(f)
# セルを解析して画像データを抽出
for cell in notebook.get("cells", []):
if cell.get("cell_type") == "markdown":
@KenjiOhtsuka
KenjiOhtsuka / calculate_days.py
Created March 18, 2025 23:14
Calculate the number of days between two dates.
from datetime import datetime
def calculate_days(from_date, to_date):
"""
Calculate the number of days between two dates.
Parameters:
from_date (str): The start date in the format 'YYYY-MM-DD'.
to_date (str): The end date in the format 'YYYY-MM-DD'.
@KenjiOhtsuka
KenjiOhtsuka / copy_files.gs
Last active February 10, 2025 15:33
Google Apps Script to Copy Folder Recursively
/**
This is a code of Google Apps Script for copying google drive folder content to other folder.
## Which situation the code resolve.
Google doesn't allow to move folder content to other folder which is managed by
other organization according to the policy of the GSUITE organization.
And, Google doesn't allow to change the content owner to the user in other
organizations.
import datetime
import time
total = 60
total_width = len(str(total))
s = datetime.datetime.now()
while True:
c = datetime.datetime.now()
t = int(total - (c - s).total_seconds())
if t <= 0:
// Require `PhoneNumberFormat`.
const PNF = require('google-libphonenumber').PhoneNumberFormat;
// Get an instance of `PhoneNumberUtil`.
const phoneUtil = require('google-libphonenumber').PhoneNumberUtil.getInstance();
function test(text) {
// Parse number with country code and keep raw input.
let number = phoneUtil.parseAndKeepRawInput(text, 'JP');
@KenjiOhtsuka
KenjiOhtsuka / generate.sh
Created February 18, 2023 02:42
Generate azure private endpoint host file
for sub in $(az account list -o tsv --query '[].name'); do
echo "# subscription = $sub"
for id in $(az network private-endpoint list --subscription $sub --query '[] | networkInterfaces[].id' -o tsv); do
address=$(az network nic show --ids $id --query 'ipConfigurations[].privateIpAddress' -o tsv)
for fqdn in $(az network nic show --ids $id --query 'ipConfigurations[].privateLinkConnectionProperties.fqdns' -o tsv); do
if [ -z "$fqdn" ]; then
continue
fi
echo "$address $fqdn"
done
@KenjiOhtsuka
KenjiOhtsuka / extract.py
Created May 27, 2024 04:17
Extract all images in a PowerPoint file
import pptx
from pptx.enum.shapes import MSO_SHAPE_TYPE
import os
# extract all images in the pptx file
def extract_images(pptx_path, output_dir):
i = 0
ppt = pptx.Presentation(pptx_path)
for slide in ppt.slides:
@KenjiOhtsuka
KenjiOhtsuka / hmac_sha256.py
Created December 2, 2023 02:56
HMAC (SHA-256) implementation
# HMAC with SHA-256
def sha256(message):
# SHA-256 implementation
import hashlib
return hashlib.sha256(message).digest()
def xor(a, b):
# XOR two byte strings
return bytes([x ^ y for x, y in zip(a, b)])
@KenjiOhtsuka
KenjiOhtsuka / sha_256.py
Last active December 1, 2023 22:55
SHA-256 Calculation in Python
# メッセージをビット列に変換する関数
def message_to_bitstring(message):
bitstring = ""
for char in message:
# ASCII コードを 8 ビットのビット列に変換する
bit = bin(ord(char))[2:].zfill(8)
bitstring += bit
return bitstring