Skip to content

Instantly share code, notes, and snippets.

View ishikawa's full-sized avatar
🏠
Working from home

Takanori Ishikawa ishikawa

🏠
Working from home
View GitHub Profile
#!/usr/bin/ruby
require 'webrick'
include WEBrick
def start_webrick(config = {})
config.update(:Port => 5001)
server = HTTPServer.new(config)
yield server if block_given?
@ishikawa
ishikawa / gist:2588
Created July 26, 2008 03:06
Apple's Modifications To Python 2.5.1 (Locale.Py, ...)
diff -ur ./Python-2.5.1.orig/Lib/locale.py ./Python-2.5.1/Lib/locale.py
--- ./Python-2.5.1.orig/Lib/locale.py 2006-05-18 11:06:40.000000000 +0900
+++ ./Python-2.5.1/Lib/locale.py 2007-09-09 12:50:14.000000000 +0900
@@ -485,7 +485,7 @@
"""
_setlocale(category, _build_localename(getdefaultlocale()))
-if sys.platform in ('win32', 'darwin', 'mac'):
+if sys.platform in ('win32', 'mac'):
# On Win32, this will return the ANSI code page
@ishikawa
ishikawa / gist:2591
Created July 26, 2008 03:10
Iterates Over The Bytes Of String
import array
for byte in array.array('B', "ABCDEFG"):
print byte # prints 65, 66, 67, 68, ... and so on.
@ishikawa
ishikawa / gist:2592
Created July 26, 2008 03:11
Name Mangling Of Python Private Variables
def _mangle_name(self, name):
"""
Any identifier of the form __spam (at least two leading underscores,
at most one trailing underscore) is textually replaced with
_classname__spam, where classname is the current class name with
leading underscore(s) stripped.
Truncation may occur when the mangled name would be longer than
255 characters. When the class name consists of only underscores,
no mangling occurs.
@ishikawa
ishikawa / sqlite_insert_or_replace.rb
Created July 29, 2008 00:41
SQLite INSERT or REPLACE
stmt = dbm.prepare "INSERT OR REPLACE INTO items VALUES ( ?, ?, ? );"
begin
items.each do |item|
stmt.execute(*item)
end
ensure
stmt.close rescue nil
end
@ishikawa
ishikawa / simple_array_shuffle.rb
Created July 29, 2008 01:05
Ruby: Random sort an array
# Random sort an array (via http://d.hatena.ne.jp/tanakaBox/20070512/1178915094)
# Not perfect, but simple.
# Usage:
# >> (1..10).to_a.shuffle
# => [1, 3, 2, 5, 9, 7, 6, 8, 4, 10]
class Array
def shuffle() self.to_a.shuffle!; end
def shuffle!() self.sort! { rand(3) - 1 }; end
end
@ishikawa
ishikawa / gist:2584
Created July 26, 2008 02:55
Parse Query String From Script Element's Src Attribute
/**
* Helper object to parse the query string variables from
* <script> element's src attribute.
*
* For example, in test.html:
*
* <script src="test.js?name=value"></script>
*
* and in test.js, you can get query as name/value pairs:
*
@ishikawa
ishikawa / gist:2586
Created July 26, 2008 03:03
Subversion quickstart
creating the Subversion repository
% svnadmin create --fs-type fsfs path-to-svn-repos
% mkdir tmp
% cd tmp
% mkdir webframework
% cd webframework
% mkdir trunk branches tags
% cd ..
% svn import . file:///path-to-svn-repos --message 'Initial repository layout'
@ishikawa
ishikawa / array_product.rb
Created July 26, 2008 03:04
Array#Product For Ruby 1.8
class Array
# Array#product
# -------------
# Returns the cartesian product of the receiver and the arrays given as arguments.
#
# Usage:
# [1, 2, 3].product([4, 5]) # => [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]]
# [1, 2].product([1, 2]) # => [[1, 1], [1, 2], [2, 1], [2, 2]]
# [1, 2].product([3, 4],[5, 6]) # => [[1, 3, 5], [1, 3, 6], [1, 4, 5], [1, 4, 6],
@ishikawa
ishikawa / sha1.js
Created July 26, 2008 03:07
JavaScript Implementation Of The SHA-1
/*
* The JavaScript implementation of the Secure Hash Algorithm 1
*
* Copyright (c) 2008 Takanori Ishikawa <takanori.ishikawa@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*