Skip to content

Instantly share code, notes, and snippets.

View sakurai-youhei's full-sized avatar

Youhei Sakurai sakurai-youhei

  • Kobe, Japan
View GitHub Profile
@sakurai-youhei
sakurai-youhei / keybase.md
Created September 11, 2024 04:12
keybase.md

Keybase proof

I hereby claim:

  • I am sakurai-youhei on github.
  • I am youheisakurai (https://keybase.io/youheisakurai) on keybase.
  • I have a public key whose fingerprint is 1FE2 AFDC 87AB FB9E EA88 C634 D985 9D02 813D F63D

To claim this, I am signing this object:

@sakurai-youhei
sakurai-youhei / kcfork-kcback.sh
Last active November 5, 2022 14:46
Aliases to start/end an isolated session on a temporal config for kubectl
alias kcfork='install -m 600 $HOME/.kube/config $HOME/.kube/config-PID$$; export KUBECONFIG=$HOME/.kube/config-PID$$'
alias kcback='rm -f $HOME/.kube/config-PID$$; unset KUBECONFIG'
# You can start an isolated session on a temporal config by `kcfork`, do whatever you want like switching contexts, and end the session by `kcback`.
@sakurai-youhei
sakurai-youhei / ndict.py
Created October 21, 2021 00:07
Nested arbitrary dict in Python
ndict = type("ndict", (dict, ), {"__getitem__": lambda *args: dict.setdefault(*args, ndict())})
intro = ndict()
intro['here']['is']['my name'] = 'Youhei Sakurai'
intro['here']['is']['my favorite'] = 'Python'
print(intro) # Outputs: {'here': {'is': {'my name': 'Youhei Sakurai', 'my favorite': 'Python'}}}
@sakurai-youhei
sakurai-youhei / MultiValueHeaders.py
Created June 16, 2021 12:35
Sample code for "multiple message-header fields with the same field-name" in Python plus requests
from collections import OrderedDict
from http.client import HTTPMessage
class MultiValueHeaders(HTTPMessage):
'''https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2'''
_separator_ = ', '
def items(self):
headers = OrderedDict()
for key, value in super().items():
@sakurai-youhei
sakurai-youhei / consecutive_3plus_daysoff.py
Last active December 3, 2020 08:25
Consecutive 3+ daysoff in Pandas
series = daysoff.astype(int).to_series()
delta_2days = pd.Timedelta(2, unit='d').to_timedelta64().astype(float)
consecutive_3days = lambda S: abs(S.iloc[0] - S.iloc[-1]) == delta_2days
start = series.iloc[::-1].rolling(3).apply(consecutive_3days)[::-1].fillna(0)
middle = series.rolling(3, center=True).apply(consecutive_3days).fillna(0)
end = series.rolling(3).apply(consecutive_3days).fillna(0)
consecutive_3plus_daysoff = daysoff[(start + middle + end).astype(bool)] # 期間中の3連休以上
@sakurai-youhei
sakurai-youhei / weekends_daysoff.py
Created December 3, 2020 08:00
Weekends & daysoff in Pandas
weekends = alldays.difference(weekdays) # 期間中の土日
daysoff = (weekends | holidays) # 期間中の休日
@sakurai-youhei
sakurai-youhei / holidays_bizdays.py
Last active December 3, 2020 07:52
Holidays & bizdays in Pandas
import shukujitsu
holidays = alldays & pd.DatetimeIndex(shukujitsu.Japan()) # 期間中の祝日
bizdays = weekdays.difference(holidays) # 期間中の平日
@sakurai-youhei
sakurai-youhei / alldays_weekdays.py
Created December 3, 2020 06:52
All days & weekdays in Pandas
import pandas as pd
period = ("2020-01-01", "2020-12-31")
alldays = pd.date_range(*period, freq="D") # 期間中の毎日
weekdays = pd.date_range(*period, freq="B") # 期間中の月~金
@sakurai-youhei
sakurai-youhei / xvalid-with-jpholiday-1990-to-2024.py
Last active February 1, 2023 09:10
Cross validation of python-shukujitsu
from datetime import date
from difflib import unified_diff
import jpholiday
import shukujitsu
a = []
for day in shukujitsu.Japan()["1990-1-1": "2024-12-31"]:
a.append("{}\n".format(day))
@sakurai-youhei
sakurai-youhei / _etc_nginx_conf.d_default.conf
Last active May 23, 2021 11:17
Reverse-proxying blynk-server by Nginx with simple WAF
# empty