Skip to content

Instantly share code, notes, and snippets.

@youhey
youhey / wav2ogg.sh
Created June 10, 2022 05:03
wav audio file to ogg format
#!/bin/bash
# Usage:
# AUDIO_PATH=~/Desktop/working/dir/path sh ./wav2ogg.sh
#
# @see [FFmpeg](https://ffmpeg.org/)
set -Ceuo pipefail
cd "$(dirname "$0")"
RUN apk add --no-cache --virtual .download-deps \
curl \
&& curl -sSL "https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh" \
-o "wait-for-it.sh" \
&& apk del .download-deps \
&& chmod +x wait-for-it.sh \
&& mv wait-for-it.sh /usr/local/bin/wait-for-it
@youhey
youhey / Distinct.php
Created July 5, 2019 02:35
Laravel の独自バリデーションを頑張って考えてたら、それは `distinct` という標準ルールだったでゴザル……。
<?php
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Http\Request;
// 'items.*.id' => [
// 'required',
// 'integer',
// 'min:1',
// 'max:999',
@youhey
youhey / birthday.py
Last active June 20, 2018 09:39
誕生日のパラドックス
from math import log1p, sqrt
def birthday(probability_exponent, bits):
probability = 10. ** probability_exponent
outputs = 2. ** bits
return sqrt(2. * outputs * -log1p(-probability))
@youhey
youhey / Laravelでセッターインジェクション.md
Last active November 16, 2017 08:27
Laravelでセッターインジェクション

Laravelでセッターインジェクションのような仕組みをしたい場合、どうやるのが正解なのか見つけられずに苦しんでいる。

例えば、Facadeを使わないという前提の開発環境で、ORMクラスにキャッシュを渡したい場合や、ロガーをセットしたい場合など、コンストラクタインジェクションではなくセッターインジェクションがしたい。

namespace App\ORM\Contracts;

use Illuminate\Cache\CacheManager;

interface CachingInterface
@youhey
youhey / elo_rating.php
Created February 20, 2017 06:28
Elo rating
<?php
const K = 4;
const Z = 400;
$rating_a = 1200.0;
$rating_b = 1500.0;
$win = function ($r, $e) {
return sprintf('%.2F', round($r + (K * (1 - $e)), 2));
# 停止しているコンテナを削除
docker ps -aq -f status=exited -f status=dead | xargs docker rm
# 不要なイメージを削除
docker images -f "dangling=true" -q | xargs docker rmi
# 不要なボリュームを削除
docker volume ls -qf "dangling=true" | xargs docker volume rm
@youhey
youhey / orphan-branch.md
Created August 29, 2016 09:28
過去を持たない空のブランチを追加したい
$ git clone git@git.example.com:hoge/hoge.git hoge
$ cd hoge
$ git checkout --orphan new_branch_name
$ git rm -rf .
$ touch README.md
$ git add README.md
$ git commit -m "Initial commit"
$ git push
@youhey
youhey / grep-php-file.md
Last active August 27, 2016 11:12
vendor を除外して PHP ファイルを grep
find . -type d -name 'vendor' -prune -o -type f -name '*.php' -print0 | xargs -0 grep "hoge"
@youhey
youhey / gist:9068421
Created February 18, 2014 10:40
MySQLのサーバタイムアウトetc..を検知
define('MYSQL_ERROR_SERVER_GONE_ERROR', '2006');
define('RESULT_DB_GONE_AWAY', '2006');
try {
....
} catch (PDOException $e) {
if ($e->errorInfo[1] === MYSQL_ERROR_SERVER_GONE_ERROR) {
// リトライ情報とか
$response = array('status' => RESULT_DB_GONE_AWAY);
$jsonText = json_encode($response);