只要对象定义了 keys
方法,就使用 keys
和 __getitem__
来生成 dict
没有定义 keys
方法的对象,尝试先使用 __iter__
转换为 list 后再转换为 dict
class Properties(object):
def __init__(self, data):
# http://melborne.github.io/2012/07/12/object-repeat-take-care-of-sequence/ | |
class Object | |
def repeat(init=true, &blk) | |
x = self | |
Enumerator.new do |y| | |
y << x if init | |
loop { y << (x = yield x) } | |
end | |
end |
import re | |
key_space_limit = 65536 | |
class RangeError(Exception): | |
pass | |
class KeySpaceConstrainedParams(object): | |
def __init__(self, limit=key_space_limit): |
def txt_to_utf(file) | |
open(file) do |f| | |
s = f.read | |
#shift_jis 必须写在前面,因为被误判为gbk的概率很高 | |
x = %w(shift_jis gbk big5) | |
begin | |
u = s.dup.encode("utf-8",x.shift) | |
rescue Encoding::UndefinedConversionError,Encoding::InvalidByteSequenceError => ex | |
if x.empty? | |
if s.chr.ord.to_s(16) == "efbb" |