Skip to content

Instantly share code, notes, and snippets.

View curegit's full-sized avatar

curegit curegit

View GitHub Profile
@curegit
curegit / extstats.py
Last active April 1, 2024 10:24
ディレクトリツリーに存在する拡張子を集計するスクリプト
#!/usr/bin/env python3
import sys
import os
from pathlib import Path
from collections import Counter
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
@curegit
curegit / once.py
Created March 11, 2024 05:57
高々一回実行される副作用を定義するデコレータ
import functools
def once(func):
fst = True
result = None
@functools.wraps(func)
def wrapper(*args, **kwargs):
nonlocal fst, result
if fst:
@curegit
curegit / memmap.py
Created February 27, 2024 05:42
大規模配列のファイルからの部分読み出し例
import numpy as np
large_arr = np.random.normal(size=(3, 10000, 10000)).astype("float64")
np.save("example.npy", large_arr)
import time
import random
from numpy.lib.format import open_memmap
@curegit
curegit / pytorch-lanczos2x.py
Last active November 1, 2023 01:38
PyTorch における Lanczos 補間による 2D マップ二倍拡大層の実装
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
def lanczos(x, n):
return 0.0 if abs(x) > n else np.sinc(x) * np.sinc(x / n)
class Lanczos2xUpsampler(nn.Module):
def __init__(self, n=3):
@curegit
curegit / tile-anim.py
Last active August 12, 2023 23:34
複数のアニメーション画像をタイル状に並べて一つの画像にするスクリプト
#!/usr/bin/env python3
x = 3
y = 2
background = "white"
gap = 10
duration = 100
anims = [
"APNGs/a1.png",
"APNGs/a2.png",
@curegit
curegit / autossh.service
Created June 5, 2023 14:53
AutoSSH によるポートフォワーディングのサービス化例
[Unit]
Description=AutoSSH
After=network.target
[Service]
User=pi
Group=pi
Type=simple
ExecStart=/usr/bin/autossh vps -N -R 8000:10.10.10.10:3389
KillSignal=SIGQUIT
@curegit
curegit / files.sh
Last active June 11, 2023 07:04
ファイルタイプ一覧を出す関数
#!/bin/bash
files () {
find "${1:-.}" -type f -not -path "*/.git/*" -not -path "*/node_modules/*" -exec file {} \;
}
@curegit
curegit / pep8.sh
Created February 20, 2023 14:29
yapf でタブインデントのまま、概ね pep8 に合わせるコマンド
# 通常の場合
yapf -r -i --style="{based_on_style: pep8, indent_width: 4, column_limit: 160}" .
# タブインデントの場合
yapf -r -i --style="{based_on_style: pep8, indent_width: 4, use_tabs: True, column_limit: 160}" .
@curegit
curegit / shifted.py
Created September 12, 2022 07:07
リストをずらしたイテレーターを返す
def shifted(list, shift=0):
n = len(list)
for i in range(shift % n, n):
yield list[i]
for i in range(0, shift % n):
yield list[i]
@curegit
curegit / fix.py
Created August 24, 2022 06:24
Python で Zコンビネータ
fix = lambda f: (lambda g: g(g))(lambda g: f(lambda *args, **kwargs: g(g)(*args, **kwargs)))
fact = fix(lambda f: lambda n: 1 if n == 0 else n * f(n - 1))
fib = fix(lambda f: lambda x: 1 if x == 0 else 1 if x == 1 else f(x - 1) + f(x - 2))
gcd = fix(lambda f: lambda a, b: abs(a) if b == 0 else f(b, a % b))
tarai = fix(lambda f: lambda x, y, z: y if x <= y else f(f(x - 1, y, z), f(y - 1, z, x), f(z - 1, x, y)))