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
@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:3081
Created July 29, 2008 13:22
Installing PIL using setuptools
% easy_install --find-links http://www.pythonware.com/products/pil/ Imaging
@ishikawa
ishikawa / gist:3430
Created July 31, 2008 09:32
Handling urllib2.HTTPError, urllib2.URLError
# Handling urllib2.HTTPError, urllib2.URLError
# (via http://www.voidspace.org.uk/python/articles/urllib2.shtml)
from urllib2 import Request, urlopen, URLError, HTTPError
req = Request(someurl)
try:
response = urlopen(req)
except HTTPError, e:
print 'The server couldn\'t fulfill the request.'
print 'Error code: ', e.code
except URLError, e:
@ishikawa
ishikawa / gist:3600
Created August 1, 2008 09:18
Python Logger Configuration
import logging
def _configureLogger():
"""Returns configured default logger for module."""
handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(logging.Formatter(LOGGER_FORMAT))
logger = logging.getLogger('org.metareal.XmlPropertyListParser')
logger.setLevel(LOGGER_LEVEL)
logger.addHandler(handler)
# Apple Property List parser for Python
# (via http://effbot.org/zone/element-iterparse.htm)
#
# ``xml.etree.ElementTree`` is fast (and smart looks) for small xml,
# but for large (more than 5MB) xml, SAX implementation is still fast.
def xml_plist_parse(self, xml_input):
import base64, time, datetime
from xml.etree.ElementTree import iterparse
callbacks = {
@ishikawa
ishikawa / bit_array.py
Created August 6, 2008 01:51
Immutable Bit Array for Python
#!/usr/bin/env python
import math
import array
class BitArray(object):
"""
A bit array (or bitmap, in some cases) is an array data structure which
compactly stores individual bits (0 or 1).
def insertion_sort(lst):
"""
>>> insertion_sort([3, 4, 2, 10, 1])
[1, 2, 3, 4, 10]
>>> insertion_sort([1, 2, 3, 4, 5])
[1, 2, 3, 4, 5]
>>> insertion_sort([])
[]
"""
for i in xrange(1, len(lst)):
def factorial(n):
return reduce(lambda x, y: x*y, range(1, n+1))