Skip to content

Instantly share code, notes, and snippets.

Avatar

Kensuke Nagae kyanny

View GitHub Profile
@kyanny
kyanny / hello_world.py
Created Aug 31, 2021
Hello World Examples
View hello_world.py
class HelloWorld:
def __init__(self, name):
self.name = name.capitalize()
def sayHi(self):
print "Hello " + self.name + "!"
hello = HelloWorld("world")
hello.sayHi()
View my-tempfile.py
import tempfile
with tempfile.NamedTemporaryFile() as tmp:
print(tmp.name)
tmp.write(b'Hello\nWorld')
tmp.seek(0)
with open(tmp.name) as f:
print(f.read())
with tempfile.NamedTemporaryFile(mode='w+t') as tmp:
View decode_uri_query.rb
require 'uri'
url = 'http://example.com?foo=1&bar=2'
uri = URI.parse(url)
decoded_query = URI.decode_www_form(uri.query)
pp decoded_query, decoded_query.class
decoded_query_hash = Hash[decoded_query]
pp decoded_query_hash, decoded_query_hash.class
View q7.py
def should_hit(dealer_total, player_total, player_low_aces, player_high_aces):
"""Return True if the player should hit (request another card) given the current game
state, or False if the player should stay.
When calculating a hand's total value, we count aces as "high" (with value 11) if doing so
doesn't bring the total above 21, otherwise we count them as low (with value 1).
For example, if the player's hand is {A, A, A, 7}, we will count it as 11 + 1 + 1 + 7,
and therefore set player_total=20, player_low_aces=2, player_high_aces=1.
"""
if dealer_total >= player_total or player_total <= 12:
@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