Skip to content

Instantly share code, notes, and snippets.

@sonkm3
sonkm3 / gist:6137749
Last active December 20, 2015 13:19
ruby: problem on get random number with specific digit. and found reason(Confusing Range object and Array object)
# this does not works. map called once.
p [1..10].map{|i|rand(9)}.join('')
# > "1"
# this works.
p [*1..10].map{|i|rand(9)}.join('')
# > "5012242033"
# difference is [*1..10] and [1..10]
# "*" expands Range object to list, but both are "Array" Class. strange...
@sonkm3
sonkm3 / gist:6103270
Created July 29, 2013 09:43
ruby: get base64 encoded sha1 hash. (for OAuth signature)
require "openssl"
key = "123"
base_string = "abc"
hash = OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new, key, base_string)
p [hash].pack('m').gsub(/\n/, '')
@sonkm3
sonkm3 / gist:6103254
Created July 29, 2013 09:40
ruby: url encode
require "cgi"
p CGI.escape("abc+&%")
# "abc%2B%26%25"
@sonkm3
sonkm3 / gist:6102865
Last active December 20, 2015 08:49
ruby: hash to query string
def get_query_string(query_hash)
query_array = []
query_hash.each{|key, val| val ? query_array.push([key, val].join('=')) : nil}
query_string = query_array != [] ? query_array.join('&') : ''
end
query_hash = {"a"=> 1, "c" =>nil, "b"=>3}
p get_query_string(query_hash)
@sonkm3
sonkm3 / gist:6102798
Last active December 20, 2015 08:49
ruby: sort hash by key.
hash1 = {:b => 1, :a => 2}
# => {:b=>1, :a=>2}
Hash[hash1.sort_by{|key, val|key}]
# => {:a=>2, :b=>1}
Hash[hash1.sort]
# => {:a=>2, :b=>1}
# sort by value
Hash[hash1.sort_by{|key, val|val}]
# => {:b=>1, :a=>2}
@sonkm3
sonkm3 / gist:6102746
Last active December 20, 2015 08:48
ruby: hash.update()
hash1 = {:a=> 1, :b=>2}
hash2 = {}
hash2[:b] = 3
p hash2
hash2.update(hash1)
p hash2
#{:b=>3}
@sonkm3
sonkm3 / gist:5786358
Created June 15, 2013 01:16
簡易csrf対策
class AdminHandler(webapp.RequestHandler):
@util.login_required
def get(self):
csrf_key = self._generate_csrf_key()
# pass csrf key as template parameter
@util.login_required
def post(self):
if users.is_current_user_admin():
if not self._check_csrf_key(self.request.get('csrf_key')):
@sonkm3
sonkm3 / gist:5516180
Last active December 16, 2015 23:49
python: raising exception from inside of generator function.
# -*- coding: utf-8 -*-
# simple generator function
def generator1(limit):
for count in range(limit):
yield count
# simple generator function with return
def generator2(limit, stop):
for count in range(limit):
if count > stop:
@sonkm3
sonkm3 / gist:5493822
Last active December 16, 2015 20:39
python: ジェネレーター関数の内側でraiseすると、その後はStopIterationしか上がってこない件 ジェネレーター関数内でのraiseはジェネレーター関数内でのreturnと同じ挙動ってこと?
def gen1():
count = 0
while True:
count = count + 1
print count
if count > 10:
return
if count == 5:
print 'raise!'
raise '5!!!'
@sonkm3
sonkm3 / gist:4482584
Last active December 10, 2015 19:38
python: fetch tweets by tweepy's Cursor but this stops before it reaches the end..
# -*- coding: utf-8 -*-
import tweepy
consumer_token = ''
consumer_secret = ''
access_token = ''
access_secret = ''
def get_tweets(api, screen_name):