Skip to content

Instantly share code, notes, and snippets.

Avatar

Kensuke Nagae kyanny

View GitHub Profile
@kyanny
kyanny / generate_random_string.rb
Last active May 28, 2021
Generate random password-ish string in Ruby.
View generate_random_string.rb
#!/usr/bin/env ruby
# numbers
def generate_random_numbers(length)
chars = (0..9).to_a.map(&:to_s)
length.times.map{ chars.shuffle.first }.join
end
# lowercase letters
def generate_random_lowercase_letters(length)
View create-ip-allow-list-entry.sh
#!/bin/bash
usage_exit() {
echo "Usage:"
echo " export GITHUB_API_TOKEN=xxxxxxxx"
echo " bash $0 org_name [ip_address]"
exit
}
if [[ -z $1 ]];then
View hello.md
View django-signals.py
#!/usr/bin/env python
# https://docs.djangoproject.com/en/3.1/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage
import django
from django.conf import settings
# https://blog.amedama.jp/entry/sqlite3-in-memory-issue
import tempfile
tfile = tempfile.NamedTemporaryFile()
import sqlite3
View django-standalone.py
#!/usr/bin/env python
# https://docs.djangoproject.com/en/3.1/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage
import django
from django.conf import settings
# https://blog.amedama.jp/entry/sqlite3-in-memory-issue
import tempfile
tfile = tempfile.NamedTemporaryFile()
import sqlite3
View query_count.py
from django.db import connections,connection,reset_queries
from django.conf import settings
settings.DEBUG = True
def query_count(reset=True):
total = sum(len(c.queries) for c in connections.all())
if reset:
reset_queries()
return total
@kyanny
kyanny / fib.exs
Created Mar 13, 2012
Elixir fibonacci #1
View fib.exs
defmodule Fib do
def fib(0) do 0 end
def fib(1) do 1 end
def fib(n) do fib(n-1) + fib(n-2) end
end
IO.puts Fib.fib(10)
@kyanny
kyanny / t.py
Created Dec 29, 2020
Run Django's SimpleTestCase assertion in single Python script (without Django project)
View t.py
import unittest
from django.test import SimpleTestCase
from django.conf import settings
settings.configure()
class InHTMLTestCase(SimpleTestCase):
databases = []
def test_ok(self):
View Vagrantfile
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.provision "shell", inline: <<-SHELL
sudo apt-get update
sudo apt-get install -y git
sudo apt-get install -y unzip
wget --quiet https://github.com/motemen/ghq/releases/download/v0.7/ghq_linux_amd64.zip -O ghq_v0.7.zip
unzip -q -d ghq_v0.7 ghq_v0.7.zip
wget --quiet https://github.com/motemen/ghq/releases/download/v0.6/ghq_linux_amd64.tar.gz -O ghq_v0.6.tar.gz
tar xzf ghq_v0.6.tar.gz
View aws-logout.sh
aws-logout() {
for env in $(awk 'BEGIN{for(v in ENVIRON) print v}' | grep ^AWS)
do
echo "unset $env"
unset $env
done
}